Skip to content

Commit bf2d9f1

Browse files
committed
Merge pull request #63 from elixir-lang/indent-anon-fns
Correctly indent one-line anon fns AND block fns. Fixes #59
2 parents e81b3c9 + 98b370f commit bf2d9f1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

elixir-smie.el

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ Return non-nil if any line breaks were skipped."
250250
(statement)
251251
(statement ";" statements))
252252
(statement
253+
(non-block-expr "fn" match-statement "end")
254+
(non-block-expr "do" statements "end")
253255
("if" non-block-expr "do" statements "else" statements "end")
254256
("if" non-block-expr "do" statements "end")
255257
("if" non-block-expr "COMMA" "do:" statement)
@@ -258,10 +260,7 @@ Return non-nil if any line breaks were skipped."
258260
("try" "do" statements "catch" match-statements "end")
259261
("try" "do" statements "end")
260262
("case" non-block-expr "do" match-statements "end")
261-
("def" non-block-expr "do" statements "end")
262-
(non-block-expr "do" statements "end")
263-
(expr)
264-
)
263+
("def" non-block-expr "do" statements "end"))
265264
(non-block-expr
266265
(non-block-expr "OP" non-block-expr)
267266
(non-block-expr "COMMA" non-block-expr)
@@ -309,9 +308,15 @@ Return non-nil if any line breaks were skipped."
309308
;; for some reason SMIE doesn't look this far when there's a
310309
;; comment terminating the previous line. Ugh.
311310
nil)
311+
;; If the parent token of `->' is `fn', then we want to align to the
312+
;; parent, and offset by `elixir-smie-indent-basic'. Otherwise, indent
313+
;; normally. This helps us work with/indent anonymous function blocks
314+
;; correctly.
312315
(`(:after . "->")
313316
(when (smie-rule-hanging-p)
314-
elixir-smie-indent-basic))
317+
(if (smie-rule-parent-p "fn")
318+
(smie-rule-parent elixir-smie-indent-basic)
319+
elixir-smie-indent-basic)))
315320
(`(,_ . ,(or `"COMMA")) (smie-rule-separator kind))
316321
(`(:after . "=") elixir-smie-indent-basic)
317322
(`(:after . "end") 0)

test/elixir-mode-indentation-tests.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ to_process = [27, 33, 35, 11, 36, 29, 18, 37, 21, 31, 19, 10, 14, 30,
363363
")
364364

365365
(elixir-def-indentation-test indent-nested-fn
366-
(:expected-result :failed) ; #59
366+
()
367367
"defmodule FooModule do
368368
def foo do
369369
x = fn(a, b) -> a + b end

0 commit comments

Comments
 (0)