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: Thu, 08 Dec 2022 20:15:37 +0100 Message-ID: <877cz1d7vq.fsf@thornhill.no> References: <5B538888-E454-4F75-B3B8-AB20E10B3E89@gmail.com> <83o7sniokt.fsf@gnu.org> <4D5A1640-4F0F-4C21-8ED6-770B88A870EA@thornhill.no> <83r0xhdluq.fsf@gnu.org> <3e25eadb-3f2b-f92d-8b18-bf1daca91b64@yandex.ru> <83wn78cqz4.fsf@gnu.org> <4d605e69-7df9-6480-c1c5-dcb22abd3bde@yandex.ru> <83y1ri2wnj.fsf@gnu.org> <875yemz55b.fsf@thornhill.no> <83r0xa2tc4.fsf@gnu.org> <87zgbyxohf.fsf@thornhill.no> <837cz22bgd.fsf@gnu.org> <2C2420B5-A0BE-4A81-A924-0F40DD57F211@thornhill.no> 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="40051"; mail-complaints-to="usenet@ciao.gmane.io" Cc: larsi@gnus.org, 59662@debbugs.gnu.org, Yuan Fu , monnier@iro.umontreal.ca To: Dmitry Gutov , Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 08 20:16:17 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 1p3MNU-000AA1-Ol for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 08 Dec 2022 20:16:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3MNO-0006PY-3s; Thu, 08 Dec 2022 14:16:10 -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 1p3MNG-0006NU-PK for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 14:16:02 -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 1p3MNG-00069p-HS for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 14:16:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p3MNG-0002Z8-DC for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2022 14:16: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: Thu, 08 Dec 2022 19:16: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.16705269449831 (code B ref 59662); Thu, 08 Dec 2022 19:16:02 +0000 Original-Received: (at 59662) by debbugs.gnu.org; 8 Dec 2022 19:15:44 +0000 Original-Received: from localhost ([127.0.0.1]:58537 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3MMx-0002YV-Cg for submit@debbugs.gnu.org; Thu, 08 Dec 2022 14:15:44 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:41004) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p3MMu-0002YI-1g for 59662@debbugs.gnu.org; Thu, 08 Dec 2022 14:15:41 -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=1670526938; 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=DE2SefOxgHesDFhoui0LtMEXLTltlQb+ZTU7nzW9SCg=; b=lMY/5qnqcYFuIsGlA4q3zNX5fl/EuidlWqqTBneVKozxYulChmWDbHjquwVvRIL5dGZerG j9LUY/9KsyQZ+VIgeOIP9uDKjoS9QoFxPrw+7UXUtvHWQxcpS+M3ghEVwo9SAlBVzoCr70 rcvrtnPsXyk52HAQgLVwuHEDx2YvtOWKJ3qCZX3ZE0Aua7bUTexzTBiWMASe+3Mdz8dcyI l9WMYH7Jh35psRS7IcVqwSTSmcVqmVxX9tcBHe38hP4w6O20O8B5DecI60tAD4QbjAGpkz ATm7hL6iH/ZdDSZDNRowmgRjC18Tn/xZKobeAoQvgmFjO5hPTgf4FmMHYqDtwA== In-Reply-To: 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:250329 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Dmitry Gutov writes: > On 08/12/2022 18:04, Theodor Thornhill via Bug reports for GNU Emacs,=20 > the Swiss army knife of text editors wrote: >>=20 >>=20 >>> syntax-ppss is used in a lot of places. A "free" improvement like the a= bove wouldn't hurt. >>> >>>>> So my preference would be to check in the , the var will be there as = well. >>>> I don't insist, but it sounds like the logical thing to do. So unless >>>> you feel strongly against that, I'd say let's add that part. This is, >>>> after all, an experiment, so let's experiment. >>> I'm not going to object. >>> >>> Doubly so if somebody else wants to finish up the patch and commit it w= ith NEWS and documentation updates. =F0=9F=98=89 >> I can finish up the patch if you don't want to =F0=9F=98=84 > > If you don't mind. Thank you. :-) Ok, will do. In preparation for that function, we need that variable on the emacs-29 branch, right? So here's a patch for that. What do you think, Dmitry, Eli and Yuan? If this gets installed on emacs-29, I can make the function we discussed on the master branch when that is merged. Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-treesit-text-type-regexp.patch >From 5edf9bd675b35f97cc0245036ae6ceeb91de1c52 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Thu, 8 Dec 2022 20:06:09 +0100 Subject: [PATCH] Add treesit-text-type-regexp Sometimes it is good to easily differentiate between textual nodes and normal code. One such case is for a filling and reindenting command, where we fill the textual node if point is on one, and reindent code otherwise. This is a part of bug#59662. * lisp/treesit.el: New variable. * lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings): Add raw_string_literal for C++. (c-ts-mode, c++-ts-mode): Add regexp for the new variable. * lisp/progmodes/csharp-mode.el: 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): Add regexp for the new variable, and use the new comment feature. * lisp/progmodes/python.el (python-ts-mode): Add regexp for the new variable. * 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/textmodes/css-mode.el (css-ts-mode): Add regexp for the new variable. --- lisp/progmodes/c-ts-mode.el | 12 +++++++++++- lisp/progmodes/csharp-mode.el | 5 +++++ lisp/progmodes/java-ts-mode.el | 9 ++++++++- lisp/progmodes/js.el | 5 +++++ lisp/progmodes/json-ts-mode.el | 8 +++++++- lisp/progmodes/python.el | 2 ++ lisp/progmodes/sh-script.el | 4 ++++ lisp/progmodes/typescript-ts-mode.el | 4 ++++ lisp/textmodes/css-mode.el | 2 ++ lisp/treesit.el | 8 ++++++++ 10 files changed, 56 insertions(+), 3 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 0b17541a0a..10aec36c2f 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -252,7 +252,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 @@ -592,6 +594,10 @@ c-ts-mode (setq-local comment-start-skip (rx (or (seq "/" (+ "/")) (seq "/" (+ "*"))) (* (syntax whitespace)))) + + (setq-local treesit-text-type-regexp + (regexp-opt '("comment"))) + (setq-local comment-end-skip (rx (* (syntax whitespace)) (group (or (syntax comment-end) @@ -628,6 +634,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 82e3bc0d54..f83ab16caa 100644 --- a/lisp/progmodes/csharp-mode.el +++ b/lisp/progmodes/csharp-mode.el @@ -912,6 +912,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 bda4ac86c8..30c920be7f 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -73,6 +73,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) @@ -162,7 +163,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 @@ -313,6 +315,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/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el index f3bd93923e..d655821ec9 100644 --- a/lisp/progmodes/json-ts-mode.el +++ b/lisp/progmodes/json-ts-mode.el @@ -75,6 +75,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,10 +162,13 @@ json-ts-mode (setq-local treesit-defun-type-regexp (rx (or "pair" "object"))) + (setq-local treesit-text-type-regexp + (regexp-opt '("comment"))) + ;; 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/python.el b/lisp/progmodes/python.el index ebee703499..f678e10fdf 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -6631,6 +6631,8 @@ python-ts-mode #'python-imenu-treesit-create-index) (setq-local treesit-defun-type-regexp (rx (or "function" "class") "_definition")) + (setq-local treesit-text-type-regexp + (regexp-opt '("comment"))) (treesit-major-mode-setup) (when python-indent-guess-indent-offset 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 a56568ae78..e502c6d249 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -329,6 +329,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/textmodes/css-mode.el b/lisp/textmodes/css-mode.el index 822097a86d..eb4fbc887c 100644 --- a/lisp/textmodes/css-mode.el +++ b/lisp/textmodes/css-mode.el @@ -1835,6 +1835,8 @@ css-ts-mode (treesit-parser-create 'css) (setq-local treesit-simple-indent-rules css--treesit-indent-rules) (setq-local treesit-defun-type-regexp "rule_set") + (setq-local treesit-text-type-regexp + (regexp-opt '("comment"))) (setq-local treesit-font-lock-settings css--treesit-settings) (setq-local treesit-font-lock-feature-list '((selector comment query keyword) diff --git a/lisp/treesit.el b/lisp/treesit.el index 85154d0d1c..cc94b460c7 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1639,6 +1639,14 @@ treesit-end-of-defun (when top (goto-char (treesit-node-end top))))) +(defvar-local treesit-text-type-regexp nil + "A regexp that matches the node type of textual nodes. + +A textual 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 \"verbatim_string_literal\" in the +case of a string.") + ;;; Activating tree-sitter (defun treesit-ready-p (language &optional quiet) -- 2.34.1 --=-=-=--