[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
feature/tree-sitter ce5ae0117e 1/2: Refine handling of node at point in
From: |
Yuan Fu |
Subject: |
feature/tree-sitter ce5ae0117e 1/2: Refine handling of node at point in treesit-indent |
Date: |
Sat, 29 Oct 2022 12:51:40 -0400 (EDT) |
branch: feature/tree-sitter
commit ce5ae0117e3ea009ccdd8d8f4c1365301d24f90b
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Refine handling of node at point in treesit-indent
* doc/lispref/modes.texi (Parser-based Indentation): Document the
semantic clearly.
* lisp/treesit.el (treesit-simple-indent-presets): Replace with
current-indentation.
(treesit--indent-1): Now NODE always starts at BOL, and parent is
always the smallest node above NODE that spans BOL.
(treesit-indent): Don't use save-excurtion, no one else does.
---
doc/lispref/modes.texi | 7 ++++++-
lisp/treesit.el | 18 +++++-------------
2 files changed, 11 insertions(+), 14 deletions(-)
diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi
index f587252422..6212c7cd87 100644
--- a/doc/lispref/modes.texi
+++ b/doc/lispref/modes.texi
@@ -4806,7 +4806,12 @@ arguments: @var{node}, @var{parent}, and @var{bol}. The
argument
position of the first non-whitespace character after the beginning of
the line. The argument @var{node} is the largest (highest-in-tree)
node that starts at that position; and @var{parent} is the parent of
-@var{node}. Emacs finds @var{bol}, @var{node} and @var{parent} and
+@var{node}. However, when that position is on a whitespace or inside
+a multi-line string, no node that starts at that position, so
+@var{node} is @code{nil}. In that case, @var{parent} would be the
+smallest node that spans that position.
+
+Emacs finds @var{bol}, @var{node} and @var{parent} and
passes them to each @var{matcher} and @var{anchor}. @var{matcher}
should return non-@code{nil} if the rule is applicable, and
@var{anchor} should return a buffer position.
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 22d1fe5f89..dd0aca5049 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -783,8 +783,7 @@ See `treesit-simple-indent-presets'.")
(lambda (_n parent &rest _)
(save-excursion
(goto-char (treesit-node-start parent))
- (back-to-indentation)
- (point)))))
+ (current-indentation)))))
(prev-sibling . ,(byte-compile
(lambda (node &rest _)
(treesit-node-start
@@ -951,7 +950,7 @@ Return (ANCHOR . OFFSET). This function is used by
(smallest-node
(cond ((null (treesit-parser-list)) nil)
((eq 1 (length (treesit-parser-list)))
- (treesit-node-at bol))
+ (treesit-node-at bol nil nil t))
((treesit-language-at (point))
(treesit-node-at bol (treesit-language-at (point))))
(t (treesit-node-at bol))))
@@ -968,26 +967,19 @@ Return (ANCHOR . OFFSET). This function is used by
;; encompass the whitespace.
(parent (cond ((and node parser)
(treesit-node-parent node))
- (parser
- (treesit-node-at bol parser))
- (t nil))))
+ (t (treesit-node-on bol bol)))))
(funcall treesit-indent-function node parent bol))))
(defun treesit-indent ()
"Indent according to the result of `treesit-indent-function'."
(treesit-update-ranges)
- (pcase-let* ((orig-pos (point))
- (bol (current-indentation))
- (`(,anchor . ,offset) (treesit--indent-1)))
+ (pcase-let* ((`(,anchor . ,offset) (treesit--indent-1)))
(when (and anchor offset)
(let ((col (+ (save-excursion
(goto-char anchor)
(current-column))
offset)))
- (if (< bol orig-pos)
- (save-excursion
- (indent-line-to col))
- (indent-line-to col))))))
+ (indent-line-to col)))))
(defvar treesit--indent-region-batch-size 400
"How many lines of indent value do we precompute.