>From b8af08cbe03b66437901af2164e3b31c7df46f04 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sat, 29 Oct 2022 21:03:23 +0200 Subject: [PATCH] Fix indent-line-function * lisp/treesit.el (treesit-simple-indent-presets): We need the actual position, not the indentation offset from column 0. Revert earlier change. (treesit-indent): Revert earlier change to preserve behavior with similar functions for other modes. Specifically. We want to preserve position in text if we are "in front of" the indentation goal. Otherwise just jump to the correct indentation. --- lisp/treesit.el | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index dd0aca5049..4032af5f14 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -783,7 +783,8 @@ treesit-simple-indent-presets (lambda (_n parent &rest _) (save-excursion (goto-char (treesit-node-start parent)) - (current-indentation))))) + (back-to-indentation) + (point))))) (prev-sibling . ,(byte-compile (lambda (node &rest _) (treesit-node-start @@ -973,13 +974,19 @@ treesit--indent-1 (defun treesit-indent () "Indent according to the result of `treesit-indent-function'." (treesit-update-ranges) - (pcase-let* ((`(,anchor . ,offset) (treesit--indent-1))) + (pcase-let* ((orig-pos (point)) + (bol (save-excursion + (back-to-indentation) + (point))) + (`(,anchor . ,offset) (treesit--indent-1))) (when (and anchor offset) (let ((col (+ (save-excursion (goto-char anchor) (current-column)) offset))) - (indent-line-to col))))) + (if (< bol orig-pos) + (save-excursion (indent-line-to col)) + (indent-line-to col)))))) (defvar treesit--indent-region-batch-size 400 "How many lines of indent value do we precompute. -- 2.34.1