Improve tree-sitter indent anchor prev-adaptive-prefix (bug#61314)
Now prev-adaptive-prefix looks at the current line and checks if it begins with a prefix itself. If it does, prev-adaptive-prefix tries to place the anchor before the prefix on the previous line, rather than after it. - prev line - this line -> This line starts with a "-", i.e., begins with a prefix, so we place the anchor at the beginning of the "-" of the previous line, rather than after it - prev line this line -> This line doesn't start with a prefix, so the anchor is placed after the previous line's "-". * doc/lispref/modes.texi (Parser-based Indentation): Update manual. * lisp/treesit.el: (treesit-simple-indent-presets): Add local variable this-line-has-prefix, base what anchor to return on the value of this-line-has-prefix and whether the prev line has a prefix.
This commit is contained in:
parent
2e6093b425
commit
afbce8bb46
3 changed files with 44 additions and 10 deletions
|
@ -5111,8 +5111,11 @@ This anchor is a function that is called with 3 arguments: @var{node},
|
||||||
@var{parent}, and @var{bol}. It tries to go to the beginning of the
|
@var{parent}, and @var{bol}. It tries to go to the beginning of the
|
||||||
previous non-empty line, and matches @code{adaptive-fill-regexp}. If
|
previous non-empty line, and matches @code{adaptive-fill-regexp}. If
|
||||||
there is a match, this function returns the end of the match,
|
there is a match, this function returns the end of the match,
|
||||||
otherwise it returns nil. This anchor is useful for a
|
otherwise it returns nil. However, if the current line begins with a
|
||||||
@code{indent-relative}-like indent behavior for block comments.
|
prefix (e.g., ``-''), return the beginning of the prefix of the
|
||||||
|
previous line instead, so that the two prefixes aligns. This anchor
|
||||||
|
is useful for a @code{indent-relative}-like indent behavior for block
|
||||||
|
comments.
|
||||||
|
|
||||||
@end ftable
|
@end ftable
|
||||||
@end defvar
|
@end defvar
|
||||||
|
|
|
@ -1186,12 +1186,18 @@ See `treesit-simple-indent-presets'.")
|
||||||
(skip-syntax-backward "-")
|
(skip-syntax-backward "-")
|
||||||
(point))))
|
(point))))
|
||||||
(cons 'prev-adaptive-prefix
|
(cons 'prev-adaptive-prefix
|
||||||
(lambda (_n parent &rest _)
|
(lambda (_n parent bol &rest _)
|
||||||
(let ((comment-start-bol
|
(let (comment-start-bol
|
||||||
|
this-line-has-prefix)
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char (treesit-node-start parent))
|
(goto-char (treesit-node-start parent))
|
||||||
(line-beginning-position))))
|
(setq comment-start-bol (line-beginning-position))
|
||||||
(save-excursion
|
|
||||||
|
(goto-char bol)
|
||||||
|
(setq this-line-has-prefix
|
||||||
|
(and (looking-at adaptive-fill-regexp)
|
||||||
|
(match-string 1)))
|
||||||
|
|
||||||
(forward-line -1)
|
(forward-line -1)
|
||||||
(and (>= (point) comment-start-bol)
|
(and (>= (point) comment-start-bol)
|
||||||
adaptive-fill-regexp
|
adaptive-fill-regexp
|
||||||
|
@ -1199,7 +1205,14 @@ See `treesit-simple-indent-presets'.")
|
||||||
;; If previous line is an empty line, don't
|
;; If previous line is an empty line, don't
|
||||||
;; indent.
|
;; indent.
|
||||||
(not (looking-at (rx (* whitespace) eol)))
|
(not (looking-at (rx (* whitespace) eol)))
|
||||||
(match-end 0))))))
|
;; Return the anchor. If the indenting line
|
||||||
|
;; has a prefix and the previous line also
|
||||||
|
;; has a prefix, indent to the beginning of
|
||||||
|
;; prev line's prefix rather than the end of
|
||||||
|
;; prev line's prefix. (Bug#61314).
|
||||||
|
(or (and this-line-has-prefix
|
||||||
|
(match-beginning 1))
|
||||||
|
(match-end 0)))))))
|
||||||
;; TODO: Document.
|
;; TODO: Document.
|
||||||
(cons 'grand-parent
|
(cons 'grand-parent
|
||||||
(lambda (_n parent &rest _)
|
(lambda (_n parent &rest _)
|
||||||
|
@ -1336,8 +1349,11 @@ prev-adaptive-prefix
|
||||||
|
|
||||||
Goes to the beginning of previous non-empty line, and tries
|
Goes to the beginning of previous non-empty line, and tries
|
||||||
to match `adaptive-fill-regexp'. If it matches, return the
|
to match `adaptive-fill-regexp'. If it matches, return the
|
||||||
end of the match, otherwise return nil. This is useful for a
|
end of the match, otherwise return nil. However, if the
|
||||||
`indent-relative'-like indent behavior for block comments.")
|
current line begins with a prefix, return the beginning of
|
||||||
|
the prefix of the previous line instead, so that the two
|
||||||
|
prefixes aligns. This is useful for a `indent-relative'-like
|
||||||
|
indent behavior for block comments.")
|
||||||
|
|
||||||
(defun treesit--simple-indent-eval (exp)
|
(defun treesit--simple-indent-eval (exp)
|
||||||
"Evaluate EXP.
|
"Evaluate EXP.
|
||||||
|
|
|
@ -242,6 +242,21 @@ line 2
|
||||||
*/
|
*/
|
||||||
=-=-=
|
=-=-=
|
||||||
|
|
||||||
|
Name: Block Comment prefixes (Bug#61314)
|
||||||
|
|
||||||
|
=-=-=
|
||||||
|
/*
|
||||||
|
- item1
|
||||||
|
- item2
|
||||||
|
- item3
|
||||||
|
*/
|
||||||
|
=-=-=
|
||||||
|
/*
|
||||||
|
- item1
|
||||||
|
- item2
|
||||||
|
- item3
|
||||||
|
*/
|
||||||
|
=-=-=
|
||||||
|
|
||||||
Code:
|
Code:
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue