Skip to content

Commit dae5af7

Browse files
authored
Merge pull request #198 from tromey/indentation-fix
fix rust indentation bug
2 parents 85befb9 + 5469d9b commit dae5af7

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

rust-mode-tests.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,34 @@ fn main() {
16281628
"
16291629
)))
16301630

1631+
(ert-deftest indent-function-after-where ()
1632+
(let ((rust-indent-method-chain t)) (test-indent
1633+
"
1634+
fn each_split_within<'a, F>(ss: &'a str, lim: usize, mut it: F)
1635+
-> bool where F: FnMut(&'a str) -> bool {
1636+
}
1637+
1638+
#[test]
1639+
fn test_split_within() {
1640+
}
1641+
"
1642+
)))
1643+
1644+
(ert-deftest indent-function-after-where-nested ()
1645+
(let ((rust-indent-method-chain t)) (test-indent
1646+
"
1647+
fn outer() {
1648+
fn each_split_within<'a, F>(ss: &'a str, lim: usize, mut it: F)
1649+
-> bool where F: FnMut(&'a str) -> bool {
1650+
}
1651+
#[test]
1652+
fn test_split_within() {
1653+
}
1654+
fn bar() {
1655+
}
1656+
}
1657+
"
1658+
)))
16311659

16321660
(ert-deftest test-for-issue-36-syntax-corrupted-state ()
16331661
"This is a test for a issue #36, which involved emacs's

rust-mode.el

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
(defconst rust-re-unsafe "unsafe")
3636
(defconst rust-re-extern "extern")
3737

38+
;;; Start of a Rust item
39+
(defvar rust-top-item-beg-re
40+
(concat "\\s-*\\(?:priv\\|pub\\)?\\s-*"
41+
(regexp-opt
42+
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
43+
"extern" "trait"))
44+
"\\_>"))
45+
3846
(defun rust-looking-back-str (str)
3947
"Like `looking-back' but for fixed strings rather than regexps (so that it's not so slow)"
4048
(let ((len (length str)))
@@ -394,6 +402,10 @@ buffer."
394402
;; our search for "where ".
395403
(let ((function-start nil) (function-level nil))
396404
(save-excursion
405+
;; If we're already at the start of a function,
406+
;; don't go back any farther. We can easily do
407+
;; this by moving to the end of the line first.
408+
(end-of-line)
397409
(rust-beginning-of-defun)
398410
(back-to-indentation)
399411
;; Avoid using multiple-value-bind
@@ -437,6 +449,10 @@ buffer."
437449
;; baseline as well.
438450
(looking-at "\\<else\\>\\|{\\|/[/*]")
439451

452+
;; If this is the start of a top-level item,
453+
;; stay on the baseline.
454+
(looking-at rust-top-item-beg-re)
455+
440456
(save-excursion
441457
(rust-rewind-irrelevant)
442458
;; Point is now at the end of the previous line
@@ -1136,13 +1152,6 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
11361152

11371153
;;; Defun Motions
11381154

1139-
;;; Start of a Rust item
1140-
(defvar rust-top-item-beg-re
1141-
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
1142-
(regexp-opt
1143-
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
1144-
"extern" "trait"))))
1145-
11461155
(defun rust-beginning-of-defun (&optional arg)
11471156
"Move backward to the beginning of the current defun.
11481157
@@ -1153,7 +1162,7 @@ This is written mainly to be used as `beginning-of-defun-function' for Rust.
11531162
Don't move to the beginning of the line. `beginning-of-defun',
11541163
which calls this, does that afterwards."
11551164
(interactive "p")
1156-
(re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)\\_>")
1165+
(re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)")
11571166
nil 'move (or arg 1)))
11581167

11591168
(defun rust-end-of-defun ()

0 commit comments

Comments
 (0)