all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#66465: [PATCH] Improve imenu support in lua-ts-mode
@ 2023-10-11 17:15 john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-10-25 13:50 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-10-11 17:15 UTC (permalink / raw)
  To: 66465

[-- Attachment #1: Type: text/plain, Size: 125 bytes --]

Tags: patch

This moves functions to the top level, adds a category for requires and
gets rid of the "Anonymous" entries.




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improve-imenu-support-in-lua-ts-mode.patch --]
[-- Type: text/patch, Size: 3949 bytes --]

From 4c67794a13b2f980b9436326512f0c9925195e28 Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Tue, 10 Oct 2023 14:39:30 -0500
Subject: [PATCH] Improve imenu support in lua-ts-mode

* 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.
---
 lisp/progmodes/lua-ts-mode.el | 43 +++++++++++++++++++++++++++++------
 lisp/speedbar.el              |  2 +-
 2 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 030a3585158..224199dff74 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -292,6 +292,33 @@ lua-ts--defun-name-function
        (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)
@@ -422,13 +449,15 @@ lua-ts-mode
 
     ;; 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))
diff --git a/lisp/speedbar.el b/lisp/speedbar.el
index 67d4e8c4df1..51c5962cb66 100644
--- a/lisp/speedbar.el
+++ b/lisp/speedbar.el
@@ -631,7 +631,7 @@ speedbar-supported-extension-expressions
   (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"
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-25 13:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-11 17:15 bug#66465: [PATCH] Improve imenu support in lua-ts-mode john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 13:50 ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.