diff options
| author | Yuan Fu | 2022-06-14 15:49:44 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-06-14 15:49:44 -0700 |
| commit | 316bdc334ca4f3b7101ac6879e84041646852488 (patch) | |
| tree | 42f6e4f1500194b9c59a43b8471e57464b8ebbcc | |
| parent | 8aa04aac65ad286a06f05af374060d6c77d76189 (diff) | |
| download | emacs-316bdc334ca4f3b7101ac6879e84041646852488.tar.gz emacs-316bdc334ca4f3b7101ac6879e84041646852488.zip | |
Add manual for treesit-traverse-forward and friends
* doc/lispref/parsing.texi (Retrieving Node): Add manual entry for
treesit-traverse-depth-first, treesit-traverse-breadth-first,
treesit-traverse-forward.
* lisp/treesit.el (treesit-traverse-forward): Fix docstring.
| -rw-r--r-- | doc/lispref/parsing.texi | 67 | ||||
| -rw-r--r-- | lisp/treesit.el | 15 |
2 files changed, 75 insertions, 7 deletions
diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index 72be91877b6..cadddf0c00a 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi | |||
| @@ -630,6 +630,73 @@ parent as the single argument). I.e., this function returns the | |||
| 630 | farthest parent that still satisfies @var{pred}. | 630 | farthest parent that still satisfies @var{pred}. |
| 631 | @end defun | 631 | @end defun |
| 632 | 632 | ||
| 633 | @cindex trees-sitter tree traversal | ||
| 634 | @defun treesit-traverse-depth-first node pred &optional step depth | ||
| 635 | Traverse the subtree of @var{node} depth-first. Traverse starting from | ||
| 636 | @var{node} (i.e., @var{node} is passed to @var{pred}). For each node | ||
| 637 | traversed, we call @var{pred} with the node, and we stop and return | ||
| 638 | the node if @var{pred} returns non-nil. If no node satisfies | ||
| 639 | @var{pred}, return nil. | ||
| 640 | |||
| 641 | If @var{step} >= 0 or nil, go forward, if @var{step} < 0, go backward. | ||
| 642 | (The quantity of @var{step} doesn't matter.) | ||
| 643 | |||
| 644 | @var{depth} can be a positive integer or 0, meaning go @var{depth} | ||
| 645 | levels deep, counting from @var{node}, or nil, meaning there is no | ||
| 646 | limit. For example, a value 0 means only traverse @var{node} itself, | ||
| 647 | a value 1 means traverse @var{node} and its immediate children. | ||
| 648 | @end defun | ||
| 649 | |||
| 650 | @defun treesit-traverse-breadth-first node pred &optional step | ||
| 651 | Traverse the subtree of @var{node} breadth-first. Traverse starting | ||
| 652 | from @var{node} (i.e., @var{node} is passed to @var{pred}). For each | ||
| 653 | node traversed, call @var{pred} with the node, stop and return the | ||
| 654 | node if @var{pred} returns non-nil. If no node satisfies @var{pred}, | ||
| 655 | return nil. | ||
| 656 | |||
| 657 | If @var{step} >= 0 or nil, go forward, if @var{step} < 0, go backward. | ||
| 658 | (The quantity of @var{step} doesn't matter.) | ||
| 659 | @end defun | ||
| 660 | |||
| 661 | @defun treesit-traverse-forward node pred &optional step depth | ||
| 662 | Traverses the whole tree forward from NODE depth-first. Traverse | ||
| 663 | starting from @var{node} (i.e., @var{node} is passed to @var{pred}). | ||
| 664 | For each node traversed, call @var{pred} with the node, stop and | ||
| 665 | return the node if @var{pred} returns non-nil. If no node satisfies | ||
| 666 | @var{pred}, return nil. | ||
| 667 | |||
| 668 | If @var{step} >= 0 or nil, go forward, if @var{step} < 0, go backward. | ||
| 669 | (The quantity of @var{step} doesn't matter.) | ||
| 670 | |||
| 671 | Traversing forward means that for a tree like the below where | ||
| 672 | @var{node} is marked 1, traverse as numbered: | ||
| 673 | |||
| 674 | @example | ||
| 675 | @group | ||
| 676 | 16 | ||
| 677 | | | ||
| 678 | 3--------4-----------8 | ||
| 679 | | | | | ||
| 680 | o--o-+--1 5--+--6 9---+-----12 | ||
| 681 | | | | | | | | ||
| 682 | o o 2 7 +-+-+ +--+--+ | ||
| 683 | | | | | | | ||
| 684 | 10 11 13 14 15 | ||
| 685 | @end group | ||
| 686 | @end example | ||
| 687 | |||
| 688 | @var{depth} can be a positive integer, 0, nil, or @code{'up}. A | ||
| 689 | positive integer or 0 means go @var{depth} deep counting from | ||
| 690 | @var{node}. A nil means no limit. And a symbol @code{'up} means go | ||
| 691 | upwards only: only traverse to sibling and parent, never go down to | ||
| 692 | children. | ||
| 693 | |||
| 694 | The difference between 0 and @code{'up} is subtle: in the above | ||
| 695 | example, if given 0 as @var{depth}, node 1 3 4 5 6 8 9 12 16 are | ||
| 696 | visited; if given @code{'up} as @var{depth}, only node 1 3 4 8 16 are | ||
| 697 | visited. | ||
| 698 | @end defun | ||
| 699 | |||
| 633 | @node Accessing Node | 700 | @node Accessing Node |
| 634 | @section Accessing Node Information | 701 | @section Accessing Node Information |
| 635 | 702 | ||
diff --git a/lisp/treesit.el b/lisp/treesit.el index d6d092ee6a2..4e35a466507 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -311,8 +311,8 @@ node if PRED returns non-nil. If STEP >= 0 or nil, go forward, | |||
| 311 | if STEP < 0, go backward. If no node satisfies PRED, return | 311 | if STEP < 0, go backward. If no node satisfies PRED, return |
| 312 | nil. | 312 | nil. |
| 313 | 313 | ||
| 314 | Traversing forward depth-first means, for a tree like the below | 314 | Traversing forward depth-first means that for a tree like the |
| 315 | where NODE is marked 1, traverse as numbered: | 315 | below where NODE is marked 1, traverse as numbered: |
| 316 | 316 | ||
| 317 | 16 | 317 | 16 |
| 318 | | | 318 | | |
| @@ -326,11 +326,12 @@ where NODE is marked 1, traverse as numbered: | |||
| 326 | 326 | ||
| 327 | DEPTH can be a positive integer, 0, nil, or 'up. A positive | 327 | DEPTH can be a positive integer, 0, nil, or 'up. A positive |
| 328 | integer or 0 means go DEPTH deep counting from NODE. A nil means | 328 | integer or 0 means go DEPTH deep counting from NODE. A nil means |
| 329 | no limit. And a symbol 'up means upward only: only traverse | 329 | no limit. And a symbol 'up means go upwards only: only traverse |
| 330 | sibling and parent, never go down. The difference between 0 and | 330 | sibling and parent, never go down to children. |
| 331 | 'up is subtle: in the above example, if given 0 as DEPTH, node 1 | 331 | |
| 332 | 3 4 5 6 8 9 12 16 are visited; if given t as DEPTH, only node 1 3 | 332 | The difference between 0 and 'up is subtle: in the above example, |
| 333 | 4 8 16 are visited." | 333 | if given 0 as DEPTH, node 1 3 4 5 6 8 9 12 16 are visited; if |
| 334 | given 'up as DEPTH, only node 1 3 4 8 16 are visited." | ||
| 334 | ;; First try NODE's subtree, but only under these conditions: if | 335 | ;; First try NODE's subtree, but only under these conditions: if |
| 335 | ;; DEPTH is a number, it has to be greater than 0, if it's a symbol, | 336 | ;; DEPTH is a number, it has to be greater than 0, if it's a symbol, |
| 336 | ;; it cannot be 'up. | 337 | ;; it cannot be 'up. |