Skip to content

Commit eafb7a0

Browse files
committed
Properly fix #151
Always check whether we're not in a string or comment when looking for the `where` keyword. Use two helpers for this: `rust-looking-at-where` and `rust-rewind-to-where`.
1 parent b971c6d commit eafb7a0

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

rust-mode-tests.el

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ 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-comment ()
626+
(ert-deftest indent-align-where-in-comment1 ()
627627
(test-indent
628628
"/// - there must not exist an edge U->V in the graph where:
629629
#[derive(Clone, PartialEq, Eq)]
@@ -633,6 +633,27 @@ pub struct Region { // <-- this should be flush with left margin!
633633
}
634634
"))
635635

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+
"))
636657

637658
(ert-deftest indent-square-bracket-alignment ()
638659
(test-indent

rust-mode.el

Lines changed: 28 additions & 19 deletions
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,18 +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-
(and
257-
;; There is a where clause,
258-
(re-search-backward "\\bwhere\\b" function-start t)
259-
;; but not inside a string,
260-
(not (nth 3 (syntax-ppss)))
261-
;; nor inside a comment
262-
(not (nth 4 (syntax-ppss)))))
271+
(rust-rewind-to-where function-start))
263272
(= current-level function-level)))
264273
(goto-char function-start)))))
265274

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

399408
;; When the user chose not to indent the start of the where
400409
;; clause, put it on the baseline.
401-
((and (not rust-indent-where-clause) (looking-at "\\bwhere\\b"))
410+
((and (not rust-indent-where-clause)
411+
(rust-looking-at-where))
402412
baseline)
403413

404414
;; If we're in any other token-tree / sexp, then:
@@ -431,17 +441,16 @@ function or trait. When nil, where will be aligned with fn or trait."
431441
;; When we're not on a line starting with "where ", but
432442
;; still on a where-clause line, go to "where "
433443
(when (and
434-
(not (looking-at "\\bwhere\\b"))
444+
(not (rust-looking-at-where))
435445
;; We're looking at something like "F: ..."
436-
(and (looking-at (concat rust-re-ident ":"))
437-
;; There is a "where " somewhere after the
438-
;; start of the function.
439-
(re-search-backward "\\bwhere\\b"
440-
function-start t)
441-
;; Make sure we're not inside the function
442-
;; already (e.g. initializing a struct) by
443-
;; checking we are the same level.
444-
(= 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))
445454
;; skip over "where"
446455
(forward-char 5)
447456
;; Unless "where" is at the end of the line

0 commit comments

Comments
 (0)