diff options
| author | Yuan Fu | 2024-11-29 20:43:09 -0800 |
|---|---|---|
| committer | Yuan Fu | 2024-12-01 17:53:22 -0800 |
| commit | e37cd4fa597beaec3b491edb1b15ea0c19e72be4 (patch) | |
| tree | c9e7f33364e95c1a4597760833f3862212509cec /lisp/treesit.el | |
| parent | 4afdb7e80febd56f4024bad0aff4356198f6ce53 (diff) | |
| download | emacs-e37cd4fa597beaec3b491edb1b15ea0c19e72be4.tar.gz emacs-e37cd4fa597beaec3b491edb1b15ea0c19e72be4.zip | |
Add baseline tree-sitter indent rule for C-like languages
I found a really good baseline indent rule that handles a wide
range of situations very well. Now major modes can just start
with this rule and add exceptions on top.
This is worth mentioning in the manual, but that'll be a large
change, and doesn't have to be included in this commit.
* lisp/progmodes/c-ts-common.el:
(c-ts-common-list-indent-style): New variable.
(c-ts-common--standalone-parent):
(c-ts-common--prev-standalone-sibling):
(c-ts-common-parent-ignore-preproc):
(c-ts-common-baseline-indent-rule): New function.
* lisp/treesit.el (treesit--indent-prev-line-node): New function.
(treesit-simple-indent-presets): Add new preset.
Diffstat (limited to 'lisp/treesit.el')
| -rw-r--r-- | lisp/treesit.el | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el index 3539942f19a..2acb46ab105 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -1589,6 +1589,14 @@ should take the same argument as MATCHER or ANCHOR. If it matches, | |||
| 1589 | return a cons (ANCHOR-POS . OFFSET), where ANCHOR-POS is a position and | 1589 | return a cons (ANCHOR-POS . OFFSET), where ANCHOR-POS is a position and |
| 1590 | OFFSET is the indent offset; if it doesn't match, return nil.") | 1590 | OFFSET is the indent offset; if it doesn't match, return nil.") |
| 1591 | 1591 | ||
| 1592 | (defun treesit--indent-prev-line-node (pos) | ||
| 1593 | "Return the largest node on the previous line of POS." | ||
| 1594 | (save-excursion | ||
| 1595 | (goto-char pos) | ||
| 1596 | (when (eq (forward-line -1) 0) | ||
| 1597 | (back-to-indentation) | ||
| 1598 | (treesit--indent-largest-node-at (point))))) | ||
| 1599 | |||
| 1592 | (defvar treesit-simple-indent-presets | 1600 | (defvar treesit-simple-indent-presets |
| 1593 | (list (cons 'match | 1601 | (list (cons 'match |
| 1594 | (lambda | 1602 | (lambda |
| @@ -1639,6 +1647,12 @@ OFFSET is the indent offset; if it doesn't match, return nil.") | |||
| 1639 | (lambda (node &rest _) | 1647 | (lambda (node &rest _) |
| 1640 | (string-match-p | 1648 | (string-match-p |
| 1641 | type (or (treesit-node-type node) ""))))) | 1649 | type (or (treesit-node-type node) ""))))) |
| 1650 | ;; FIXME: Add to manual. | ||
| 1651 | (cons 'prev-line-is (lambda (type) | ||
| 1652 | (lambda (_n _p bol &rest _) | ||
| 1653 | (treesit-node-match-p | ||
| 1654 | (treesit--indent-prev-line-node bol) | ||
| 1655 | type)))) | ||
| 1642 | (cons 'field-is (lambda (name) | 1656 | (cons 'field-is (lambda (name) |
| 1643 | (lambda (node &rest _) | 1657 | (lambda (node &rest _) |
| 1644 | (string-match-p | 1658 | (string-match-p |