diff options
| author | Yuan Fu | 2022-10-29 11:50:15 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-10-29 11:59:45 -0700 |
| commit | a915d836fce238cd57233f67e8d2451a26d2fb6b (patch) | |
| tree | 051c3686fdcf41d70e6a76a32292b2cf4364fb24 | |
| parent | 8607f018fd8bef7443415ff2e356d035843ad828 (diff) | |
| download | emacs-a915d836fce238cd57233f67e8d2451a26d2fb6b.tar.gz emacs-a915d836fce238cd57233f67e8d2451a26d2fb6b.zip | |
Keep cursor position in treesit-indent
* lisp/treesit.el (treesit-indent): Create a marker and jump back to
that marker after indentation.
| -rw-r--r-- | lisp/treesit.el | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el index dd0aca50494..90e5d09693f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -974,12 +974,18 @@ Return (ANCHOR . OFFSET). This function is used by | |||
| 974 | "Indent according to the result of `treesit-indent-function'." | 974 | "Indent according to the result of `treesit-indent-function'." |
| 975 | (treesit-update-ranges) | 975 | (treesit-update-ranges) |
| 976 | (pcase-let* ((`(,anchor . ,offset) (treesit--indent-1))) | 976 | (pcase-let* ((`(,anchor . ,offset) (treesit--indent-1))) |
| 977 | (when (and anchor offset) | 977 | (if (and anchor offset) |
| 978 | (let ((col (+ (save-excursion | 978 | (let ((col (+ (save-excursion |
| 979 | (goto-char anchor) | 979 | (goto-char anchor) |
| 980 | (current-column)) | 980 | (current-column)) |
| 981 | offset))) | 981 | offset)) |
| 982 | (indent-line-to col))))) | 982 | (delta (- (point-max) (point)))) |
| 983 | (indent-line-to col) | ||
| 984 | ;; Now point is at the end of indentation. If we started | ||
| 985 | ;; from within the line, go back to where we started. | ||
| 986 | (when (> (- (point-max) delta) (point)) | ||
| 987 | (goto-char (- (point-max) delta)))) | ||
| 988 | 'noindent))) | ||
| 983 | 989 | ||
| 984 | (defvar treesit--indent-region-batch-size 400 | 990 | (defvar treesit--indent-region-batch-size 400 |
| 985 | "How many lines of indent value do we precompute. | 991 | "How many lines of indent value do we precompute. |