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..d8ca4f0220de 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 @@ -2310,7 +2324,9 @@ public void visit(ReturnStatement node) { addFormatToken(formatTokens); } - boolean addIndent = !isAnonymousClass(node.getExpression()) + boolean addIndent = + !isAnonymousClass(node.getExpression()) + && !isMethodCall(node.getExpression()) && !(path.size() > 3 && path.get(2) instanceof LambdaFunctionDeclaration && !(path.get(3) instanceof FunctionInvocation)) // #259111 GH-7140 && !(node.getExpression() instanceof LambdaFunctionDeclaration) // NETBEANS-4970 && !(node.getExpression() instanceof MatchExpression); diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php new file mode 100644 index 000000000000..2ddb333158a5 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php @@ -0,0 +1,59 @@ +message('a') + ->code(1); + return $exception; +} + +function returnMethodChainRef2($factory): Exception { + $exception = $factory->create() + ->message('a') + ->code(1); + return $exception; +} + +function returnMethodChain1($factory): Exception { + return Exception::create() + ->message('a') + ->code(1); +} + +function returnMethodChain2($factory): Exception { + return Exception::create()->message('a') + ->code(1); +} + +function returnMethodChain3($factory): Exception { + return $factory->create() + ->message('a') + ->code(1); +} + +function returnMethodChain4($factory): Exception { + return $factory + ->create() + ->message('a') + ->code(1); +} + diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_01.formatted b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_01.formatted new file mode 100644 index 000000000000..b14bef9da2c5 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_01.formatted @@ -0,0 +1,58 @@ +message('a') + ->code(1); + return $exception; +} + +function returnMethodChainRef2($factory): Exception { + $exception = $factory->create() + ->message('a') + ->code(1); + return $exception; +} + +function returnMethodChain1($factory): Exception { + return Exception::create() + ->message('a') + ->code(1); +} + +function returnMethodChain2($factory): Exception { + return Exception::create()->message('a') + ->code(1); +} + +function returnMethodChain3($factory): Exception { + return $factory->create() + ->message('a') + ->code(1); +} + +function returnMethodChain4($factory): Exception { + return $factory + ->create() + ->message('a') + ->code(1); +} diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_02.formatted b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_02.formatted new file mode 100644 index 000000000000..d80b3a046c1b --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueReturnMethodChain.php.testReturnStatementMethodChain_02.formatted @@ -0,0 +1,58 @@ +message('a') + ->code(1); + return $exception; +} + +function returnMethodChainRef2($factory): Exception { + $exception = $factory->create() + ->message('a') + ->code(1); + return $exception; +} + +function returnMethodChain1($factory): Exception { + return Exception::create() + ->message('a') + ->code(1); +} + +function returnMethodChain2($factory): Exception { + return Exception::create()->message('a') + ->code(1); +} + +function returnMethodChain3($factory): Exception { + return $factory->create() + ->message('a') + ->code(1); +} + +function returnMethodChain4($factory): Exception { + return $factory + ->create() + ->message('a') + ->code(1); +} 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..a5a0c144e81b 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); @@ -1392,4 +1404,16 @@ public void testPipeOperatorMultilineExpression_01() throws Exception { HashMap options = new HashMap<>(FmtOptions.getDefaults()); reformatFileContents("testfiles/formatting/php85/pipeOperatorMultilineExpression_01.php", options); } + + public void testReturnStatementMethodChain_01() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 4); + reformatFileContents("testfiles/formatting/issueReturnMethodChain.php", options, false, true); + } + + public void testReturnStatementMethodChain_02() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 8); + reformatFileContents("testfiles/formatting/issueReturnMethodChain.php", options, false, true); + } }