Skip to content

Commit

Permalink
Emacs: stay at the correct position when indenting
Browse files Browse the repository at this point in the history
When indenting a non-blank line, stay at the same cursor position
relative to the content after indenting.
  • Loading branch information
MicahChalmer committed Feb 23, 2014
1 parent 7fbcda1 commit 45008f9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
67 changes: 66 additions & 1 deletion src/etc/emacs/rust-mode-tests.el
Expand Up @@ -488,6 +488,26 @@ struct Foo {
}
"
rust-test-region-string rust-test-motion-string
rust-test-indent-motion-string
"
fn blank_line(arg:int) -> bool {
}
fn indenting_closing_brace() {
if(true) {
}
}
fn indenting_middle_of_line() {
if(true) {
push_me_out();
} else {
pull_me_back_in();
}
}
"

;; Symbol -> (line column)
rust-test-positions-alist '((start-of-fn1 (2 0))
(start-of-fn1-middle-of-line (2 15))
Expand All @@ -502,7 +522,17 @@ struct Foo {
(middle-of-fn3 (16 4))
(middle-of-struct (21 10))
(before-start-of-struct (19 0))
(after-end-of-struct (23 0))))
(after-end-of-struct (23 0))
(blank-line-indent-start (3 0))
(blank-line-indent-target (3 4))
(closing-brace-indent-start (8 1))
(closing-brace-indent-target (8 5))
(middle-push-indent-start (13 2))
(middle-push-indent-target (13 9))
(after-whitespace-indent-start (13 1))
(after-whitespace-indent-target (13 8))
(middle-pull-indent-start (15 19))
(middle-pull-indent-target (15 12))))

(defun rust-get-buffer-pos (pos-symbol)
"Get buffer position from POS-SYMBOL.
Expand Down Expand Up @@ -664,3 +694,38 @@ All positions are position symbols found in `rust-test-positions-alist'."
'middle-of-struct
'before-start-of-struct 'after-end-of-struct
#'mark-defun))

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

(ert-deftest indent-line-closing-brace-motion ()
(rust-test-motion
rust-test-indent-motion-string
'closing-brace-indent-start
'closing-brace-indent-target
#'indent-for-tab-command))

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

(ert-deftest indent-line-after-whitespace-motion ()
(rust-test-motion
rust-test-indent-motion-string
'after-whitespace-indent-start
'after-whitespace-indent-target
#'indent-for-tab-command))

(ert-deftest indent-line-middle-pull-motion ()
(rust-test-motion
rust-test-indent-motion-string
'middle-pull-indent-start
'middle-pull-indent-target
#'indent-for-tab-command))
16 changes: 8 additions & 8 deletions src/etc/emacs/rust-mode.el
Expand Up @@ -114,14 +114,14 @@

;; Otherwise we're in a column-zero definition
(t 0))))))
(cond
;; If we're to the left of the indentation, reindent and jump to it.
((<= (current-column) indent)
(indent-line-to indent))

;; We're to the right; if it needs indent, do so but save excursion.
((not (eq (current-indentation) indent))
(save-excursion (indent-line-to indent))))))
(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))))))


;; Font-locking definitions and helpers
Expand Down

0 comments on commit 45008f9

Please sign in to comment.