From 76b86d98534da2c9e6cf8fd1c7edcbe53d84eb82 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Wed, 26 Oct 2022 18:06:07 -0700 Subject: [PATCH] Generalize js--treesit-imenu-top-level-p * lisp/progmodes/js.el (js--treesit-imenu-top-level-p): Move. (js--treesit-font-lock-settings): Fix usage. * lisp/treesit.el (treesit-node-top-level-p): Move to here. --- lisp/progmodes/js.el | 14 +------------- lisp/treesit.el | 12 ++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 8802ea0b5f4..7e1d7569cab 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3601,18 +3601,6 @@ This function can be used as a value in `which-func-functions'" do (setq node (treesit-node-parent node)) finally return (string-join name-list ".")))) -(defun js--treesit-imenu-top-level-p (node) - "Return t if NODE is top-level, nil otherwise. -Being top-level means there is no parent of NODE that has the -same type." - (when node - (catch 'term - (let ((type (treesit-node-type node))) - (while (setq node (treesit-node-parent node)) - (when (equal (treesit-node-type node) type) - (throw 'term nil)))) - t))) - ;; Keep this private since we might later change it or generalize it. (defvar js--treesit-imenu-type-alist '((variable . "V") @@ -3673,7 +3661,7 @@ definition*\"." ((null ts-node) subtrees) ((and (eq type 'variable) - (not (js--treesit-imenu-top-level-p ts-node))) + (not (treesit-node-top-level-p ts-node))) nil) (subtrees (let ((parent-label (js--treesit-imenu-label type name)) diff --git a/lisp/treesit.el b/lisp/treesit.el index 8426a02a262..f07fe7d609c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -195,6 +195,18 @@ that language in the current buffer, and use that." (treesit-buffer-root-node parser-or-lang)))) (treesit-node-descendant-for-range root beg (or end beg) named))) +(defun treesit-node-top-level-p (node) + "Return non-nil if NODE is top-level, nil otherwise. +Being top-level means there is no parent of NODE that has the +same type." + (when node + (catch 'term + (let ((type (treesit-node-type node))) + (while (setq node (treesit-node-parent node)) + (when (equal (treesit-node-type node) type) + (throw 'term nil)))) + t))) + (defun treesit-buffer-root-node (&optional language) "Return the root node of the current buffer. Use the first parser in (`treesit-parser-list'), if LANGUAGE is -- 2.39.2