Skip to content

Commit ea42d33

Browse files
committed
Merge pull request #214 from elixir-lang/issue-212
correct indentation after one line definition with if keyword
2 parents 1e63148 + ba74e75 commit ea42d33

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

elixir-smie.el

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@
330330
((smie-rule-hanging-p)
331331
(smie-rule-parent elixir-smie-indent-basic))
332332
(t elixir-smie-indent-basic)))
333+
(`(:before . "if")
334+
(cond
335+
((smie-rule-parent-p ";")
336+
(smie-rule-parent))))
333337
(`(:before . "->")
334338
(cond
335339
((smie-rule-hanging-p)
@@ -364,9 +368,23 @@
364368
(t (smie-rule-parent elixir-smie-indent-basic))))))
365369
(`(:before . ";")
366370
(cond
371+
;; There is a case after an one line definition of functions/macros
372+
;; when an `if' keyword token is involved, where the next block `end'
373+
;; token will have a `if' as parent and it's hanging.
374+
;;
375+
;; Example:
376+
;;
377+
;; defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil)
378+
;; defmacro my_if(expr, do: if_block, else: else_block) do
379+
;; ...
380+
;; end <- parent is `if`
381+
((and (smie-rule-parent-p "if")
382+
(smie-rule-hanging-p))
383+
(smie-rule-parent))
367384
((smie-rule-parent-p "after" "catch" "def" "defmodule" "defp" "do" "else"
368385
"fn" "if" "rescue" "try" "unless")
369-
(smie-rule-parent elixir-smie-indent-basic))))
386+
(smie-rule-parent elixir-smie-indent-basic))
387+
))
370388
(`(:after . ";")
371389
(cond
372390
((smie-rule-parent-p "def")

test/elixir-mode-indentation-test.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,34 @@ defmodule Greeter do
873873
end
874874
end")
875875

876+
(elixir-def-indentation-test indent-after-def-do-online/2
877+
(:tags '(indentation))
878+
879+
"defmodule ControlFlow do
880+
defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil)
881+
defmacro my_if(expr, do: if_block, else: else_block) do
882+
quote do
883+
case unquote(expr) do
884+
result when result in [false, nil] -> unquote(else_block)
885+
_ -> unquote(if_block)
886+
end
887+
end
888+
end
889+
end"
890+
891+
"defmodule ControlFlow do
892+
defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil)
893+
defmacro my_if(expr, do: if_block, else: else_block) do
894+
quote do
895+
case unquote(expr) do
896+
result when result in [false, nil] -> unquote(else_block)
897+
_ -> unquote(if_block)
898+
end
899+
end
900+
end
901+
end")
902+
903+
876904
(elixir-def-indentation-test indent-binary-sequence
877905
(:tags '(indentation))
878906
"

0 commit comments

Comments
 (0)