aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/treesit.el15
1 files changed, 11 insertions, 4 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 5e87737659e..85154d0d1c7 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -833,21 +833,28 @@ The range is between START and END."
833 (nreverse result)) 833 (nreverse result))
834 (list node))) 834 (list node)))
835 835
836(defun treesit--children-covering-range-recurse (node start end threshold) 836(defun treesit--children-covering-range-recurse
837 (node start end threshold &optional limit)
837 "Return a list of children of NODE covering a range. 838 "Return a list of children of NODE covering a range.
839
838Recursively go down the parse tree and collect children, until 840Recursively go down the parse tree and collect children, until
839all nodes in the returned list are smaller than THRESHOLD. The 841all nodes in the returned list are smaller than THRESHOLD. The
840range is between START and END." 842range is between START and END.
843
844LIMIT is the recursion limit, which defaults to 100."
841 (let* ((child (treesit-node-first-child-for-pos node start)) 845 (let* ((child (treesit-node-first-child-for-pos node start))
846 (limit (or limit 100))
842 result) 847 result)
843 (while (and child (<= (treesit-node-start child) end)) 848 ;; If LIMIT is exceeded, we are probably seeing the erroneously
849 ;; tall tree, in that case, just give up.
850 (while (and (> limit 0) child (<= (treesit-node-start child) end))
844 ;; If child still too large, recurse down. Otherwise collect 851 ;; If child still too large, recurse down. Otherwise collect
845 ;; child. 852 ;; child.
846 (if (> (- (treesit-node-end child) 853 (if (> (- (treesit-node-end child)
847 (treesit-node-start child)) 854 (treesit-node-start child))
848 threshold) 855 threshold)
849 (dolist (r (treesit--children-covering-range-recurse 856 (dolist (r (treesit--children-covering-range-recurse
850 child start end threshold)) 857 child start end threshold (1- limit)))
851 (push r result)) 858 (push r result))
852 (push child result)) 859 (push child result))
853 (setq child (treesit-node-next-sibling child))) 860 (setq child (treesit-node-next-sibling child)))