From ee05d56f364b541efb4b4c6db8bb26502ea43dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=BDelezn=C3=BD?= Date: Mon, 30 Nov 2026 08:33:49 +0100 Subject: [PATCH] Fix the indent of a method chain after throw [GH-9841] - https://github.com/apache/netbeans/issues/9841 - A throw statement is an ExpressionStatement with a ThrowExpression since PHP 8.0 support (NETBEANS-4443). visit(ExpressionStatement) added the indent of a statement to it, and visit(MethodInvocation) added the indent of the chain, so the lines of a chain after throw got the continuation indent twice - Look through the ThrowExpression when deciding whether the statement adds an indent, as is done for a method call statement - Add unit tests The change and the tests were written with the help of an AI model and were not run: the formatter tests were not built or executed, only the changed class was compiled against the jars of NetBeans 31. The expected files are written by hand. The author is not a Java developer. Assisted-by: Claude Sonnet 5.5 --- .../php/editor/indent/FormatVisitor.java | 18 ++++++++- .../formatting/issueThrowMethodChain.php | 38 +++++++++++++++++++ ...estThrowExpressionMethodChain_01.formatted | 38 +++++++++++++++++++ ...estThrowExpressionMethodChain_02.formatted | 38 +++++++++++++++++++ .../php/editor/indent/PHPFormatterTest.java | 12 ++++++ 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_01.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_02.formatted diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java index f5337645e89c..a1631670c8b6 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java @@ -109,6 +109,7 @@ import org.netbeans.modules.php.editor.parser.astnodes.StaticStatement; import org.netbeans.modules.php.editor.parser.astnodes.SwitchCase; import org.netbeans.modules.php.editor.parser.astnodes.SwitchStatement; +import org.netbeans.modules.php.editor.parser.astnodes.ThrowExpression; import org.netbeans.modules.php.editor.parser.astnodes.TraitConflictResolutionDeclaration; import org.netbeans.modules.php.editor.parser.astnodes.TraitDeclaration; import org.netbeans.modules.php.editor.parser.astnodes.TraitMethodAliasDeclaration; @@ -1295,6 +1296,20 @@ public void visit(DoStatement node) { addAllUntilOffset(node.getEndOffset()); } + /** + * Whether the expression is a method call that adds the indent of its own + * chain, so the statement must not add one more. {@code throw} is an + * expression since PHP 8.0, so look at what it throws. + */ + private static boolean isMethodCall(Expression expression) { + Expression target = expression; + if (target instanceof ThrowExpression) { + target = ((ThrowExpression) target).getExpression(); + } + return target instanceof MethodInvocation + || target instanceof StaticMethodInvocation; + } + @Override public void visit(ExpressionStatement node) { if (node.getExpression() instanceof FunctionInvocation) { @@ -1304,8 +1319,7 @@ public void visit(ExpressionStatement node) { if (moveNext() && lastIndex < ts.index()) { addFormatToken(formatTokens); // add the first token of the expression and then add the indentation Expression expression = node.getExpression(); - boolean addIndent = !(expression instanceof MethodInvocation - || expression instanceof StaticMethodInvocation + boolean addIndent = !(isMethodCall(expression) || isAnonymousClass(expression)); if (expression instanceof Assignment) { // anonymous classes diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php new file mode 100644 index 000000000000..2b9d8358fd1b --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php @@ -0,0 +1,38 @@ +message('a') + ->code(1); + + throw $factory->create() + ->message('a') + ->code(1); + +$exception = Exception::create() + ->message('a') + ->code(1); + + $factory->create() + ->message('a') + ->code(1); +} diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_01.formatted b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_01.formatted new file mode 100644 index 000000000000..1c8fede1f36e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_01.formatted @@ -0,0 +1,38 @@ +message('a') + ->code(1); + + throw $factory->create() + ->message('a') + ->code(1); + + $exception = Exception::create() + ->message('a') + ->code(1); + + $factory->create() + ->message('a') + ->code(1); +} diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_02.formatted b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_02.formatted new file mode 100644 index 000000000000..ea80495200cc --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueThrowMethodChain.php.testThrowExpressionMethodChain_02.formatted @@ -0,0 +1,38 @@ +message('a') + ->code(1); + + throw $factory->create() + ->message('a') + ->code(1); + + $exception = Exception::create() + ->message('a') + ->code(1); + + $factory->create() + ->message('a') + ->code(1); +} diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java index 92efdb574156..c420828579a6 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java @@ -895,6 +895,18 @@ public void testThrowExpression_01() throws Exception { reformatFileContents("testfiles/formatting/throwExpression01.php", options); } + public void testThrowExpressionMethodChain_01() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 4); + reformatFileContents("testfiles/formatting/issueThrowMethodChain.php", options, false, true); + } + + public void testThrowExpressionMethodChain_02() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 8); + reformatFileContents("testfiles/formatting/issueThrowMethodChain.php", options, false, true); + } + public void testMatchExpressionNewLineBracePlacement_01() throws Exception { HashMap options = new HashMap<>(FmtOptions.getDefaults()); options.put(FmtOptions.MATCH_BRACE_PLACEMENT, CodeStyle.BracePlacement.NEW_LINE);