From b889eced4449555373e53c26c280dffa548dcfc3 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Fri, 9 Dec 2022 20:12:51 +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): 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): Add regexp for the new variable. * lisp/progmodes/js.el (js-ts-mode): Add regexp for the new variable. * 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. * lisp/treesit.el (treesit-text-type-regexp): New variable. --- doc/emacs/programs.texi | 19 +++++++++++++++ etc/NEWS | 7 ++++++ lisp/progmodes/c-ts-mode.el | 4 ++++ lisp/progmodes/csharp-mode.el | 5 ++++ lisp/progmodes/java-ts-mode.el | 5 ++++ lisp/progmodes/js.el | 5 ++++ lisp/progmodes/prog-mode.el | 35 ++++++++++++++++++++++++++-- lisp/progmodes/sh-script.el | 4 ++++ lisp/progmodes/typescript-ts-mode.el | 4 ++++ lisp/treesit.el | 9 +++++++ 10 files changed, 95 insertions(+), 2 deletions(-) diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index ba8475e86ac..3b60732171e 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi @@ -409,6 +409,9 @@ large chunks of code: @table @kbd @item C-M-q Reindent all the lines within one parenthetical grouping. +@item M-q +Fill a single paragraph in a defun, or reindent all the lines within +that defun. @item C-u @key{TAB} Shift an entire parenthetical grouping rigidly sideways so that its first line is properly indented. @@ -429,6 +432,22 @@ indentation of the line where the grouping starts). The function that etc. To correct the overall indentation as well, type @kbd{@key{TAB}} first. +@kindex M-q +@findex prog-fill-reindent-defun +@vindex beginning-of-defun-function +@vindex end-of-defun-function +@vindex fill-paragraph-function + Major modes that derive from @code{prog-mode} can either fill a +single paragraph in a defun, such as a doc-string, or a comment, or +(re)indent the surrounding defun if point is not in a comment or a +string by typing @kbd{M-q} or using the command @kbd{M-x +prog-fill-reindent-defun}. The bounds of a defun is decided by the +variable @code{beginning-of-defun-function} and +@code{end-of-defun-function}, and the filling mechanism is decided by +@code{fill-paragraph-function} (@ref{List Motion,,, elisp, The Emacs +Lisp Reference Manual}, or @ref{Filling,,, elisp, The Emacs Lisp +Reference Manual} for more information). + @kindex C-u TAB If you like the relative indentation within a grouping but not the indentation of its first line, move point to that first line and type diff --git a/etc/NEWS b/etc/NEWS index 3338c06f037..8f6c67a3cb1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -76,6 +76,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, or (re)indents the surrounding defun if +point is not in a comment or a string. It is by default bound to +'M-q' in 'prog-mode' and all its descendants. * 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 824325d83e0..d21937f3556 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -627,6 +627,10 @@ the subtrees." (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 8a7313b1ce8..306a1e2bf8f 100644 --- a/lisp/progmodes/csharp-mode.el +++ b/lisp/progmodes/csharp-mode.el @@ -918,6 +918,11 @@ Key bindings: (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 9155a7fff25..d5f4f55fe0a 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -313,6 +313,11 @@ the subtrees." (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) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index f7318c481a2..da47f682d70 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3860,6 +3860,11 @@ Currently there are `js-mode' and `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 "[]*". diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index 58cb48f1829..1bd8234dc9c 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 @@ (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 @@ instead." (end (progn (forward-sexp 1) (point)))) (indent-region start end nil)))) +(defun prog-fill-reindent-defun (&optional argument) + "Refill or reindent the paragraph or defun that contains point. + +If the point is in a string or a comment, fill the paragraph that +contains point or follows point. + +Otherwise, reindent the definition that contains point or follows +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 e170d18afeb..1605e403473 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -1619,6 +1619,10 @@ not written in Bash or sh." ( 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 8c4364ecc5b..aaf551850d5 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -329,6 +329,10 @@ Argument LANGUAGE is either `typescript' or `tsx'." (group (or (syntax comment-end) (seq (+ "*") "/"))))) + (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 85154d0d1c7..133564f6c8e 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1639,6 +1639,15 @@ ARG is the same as in `beginning-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.39.2