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: =?UTF-8?Q?=E5=9B=9E=E5=A4=8D:?= bug#61142: 29.0.60; java-ts-mode - Wrong indentation for the body of multiple lines conditions Date: Sun, 05 Feb 2023 21:12:23 +0100 Message-ID: <87bkm77ty0.fsf@thornhill.no> References: <87wn4x7lpw.fsf_-_@thornhill.no> <87r0v47dky.fsf@thornhill.no> <87h6vz7weg.fsf@thornhill.no> <83ilgfrju2.fsf@gnu.org> <87edr37vrw.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="12458"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61142@debbugs.gnu.org, zjyzhaojiyang@hotmail.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 05 21:13:28 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 1pOlOB-00034T-S1 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 05 Feb 2023 21:13:28 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOlNp-0001eE-3A; Sun, 05 Feb 2023 15:13:05 -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 1pOlNm-0001dv-El for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 15:13: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 1pOlNl-0000By-TN for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 15:13:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pOlNl-000703-LF for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 15:13: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 20:13: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.167562794826865 (code B ref 61142); Sun, 05 Feb 2023 20:13:01 +0000 Original-Received: (at 61142) by debbugs.gnu.org; 5 Feb 2023 20:12:28 +0000 Original-Received: from localhost ([127.0.0.1]:46527 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOlND-0006zF-HM for submit@debbugs.gnu.org; Sun, 05 Feb 2023 15:12:28 -0500 Original-Received: from out-184.mta1.migadu.com ([95.215.58.184]:18758) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOlNB-0006z5-Co for 61142@debbugs.gnu.org; Sun, 05 Feb 2023 15:12:26 -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=1675627944; 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=Gz8TArh8ko02IgYMxXgtnkffFyYT/3wBpG/9wXbEJ7c=; b=CnR9jkwZoAPQGkzYVAq5p1BUjSznUAWOqxHlqrn9muYwCekvGBr8czvJ2lC6sJxue3BwtU wOwpO8VBRamXMaG+WPBKEmsxAX0J6SIxrIXFt49qZYx1vbtN4bB6R+FzmYGXWMYXPUQ1Ue NLCHE0cmXagwfYKFfjmYgTOJXz2fhKz+Smg29ngQ3+mPLp4xEcCUNAa3U7TCvo523q5s0N WUwImuJnV1dD7ZR3XH9DUc2CkPfWqTskkGaVdmEYxxjlXQJ//r8Gy8901Qw6kFxyZ01PbM oSK38KEPNWkYrtYdNp8Da8r2ozDbXAyqPhzDTh95TGRXBfmONO2FvU9plmaDHw== In-Reply-To: <87edr37vrw.fsf@thornhill.no> 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:254890 Archived-At: --=-=-= Content-Type: text/plain Theodor Thornhill writes: > Eli Zaretskii writes: > >>> Cc: "61142@debbugs.gnu.org" <61142@debbugs.gnu.org> >>> Date: Sun, 05 Feb 2023 20:19:19 +0100 >>> From: Theodor Thornhill via "Bug reports for GNU Emacs, >>> the Swiss army knife of text editors" >>> >>> +(defvar c-ts-common-nestable-if-statement-p t >>> + "Does the current parser nest if-else statements? >>> + >>> +T if the current tree-sitter grammar nests the else if >>> +statements, NIL otherwise.") >> >> A nit: we use t and nil, lowercase, to refer to these two symbols. >> >> Thanks. > > Thanks! > > Theo I added some small tweaks, and will add the following as a test later: ``` public class Java { public Java( String foo) { this.foo = foo; } void foo( String foo) { for (var f : rs) return new String[]{ "foo", "bar" }; if (a == 0 && b == 1 && foo) { return 0; } else if (a == 1) { return 1; } else if (true) return 5; else { if (a == 0 && b == 1 && foo) while (true) for ( ;;) if (true) return 5; else if (false) { return 6; } else if (true && false) return 6; } } } ``` Even though its some pretty wild code, its indented correctly after this patch. I'll create something similar for C as well. Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Use-c-ts-common-statement-offset-in-java-ts-mode-bug.patch >From e2e931843936134f157ee6259167b6aae429da3a 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 (bug#61142) * 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 in documentation and use the new if statement helpers. (c-ts-common-if-statement-regexp): New defvar. (c-ts-common-nestable-if-statement-p): New defvar. (c-ts-common--fix-nestable-if-statement): New helper. --- lisp/progmodes/c-ts-common.el | 33 ++++++++++++++++++++++++++++++- lisp/progmodes/java-ts-mode.el | 36 +++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 8729cae4ba..affb38139a 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -281,11 +281,22 @@ c-ts-common-indent-bracketless-type-regexp This can be nil, meaning such special handling is not needed.") +(defvar c-ts-common-if-statement-regexp "if_statement" + "Regexp used to select an if statement in a C like language. + +This can be set to a different regexp if needed.") + +(defvar c-ts-common-nestable-if-statement-p t + "Does the current parser nest if-else statements? + +t if the current tree-sitter grammar nests the else if +statements, nil otherwise.") + (defun c-ts-common-statement-offset (node parent bol &rest _) "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'. @@ -312,6 +323,9 @@ c-ts-common-statement-offset (while (if (eq node t) (setq node parent) node) + ;; Subtract one indent level if the language nests + ;; if-statements and node is if_statement. + (setq level (c-ts-common--fix-nestable-if-statement level node)) (when (string-match-p c-ts-common-indent-block-type-regexp (treesit-node-type node)) (cl-incf level) @@ -354,6 +368,23 @@ c-ts-mode--fix-bracketless-indent (1+ level) level))) +(defun c-ts-common--fix-nestable-if-statement (level node) + "Takes LEVEL and NODE and return adjusted LEVEL. +Look at the type of NODE, when it is an if-statement node, as +defined by `c-ts-common-if-statement-regexp' and its parent is +also an if-statement node, subtract one level. Otherwise return +the value unchanged. Whether or not if-statements are nestable +is controlled by `c-ts-common-nestable-if-statement-p'." + ;; This fixes indentation for cases shown in bug#61142. + (or (and node + (treesit-node-parent node) + c-ts-common-nestable-if-statement-p + (equal (treesit-node-type node) c-ts-common-if-statement-regexp) + (equal (treesit-node-type (treesit-node-parent node)) + c-ts-common-if-statement-regexp) + (cl-decf level)) + level)) + (provide 'c-ts-common) ;;; c-ts-common.el ends here diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index b9f7894095..1d7bdb0722 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -70,22 +70,25 @@ 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 "else") 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 +121,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 +303,21 @@ java-ts-mode (c-ts-common-comment-setup) ;; Indent. + (setq-local c-ts-common-indent-block-type-regexp + (regexp-opt '("class_body" + "array_initializer" + "constructor_body" + "annotation_type_body" + "interface_body" + "enum_body" + "switch_block" + "record_declaration_body" + "block"))) + (setq-local c-ts-common-indent-bracketless-type-regexp + (regexp-opt '("if_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 --=-=-=--