Skip to content

Commit 45008f9

Browse files
committed
Emacs: stay at the correct position when indenting
When indenting a non-blank line, stay at the same cursor position relative to the content after indenting.
1 parent 7fbcda1 commit 45008f9

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,26 @@ struct Foo {
488488
}
489489
"
490490
rust-test-region-string rust-test-motion-string
491+
rust-test-indent-motion-string
492+
"
493+
fn blank_line(arg:int) -> bool {
494+
495+
}
496+
497+
fn indenting_closing_brace() {
498+
if(true) {
499+
}
500+
}
501+
502+
fn indenting_middle_of_line() {
503+
if(true) {
504+
push_me_out();
505+
} else {
506+
pull_me_back_in();
507+
}
508+
}
509+
"
510+
491511
;; Symbol -> (line column)
492512
rust-test-positions-alist '((start-of-fn1 (2 0))
493513
(start-of-fn1-middle-of-line (2 15))
@@ -502,7 +522,17 @@ struct Foo {
502522
(middle-of-fn3 (16 4))
503523
(middle-of-struct (21 10))
504524
(before-start-of-struct (19 0))
505-
(after-end-of-struct (23 0))))
525+
(after-end-of-struct (23 0))
526+
(blank-line-indent-start (3 0))
527+
(blank-line-indent-target (3 4))
528+
(closing-brace-indent-start (8 1))
529+
(closing-brace-indent-target (8 5))
530+
(middle-push-indent-start (13 2))
531+
(middle-push-indent-target (13 9))
532+
(after-whitespace-indent-start (13 1))
533+
(after-whitespace-indent-target (13 8))
534+
(middle-pull-indent-start (15 19))
535+
(middle-pull-indent-target (15 12))))
506536

507537
(defun rust-get-buffer-pos (pos-symbol)
508538
"Get buffer position from POS-SYMBOL.
@@ -664,3 +694,38 @@ All positions are position symbols found in `rust-test-positions-alist'."
664694
'middle-of-struct
665695
'before-start-of-struct 'after-end-of-struct
666696
#'mark-defun))
697+
698+
(ert-deftest indent-line-blank-line-motion ()
699+
(rust-test-motion
700+
rust-test-indent-motion-string
701+
'blank-line-indent-start
702+
'blank-line-indent-target
703+
#'indent-for-tab-command))
704+
705+
(ert-deftest indent-line-closing-brace-motion ()
706+
(rust-test-motion
707+
rust-test-indent-motion-string
708+
'closing-brace-indent-start
709+
'closing-brace-indent-target
710+
#'indent-for-tab-command))
711+
712+
(ert-deftest indent-line-middle-push-motion ()
713+
(rust-test-motion
714+
rust-test-indent-motion-string
715+
'middle-push-indent-start
716+
'middle-push-indent-target
717+
#'indent-for-tab-command))
718+
719+
(ert-deftest indent-line-after-whitespace-motion ()
720+
(rust-test-motion
721+
rust-test-indent-motion-string
722+
'after-whitespace-indent-start
723+
'after-whitespace-indent-target
724+
#'indent-for-tab-command))
725+
726+
(ert-deftest indent-line-middle-pull-motion ()
727+
(rust-test-motion
728+
rust-test-indent-motion-string
729+
'middle-pull-indent-start
730+
'middle-pull-indent-target
731+
#'indent-for-tab-command))

src/etc/emacs/rust-mode.el

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@
114114

115115
;; Otherwise we're in a column-zero definition
116116
(t 0))))))
117-
(cond
118-
;; If we're to the left of the indentation, reindent and jump to it.
119-
((<= (current-column) indent)
120-
(indent-line-to indent))
121-
122-
;; We're to the right; if it needs indent, do so but save excursion.
123-
((not (eq (current-indentation) indent))
124-
(save-excursion (indent-line-to indent))))))
117+
(when (not (eq (current-indentation) indent))
118+
;; If we're at the beginning of the line (before or at the current
119+
;; indentation), jump with the indentation change. Otherwise, save the
120+
;; excursion so that adding the indentations will leave us at the
121+
;; equivalent position within the line to where we were before.
122+
(if (<= (current-column) (current-indentation))
123+
(indent-line-to indent)
124+
(save-excursion (indent-line-to indent))))))
125125

126126

127127
;; Font-locking definitions and helpers

0 commit comments

Comments
 (0)