Allow offset in tree-sitter indent rules to be functions

This is needed for fixing C indentation.  See next comment.

* doc/lispref/modes.texi (Parser-based Indentation): Update manual.
* lisp/treesit.el (treesit-simple-indent): Try evaluating OFFSET as a
function if it's not integer nor variable.
This commit is contained in:
Yuan Fu 2023-01-15 00:15:25 -08:00
parent d13a329acf
commit c78e19d99c
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 17 additions and 10 deletions

View file

@ -4926,8 +4926,7 @@ the current line to @var{matcher}; if it returns non-@code{nil}, this
rule is applicable. Then Emacs passes the node to @var{anchor}, which rule is applicable. Then Emacs passes the node to @var{anchor}, which
returns a buffer position. Emacs takes the column number of that returns a buffer position. Emacs takes the column number of that
position, adds @var{offset} to it, and the result is the indentation position, adds @var{offset} to it, and the result is the indentation
column for the current line. @var{offset} can be an integer or a column for the current line.
variable whose value is an integer.
The @var{matcher} and @var{anchor} are functions, and Emacs provides The @var{matcher} and @var{anchor} are functions, and Emacs provides
convenient defaults for them. convenient defaults for them.
@ -4943,10 +4942,13 @@ inside a multi-line string, no node can start at that position, so
@var{node} is @code{nil}. In that case, @var{parent} would be the @var{node} is @code{nil}. In that case, @var{parent} would be the
smallest node that spans that position. smallest node that spans that position.
Emacs finds @var{bol}, @var{node} and @var{parent} and @var{matcher} should return non-@code{nil} if the rule is applicable,
passes them to each @var{matcher} and @var{anchor}. @var{matcher} and @var{anchor} should return a buffer position.
should return non-@code{nil} if the rule is applicable, and
@var{anchor} should return a buffer position. @var{offset} can be an integer, a variable whose value is an integer,
or a function that returns an integer. If it is a function, it is
passed @var{node}, @var{parent}, and @var{bol}, like matchers and
anchors.
@end defvar @end defvar
@defvar treesit-simple-indent-presets @defvar treesit-simple-indent-presets

View file

@ -1511,10 +1511,15 @@ OFFSET."
return return
(let ((anchor-pos (let ((anchor-pos
(treesit--simple-indent-eval (treesit--simple-indent-eval
(list anchor node parent bol)))) (list anchor node parent bol)))
(cons anchor-pos (if (symbolp offset) (offset-val
(symbol-value offset) (cond ((numberp offset) offset)
offset))) ((and (symbolp offset)
(boundp offset))
(symbol-value offset))
(t (treesit--simple-indent-eval
(list offset node parent bol))))))
(cons anchor-pos offset-val))
finally return finally return
(progn (when treesit--indent-verbose (progn (when treesit--indent-verbose
(message "No matched rule")) (message "No matched rule"))