Skip to content

Commit 60d83a7

Browse files
committed
Add fn for comparing line numbers.
Elixir is quite flexible wrt syntax around the `->` token. It can follow `do` or `fn`, either on the same line or separately. Because of this, a function that can detect whether it's on the same line as its parent was needed. After adding this function, all tests passed. I'm skeptical about the robustness of this function but it will have to be battle hardened by users reporting bugs.
1 parent 56e125b commit 60d83a7

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

elixir-smie.el

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@
106106
(progn (move-end-of-line 1)
107107
(looking-back elixir-smie--block-operator-regexp)))))
108108

109+
(defun elixir-smie--same-line-as-parent (parent-pos child-pos)
110+
"Return non-nil if `child-pos' is on same line as `parent-pos'."
111+
(= (line-number-at-pos parent-pos) (line-number-at-pos child-pos)))
112+
109113
(defun elixir-smie-forward-token ()
110114
(cond
111115
;; If there is nothing but whitespace between the last token and eol, emit
@@ -184,8 +188,29 @@
184188
(smie-rule-parent elixir-smie-indent-basic))))
185189

186190
(`(:after . "->")
187-
(if (smie-rule-hanging-p)
188-
elixir-smie-indent-basic))
191+
(cond
192+
;; This first condition is kind of complicated so I'll try to make this
193+
;; comment as clear as possible.
194+
195+
;; "If `->' is the last thing on the line, and its parent token
196+
;; is `fn' ..."
197+
((and (smie-rule-hanging-p)
198+
(smie-rule-parent-p "fn"))
199+
;; "... and if:
200+
201+
;; 1. `smie--parent' is non-nil
202+
;; 2. the `->' token in question is on the same line as its parent (if
203+
;; the logic has gotten this far, its parent will be `fn')
204+
205+
;; ... then indent the line after the `->' aligned with the
206+
;; parent, offset by `elixir-smie-indent-basic'."
207+
(if (and smie--parent (elixir-smie--same-line-as-parent
208+
(nth 1 smie--parent)
209+
(point)))
210+
(smie-rule-parent elixir-smie-indent-basic)))
211+
;; Otherwise, if just indent by two.
212+
((smie-rule-hanging-p)
213+
elixir-smie-indent-basic)))
189214

190215
(`(:before . ";")
191216
(cond

0 commit comments

Comments
 (0)