From: Yuan Fu Date: Fri, 14 Feb 2025 02:24:41 +0000 (-0800) Subject: Use c-ts-common baseline rule in java-ts-mode (bug#75154) X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=7cefb6a78c1fb9bc40a6bfa7f6b10eb1f89aae4a;p=emacs.git Use c-ts-common baseline rule in java-ts-mode (bug#75154) Use it for function parameters. * lisp/progmodes/java-ts-mode.el: (java-ts-mode--standalone-predicate): New function. (java-ts-mode--indent-rules): Comment out rules for function parameters and statements, and add c-ts-common-baseline-indent-rule as fallback. (java-ts-mode): Setup. (java-ts-mode--first-line-on-multi-line-string): Mark BOL as unused. * test/lisp/progmodes/java-ts-mode-resources/indent.erts: New test. (cherry picked from commit 93cd55f40e5e1788e55d82c572f1fafac1dee499) --- diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index d92850bf629..28f8ef3c8f2 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -99,7 +99,18 @@ again." table) "Syntax table for `java-ts-mode'.") -(defun java-ts-mode--first-line-on-multi-line-string (_node parent bol &rest _) +(defun java-ts-mode--standalone-predicate (node) + "Java's standalone predicate. +Return t if NODE is on the start of a line." + (save-excursion + (goto-char (treesit-node-start node)) + (if (looking-back (rx bol (* whitespace) (? ".")) (pos-bol)) + t + (back-to-indentation) + (when (eq (char-after) ?.) + (point))))) + +(defun java-ts-mode--first-line-on-multi-line-string (_node parent _bol &rest _) "Simple-indent matcher for the first line in a multi-line string block. PARENT and BOL are the as in other matchers." (and (treesit-node-match-p parent "multiline_string_fragment") @@ -162,8 +173,8 @@ PARENT and BOL are the as in other matchers." ((parent-is "argument_list") parent-bol java-ts-mode-indent-offset) ((parent-is "annotation_argument_list") parent-bol java-ts-mode-indent-offset) ((parent-is "modifiers") parent-bol 0) - ((parent-is "formal_parameters") parent-bol java-ts-mode-indent-offset) - ((parent-is "formal_parameter") parent-bol 0) + ;; ((parent-is "formal_parameters") parent-bol java-ts-mode-indent-offset) + ;; ((parent-is "formal_parameter") parent-bol 0) ((parent-is "init_declarator") parent-bol java-ts-mode-indent-offset) ((parent-is "if_statement") parent-bol java-ts-mode-indent-offset) ((parent-is "for_statement") parent-bol java-ts-mode-indent-offset) @@ -172,7 +183,8 @@ PARENT and BOL are the as in other matchers." ((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") standalone-parent java-ts-mode-indent-offset))) + ;; ((parent-is "block") standalone-parent java-ts-mode-indent-offset) + c-ts-common-baseline-indent-rule)) "Tree-sitter indent rules.") (defvar java-ts-mode--keywords @@ -409,6 +421,9 @@ Return nil if there is no name or if NODE is not a defun node." (do . "do_statement"))) (setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset) (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) + (setq-local treesit-simple-indent-standalone-predicate + #'java-ts-mode--standalone-predicate) + (setq-local c-ts-common-list-indent-style 'simple) ;; Electric (setq-local electric-indent-chars diff --git a/test/lisp/progmodes/java-ts-mode-resources/indent.erts b/test/lisp/progmodes/java-ts-mode-resources/indent.erts index 514d2e08977..7cf59340454 100644 --- a/test/lisp/progmodes/java-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/java-ts-mode-resources/indent.erts @@ -141,3 +141,31 @@ public class Java { } } =-=-= + +Name: Method chaining + +=-= +public class FloodFill { +public static void main(String[] args) { +List stream = students.stream(MAX_VALUE) +.filter(item -> { +return item.getValue() > 100 && +item.isActive(); +}) +.map() +.collect(); +} +} +=-= +public class FloodFill { + public static void main(String[] args) { + List stream = students.stream(MAX_VALUE) + .filter(item -> { + return item.getValue() > 100 && + item.isActive(); + }) + .map() + .collect(); + } +} +=-=-=