aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2023-02-19 12:22:27 -0800
committerYuan Fu2023-02-19 12:34:19 -0800
commitafbce8bb46798518998f517cbacdbd65d4531a3f (patch)
tree3062209a032808165a8906d24440693d0e5e078e
parent2e6093b425e21551806abed746765631a00942d6 (diff)
downloademacs-afbce8bb46798518998f517cbacdbd65d4531a3f.tar.gz
emacs-afbce8bb46798518998f517cbacdbd65d4531a3f.zip
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.
-rw-r--r--doc/lispref/modes.texi7
-rw-r--r--lisp/treesit.el32
-rw-r--r--test/lisp/progmodes/c-ts-mode-resources/indent.erts15
3 files changed, 44 insertions, 10 deletions
diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi
index 750c4b47894..052a10e3797 100644
--- a/doc/lispref/modes.texi
+++ b/doc/lispref/modes.texi
@@ -5111,8 +5111,11 @@ This anchor is a function that is called with 3 arguments: @var{node},
5111@var{parent}, and @var{bol}. It tries to go to the beginning of the 5111@var{parent}, and @var{bol}. It tries to go to the beginning of the
5112previous non-empty line, and matches @code{adaptive-fill-regexp}. If 5112previous non-empty line, and matches @code{adaptive-fill-regexp}. If
5113there is a match, this function returns the end of the match, 5113there is a match, this function returns the end of the match,
5114otherwise it returns nil. This anchor is useful for a 5114otherwise it returns nil. However, if the current line begins with a
5115@code{indent-relative}-like indent behavior for block comments. 5115prefix (e.g., ``-''), return the beginning of the prefix of the
5116previous line instead, so that the two prefixes aligns. This anchor
5117is useful for a @code{indent-relative}-like indent behavior for block
5118comments.
5116 5119
5117@end ftable 5120@end ftable
5118@end defvar 5121@end defvar
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 297812f23f7..045fdf21cba 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1186,12 +1186,18 @@ See `treesit-simple-indent-presets'.")
1186 (skip-syntax-backward "-") 1186 (skip-syntax-backward "-")
1187 (point)))) 1187 (point))))
1188 (cons 'prev-adaptive-prefix 1188 (cons 'prev-adaptive-prefix
1189 (lambda (_n parent &rest _) 1189 (lambda (_n parent bol &rest _)
1190 (let ((comment-start-bol 1190 (let (comment-start-bol
1191 (save-excursion 1191 this-line-has-prefix)
1192 (goto-char (treesit-node-start parent))
1193 (line-beginning-position))))
1194 (save-excursion 1192 (save-excursion
1193 (goto-char (treesit-node-start parent))
1194 (setq comment-start-bol (line-beginning-position))
1195
1196 (goto-char bol)
1197 (setq this-line-has-prefix
1198 (and (looking-at adaptive-fill-regexp)
1199 (match-string 1)))
1200
1195 (forward-line -1) 1201 (forward-line -1)
1196 (and (>= (point) comment-start-bol) 1202 (and (>= (point) comment-start-bol)
1197 adaptive-fill-regexp 1203 adaptive-fill-regexp
@@ -1199,7 +1205,14 @@ See `treesit-simple-indent-presets'.")
1199 ;; If previous line is an empty line, don't 1205 ;; If previous line is an empty line, don't
1200 ;; indent. 1206 ;; indent.
1201 (not (looking-at (rx (* whitespace) eol))) 1207 (not (looking-at (rx (* whitespace) eol)))
1202 (match-end 0)))))) 1208 ;; Return the anchor. If the indenting line
1209 ;; has a prefix and the previous line also
1210 ;; has a prefix, indent to the beginning of
1211 ;; prev line's prefix rather than the end of
1212 ;; prev line's prefix. (Bug#61314).
1213 (or (and this-line-has-prefix
1214 (match-beginning 1))
1215 (match-end 0)))))))
1203 ;; TODO: Document. 1216 ;; TODO: Document.
1204 (cons 'grand-parent 1217 (cons 'grand-parent
1205 (lambda (_n parent &rest _) 1218 (lambda (_n parent &rest _)
@@ -1336,8 +1349,11 @@ prev-adaptive-prefix
1336 1349
1337 Goes to the beginning of previous non-empty line, and tries 1350 Goes to the beginning of previous non-empty line, and tries
1338 to match `adaptive-fill-regexp'. If it matches, return the 1351 to match `adaptive-fill-regexp'. If it matches, return the
1339 end of the match, otherwise return nil. This is useful for a 1352 end of the match, otherwise return nil. However, if the
1340 `indent-relative'-like indent behavior for block comments.") 1353 current line begins with a prefix, return the beginning of
1354 the prefix of the previous line instead, so that the two
1355 prefixes aligns. This is useful for a `indent-relative'-like
1356 indent behavior for block comments.")
1341 1357
1342(defun treesit--simple-indent-eval (exp) 1358(defun treesit--simple-indent-eval (exp)
1343 "Evaluate EXP. 1359 "Evaluate EXP.
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
index 05d59c10a42..09c8ebcec44 100644
--- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
@@ -242,6 +242,21 @@ line 2
242 */ 242 */
243=-=-= 243=-=-=
244 244
245Name: Block Comment prefixes (Bug#61314)
246
247=-=-=
248/*
249- item1
250- item2
251- item3
252*/
253=-=-=
254/*
255 - item1
256 - item2
257 - item3
258 */
259=-=-=
245 260
246Code: 261Code:
247 (lambda () 262 (lambda ()