Skip to content

Commit b23efef

Browse files
committed
Merge pull request #153 from mrBliss/fix-151
Fix #151
2 parents ed0b390 + eafb7a0 commit b23efef

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

rust-mode-tests.el

+32
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,38 @@ impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
623623
}
624624
"))
625625

626+
(ert-deftest indent-align-where-in-comment1 ()
627+
(test-indent
628+
"/// - there must not exist an edge U->V in the graph where:
629+
#[derive(Clone, PartialEq, Eq)]
630+
pub struct Region { // <-- this should be flush with left margin!
631+
entry: BasicBlockIndex,
632+
leaves: BTreeMap<BasicBlockIndex, usize>,
633+
}
634+
"))
635+
636+
(ert-deftest indent-align-where-in-comment2 ()
637+
(test-indent
638+
"fn foo<F,G>(f:F, g:G)
639+
where F:Send,
640+
// where
641+
G:Sized
642+
{
643+
let body;
644+
}
645+
"))
646+
647+
(ert-deftest indent-align-where-in-comment3 ()
648+
(test-indent
649+
"fn foo<F,G>(f:F, g:G)
650+
where F:Send,
651+
// where F:ThisIsNotActualCode,
652+
G:Sized
653+
{
654+
let body;
655+
}
656+
"))
657+
626658
(ert-deftest indent-square-bracket-alignment ()
627659
(test-indent
628660
"

rust-mode.el

+28-13
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ function or trait. When nil, where will be aligned with fn or trait."
218218
(rust-in-macro))
219219
)))
220220

221+
(defun rust-looking-at-where ()
222+
"Return T when looking at the \"where\" keyword."
223+
(and (looking-at-p "\\bwhere\\b")
224+
(not (rust-in-str-or-cmnt))))
225+
226+
(defun rust-rewind-to-where (&optional limit)
227+
"Rewind the point to the closest occurrence of the \"where\" keyword.
228+
Return T iff a where-clause was found. Does not rewind past
229+
LIMIT when passed, otherwise only stops at the beginning of the
230+
buffer."
231+
(when (re-search-backward "\\bwhere\\b" limit t)
232+
(if (rust-in-str-or-cmnt)
233+
(rust-rewind-to-where limit)
234+
t)))
235+
221236
(defun rust-align-to-expr-after-brace ()
222237
(save-excursion
223238
(forward-char)
@@ -248,12 +263,12 @@ function or trait. When nil, where will be aligned with fn or trait."
248263
(setq function-start (point)
249264
function-level (rust-paren-level)))
250265
;; On a where clause
251-
(when (or (looking-at "\\bwhere\\b")
266+
(when (or (rust-looking-at-where)
252267
;; or in one of the following lines, e.g.
253268
;; where A: Eq
254269
;; B: Hash <- on this line
255270
(and (save-excursion
256-
(re-search-backward "\\bwhere\\b" function-start t))
271+
(rust-rewind-to-where function-start))
257272
(= current-level function-level)))
258273
(goto-char function-start)))))
259274

@@ -392,7 +407,8 @@ function or trait. When nil, where will be aligned with fn or trait."
392407

393408
;; When the user chose not to indent the start of the where
394409
;; clause, put it on the baseline.
395-
((and (not rust-indent-where-clause) (looking-at "\\bwhere\\b"))
410+
((and (not rust-indent-where-clause)
411+
(rust-looking-at-where))
396412
baseline)
397413

398414
;; If we're in any other token-tree / sexp, then:
@@ -425,17 +441,16 @@ function or trait. When nil, where will be aligned with fn or trait."
425441
;; When we're not on a line starting with "where ", but
426442
;; still on a where-clause line, go to "where "
427443
(when (and
428-
(not (looking-at "\\bwhere\\b"))
444+
(not (rust-looking-at-where))
429445
;; We're looking at something like "F: ..."
430-
(and (looking-at (concat rust-re-ident ":"))
431-
;; There is a "where " somewhere after the
432-
;; start of the function.
433-
(re-search-backward "\\bwhere\\b"
434-
function-start t)
435-
;; Make sure we're not inside the function
436-
;; already (e.g. initializing a struct) by
437-
;; checking we are the same level.
438-
(= function-level level)))
446+
(looking-at (concat rust-re-ident ":"))
447+
;; There is a "where " somewhere after the
448+
;; start of the function.
449+
(rust-rewind-to-where function-start)
450+
;; Make sure we're not inside the function
451+
;; already (e.g. initializing a struct) by
452+
;; checking we are the same level.
453+
(= function-level level))
439454
;; skip over "where"
440455
(forward-char 5)
441456
;; Unless "where" is at the end of the line

0 commit comments

Comments
 (0)