Skip to content

Commit 7fbcda1

Browse files
committed
Fix emacs indentation of multi-line match patterns
Aligns to the same column if the previous line ends in a single '|' (but not a '||').
1 parent 52755b7 commit 7fbcda1

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/etc/emacs/rust-mode-tests.el

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,44 @@ fn foo()
425425
"
426426
))
427427

428+
(ert-deftest indent-match ()
429+
(test-indent
430+
"
431+
fn foo() {
432+
match blah {
433+
Pattern => stuff(),
434+
_ => whatever
435+
}
436+
}
437+
"
438+
))
439+
440+
(ert-deftest indent-match-multiline-pattern ()
441+
(test-indent
442+
"
443+
fn foo() {
444+
match blah {
445+
Pattern |
446+
Pattern2 => {
447+
hello()
448+
},
449+
_ => whatever
450+
}
451+
}
452+
"
453+
))
454+
455+
;; Make sure that in effort to cover match patterns we don't mistreat || or expressions
456+
(ert-deftest indent-nonmatch-or-expression ()
457+
(test-indent
458+
"
459+
fn foo() {
460+
let x = foo() ||
461+
bar();
462+
}
463+
"
464+
))
465+
428466
(setq rust-test-motion-string
429467
"
430468
fn fn1(arg: int) -> bool {

src/etc/emacs/rust-mode.el

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
;; - { means indent to either nesting-level * rust-indent-offset,
8686
;; or one further indent from that if either current line
8787
;; begins with 'else', or previous line didn't end in
88-
;; semi, comma or brace (other than whitespace and line
88+
;; semi, comma, brace or single pipe (other than whitespace and line
8989
;; comments) , and wasn't an attribute. But if we have
9090
;; something after the open brace and ending with a comma,
9191
;; treat it as fields and align them. PHEW.
@@ -105,8 +105,7 @@
105105
(beginning-of-line)
106106
(rust-rewind-irrelevant)
107107
(end-of-line)
108-
(if (looking-back
109-
"[[,;{}(][[:space:]]*\\(?://.*\\)?")
108+
(if (looking-back "\\(?:[(,:;?[{}]\\|[^|]|\\)[[:space:]]*\\(?://.*\\)?")
110109
(* rust-indent-offset level)
111110
(back-to-indentation)
112111
(if (looking-at "#")

0 commit comments

Comments
 (0)