Subject: [PATCH] Improve completion of treesit-check-indent command
From: Daniel Martín <[email-masked]>
To: [email-masked]
Date: Mon, 12 Dec 2022 11:53:00 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

The treesit-check-indent command is not really related to Tree-sitter
and could be in a more general place than treesit.el.  WDYT?

In any case, I have a patch to improve this command a bit by offering
more accurate completion for major modes, not Emacs commands.  I have
also tried to improve its documentation a bit.

Thanks.

From e6699c1671f3f13fa7d98ef958c0159e80aa2645 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <[email-masked]>
Date: Mon, 12 Dec 2022 11:33:01 +0100
Subject: [PATCH] Improve completion of treesit-check-indent command

* lisp/treesit.el (treesit--read-major-mode): Helper function to
provide completion for most major modes.
(treesit-check-indent): Use the new interactive spec and improve the
docstring.
---
 lisp/treesit.el | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 85154d0d1c..de18796628 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1469,12 +1469,28 @@ treesit-simple-indent
                         (message "No matched rule"))
                       (cons nil nil))))))
 
-(defun treesit-check-indent (mode)
-  "Check current buffer's indentation against a major mode MODE.
+(defun treesit--read-major-mode ()
+  "Read a major mode using completion.
+Helper function to use in the `interactive' spec of `treesit-check-indent'."
+  (let* ((default (and (symbolp major-mode) (symbol-name major-mode)))
+	 (mode
+	  (completing-read
+	   (format-prompt "Target major mode" default)
+	   obarray
+	   (lambda (sym)
+	     (and (string-match-p "-mode\\'" (symbol-name sym))
+		  (not (or (memq sym minor-mode-list)
+                           (string-match-p "-minor-mode\\'"
+                                           (symbol-name sym))))))
+	   nil nil nil default nil)))
+    (cond
+     ((equal mode "nil") nil)
+     ((and (stringp mode) (fboundp (intern mode))) (intern mode))
+     (t mode))))
 
-Pop up a diff buffer showing the difference.  Correct
-indentation (target) is in green, current indentation is in red."
-  (interactive "CTarget major mode: ")
+(defun treesit-check-indent (mode)
+  "Compare the current buffer with how major mode MODE would indent it."
+  (interactive (list (treesit--read-major-mode)))
   (let ((source-buf (current-buffer)))
     (with-temp-buffer
       (insert-buffer-substring source-buf)
-- 
2.34.1