Skip to content

Commit 4659aee

Browse files
committed
rollup merge of #19913: KOMON/rust-mode-emacs-indentation
I added an option to auto-indent method chains to line up along their '.' operators. Like so: ``` let input = io::stdin().readline() .ok() .expect("Failed to read line"); ``` The old default would indent like so: ``` let input = io::stdin().readme() .ok() .expect("Failed to read line"); ``` The Rust guide explicitly condones the former, so I thought it would be nice for the emacs mode to support it. It's off by default, you have to set ```rust-indent-method-chain``` to ```t``` via your .emacs or the customize menu
2 parents 186583d + f292554 commit 4659aee

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

rust-mode.el

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
:type 'integer
5555
:group 'rust-mode)
5656

57+
(defcustom rust-indent-method-chain nil
58+
"Indent Rust method chains, aligned by the '.' operators"
59+
:type 'boolean
60+
:group 'rust-mode)
61+
5762
(defun rust-paren-level () (nth 0 (syntax-ppss)))
5863
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
5964
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
@@ -73,10 +78,19 @@
7378
;; open bracket ends the line
7479
(when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
7580
(when (looking-at "[[:space:]]")
76-
(forward-word 1)
77-
(backward-word 1))
81+
(forward-word 1)
82+
(backward-word 1))
7883
(current-column))))
7984

85+
(defun rust-align-to-method-chain ()
86+
(save-excursion
87+
(previous-line)
88+
(end-of-line)
89+
(backward-word 1)
90+
(backward-char)
91+
(when (looking-at "\\..+\(.*\)\n")
92+
(- (current-column) rust-indent-offset))))
93+
8094
(defun rust-rewind-to-beginning-of-current-level-expr ()
8195
(let ((current-level (rust-paren-level)))
8296
(back-to-indentation)
@@ -99,10 +113,13 @@
99113
;; the inside of it correctly relative to the outside.
100114
(if (= 0 level)
101115
0
116+
(or
117+
(when rust-indent-method-chain
118+
(rust-align-to-method-chain))
102119
(save-excursion
103120
(backward-up-list)
104121
(rust-rewind-to-beginning-of-current-level-expr)
105-
(+ (current-column) rust-indent-offset)))))
122+
(+ (current-column) rust-indent-offset))))))
106123
(cond
107124
;; A function return type is indented to the corresponding function arguments
108125
((looking-at "->")
@@ -114,6 +131,16 @@
114131
;; A closing brace is 1 level unindended
115132
((looking-at "}") (- baseline rust-indent-offset))
116133

134+
;;Line up method chains by their .'s
135+
((when (and rust-indent-method-chain
136+
(looking-at "\..+\(.*\);?\n"))
137+
(or
138+
(let ((method-indent (rust-align-to-method-chain)))
139+
(when method-indent
140+
(+ method-indent rust-indent-offset)))
141+
(+ baseline rust-indent-offset))))
142+
143+
117144
;; Doc comments in /** style with leading * indent to line up the *s
118145
((and (nth 4 (syntax-ppss)) (looking-at "*"))
119146
(+ 1 baseline))

0 commit comments

Comments
 (0)