Subject: Re: bug#76693: [PATCH] Use TS to support 'hs-minor-mode' in 'lua-ts-mode'

From: john muhl <[email-masked]>
Date: Sun, 02 Mar 2025 15:45:45 -0600

>From e80d57b1f71af418642f8c13e6bdbbed92ca5339 Mon Sep 17 00:00:00 2001
From: john muhl <[email-masked]>
Date: Wed, 26 Feb 2025 10:31:24 -0600
Subject: [PATCH] Use TS to support 'hs-minor-mode' in 'lua-ts-mode'

* lisp/progmodes/lua-ts-mode.el (lua-ts-mode): Add list type to
'treesit-thing-settings'.
* lisp/progmodes/hideshow.el (hs-special-modes-alist):
Remove regular expression based implementation.
* test/lisp/progmodes/lua-ts-mode-resources/hide-show.lua: New file.
* test/lisp/progmodes/lua-ts-mode-tests.el (lua-ts-test-hideshow):
Add test.  (Bug#76693)
---
 lisp/progmodes/hideshow.el                    |  1 -
 lisp/progmodes/lua-ts-mode.el                 |  7 +++-
 .../lua-ts-mode-resources/hide-show.lua       | 33 +++++++++++++++++++
 test/lisp/progmodes/lua-ts-mode-tests.el      | 12 +++++++
 4 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 test/lisp/progmodes/lua-ts-mode-resources/hide-show.lua

diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el
index c9b43fe8e16..c1d62fb92ab 100644
--- a/lisp/progmodes/hideshow.el
+++ b/lisp/progmodes/hideshow.el
@@ -266,7 +266,6 @@ hs-special-modes-alist
     (java-ts-mode "{" "}" "/[*/]" nil nil)
     (js-mode "{" "}" "/[*/]" nil)
     (js-ts-mode "{" "}" "/[*/]" nil)
-    (lua-ts-mode "{\\|\\[\\[" "}\\|\\]\\]" "--" nil)
     (mhtml-mode "{\\|<[^/>]*?" "}\\|</[^/>]*[^/]>" "<!--" mhtml-forward nil)
     ;; Add more support here.
     )
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index de93d0fdaba..81a9acb2405 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -815,7 +815,12 @@ lua-ts-mode
                                       "parenthesized_expression"
                                       "string"
                                       "table_constructor"))))
-                   (text "comment"))))
+                   (list (or function
+                             loop-statement
+                             "if_statement"
+                             "table_constructor"
+                             "parenthesized_expression"))
+                   (text (or "string" "comment")))))
 
     ;; Imenu/Outline/Which-function.
     (setq-local treesit-simple-imenu-settings
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/hide-show.lua b/test/lisp/progmodes/lua-ts-mode-resources/hide-show.lua
new file mode 100644
index 00000000000..5dea5dcdb58
--- /dev/null
+++ b/test/lisp/progmodes/lua-ts-mode-resources/hide-show.lua
@@ -0,0 +1,33 @@
+--[[
+This is a
+comment block.
+]]
+local function fun ()
+    print("fun")
+end
+local f = (function ()
+    print(1)
+end)
+for i = 1, 10 do
+  print(i)
+end
+repeat
+    print("repeat")
+until false
+while true do
+    print("while")
+end
+do
+    print(1)
+end
+local t = {
+    a=1,
+    b=2,
+}
+if true then
+    print(1)
+elseif false then
+    print(0)
+else
+    print(0)
+end
diff --git a/test/lisp/progmodes/lua-ts-mode-tests.el b/test/lisp/progmodes/lua-ts-mode-tests.el
index 44c31648586..ba3fdf3fdd0 100644
--- a/test/lisp/progmodes/lua-ts-mode-tests.el
+++ b/test/lisp/progmodes/lua-ts-mode-tests.el
@@ -48,6 +48,18 @@ lua-ts-test-which-function
     (should (equal "f" (which-function)))
     (which-function-mode -1)))
 
+(ert-deftest lua-ts-test-hideshow ()
+  (skip-unless (treesit-ready-p 'lua t))
+  (with-temp-buffer
+    (insert-file-contents (ert-resource-file "hide-show.lua"))
+    (lua-ts-mode)
+    (hs-minor-mode)
+    (hs-hide-all)
+    (should (= 9 (length (overlays-in (point-min) (point-max)))))
+    (hs-show-all)
+    (should (= 0 (length (overlays-in (point-min) (point-max)))))
+    (hs-minor-mode -1)))
+
 (provide 'lua-ts-mode-tests)
 
 ;;; lua-ts-mode-tests.el ends here
-- 
2.48.1

[email-masked] writes:

> Tags: patch
>
> This changes the hs-minor-mode suppport to use tree-sitter list
> things instead of regexps.