Skip to content

Commit 2a81393

Browse files
committed
added optional method chain indentations for emacs major mode
1 parent 079f0c2 commit 2a81393

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

rust-mode.el

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
:type 'integer
5050
:group 'rust-mode)
5151

52+
(defcustom rust-indent-method-chain nil
53+
"Indent Rust method chains, aligned by the '.' operators"
54+
:type 'boolean
55+
:group 'rust-mode)
56+
5257
(defun rust-paren-level () (nth 0 (syntax-ppss)))
5358
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
5459
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
@@ -72,6 +77,15 @@
7277
(backward-word 1))
7378
(current-column))))
7479

80+
(defun rust-align-to-method-chain ()
81+
(save-excursion
82+
(previous-line)
83+
(end-of-line)
84+
(backward-word 1)
85+
(backward-char)
86+
(when (looking-at "\\..+\(.*\)\n")
87+
(- (current-column) rust-indent-offset))))
88+
7589
(defun rust-rewind-to-beginning-of-current-level-expr ()
7690
(let ((current-level (rust-paren-level)))
7791
(back-to-indentation)
@@ -94,10 +108,13 @@
94108
;; the inside of it correctly relative to the outside.
95109
(if (= 0 level)
96110
0
111+
(or
112+
(when rust-indent-method-chain
113+
(rust-align-to-method-chain))
97114
(save-excursion
98115
(backward-up-list)
99116
(rust-rewind-to-beginning-of-current-level-expr)
100-
(+ (current-column) rust-indent-offset)))))
117+
(+ (current-column) rust-indent-offset))))))
101118
(cond
102119
;; A function return type is indented to the corresponding function arguments
103120
((looking-at "->")
@@ -109,6 +126,16 @@
109126
;; A closing brace is 1 level unindended
110127
((looking-at "}") (- baseline rust-indent-offset))
111128

129+
;;Line up method chains by their .'s
130+
((when (and rust-indent-method-chain
131+
(looking-at "\..+\(.*\);?\n"))
132+
(or
133+
(let ((method-indent (rust-align-to-method-chain)))
134+
(when method-indent
135+
(+ method-indent rust-indent-offset)))
136+
(+ baseline rust-indent-offset))))
137+
138+
112139
;; Doc comments in /** style with leading * indent to line up the *s
113140
((and (nth 4 (syntax-ppss)) (looking-at "*"))
114141
(+ 1 baseline))

0 commit comments

Comments
 (0)