Skip to content

Commit d28d5b7

Browse files
MicahChalmeralexcrichton
authored andcommitted
Emacs: always jump the cursor if needed on indent
The rust-mode-indent-line function had a check, which ran after all the calculations for how to indent had already happened, that skipped actually performing the indent if the line was already at the right indentation. Because of that, the cursor did not jump to the indentation if the line wasn't changing. This was particularly annoying if there was nothing but spaces on the line and you were at the beginning of it--it looked like the indent just wasn't working. This removes the check and adds test cases to cover this.
1 parent aac6e31 commit d28d5b7

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ fn indenting_middle_of_line() {
570570
pull_me_back_in();
571571
}
572572
}
573+
574+
fn indented_already() {
575+
576+
// The previous line already has its spaces
577+
}
573578
"
574579

575580
;; Symbol -> (line column)
@@ -596,7 +601,15 @@ fn indenting_middle_of_line() {
596601
(after-whitespace-indent-start (13 1))
597602
(after-whitespace-indent-target (13 8))
598603
(middle-pull-indent-start (15 19))
599-
(middle-pull-indent-target (15 12))))
604+
(middle-pull-indent-target (15 12))
605+
(blank-line-indented-already-bol-start (20 0))
606+
(blank-line-indented-already-bol-target (20 4))
607+
(blank-line-indented-already-middle-start (20 2))
608+
(blank-line-indented-already-middle-target (20 4))
609+
(nonblank-line-indented-already-bol-start (21 0))
610+
(nonblank-line-indented-already-bol-target (21 4))
611+
(nonblank-line-indented-already-middle-start (21 2))
612+
(nonblank-line-indented-already-middle-target (21 4))))
600613

601614
(defun rust-get-buffer-pos (pos-symbol)
602615
"Get buffer position from POS-SYMBOL.
@@ -793,3 +806,31 @@ All positions are position symbols found in `rust-test-positions-alist'."
793806
'middle-pull-indent-start
794807
'middle-pull-indent-target
795808
#'indent-for-tab-command))
809+
810+
(ert-deftest indent-line-blank-line-indented-already-bol ()
811+
(rust-test-motion
812+
rust-test-indent-motion-string
813+
'blank-line-indented-already-bol-start
814+
'blank-line-indented-already-bol-target
815+
#'indent-for-tab-command))
816+
817+
(ert-deftest indent-line-blank-line-indented-already-middle ()
818+
(rust-test-motion
819+
rust-test-indent-motion-string
820+
'blank-line-indented-already-middle-start
821+
'blank-line-indented-already-middle-target
822+
#'indent-for-tab-command))
823+
824+
(ert-deftest indent-line-nonblank-line-indented-already-bol ()
825+
(rust-test-motion
826+
rust-test-indent-motion-string
827+
'nonblank-line-indented-already-bol-start
828+
'nonblank-line-indented-already-bol-target
829+
#'indent-for-tab-command))
830+
831+
(ert-deftest indent-line-nonblank-line-indented-already-middle ()
832+
(rust-test-motion
833+
rust-test-indent-motion-string
834+
'nonblank-line-indented-already-middle-start
835+
'nonblank-line-indented-already-middle-target
836+
#'indent-for-tab-command))

src/etc/emacs/rust-mode.el

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@
140140
;; Otherwise, we are continuing the same expression from the previous line,
141141
;; so add one additional indent level
142142
(+ baseline rust-indent-offset))))))))))
143-
(when (not (eq (current-indentation) indent))
144-
;; If we're at the beginning of the line (before or at the current
145-
;; indentation), jump with the indentation change. Otherwise, save the
146-
;; excursion so that adding the indentations will leave us at the
147-
;; equivalent position within the line to where we were before.
148-
(if (<= (current-column) (current-indentation))
149-
(indent-line-to indent)
150-
(save-excursion (indent-line-to indent))))))
143+
144+
;; If we're at the beginning of the line (before or at the current
145+
;; indentation), jump with the indentation change. Otherwise, save the
146+
;; excursion so that adding the indentations will leave us at the
147+
;; equivalent position within the line to where we were before.
148+
(if (<= (current-column) (current-indentation))
149+
(indent-line-to indent)
150+
(save-excursion (indent-line-to indent)))))
151151

152152

153153
;; Font-locking definitions and helpers

0 commit comments

Comments
 (0)