From 4c67794a13b2f980b9436326512f0c9925195e28 Mon Sep 17 00:00:00 2001 From: john muhl 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