Skip to content

Commit 8e3efc1

Browse files
committed
auto merge of #8872 : MicahChalmer/rust/emacs-indent-fix, r=pnkfelix
This fixes some, but not all, of the issues mentioned in #8787
2 parents f914253 + 7a42dd8 commit 8e3efc1

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/etc/emacs/rust-mode.el

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
;; Url: https://github.com/mozilla/rust
66

77
(eval-when-compile (require 'cl))
8+
(eval-when-compile (require 'misc))
89

910
;; Syntax definitions and helpers
1011
(defvar rust-mode-syntax-table
@@ -57,19 +58,39 @@
5758
;; A closing brace is 1 level unindended
5859
((looking-at "}") (* rust-indent-offset (- level 1)))
5960

61+
; Doc comments in /** style with leading * indent to line up the *s
62+
((and (nth 4 (syntax-ppss)) (looking-at "*"))
63+
(+ 1 (* rust-indent-offset level)))
64+
6065
;; If we're in any other token-tree / sexp, then:
6166
;; - [ or ( means line up with the opening token
6267
;; - { means indent to either nesting-level * rust-indent-offset,
6368
;; or one further indent from that if either current line
6469
;; begins with 'else', or previous line didn't end in
65-
;; semi, comma or brace, and wasn't an attribute. PHEW.
70+
;; semi, comma or brace (other than whitespace and line
71+
;; comments) , and wasn't an attribute. But if we have
72+
;; something after the open brace and ending with a comma,
73+
;; treat it as fields and align them. PHEW.
6674
((> level 0)
6775
(let ((pt (point)))
6876
(rust-rewind-irrelevant)
6977
(backward-up-list)
70-
(if (looking-at "[[(]")
71-
(+ 1 (current-column))
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 "{[[:blank:]]*[^}\n]*,[[:space:]]*$")
7289
(progn
90+
(forward-char)
91+
(forward-to-word 1)
92+
(current-column)))
93+
(t (progn
7394
(goto-char pt)
7495
(back-to-indentation)
7596
(if (looking-at "\\<else\\>")
@@ -79,12 +100,12 @@
79100
(beginning-of-line)
80101
(rust-rewind-irrelevant)
81102
(end-of-line)
82-
(if (looking-back "[{};,]")
103+
(if (looking-back "[,;{}(][[:space:]]*\\(?://.*\\)?")
83104
(* rust-indent-offset level)
84105
(back-to-indentation)
85106
(if (looking-at "#")
86107
(* rust-indent-offset level)
87-
(* rust-indent-offset (+ 1 level))))))))))
108+
(* rust-indent-offset (+ 1 level)))))))))))
88109

89110
;; Otherwise we're in a column-zero definition
90111
(t 0))))))

0 commit comments

Comments
 (0)