1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-23 18:47:57 +00:00

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:
Yuan Fu 2023-02-19 12:22:27 -08:00
parent 2e6093b425
commit afbce8bb46
No known key found for this signature in database
GPG Key ID: 56E19BC57664A442
3 changed files with 44 additions and 10 deletions

View File

@ -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
previous non-empty line, and matches @code{adaptive-fill-regexp}. If
there is a match, this function returns the end of the match,
otherwise it returns nil. This anchor is useful for a
@code{indent-relative}-like indent behavior for block comments.
otherwise it returns nil. However, if the current line begins with a
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 defvar

View File

@ -1186,12 +1186,18 @@ See `treesit-simple-indent-presets'.")
(skip-syntax-backward "-")
(point))))
(cons 'prev-adaptive-prefix
(lambda (_n parent &rest _)
(let ((comment-start-bol
(save-excursion
(goto-char (treesit-node-start parent))
(line-beginning-position))))
(lambda (_n parent bol &rest _)
(let (comment-start-bol
this-line-has-prefix)
(save-excursion
(goto-char (treesit-node-start parent))
(setq comment-start-bol (line-beginning-position))
(goto-char bol)
(setq this-line-has-prefix
(and (looking-at adaptive-fill-regexp)
(match-string 1)))
(forward-line -1)
(and (>= (point) comment-start-bol)
adaptive-fill-regexp
@ -1199,7 +1205,14 @@ See `treesit-simple-indent-presets'.")
;; If previous line is an empty line, don't
;; indent.
(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.
(cons 'grand-parent
(lambda (_n parent &rest _)
@ -1336,8 +1349,11 @@ prev-adaptive-prefix
Goes to the beginning of previous non-empty line, and tries
to match `adaptive-fill-regexp'. If it matches, return the
end of the match, otherwise return nil. This is useful for a
`indent-relative'-like indent behavior for block comments.")
end of the match, otherwise return nil. However, if the
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)
"Evaluate EXP.

View File

@ -242,6 +242,21 @@ line 2
*/
=-=-=
Name: Block Comment prefixes (Bug#61314)
=-=-=
/*
- item1
- item2
- item3
*/
=-=-=
/*
- item1
- item2
- item3
*/
=-=-=
Code:
(lambda ()