Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun

From: Theodor Thornhill <[email-masked]>
Date: Fri, 09 Dec 2022 16:50:25 +0100

Dmitry Gutov <[email-masked]> writes:

> On 09/12/2022 17:13, Theodor Thornhill via Bug reports for GNU Emacs, 
> the Swiss army knife of text editors wrote:
>>>> Something like this?
>>>
>>> Yes. But... :-)
>>>
>>> I guess you can replace (regexp-opt '("comment")) with just "comment"?
>>> Both regexps are equivalent, and neither uses anchoring (like \` and
>>> \'). Should they have anchoring?
>>>
>> 
>> Hehe, I did that first, but I trusted you knew some secret about that
>> expression.  No, we get the names, so I think it's good.
>
> Shouldn't trust me that much ;-)
>

:-)

> It could be a problem if some grammar somewhere will have a node type 
> called "not_comment", and the regexp will still match it. Or 
> "pseudo_string", etc.
>
> Not sure how realistic that scenario is, up to you. The authors of such 
> major modes could opt for a stricted regexp individually, too.
>

Right.  Yeah, I don't think it'll be a problem, but better to be on the
safe side.  Tweaked the regexp a little

>>> The programs.texi addition should probably mention refilling too.
>>>
>> 
>> Good point.  Will fix that.
>
> Still not ideal: it says "refill all the lines within a defun", but the 
> refilling step only touches the paragraph around point.
>
> Also about NEWS: "textual element" is a little value; it really refills 
> a single paragraph. That distinction is meaningful when a string or a 
> comment are long, containing several paragraphs inside.

Something like this?

Theo

>From 8e8e045423f6aba96d660f30a7a4e96073d2ad75 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Thu, 8 Dec 2022 20:06:09 +0100
Subject: [PATCH] Add prog-fill-reindent-defun (bug#59664)

Introduce a new command that aims to reindent code in a defun, or fill
a paragraph of text.  The command uses treesit.el when available,
otherwise falls back to using syntax-ppss and regexps.  Treesit.el
needs a new variable that is intended to be set by the major modes so
that this and other future functions can know what kind of node we are
looking at.

* doc/emacs/programs.texi: Mention the new command.
* etc/NEWS: Mention the new command.
* lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings): Add
raw_string_literal for C++.
(c++-ts-mode): Add regexp for the new variable.
* lisp/progmodes/csharp-mode.el (csharp-ts-mode): Add regexp for the
new variable.
* lisp/progmodes/java-ts-mode.el (java-ts-mode--indent-rules): Add
text_block as no-indent.
(java-ts-mode--font-lock-settings): Add text_block font-lock-rule.
(java-ts-mode): Add regexp for the new variable.
* lisp/progmodes/js.el (js-ts-mode): Add regexp for the new variable.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Add comment font-lock-rule.
(json-ts-mode): Use the new comment feature.
* list/progmodes/prog-mode.el (prog-mode-map): Bind the new command by
default.
(prog-fill-reindent-defun): New command.
* lisp/progmodes/sh-script.el (bash-ts-mode): Add regexp for the new
variable.
* lisp/progmodes/typescript-ts-mode.el (typescript-ts-base-mode): Add
regexp for the new variable.
(typescript-ts-mode--indent-rules): Add class_body.
* lisp/treesit.el (treesit-text-type-regexp): New variable.
---
 doc/emacs/programs.texi              |  8 +++++++
 etc/NEWS                             |  7 ++++++
 lisp/progmodes/c-ts-mode.el          |  8 ++++++-
 lisp/progmodes/csharp-mode.el        |  5 ++++
 lisp/progmodes/java-ts-mode.el       | 19 +++++++++++++--
 lisp/progmodes/js.el                 |  6 +++++
 lisp/progmodes/json-ts-mode.el       |  5 +++-
 lisp/progmodes/prog-mode.el          | 35 ++++++++++++++++++++++++++--
 lisp/progmodes/sh-script.el          |  4 ++++
 lisp/progmodes/typescript-ts-mode.el |  6 +++++
 lisp/treesit.el                      |  9 +++++++
 11 files changed, 106 insertions(+), 6 deletions(-)

diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi
index ba8475e86a..cc06a003e2 100644
--- a/doc/emacs/programs.texi
+++ b/doc/emacs/programs.texi
@@ -409,6 +409,14 @@ Multi-line Indent
 @table @kbd
 @item C-M-q
 Reindent all the lines within one parenthetical grouping.
+@item M-q
+Major modes that derive from @code{prog-mode} can reindent all the
+lines within a defun around point using
+@code{prog-mode-fill-reindent-defun}.  The bounds of of a defun is
+decided by @code{beginning-of-defun-function} and
+@code{end-of-defun-function}.  If point is inside a comment or a
+doc-string, refill that paragraph instead, using
+@code{fill-paragraph-function}.
 @item C-u @key{TAB}
 Shift an entire parenthetical grouping rigidly sideways so that its
 first line is properly indented.
diff --git a/etc/NEWS b/etc/NEWS
index 61f813568f..659ee39faf 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -61,6 +61,13 @@ using this new option.  (Or set 'display-buffer-alist' directly.)
 After manually editing 'eshell-aliases-file', you can use
 'M-x eshell-read-aliases-list' to load the edited aliases.
 
+** Prog Mode
++++
+*** New command 'prog-fill-reindent-defun'
+This command either fills a single paragraph in a defun, such as a
+doc-string, or a comment.  If point is inside normal code, it
+(re)indents the surrounding defun.  Bound by default in
+'prog-mode-map' to 'M-q'.
 
 * New Modes and Packages in Emacs 30.1
 
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 08b03d5666..f7285e8dd6 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -248,7 +248,9 @@ c-ts-mode--font-lock-settings
    :language mode
    :feature 'string
    `((string_literal) @font-lock-string-face
-     (system_lib_string) @font-lock-string-face)
+     (system_lib_string) @font-lock-string-face
+     ,@(when (eq mode 'cpp)
+         '((raw_string_literal) @font-lock-string-face)))
 
    :language mode
    :feature 'literal
@@ -603,6 +605,10 @@ c++-ts-mode
                   (group (or (syntax comment-end)
                              (seq (+ "*") "/")))))
 
+  (setq-local treesit-text-type-regexp
+              (regexp-opt '("comment"
+                            "raw_string_literal")))
+
   (treesit-parser-create 'cpp)
 
   (setq-local treesit-simple-indent-rules
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index 8ab5fbc91d..3faeb54fc5 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -911,6 +911,11 @@ csharp-ts-mode
                   (group (or (syntax comment-end)
                              (seq (+ "*") "/")))))
 
+  (setq-local treesit-text-type-regexp
+              (regexp-opt '("comment"
+                            "verbatim_string-literal"
+                            "interpolated_verbatim_string-text")))
+
   ;; Indent.
   (setq-local treesit-simple-indent-rules csharp-ts-mode--indent-rules)
 
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 96e0d5244c..f29445195a 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -68,6 +68,7 @@ java-ts-mode--indent-rules
      ((node-is "]") parent-bol 0)
      ((and (parent-is "comment") comment-end) comment-start -1)
      ((parent-is "comment") comment-start-skip 0)
+     ((parent-is "text_block") no-indent)
      ((parent-is "class_body") parent-bol java-ts-mode-indent-offset)
      ((parent-is "interface_body") parent-bol java-ts-mode-indent-offset)
      ((parent-is "constructor_body") parent-bol java-ts-mode-indent-offset)
@@ -157,7 +158,8 @@ java-ts-mode--font-lock-settings
    :language 'java
    :override t
    :feature 'string
-   `((string_literal) @font-lock-string-face)
+   `((string_literal) @font-lock-string-face
+     (text_block) @font-lock-string-face)
    :language 'java
    :override t
    :feature 'literal
@@ -308,6 +310,11 @@ java-ts-mode
                   (group (or (syntax comment-end)
                              (seq (+ "*") "/")))))
 
+  (setq-local treesit-text-type-regexp
+              (regexp-opt '("line_comment"
+                            "block_comment"
+                            "text_block")))
+
   ;; Indent.
   (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules)
 
@@ -316,7 +323,15 @@ java-ts-mode
               (append "{}():;," electric-indent-chars))
 
   ;; Navigation.
-  (setq-local treesit-defun-type-regexp "declaration")
+  (setq-local treesit-defun-type-regexp
+              (regexp-opt '("method_declaration"
+                            "class_declaration"
+                            "record_declaration"
+                            "interface_declaration"
+                            "enum_declaration"
+                            "import_declaration"
+                            "package_declaration"
+                            "module_declaration")))
 
   ;; Font-lock.
   (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 45dfef372c..02cc558366 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3857,6 +3857,11 @@ js-ts-mode
                     (group (or (syntax comment-end)
                                (seq (+ "*") "/")))))
     (setq-local comment-multi-line t)
+
+    (setq-local treesit-text-type-regexp
+                (regexp-opt '("comment"
+                              "template_string")))
+
     ;; Electric-indent.
     (setq-local electric-indent-chars
 	        (append "{}():;," electric-indent-chars)) ;FIXME: js2-mode adds "[]*".
@@ -3868,6 +3873,7 @@ js-ts-mode
     ;; Indent.
     (setq-local treesit-simple-indent-rules js--treesit-indent-rules)
     ;; Navigation.
+    (setq-local treesit-defun-prefer-top-level t)
     (setq-local treesit-defun-type-regexp
                 (rx (or "class_declaration"
                         "method_definition"
diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el
index 0a0113d1d8..736ff7f3cd 100644
--- a/lisp/progmodes/json-ts-mode.el
+++ b/lisp/progmodes/json-ts-mode.el
@@ -72,6 +72,9 @@ json-ts--indent-rules
 
 (defvar json-ts-mode--font-lock-settings
   (treesit-font-lock-rules
+   :language 'json
+   :feature 'comment
+   '((comment) @font-lock-comment-face)
    :language 'json
    :feature 'bracket
    '((["[" "]" "{" "}"]) @font-lock-bracket-face)
@@ -159,7 +162,7 @@ json-ts-mode
   ;; Font-lock.
   (setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings)
   (setq-local treesit-font-lock-feature-list
-              '((constant number pair string)
+              '((comment constant number pair string)
                 (escape-sequence)
                 (bracket delimiter error)))
 
diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
index 58cb48f182..c29efa3a21 100644
--- a/lisp/progmodes/prog-mode.el
+++ b/lisp/progmodes/prog-mode.el
@@ -30,7 +30,11 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl-lib)
-                   (require 'subr-x))
+                   (require 'subr-x)
+                   (require 'treesit))
+
+(declare-function treesit-parser-list "treesit.c")
+(declare-function treesit-node-type "treesit.c")
 
 (defgroup prog-mode nil
   "Generic programming mode, from which others derive."
@@ -102,7 +106,8 @@ prog-context-menu
 
 (defvar-keymap prog-mode-map
   :doc "Keymap used for programming modes."
-  "C-M-q" #'prog-indent-sexp)
+  "C-M-q" #'prog-indent-sexp
+  "M-q" #'prog-fill-reindent-defun)
 
 (defvar prog-indentation-context nil
   "When non-nil, provides context for indenting embedded code chunks.
@@ -140,6 +145,32 @@ prog-indent-sexp
 	  (end (progn (forward-sexp 1) (point))))
       (indent-region start end nil))))
 
+(defun prog-fill-reindent-defun (&optional argument)
+  "Refill paragraph or reindent the definition that the point is on.
+
+If the point is in a string, or in a comment, or there is a
+comment on the current line, fill the paragraph that the point is
+in or is on the same line.
+
+Otherwise, reindent the definition around or below point."
+  (interactive "P")
+  (save-excursion
+    (let ((treesit-text-node
+           (and (treesit-parser-list)
+                (string-match-p
+                 treesit-text-type-regexp
+                 (treesit-node-type (treesit-node-at (point)))))))
+      (if (or treesit-text-node
+              (nth 8 (syntax-ppss))
+              (re-search-forward comment-start-skip (line-end-position) t))
+          (if (memq fill-paragraph-function '(t nil))
+              (lisp-fill-paragraph argument)
+            (funcall fill-paragraph-function argument))
+        (beginning-of-defun)
+        (let ((start (point)))
+          (end-of-defun)
+          (indent-region start (point) nil))))))
+
 (defun prog-first-column ()
   "Return the indentation column normally used for top-level constructs."
   (or (car prog-indentation-context) 0))
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index e170d18afe..1605e40347 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -1619,6 +1619,10 @@ bash-ts-mode
                   ( bracket delimiter misc-punctuation operator)))
     (setq-local treesit-font-lock-settings
                 sh-mode--treesit-settings)
+    (setq-local treesit-text-type-regexp
+                (regexp-opt '("comment"
+                              "heredoc_start"
+                              "heredoc_body")))
     (treesit-major-mode-setup)))
 
 (advice-add 'bash-ts-mode :around #'sh--redirect-bash-ts-mode
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 20916eaf37..74462bd232 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -74,6 +74,7 @@ typescript-ts-mode--indent-rules
      ((parent-is "member_expression") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "named_imports") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "statement_block") parent-bol typescript-ts-mode-indent-offset)
+     ((parent-is "class_body") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "type_arguments") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "variable_declarator") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "arguments") parent-bol typescript-ts-mode-indent-offset)
@@ -323,6 +324,11 @@ typescript-ts-base-mode
                   (group (or (syntax comment-end)
                              (seq (+ "*") "/")))))
 
+  (setq-local treesit-defun-prefer-top-level t)
+  (setq-local treesit-text-type-regexp
+              (regexp-opt '("comment"
+                            "template_string")))
+
   ;; Electric
   (setq-local electric-indent-chars
               (append "{}():;," electric-indent-chars))
diff --git a/lisp/treesit.el b/lisp/treesit.el
index dbbf7ec18c..41feac35c2 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1626,6 +1626,15 @@ treesit-end-of-defun
     (when top
       (goto-char (treesit-node-end top)))))
 
+(defvar-local treesit-text-type-regexp "\\`comment\\'"
+  "A regexp that matches the node type of textual nodes.
+
+A textual node is a node that is not normal code, such as
+comments and multiline string literals.  For example,
+\"(line|block)_comment\" in the case of a comment, or
+\"text_block\" in the case of a string.  This is used by
+`prog-fill-reindent-defun' and friends.")
+
 ;;; Activating tree-sitter
 
 (defun treesit-ready-p (language &optional quiet)
-- 
2.34.1