unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: "赵 纪阳" <zjyzhaojiyang@hotmail.com>
Cc: "61142@debbugs.gnu.org" <61142@debbugs.gnu.org>
Subject: bug#61142: 回复: bug#61142: 29.0.60; java-ts-mode - Wrong indentation for the body of multiple lines conditions
Date: Sun, 05 Feb 2023 20:19:19 +0100	[thread overview]
Message-ID: <87h6vz7weg.fsf@thornhill.no> (raw)
In-Reply-To: <OSZP286MB141720BF3D08F8E5195C4FAFA8D59@OSZP286MB1417.JPNP286.PROD.OUTLOOK.COM>

[-- Attachment #1: Type: text/plain, Size: 1180 bytes --]

赵 纪阳 <zjyzhaojiyang@hotmail.com> writes:

> Thanks for the patch.
>

Thanks for testing!

> I've not tested this patch adequately, but I found an issue:
> java-ts-mode indents the body of `else if` with one more level of indentation.
>
> ```
> public class T {
>     int f() {
>         int a = 0;
>         int b = 1;
>         if (a == 0
>             && b == 1) {
>             // indentation ok
>             return 0;
>         } else if (a == 1 ) {
>                 // indentation more
>                 return 1;
>             }
>     }
> }
> ```
> If the second branch of the if-statement is just an `else` (not `else if`), the body of it will be indented correctly.
>
>
> I've also tested c-ts-mode (use 'linux as c-ts-mode-indent-style) and it also has this kind of problem.
> ```
> int main() {
>     int a = 0;
>     if (a == 0) {
>         // indentation ok
>     } else if (a == 2) {
>             // indentation more
>         }
> }
> ```
>

Right, I remember this.  This finally made me create a fix - can you
test the provided patch and check if this works for you?  It works
nicely for me, at least.

Theo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-c-ts-common-statement-offset-in-java-ts-mode.patch --]
[-- Type: text/x-patch, Size: 6547 bytes --]

From 95c381d40cf6907e333acb107160d901740da79d Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
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 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  | 26 +++++++++++++++++++++++-
 lisp/progmodes/java-ts-mode.el | 36 +++++++++++++++++++++++++---------
 2 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el
index 8729cae4ba..2edb979be5 100644
--- a/lisp/progmodes/c-ts-common.el
+++ b/lisp/progmodes/c-ts-common.el
@@ -281,11 +281,32 @@ 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--fix-nestable-if-statement (level node)
+  (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))
+
 (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 +333,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)
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index b9f7894095..b3ecbd5987 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,22 @@ java-ts-mode
   (c-ts-common-comment-setup)
 
   ;; Indent.
+  (setq-local c-ts-common-indent-block-type-regexp
+              (regexp-opt '("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
+              (regexp-opt '("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


  reply	other threads:[~2023-02-05 19:19 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <handler.61142.B.16749811447164.ack@debbugs.gnu.org>
2023-02-01  8:46 ` bug#61142: Acknowledgement (29.0.60; java-ts-mode - Wrong indentation for the body of multiple lines conditions) 太阳
2023-02-04 10:45   ` bug#61142: 29.0.60; java-ts-mode - Wrong indentation for the body of multiple lines conditions Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05  7:53     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05  7:54       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05  9:20       ` Eli Zaretskii
2023-02-05  9:28         ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05 10:15           ` Eli Zaretskii
2023-02-05 10:25             ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05 10:59               ` Eli Zaretskii
2023-02-05 13:09       ` bug#61142: 回复: " 赵 纪阳
2023-02-05 19:19         ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-02-05 19:30           ` Eli Zaretskii
2023-02-05 19:32             ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05 20:12               ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-05 21:20                 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-06  2:34                   ` bug#61142: 回复: " 赵 纪阳
2023-02-06  6:45                     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h6vz7weg.fsf@thornhill.no \
    --to=bug-gnu-emacs@gnu.org \
    --cc=61142@debbugs.gnu.org \
    --cc=theo@thornhill.no \
    --cc=zjyzhaojiyang@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).