From b341864d9002fd081a8a6dfeb5a5a00373738a2f Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 10 Oct 2022 11:14:35 -0700 Subject: [PATCH] Improve treesit-search-forward-goto so it doens't stuck at EOF * lisp/treesit.el (treesit-search-forward-goto): Handle the edge case. --- lisp/treesit.el | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lisp/treesit.el b/lisp/treesit.el index 34b501cfba8..2bcfddb56b6 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -826,6 +826,12 @@ or end of the matches node, it can be either \\='start or ALL, BACKWARD, and UP are the same as in `treesit-search-forward'." (let ((node (treesit-node-at (point))) (start (point))) + ;; Often the EOF (point-max) is a newline, and `treesit-node-at' + ;; will return nil at that point (which is fair). But we need a + ;; node as the starting point to traverse the tree. So we try to + ;; use the node before point. + (when (and (not node) (eq (point) (point-max))) + (setq node (treesit-node-at (max (1- (point)) (point-min))))) ;; When searching forward, it is possible for (point) < start, ;; because `treesit-search-forward' goes to parents. (while (and node (if backward -- 2.39.2