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#61142: 29.0.60; java-ts-mode - Wrong indentation for the body of multiple lines conditions Date: Sun, 05 Feb 2023 08:53:33 +0100 Message-ID: <87r0v47dky.fsf@thornhill.no> References: <87wn4x7lpw.fsf_-_@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="4549"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61142@debbugs.gnu.org To: =?UTF-8?Q?=E5=A4=AA=E9=98=B3?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 05 08:54:16 2023 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 1pOZqo-0000xy-US for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 05 Feb 2023 08:54:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOZqg-0007z3-62; Sun, 05 Feb 2023 02:54:06 -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 1pOZqc-0007yq-NE for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 02:54: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 1pOZqc-0001hV-Ax for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 02:54:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pOZqb-0000FB-TX for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 02:54:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 05 Feb 2023 07:54:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61142 X-GNU-PR-Package: emacs Original-Received: via spool by 61142-submit@debbugs.gnu.org id=B61142.1675583622911 (code B ref 61142); Sun, 05 Feb 2023 07:54:01 +0000 Original-Received: (at 61142) by debbugs.gnu.org; 5 Feb 2023 07:53:42 +0000 Original-Received: from localhost ([127.0.0.1]:44011 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOZqH-0000Ed-N0 for submit@debbugs.gnu.org; Sun, 05 Feb 2023 02:53:42 -0500 Original-Received: from out-82.mta1.migadu.com ([95.215.58.82]:51204) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOZqF-0000ET-Br for 61142@debbugs.gnu.org; Sun, 05 Feb 2023 02:53:40 -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=1675583615; 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=kr8nA74M4qEIq7l9zjFd/HN+vAetPVIUPcqxiGhWquI=; b=pQV6CwvVpXckLhV5mBoi05xu9Op28A5r4LaqiiOcFVErz19NDSL2DT3PvzxfEnG5ai1LDt sZsbNgT7P1wH06YexyIZndt1HDO1kiVzEw7rms4QDhgiWHQ/yb0QJBV+u+f6c/u9OCOXip YxlPJc9CpL2PM7qH9o6Co0a9c4xRaR+FFZnL5oZYH+DjNE3gJRSlCk4Tblp6/pg56Q6Gyi 3K8VInVll9jGNu0oylwWMJ+/VthF4n4ySQ9bUoO7+Ensas2I1mscJnlCXBtl+vhuErcnHy +AwSa417zVBbocgqtN+x6QasGVGE/u60Gt/ACftlc1ABIAoNPNIe3Hwgt2m9YQ== In-Reply-To: <87wn4x7lpw.fsf_-_@thornhill.no> (Theodor Thornhill's message of "Sat, 04 Feb 2023 11:45:31 +0100") 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:254823 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Theodor Thornhill writes: > =E5=A4=AA=E9=98=B3 writes: > >> After reading treesit's manual, I found that we can use `n-p-gp` as the = matcher >> and `grand-parent` as the anchor to match the body of `if` (and `while`,= which >> has a similar issue). >> >> I mean a rule looks like the following: >> >> ``` >> ((n-p-gp nil "block" "if_statement") grand-parent java-ts-mode-indent-of= fset) >> ``` >> >> > > > Thanks! We have a mechanism in place for this, see > 'c-ts-common-statement-offset' in 'c-ts-common.el'. I didn't get to it > yet, but I'll try to look at it this evening, unless you want to? See > c-ts-mode for examples of how to use it, if you're interested! > > Theo Hi again! Can you test this patch for me? @Eli: Is this ok for emacs-29? I'd love to add some test for this, but now they are on the master branch. What is the common procedure to move stuff from master branch to the release branch? I _can_ just add some test to the master branch, but it would be nice to have them on emacs-29 too :-) Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Use-c-ts-common-statement-offset-in-java-ts-mode.patch >From 18beb67646446363cec98e4074822b9fcf270d12 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 5 Feb 2023 08:49:08 +0100 Subject: [PATCH] Use c-ts-common-statement-offset in java-ts-mode * lisp/progmodes/java-ts-mode.el (java-ts-mode--indent-rules): Add new matchers to enable c-ts-common machinery. (java-ts-mode): Add regexps. * lisp/progmodes/c-ts-common.el (c-ts-common-statement-offset): Fix typo. --- lisp/progmodes/c-ts-common.el | 2 +- lisp/progmodes/java-ts-mode.el | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 8729cae4ba..188734badb 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -285,7 +285,7 @@ c-ts-common-statement-offset "This anchor is used for children of a statement inside a block. This function basically counts the number of block nodes (i.e., -brackets) (defined by `c-ts-mode--indent-block-type-regexp') +brackets) (defined by `c-ts-common-indent-block-type-regexp') between NODE and the root node (not counting NODE itself), and multiply that by `c-ts-common-indent-offset'. diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index b9f7894095..1b6f2ba8b3 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -70,22 +70,24 @@ java-ts-mode--syntax-table (defvar java-ts-mode--indent-rules `((java ((parent-is "program") point-min 0) - ((node-is "}") (and parent parent-bol) 0) + ((match "}" "element_value_array_initializer") + parent-bol 0) + ((node-is "}") point-min c-ts-common-statement-offset) ((node-is ")") parent-bol 0) ((node-is "]") parent-bol 0) ((and (parent-is "comment") c-ts-common-looking-at-star) c-ts-common-comment-start-after-first-star -1) ((parent-is "comment") prev-adaptive-prefix 0) ((parent-is "text_block") no-indent) - ((parent-is "class_body") parent-bol java-ts-mode-indent-offset) + ((parent-is "class_body") point-min c-ts-common-statement-offset) ((parent-is "array_initializer") parent-bol java-ts-mode-indent-offset) - ((parent-is "annotation_type_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) + ((parent-is "annotation_type_body") point-min c-ts-common-statement-offset) + ((parent-is "interface_body") point-min c-ts-common-statement-offset) + ((parent-is "constructor_body") point-min c-ts-common-statement-offset) ((parent-is "enum_body_declarations") parent-bol 0) - ((parent-is "enum_body") parent-bol java-ts-mode-indent-offset) - ((parent-is "switch_block") parent-bol java-ts-mode-indent-offset) - ((parent-is "record_declaration_body") parent-bol java-ts-mode-indent-offset) + ((parent-is "enum_body") point-min c-ts-common-statement-offset) + ((parent-is "switch_block") point-min c-ts-common-statement-offset) + ((parent-is "record_declaration_body") point-min c-ts-common-statement-offset) ((query "(method_declaration (block _ @indent))") parent-bol java-ts-mode-indent-offset) ((query "(method_declaration (block (_) @indent))") parent-bol java-ts-mode-indent-offset) ((parent-is "local_variable_declaration") parent-bol java-ts-mode-indent-offset) @@ -118,7 +120,7 @@ java-ts-mode--indent-rules ((parent-is "case_statement") parent-bol java-ts-mode-indent-offset) ((parent-is "labeled_statement") parent-bol java-ts-mode-indent-offset) ((parent-is "do_statement") parent-bol java-ts-mode-indent-offset) - ((parent-is "block") (and parent parent-bol) java-ts-mode-indent-offset))) + ((parent-is "block") point-min c-ts-common-statement-offset))) "Tree-sitter indent rules.") (defvar java-ts-mode--keywords @@ -300,6 +302,20 @@ java-ts-mode (c-ts-common-comment-setup) ;; Indent. + (setq-local c-ts-common-indent-block-type-regexp + (rx (or "class_body" + "\\`array_initializer" + "annotation_type_body" + "interface_body" + "interface_constructor_body" + "enum_body" + "switch_block" + "record_declaration_body" + "block"))) + (setq-local c-ts-common-indent-bracketless-type-regexp + (rx (or "if_statement" "do_statement" + "for_statement" "while_statement"))) + (setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset) (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) ;; Electric -- 2.34.1 --=-=-=--