1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 14:30:50 -08:00

Fix imenu for c-ts-mode (bug#60296)

* lisp/progmodes/c-ts-mode.el (c-ts-mode--imenu-1): Use
c-ts-mode--defun-valid-p to filter out nested matches.
(c-ts-mode--defun-valid-p): Handle more types of nodes.
This commit is contained in:
Yuan Fu 2022-12-26 01:01:41 -08:00
parent 8f68b6497e
commit eb26872837
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -510,21 +510,10 @@ the subtrees."
(set-marker (make-marker) (set-marker (make-marker)
(treesit-node-start ts-node))))) (treesit-node-start ts-node)))))
(cond (cond
;; A struct_specifier could be inside a parameter list, another ((or (null ts-node) (null name))
;; struct definition, a variable declaration, a function subtrees)
;; declaration. In those cases we don't include it. ((null (c-ts-mode--defun-valid-p ts-node))
((string-match-p
(rx (or "parameter_declaration" "field_declaration"
"declaration" "function_definition"))
(or (treesit-node-type (treesit-node-parent ts-node))
""))
nil) nil)
;; Ignore function local variable declarations.
((and (equal (treesit-node-type ts-node) "declaration")
(not (equal (treesit-node-type (treesit-node-parent ts-node))
"translation_unit")))
nil)
((or (null ts-node) (null name)) subtrees)
(subtrees (subtrees
`((,name ,(cons name marker) ,@subtrees))) `((,name ,(cons name marker) ,@subtrees)))
(t (t
@ -550,16 +539,30 @@ the subtrees."
;;; Defun navigation ;;; Defun navigation
(defun c-ts-mode--defun-valid-p (node) (defun c-ts-mode--defun-valid-p (node)
(if (string-match-p "Return non-nil if NODE is a valid defun node.
(rx (or "struct_specifier" Ie, NODE is not nested."
"enum_specifier" (not (or (and (member (treesit-node-type node)
"union_specifier")) '("struct_specifier"
(treesit-node-type node)) "enum_specifier"
(null "union_specifier"
(treesit-node-top-level "declaration"))
node (rx (or "function_definition" ;; If NODE's type is one of the above, make sure it is
"type_definition")))) ;; top-level.
t)) (treesit-node-top-level
node (rx (or "function_definition"
"type_definition"
"struct_specifier"
"enum_specifier"
"union_specifier"
"declaration"))))
(and (equal (treesit-node-type node) "declaration")
;; If NODE is a declaration, make sure it is not a
;; function declaration.
(equal (treesit-node-type
(treesit-node-child-by-field-name
node "declarator"))
"function_declarator")))))
(defun c-ts-mode--defun-skipper () (defun c-ts-mode--defun-skipper ()
"Custom defun skipper for `c-ts-mode' and friends. "Custom defun skipper for `c-ts-mode' and friends.