From: Yuan Fu Date: Thu, 27 Oct 2022 01:06:07 +0000 (-0700) Subject: Generalize js--treesit-imenu-top-level-p X-Git-Tag: emacs-29.0.90~1780 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=76b86d98534da2c9e6cf8fd1c7edcbe53d84eb82;p=emacs.git 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. --- 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