]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix c-ts-mode-imenu
authorYuan Fu <casouri@gmail.com>
Wed, 16 Nov 2022 18:00:42 +0000 (10:00 -0800)
committerYuan Fu <casouri@gmail.com>
Wed, 16 Nov 2022 22:40:41 +0000 (14:40 -0800)
* lisp/progmodes/c-ts-mode.el (c-ts-mode--imenu-1): If cannot find
its name, ignore the node.
(c-ts-mode--imenu): Don't include a category if it's empty.

lisp/progmodes/c-ts-mode.el

index 950ef1f65e60d615846b21f601e2258133b92fae..d4f25f784573544e42c2d65630bf479bc886e9ae 100644 (file)
@@ -388,42 +388,41 @@ the subtrees."
   (let* ((ts-node (car node))
          (subtrees (mapcan #'c-ts-mode--imenu-1 (cdr node)))
          (name (when ts-node
-                 (or (treesit-node-text
-                      (pcase (treesit-node-type ts-node)
-                        ("function_definition"
-                         (treesit-node-child-by-field-name
-                          (treesit-node-child-by-field-name
-                           ts-node "declarator")
-                          "declarator"))
-                        ("declaration"
-                         (let ((child (treesit-node-child ts-node -1 t)))
-                           (pcase (treesit-node-type child)
-                             ("identifier" child)
-                             (_ (treesit-node-child-by-field-name
-                                 child "declarator")))))
-                        ("struct_specifier"
-                         (treesit-node-child-by-field-name
-                          ts-node "name")))
-                      t)
-                     "Unnamed node")))
+                 (treesit-node-text
+                  (pcase (treesit-node-type ts-node)
+                    ("function_definition"
+                     (treesit-node-child-by-field-name
+                      (treesit-node-child-by-field-name
+                       ts-node "declarator")
+                      "declarator"))
+                    ("declaration"
+                     (let ((child (treesit-node-child ts-node -1 t)))
+                       (pcase (treesit-node-type child)
+                         ("identifier" child)
+                         (_ (treesit-node-child-by-field-name
+                             child "declarator")))))
+                    ("struct_specifier"
+                     (treesit-node-child-by-field-name
+                      ts-node "name"))))))
          (marker (when ts-node
                    (set-marker (make-marker)
                                (treesit-node-start ts-node)))))
-    ;; A struct_specifier could be inside a parameter list, another
-    ;; struct definition, a variable declaration, a function
-    ;; declaration.  In those cases we don't include it.
     (cond
+     ;; A struct_specifier could be inside a parameter list, another
+     ;; struct definition, a variable declaration, a function
+     ;; declaration.  In those cases we don't include it.
      ((string-match-p
        (rx (or "parameter_declaration" "field_declaration"
                "declaration" "function_definition"))
        (or (treesit-node-type (treesit-node-parent ts-node))
            ""))
       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)
-     ((null ts-node) subtrees)
+     ((or (null ts-node) (null name)) subtrees)
      (subtrees
       `((,name ,(cons name marker) ,@subtrees)))
      (t
@@ -437,10 +436,14 @@ the subtrees."
          (var-tree (treesit-induce-sparse-tree
                     node "^declaration$"))
          (struct-tree (treesit-induce-sparse-tree
-                       node "^struct_specifier$")))
-    `(("Struct" . ,(c-ts-mode--imenu-1 struct-tree))
-      ("Variable" . ,(c-ts-mode--imenu-1 var-tree))
-      ("Function" . ,(c-ts-mode--imenu-1 func-tree)))))
+                       node "^struct_specifier$"))
+         (func-index (c-ts-mode--imenu-1 func-tree))
+         (var-index (c-ts-mode--imenu-1 var-tree))
+         (struct-index (c-ts-mode--imenu-1 struct-tree)))
+    (append
+     (when struct-index `(("Struct" . ,struct-index)))
+     (when var-index `(("Variable" . ,var-index)))
+     (when func-index `(("Function" . ,func-index))))))
 
 ;;;###autoload
 (define-derived-mode c-ts-mode--base-mode prog-mode "C"