Skip to content

Commit d06baaa

Browse files
authored
Merge pull request #62 from ttn-ttn/master
Indent method calls when they happen on a new line
2 parents 14f9b9b + 5e36e7f commit d06baaa

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

raku-indent.el

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(defconst raku-smie-grammar
1313
(smie-prec2->grammar
1414
(smie-precs->prec2
15-
'((assoc ";") (assoc "=") (assoc ",") (left ":")))))
15+
'((assoc ";") (assoc "=") (assoc ",") (left ":") (left ".")))))
1616

1717
(defcustom raku-indent-offset 4
1818
"Basic size of one indentation step."
@@ -33,8 +33,33 @@
3333
;; if this does NOT match, we are not in an interpolation
3434
(not (looking-back "#{\\$" (- (point) 3))))))
3535

36+
(defun raku-smie--method-chain-p ()
37+
"Check if current line starts with a method call (dot operator)."
38+
(save-excursion
39+
(beginning-of-line)
40+
(skip-chars-forward " \t")
41+
(eq (char-after) ?\.)))
42+
43+
(defun raku-smie--find-chain-root-indentation ()
44+
"Find the indentation of the root line in a method chain."
45+
(save-excursion
46+
;; Move to previous line and scan backward to find the chain root
47+
(forward-line -1)
48+
(end-of-line)
49+
;; Keep going back while we see lines starting with dots
50+
(while (and (not (bobp))
51+
(save-excursion
52+
(beginning-of-line)
53+
(skip-chars-forward " \t")
54+
(eq (char-after) ?\.)))
55+
(forward-line -1)
56+
(end-of-line))
57+
;; Now we're at the root line, return its indentation
58+
(beginning-of-line)
59+
(current-indentation)))
60+
3661
(defun raku-smie--forward-token ()
37-
(cond
62+
(cond
3863
;; Return `;` to fudge end-of-block indentation (I think), as ; is optional after a block
3964
((and (eq (char-before) ?\}) ;; Character immediately prior to point is `}`
4065
(raku-smie--not-interpolation-p) ;; And, not in an interpolation
@@ -51,7 +76,7 @@
5176
"=")
5277

5378
((progn (forward-comment (point-max)) ;; Read past ALL comments
54-
(looking-at "[;,:]")) ;; Are we looking at ; , or :
79+
(looking-at "[;,:.])")) ;; Are we looking at ; , : or .
5580

5681
(forward-char 1) ;; If so, advance one character
5782
(match-string 0)) ;; And then return whatever looking-at found (?)
@@ -74,8 +99,8 @@
7499
(forward-char -1)
75100
"=")
76101

77-
;; Cond #2 - Get whatever precedes [,:;]
78-
((memq (char-before) '(?\; ?\, ?\:)) ;; Point is preceded immediately by `;`, `,`, or `:`
102+
;; Cond #2 - Get whatever precedes [,:;.]
103+
((memq (char-before) '(?\; ?\, ?\: ?\.)) ;; Point is preceded immediately by `;`, `,`, `:`, or `.`
79104
(forward-char -1) ;; Retreat one char
80105
(string (char-after))) ;; Return char after point (the char we just retreated past)
81106

@@ -103,7 +128,12 @@
103128

104129
(`(:before . ,(or "{" "("))
105130
(if (smie-rule-hanging-p)
106-
(smie-rule-parent 0)))))
131+
(smie-rule-parent 0)))
132+
133+
;; Method chaining indentation
134+
(`(:before . ".")
135+
(when (raku-smie--method-chain-p)
136+
(+ (raku-smie--find-chain-root-indentation) raku-indent-offset)))))
107137

108138
(provide 'raku-indent)
109139

0 commit comments

Comments
 (0)