From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun Date: Sat, 10 Dec 2022 21:21:53 +0100 Message-ID: <87y1rfxb4u.fsf@thornhill.no> References: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> <87zgbyxohf.fsf@thornhill.no> <837cz22bgd.fsf@gnu.org> <2C2420B5-A0BE-4A81-A924-0F40DD57F211@thornhill.no> <877cz1d7vq.fsf@thornhill.no> <83ilil1wot.fsf@gnu.org> <877E42C0-4FDD-4ED7-933D-AB2199BBCEF3@thornhill.no> <83edt917z5.fsf@gnu.org> <87o7scww07.fsf@thornhill.no> <40acda30-cf00-b419-b726-2794206c96e9@yandex.ru> <87ilikws6a.fsf@thornhill.no> <87edt8wqyo.fsf@thornhill.no> <64801406-62b9-c691-da3e-b8dbcee203be@yandex.ru> <87wn70injy.fsf@thornhill.no> <834ju4z5vu.fsf@gnu.org> <87mt7wi8sp.fsf@thornhill.no> <83r0x7wdtm.fsf@gnu.org> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21881"; mail-complaints-to="usenet@ciao.gmane.io" Cc: larsi@gnus.org, 59662@debbugs.gnu.org, casouri@gmail.com, monnier@iro.umontreal.ca, dgutov@yandex.ru To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Dec 10 21:23:16 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p46NQ-0005Xo-QR for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 10 Dec 2022 21:23:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p46NE-0007Qv-L1; Sat, 10 Dec 2022 15:23:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p46ND-0007Ql-2I for bug-gnu-emacs@gnu.org; Sat, 10 Dec 2022 15:23:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p46NC-0000Mq-QB for bug-gnu-emacs@gnu.org; Sat, 10 Dec 2022 15:23:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p46NC-0005cY-Dd for bug-gnu-emacs@gnu.org; Sat, 10 Dec 2022 15:23:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 10 Dec 2022 20:23:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59662 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 59662-submit@debbugs.gnu.org id=B59662.167070372321580 (code B ref 59662); Sat, 10 Dec 2022 20:23:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 10 Dec 2022 20:22:03 +0000 Original-Received: from localhost ([127.0.0.1]:44868 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p46ME-0005c0-Q3 for submit@debbugs.gnu.org; Sat, 10 Dec 2022 15:22:03 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:15713) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p46M9-0005bc-Mv for 59662@debbugs.gnu.org; Sat, 10 Dec 2022 15:22:01 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1670703715; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YSisTZG1MXDDaEntyQzZxKFH0GvSXUvOjJy4MjsyrFQ=; b=Y21O/TT1xQMB0zzkIdmwJ5Ry1pTAzhhBiJqLEDYfe0TBESfH2yyYVYiKmn6c/SRsVacZdl pdCYmBX5XgBQI32+gMjP5oN+6SVCM39IDVVmGHerNytWCFiR8IH76W7R/gf8Ko1SLQWtqd KZVEg7u3JfpKi/WXJz+7USrGBnJEK1z0TqWEblVvwPwPE/MvZKo65pW6/dMxnviIusltRF KMi2AVRN5/ep8oN5ZbC226xlGTA1afqDSWvGiSChVVO7CKiD6bNiJpS8Tb0dkygZerXSWy 8bWJbJSLG4lcT2p/BA3b5iBupEQGbsR6iZmXS8s+T11JkpJQ1TxfYjyNZE+CQg== In-Reply-To: <83r0x7wdtm.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:250558 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: dgutov@yandex.ru, larsi@gnus.org, 59662@debbugs.gnu.org, >> casouri@gmail.com, monnier@iro.umontreal.ca >> Date: Fri, 09 Dec 2022 22:09:10 +0100 >> >> Something like the patch below? Forgive me, but my texinfo-fu is >> limited. > > You have nothing to worry in that department. > >> I'm a little unsure if I xrefed to the correct nodes. Maybe >> it is better to just mention 'beginning-of-defun' rather than the >> variable 'beginning-of-defun-function'? > > I meant a reference to where beginning-of-defun-function is mentioned, > and that is in the ELisp manual. You can see examples of how to > reference another manual in many places in doc/emacs/*.texi; seartch > for "elisp". > Ok, thanks! >> >> +(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. >> > >> > I don't understand the difference between "point is in a string, or in >> > a comment" and "there is a comment on the current line". >> > >> >> +Otherwise, reindent the definition around or below point." >> > >> > Likewise this one is confusing. >> >> I'm not sure how to word this differently, but I agree it's a mouthful. > > If you explain to me in plain English what you meant, I can suggest a > better wording. > I made another attempt - I think it's a little clearer. What I want to say is something like this (| denotes point): 1. If point is inside a comment, refill the paragraph: // foo bar | baz 2. If point is before a comment, refill the paragraph: | // foo bar baz 3. If point is inside code, reindent the defun: int main(void) { int i; // <-- This will be reindented /* Some very long comment that will _not_ be refilled in this case. */ for(;;) { | int x; } } Anyway, see attached patch: Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-prog-fill-reindent-defun-bug-59664.patch >From aeb709da894d19ec4fe5b05be1eb957861eb3dba 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 | 18 ++++++++++++++ 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, 94 insertions(+), 2 deletions(-) diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index ba8475e86a..d85fcd539c 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi @@ -409,6 +409,9 @@ Multi-line Indent @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,21 @@ Multi-line Indent etc. To correct the overall indentation as well, type @kbd{@key{TAB}} first. +@kindex M-q +@findex prog-mode-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}. 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} (@pxref{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 61f813568f..a5eb7f332d 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, 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 08b03d5666..114ffc0b03 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -603,6 +603,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..b1bf965e4f 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -308,6 +308,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) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 45dfef372c..d0b243a22f 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 "[]*". diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index 58cb48f182..1bd8234dc9 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 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 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..59598b4c63 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -323,6 +323,10 @@ typescript-ts-base-mode (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 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 --=-=-=--