Skip to content

Commit 60aeb48

Browse files
committed
Merge pull request #241 from elixir-lang/one-line-do-indentation
correct indent for oneline `do:` when moved to next line
2 parents bcb7261 + e109f09 commit 60aeb48

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

elixir-smie.el

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
(statements (statement)
107107
(statement ";" statements))
108108
(statement ("def" non-block-expr "do" statements "end")
109+
("defp" non-block-expr "do" statements "end")
110+
("defmacro" non-block-expr "do" statements "end")
111+
("defmacrop" non-block-expr "do" statements "end")
109112
(non-block-expr "fn" match-statements "end")
110113
(non-block-expr "do" statements "end")
111114
("if" non-block-expr "do" statements "else" statements "end")
@@ -192,6 +195,11 @@
192195
(beginning-of-line)
193196
(looking-at "^\s+->.+$")))
194197

198+
(defun elixir-smie-line-starts-with-do-colon-p ()
199+
(save-excursion
200+
(beginning-of-line)
201+
(looking-at "^\s+do:")))
202+
195203
(defun elixir-smie--semi-ends-match ()
196204
"Return non-nil if the current line concludes a match block."
197205
(when (not (eobp))
@@ -353,8 +361,11 @@
353361
(smie-rule-parent))
354362
(`(:before . "do:")
355363
(cond
356-
((smie-rule-parent-p "def" "if")
357-
(smie-rule-parent))))
364+
((smie-rule-parent-p "def" "if" "defp" "defmacro" "defmacrop")
365+
(smie-rule-parent))
366+
((and (smie-rule-parent-p ";")
367+
(not (smie-rule-hanging-p)))
368+
(smie-rule-parent elixir-smie-indent-basic))))
358369
(`(:before . "do")
359370
(cond
360371
((and (smie-rule-parent-p "case")
@@ -444,7 +455,7 @@
444455
(t elixir-smie-indent-basic)))
445456
(`(:before . "if")
446457
(cond
447-
((smie-rule-parent-p ";")
458+
(t
448459
(smie-rule-parent))))
449460
(`(:before . "->")
450461
(cond
@@ -528,7 +539,7 @@
528539
(smie-rule-hanging-p))
529540
(smie-rule-parent elixir-smie-indent-basic))
530541
((smie-rule-parent-p "after" "catch" "def" "defmodule" "defp" "do" "else"
531-
"fn" "if" "rescue" "try" "unless")
542+
"fn" "if" "rescue" "try" "unless" "defmacro" "defmacrop")
532543
(smie-rule-parent))
533544
;; Example
534545
;;
@@ -541,7 +552,14 @@
541552
((and (smie-rule-parent-p "->")
542553
(smie-rule-hanging-p))
543554
(smie-rule-parent))
544-
))
555+
;; Example
556+
;;
557+
;; defp skip,
558+
;; do: true <- indent two spaces
559+
((and (smie-rule-parent-p ";")
560+
(smie-rule-hanging-p)
561+
(elixir-smie-line-starts-with-do-colon-p))
562+
(smie-rule-parent (- elixir-smie-indent-basic)))))
545563
(`(:after . ";")
546564
(cond
547565
((smie-rule-parent-p "def")

test/elixir-mode-indentation-test.el

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,30 @@ end"
944944
end")
945945

946946

947+
(elixir-def-indentation-test indent-after-not-finished-one-line-def
948+
(:tags '(indentation))
949+
"
950+
defmodule Hello do
951+
defp skip,
952+
do: true
953+
def on?, do: true
954+
defmacro switch,
955+
do: on!
956+
defp self, do: value
957+
defmacrop whatever, do: do_it!
958+
end"
959+
960+
"
961+
defmodule Hello do
962+
defp skip,
963+
do: true
964+
def on?, do: true
965+
defmacro switch,
966+
do: on!
967+
defp self, do: value
968+
defmacrop whatever, do: do_it!
969+
end")
970+
947971
(elixir-def-indentation-test indent-binary-sequence
948972
(:tags '(indentation))
949973
"

0 commit comments

Comments
 (0)