]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve imenu support in lua-ts-mode
authorjohn muhl <jm@pub.pink>
Tue, 10 Oct 2023 19:39:30 +0000 (14:39 -0500)
committerEli Zaretskii <eliz@gnu.org>
Wed, 25 Oct 2023 13:49:31 +0000 (16:49 +0300)
* lisp/progmodes/lua-ts-mode.el (lua-ts-mode): Include require
statements and remove anonymous entries.
(lua-ts--named-function-p, lua-ts--require-name-function)
(lua-ts--require-p): New functions.
* lisp/speedbar.el (speedbar-supported-extension-expressions):
Add Lua to the list of supported file types.  (Bug#66465)

lisp/progmodes/lua-ts-mode.el
lisp/speedbar.el

index 80cf119f75c90ba6d85bbc784ef208ad78f86e10..2193779b759f634b7af9fa98653d86cb4c4eba50 100644 (file)
@@ -443,6 +443,33 @@ Return nil if there is no name or if NODE is not a defun node."
        (and (treesit-search-subtree node "function_definition" nil nil 1)
             (treesit-node-text child t))))))
 
+(defun lua-ts--named-function-p (node)
+  "Matches if NODE is a named function."
+  (let ((type (treesit-node-type node)))
+    (or (equal "function_declaration" type)
+        (and (equal "field" type)
+             (equal "function_definition"
+                    (treesit-node-type
+                     (treesit-node-child-by-field-name
+                      node "value")))
+             (treesit-node-child-by-field-name node "name")))))
+
+(defun lua-ts--require-name-function (node)
+  "Return name of NODE to use for requires in imenu."
+  (when-let* (((lua-ts--require-p node))
+              (parent (treesit-node-parent node))
+              (parent-type (treesit-node-type parent)))
+    (if (equal "expression_list" parent-type)
+        (let* ((g-parent (treesit-node-parent parent))
+               (name (treesit-node-child-by-field-name g-parent "name")))
+          (treesit-node-text name t))
+      (treesit-node-text (treesit-search-subtree node "string_content") t))))
+
+(defun lua-ts--require-p (node)
+  "Matches if NODE is a require statement."
+  (let ((name (treesit-node-child-by-field-name node "name")))
+    (equal "require" (treesit-node-text name t))))
+
 (defvar-local lua-ts--flymake-process nil)
 
 (defun lua-ts-flymake-luacheck (report-fn &rest _args)
@@ -692,13 +719,15 @@ Calls REPORT-FN directly."
 
     ;; Imenu.
     (setq-local treesit-simple-imenu-settings
-                `(("Variable" ,(rx bos "variable_declaration" eos) nil nil)
-                  ("Function" ,(rx bos
-                                   (or "function_declaration"
-                                       "function_definition"
-                                       "field")
-                                   eos)
-                   nil nil)))
+                `(("Requires"
+                   "\\`function_call\\'"
+                   lua-ts--require-p
+                   lua-ts--require-name-function)
+                  ("Variables" "\\`variable_declaration\\'" nil nil)
+                  (nil
+                   "\\`\\(?:f\\(?:ield\\|unction_declaration\\)\\)\\'"
+                   lua-ts--named-function-p
+                   nil)))
 
     ;; Which-function.
     (setq-local which-func-functions (treesit-defun-at-point))
index 67d4e8c4df1625237736009345c773158b337eed..51c5962cb66f9febb6f092ff28b75af59a109026 100644 (file)
@@ -631,7 +631,7 @@ function `speedbar-extension-list-to-regex'.")
   (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
            ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".js" ".f\\(90\\|77\\|or\\)?")
          (if speedbar-use-imenu-flag
-             '(".ad[abs]" ".p[lm]" ".tcl" ".m" ".scm" ".pm" ".py" ".g"
+             '(".ad[abs]" ".p[lm]" ".tcl" ".m" ".scm" ".pm" ".py" ".g" ".lua"
                ;; html is not supported by default, but an imenu tags package
                ;; is available.  Also, html files are nice to be able to see.
                ".s?html"