Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emacs: always jump the cursor if needed on indent #12835

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/etc/emacs/rust-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ fn indenting_middle_of_line() {
pull_me_back_in();
}
}

fn indented_already() {

// The previous line already has its spaces
}
"

;; Symbol -> (line column)
Expand All @@ -596,7 +601,15 @@ fn indenting_middle_of_line() {
(after-whitespace-indent-start (13 1))
(after-whitespace-indent-target (13 8))
(middle-pull-indent-start (15 19))
(middle-pull-indent-target (15 12))))
(middle-pull-indent-target (15 12))
(blank-line-indented-already-bol-start (20 0))
(blank-line-indented-already-bol-target (20 4))
(blank-line-indented-already-middle-start (20 2))
(blank-line-indented-already-middle-target (20 4))
(nonblank-line-indented-already-bol-start (21 0))
(nonblank-line-indented-already-bol-target (21 4))
(nonblank-line-indented-already-middle-start (21 2))
(nonblank-line-indented-already-middle-target (21 4))))

(defun rust-get-buffer-pos (pos-symbol)
"Get buffer position from POS-SYMBOL.
Expand Down Expand Up @@ -793,3 +806,31 @@ All positions are position symbols found in `rust-test-positions-alist'."
'middle-pull-indent-start
'middle-pull-indent-target
#'indent-for-tab-command))

(ert-deftest indent-line-blank-line-indented-already-bol ()
(rust-test-motion
rust-test-indent-motion-string
'blank-line-indented-already-bol-start
'blank-line-indented-already-bol-target
#'indent-for-tab-command))

(ert-deftest indent-line-blank-line-indented-already-middle ()
(rust-test-motion
rust-test-indent-motion-string
'blank-line-indented-already-middle-start
'blank-line-indented-already-middle-target
#'indent-for-tab-command))

(ert-deftest indent-line-nonblank-line-indented-already-bol ()
(rust-test-motion
rust-test-indent-motion-string
'nonblank-line-indented-already-bol-start
'nonblank-line-indented-already-bol-target
#'indent-for-tab-command))

(ert-deftest indent-line-nonblank-line-indented-already-middle ()
(rust-test-motion
rust-test-indent-motion-string
'nonblank-line-indented-already-middle-start
'nonblank-line-indented-already-middle-target
#'indent-for-tab-command))
16 changes: 8 additions & 8 deletions src/etc/emacs/rust-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@
;; Otherwise, we are continuing the same expression from the previous line,
;; so add one additional indent level
(+ baseline rust-indent-offset))))))))))
(when (not (eq (current-indentation) indent))
;; If we're at the beginning of the line (before or at the current
;; indentation), jump with the indentation change. Otherwise, save the
;; excursion so that adding the indentations will leave us at the
;; equivalent position within the line to where we were before.
(if (<= (current-column) (current-indentation))
(indent-line-to indent)
(save-excursion (indent-line-to indent))))))

;; If we're at the beginning of the line (before or at the current
;; indentation), jump with the indentation change. Otherwise, save the
;; excursion so that adding the indentations will leave us at the
;; equivalent position within the line to where we were before.
(if (<= (current-column) (current-indentation))
(indent-line-to indent)
(save-excursion (indent-line-to indent)))))


;; Font-locking definitions and helpers
Expand Down