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);