diff options
| author | Yuan Fu | 2023-02-19 12:22:27 -0800 |
|---|---|---|
| committer | Yuan Fu | 2023-02-19 12:34:19 -0800 |
| commit | afbce8bb46798518998f517cbacdbd65d4531a3f (patch) | |
| tree | 3062209a032808165a8906d24440693d0e5e078e /lisp | |
| parent | 2e6093b425e21551806abed746765631a00942d6 (diff) | |
| download | emacs-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.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/treesit.el | 32 |
1 files changed, 24 insertions, 8 deletions
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. |