diff options
| author | Yuan Fu | 2025-02-13 20:47:23 -0800 |
|---|---|---|
| committer | Yuan Fu | 2025-02-13 20:47:23 -0800 |
| commit | 34f90bf2cab7c1711136dc055b665e46f714c3e1 (patch) | |
| tree | e89cb4a65d08584c6de04e04aafaf2986aa5b666 | |
| parent | adabee88730017e81bbb0ee761b6720fd579535d (diff) | |
| download | emacs-34f90bf2cab7c1711136dc055b665e46f714c3e1.tar.gz emacs-34f90bf2cab7c1711136dc055b665e46f714c3e1.zip | |
Allow treesit-simple-indent-standalone-predicate to return anchor
* lisp/treesit.el:
(treesit-simple-indent-standalone-predicate): Allow it to return
an anchor instead of t.
(treesit-simple-indent-presets): Supports number.
* lisp/progmodes/c-ts-common.el:
(c-ts-common--standalone-parent):
(c-ts-common--prev-standalone-sibling): Supports number.
| -rw-r--r-- | lisp/progmodes/c-ts-common.el | 52 | ||||
| -rw-r--r-- | lisp/treesit.el | 34 |
2 files changed, 47 insertions, 39 deletions
diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 404d767cb3a..7d3ea76cb1b 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el | |||
| @@ -569,20 +569,22 @@ chaining like | |||
| 569 | 569 | ||
| 570 | But ff `treesit-simple-indent-standalone-predicate' is non-nil, use that | 570 | But ff `treesit-simple-indent-standalone-predicate' is non-nil, use that |
| 571 | for determining standlone line." | 571 | for determining standlone line." |
| 572 | (save-excursion | 572 | (let (anchor) |
| 573 | (catch 'term | 573 | (save-excursion |
| 574 | (while parent | 574 | (catch 'term |
| 575 | (goto-char (treesit-node-start parent)) | 575 | (while parent |
| 576 | (when (if treesit-simple-indent-standalone-predicate | 576 | (goto-char (treesit-node-start parent)) |
| 577 | (funcall treesit-simple-indent-standalone-predicate | 577 | (when (if treesit-simple-indent-standalone-predicate |
| 578 | parent) | 578 | (setq anchor |
| 579 | (looking-back (rx bol (* whitespace) (? ".")) | 579 | (funcall treesit-simple-indent-standalone-predicate |
| 580 | (line-beginning-position))) | 580 | parent)) |
| 581 | (throw 'term (point))) | 581 | (looking-back (rx bol (* whitespace) (? ".")) |
| 582 | (setq parent (treesit-node-parent parent)))))) | 582 | (line-beginning-position))) |
| 583 | (throw 'term (if (numberp anchor) anchor (point)))) | ||
| 584 | (setq parent (treesit-node-parent parent))))))) | ||
| 583 | 585 | ||
| 584 | (defun c-ts-common--prev-standalone-sibling (node) | 586 | (defun c-ts-common--prev-standalone-sibling (node) |
| 585 | "Return the previous sibling of NODE that starts on a new line. | 587 | "Return the start of the previous sibling of NODE that starts on a new line. |
| 586 | Return nil if no sibling satisfies the condition. | 588 | Return nil if no sibling satisfies the condition. |
| 587 | 589 | ||
| 588 | Unlike simple-indent's standalone preset, this function handles method | 590 | Unlike simple-indent's standalone preset, this function handles method |
| @@ -597,15 +599,18 @@ for determining standlone line." | |||
| 597 | (save-excursion | 599 | (save-excursion |
| 598 | (setq node (treesit-node-prev-sibling node 'named)) | 600 | (setq node (treesit-node-prev-sibling node 'named)) |
| 599 | (goto-char (treesit-node-start node)) | 601 | (goto-char (treesit-node-start node)) |
| 600 | (while (and node | 602 | (let (anchor) |
| 601 | (goto-char (treesit-node-start node)) | 603 | (while (and node |
| 602 | (not (if treesit-simple-indent-standalone-predicate | 604 | (goto-char (treesit-node-start node)) |
| 603 | (funcall treesit-simple-indent-standalone-predicate | 605 | (not (if treesit-simple-indent-standalone-predicate |
| 604 | node) | 606 | (setq anchor |
| 605 | (looking-back (rx bol (* whitespace) (? ".")) | 607 | (funcall |
| 606 | (pos-bol))))) | 608 | treesit-simple-indent-standalone-predicate |
| 607 | (setq node (treesit-node-prev-sibling node 'named))) | 609 | node)) |
| 608 | node)) | 610 | (looking-back (rx bol (* whitespace) (? ".")) |
| 611 | (pos-bol))))) | ||
| 612 | (setq node (treesit-node-prev-sibling node 'named)))) | ||
| 613 | (if (numberp anchor) anchor (treesit-node-start node)))) | ||
| 609 | 614 | ||
| 610 | (defun c-ts-common-parent-ignore-preproc (node) | 615 | (defun c-ts-common-parent-ignore-preproc (node) |
| 611 | "Return the parent of NODE, skipping preproc nodes." | 616 | "Return the parent of NODE, skipping preproc nodes." |
| @@ -696,9 +701,8 @@ The rule also handles method chaining like | |||
| 696 | (cons (c-ts-common--standalone-parent parent) | 701 | (cons (c-ts-common--standalone-parent parent) |
| 697 | offset))) | 702 | offset))) |
| 698 | ;; Not first sibling | 703 | ;; Not first sibling |
| 699 | (t (cons (treesit-node-start | 704 | (t (cons (or (c-ts-common--prev-standalone-sibling node) |
| 700 | (or (c-ts-common--prev-standalone-sibling node) | 705 | (treesit-node-start first-sibling)) |
| 701 | first-sibling)) | ||
| 702 | 0))))) | 706 | 0))))) |
| 703 | ;; Condition 2 for initializer list, only apply to | 707 | ;; Condition 2 for initializer list, only apply to |
| 704 | ;; second line. Eg, | 708 | ;; second line. Eg, |
diff --git a/lisp/treesit.el b/lisp/treesit.el index e35efd9b0db..b923545d50c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -1821,7 +1821,8 @@ to be standalone too: | |||
| 1821 | }); | 1821 | }); |
| 1822 | 1822 | ||
| 1823 | The value should be a function that takes a node, and return t if it's | 1823 | The value should be a function that takes a node, and return t if it's |
| 1824 | standalone.") | 1824 | standalone. If the function returns a position, that position is used |
| 1825 | as the anchor.") | ||
| 1825 | 1826 | ||
| 1826 | (defvar treesit-simple-indent-presets | 1827 | (defvar treesit-simple-indent-presets |
| 1827 | (list (cons 'match | 1828 | (list (cons 'match |
| @@ -1956,20 +1957,23 @@ standalone.") | |||
| 1956 | (goto-char (treesit-node-start parent)) | 1957 | (goto-char (treesit-node-start parent)) |
| 1957 | (back-to-indentation) | 1958 | (back-to-indentation) |
| 1958 | (point)))) | 1959 | (point)))) |
| 1959 | (cons 'standalone-parent | 1960 | (cons |
| 1960 | (lambda (_n parent &rest _) | 1961 | 'standalone-parent |
| 1961 | (save-excursion | 1962 | (lambda (_n parent &rest _) |
| 1962 | (catch 'term | 1963 | (save-excursion |
| 1963 | (while parent | 1964 | (let (anchor) |
| 1964 | (goto-char (treesit-node-start parent)) | 1965 | (catch 'term |
| 1965 | (when | 1966 | (while parent |
| 1966 | (if (null treesit-simple-indent-standalone-predicate) | 1967 | (goto-char (treesit-node-start parent)) |
| 1967 | (looking-back (rx bol (* whitespace)) | 1968 | (when (if (null treesit-simple-indent-standalone-predicate) |
| 1968 | (line-beginning-position)) | 1969 | (looking-back (rx bol (* whitespace)) |
| 1969 | (funcall treesit-simple-indent-standalone-predicate | 1970 | (line-beginning-position)) |
| 1970 | parent)) | 1971 | (setq anchor |
| 1971 | (throw 'term (point))) | 1972 | (funcall |
| 1972 | (setq parent (treesit-node-parent parent))))))) | 1973 | treesit-simple-indent-standalone-predicate |
| 1974 | parent))) | ||
| 1975 | (throw 'term (if (numberp anchor) anchor (point)))) | ||
| 1976 | (setq parent (treesit-node-parent parent)))))))) | ||
| 1973 | (cons 'prev-sibling (lambda (node parent bol &rest _) | 1977 | (cons 'prev-sibling (lambda (node parent bol &rest _) |
| 1974 | (treesit-node-start | 1978 | (treesit-node-start |
| 1975 | (or (treesit-node-prev-sibling node t) | 1979 | (or (treesit-node-prev-sibling node t) |