Skip to content

Commit 9ef0b9c

Browse files
committed
Allow indenting to align struct fields after curly brace
1 parent 1c527df commit 9ef0b9c

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/etc/emacs/rust-mode.el

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
(if (/= starting (point))
4646
(rust-rewind-irrelevant))))
4747

48+
(defun rust-first-indent-after-brace ()
49+
(save-excursion
50+
(forward-char)
51+
(if (looking-at "[[:blank:]]*\\(?://.*\\)?$")
52+
;; We don't want to indent out to the open bracket if the
53+
;; open bracket ends the line
54+
(* rust-indent-offset (rust-paren-level))
55+
(when (looking-at "[[:space:]]") (forward-to-word 1))
56+
(current-column))))
57+
4858
(defun rust-mode-indent-line ()
4959
(interactive)
5060
(let ((indent
@@ -58,7 +68,7 @@
5868
;; A closing brace is 1 level unindended
5969
((looking-at "}") (* rust-indent-offset (- level 1)))
6070

61-
; Doc comments in /** style with leading * indent to line up the *s
71+
;; Doc comments in /** style with leading * indent to line up the *s
6272
((and (nth 4 (syntax-ppss)) (looking-at "*"))
6373
(+ 1 (* rust-indent-offset level)))
6474

@@ -75,22 +85,9 @@
7585
(let ((pt (point)))
7686
(rust-rewind-irrelevant)
7787
(backward-up-list)
78-
(cond
79-
((and
80-
(looking-at "[[(]")
81-
; We don't want to indent out to the open bracket if the
82-
; open bracket ends the line
83-
(save-excursion
84-
(forward-char)
85-
(not (looking-at "[[:space:]]*\\(?://.*\\)?$"))))
86-
(+ 1 (current-column)))
87-
;; Check for fields on the same line as the open curly brace:
88-
((looking-at "{[[:space:]]*[^\n]*,[[:space:]]*$")
88+
(if (looking-at "[[({]")
89+
(rust-first-indent-after-brace)
8990
(progn
90-
(forward-char)
91-
(when (looking-at "[[:space:]]") (forward-to-word 1))
92-
(current-column)))
93-
(t (progn
9491
(goto-char pt)
9592
(back-to-indentation)
9693
(if (looking-at "\\<else\\>")
@@ -105,7 +102,7 @@
105102
(back-to-indentation)
106103
(if (looking-at "#")
107104
(* rust-indent-offset level)
108-
(* rust-indent-offset (+ 1 level)))))))))))
105+
(* rust-indent-offset (+ 1 level))))))))))
109106

110107
;; Otherwise we're in a column-zero definition
111108
(t 0))))))

0 commit comments

Comments
 (0)