zeleznypa opened a new issue, #9841:
URL: https://github.com/apache/netbeans/issues/9841
### Apache NetBeans version
Apache NetBeans 31 (macOS). The code I analysed is `master` at df6bd37ef3.
This is **not a recent regression**: the cause has been in the source since
July 2020 (see History).
### Steps to reproduce
Formatting > PHP > Tabs and Indents: Number of Spaces per Indent 4,
Continuation Indentation 4. Format this with Source > Format:
```php
<?php
function a($factory): void
{
$e = Exception::create()
->message('a');
$factory->create()
->message('a');
throw Exception::create()
->message('a');
}
```
### Expected
All three chains are indented once (4 spaces) relative to the line the
statement starts on. That is also what PER Coding Style 4.7 asks for.
### Actual
The chain after `throw` is indented twice, 8 spaces. The assignment and the
plain call get 4 spaces.
In a PHP code base maintained with NetBeans (Continuation Indentation 4), 32
of 36 multi-line chains after `throw` have 8 spaces. 16 of 19 chains after an
assignment have 4 (the 3 others are in one package). I did not run NetBeans on
the sample above; the sample follows from the source and from that code base.
### Cause
Two indents are added independently, and each one has an exception for the
shape that the other one handles:
1.
[`visit(ExpressionStatement)`](https://github.com/apache/netbeans/blob/df6bd37ef3246b80346341cfa0c2866f79f996bc/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java#L1299)
adds one `continualIndentSize` to every expression statement, except when the
expression is a `MethodInvocation`, a `StaticMethodInvocation` or an anonymous
class
([L1307-L1309](https://github.com/apache/netbeans/blob/df6bd37ef3246b80346341cfa0c2866f79f996bc/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java#L1307-L1309)).
2.
[`visit(MethodInvocation)`](https://github.com/apache/netbeans/blob/df6bd37ef3246b80346341cfa0c2866f79f996bc/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java#L2200)
adds a second one when the chain starts on a new line, except when the parent
is an `Assignment`
([`addIndentToMethodInvocation()`](https://github.com/apache/netbeans/blob/df6bd37ef3246b80346341cfa0c2866f79f996bc/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java#L2245-L2268),
the check is
[L2267](https://github.com/apache/netbeans/blob/df6bd37ef3246b80346341cfa0c2866f79f996bc/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java#L2267)).
- An assignment (`$e = A::b()->c()`) gets only the first indent: 4.
- A plain call statement (`$m->a()->b()`) gets only the second one: 4.
- `throw A::b()->c()` gets both: 4 + 4 = 8, because the expression of the
statement is a `ThrowExpression`, which is neither of the excluded types.
### History
- `ThrowStatement` was a `Statement`, so it never went through
`visit(ExpressionStatement)`. Chains after `throw` got 4 spaces.
- [6e0c6cabea](https://github.com/apache/netbeans/commit/6e0c6cabea)
(2020-07-26, NETBEANS-4443 "PHP 8.0 Support: throw Expression part") replaced
`ThrowStatement` with `ThrowExpression` (the grammar now makes `throw expr` an
expression, so `throw ...;` is an `ExpressionStatement`).
`visit(ExpressionStatement)` was not changed, and since then the chain after
`throw` gets 8. The commit added `throwExpression01.php` to the formatter
tests, but no multi-line chain.
- The exceptions in both visitors are older. `visit(ExpressionStatement)`
has had its `MethodInvocation || StaticMethodInvocation` exclusion since the
second donation (2018-06-26). The `Assignment` exclusion in
`visit(MethodInvocation)` was only moved into `addIndentToMethodInvocation()`
by [c7f9f21bd7](https://github.com/apache/netbeans/commit/c7f9f21bd7) (GH-7172).
So the behaviour has existed for more than five years, and every PHP 8
project that formats a chain after `throw` with NetBeans is affected.
### Suggested fix
Treat `throw <call chain>` like the call statement it contains: look through
the `ThrowExpression` when deciding whether the statement adds its own indent.
```diff
---
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.StaticMethodInvocation;
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;
@@ -1295,6 +1296,20 @@ public class FormatVisitor extends DefaultVisitor {
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) {
@@ -1304,8 +1319,7 @@ public class FormatVisitor extends DefaultVisitor {
Expression expression = node.getExpression();
- boolean addIndent = !(expression instanceof MethodInvocation
- || expression instanceof StaticMethodInvocation
+ boolean addIndent = !(isMethodCall(expression)
|| isAnonymousClass(expression));
```
Tests I would add to `PHPFormatterTest` (next to `testThrowExpression_01`),
with the data file `testfiles/formatting/issueThrowMethodChain.php` (the usual
licence header, then the four unformatted statements of the sample above, one
of them after `throw $factory->create()`) and one expected file per test:
```java
public void testThrowExpressionMethodChain_01() throws Exception {
HashMap<String, Object> 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<String, Object> options = new
HashMap<>(FmtOptions.getDefaults());
options.put(FmtOptions.CONTINUATION_INDENT_SIZE, 8);
reformatFileContents("testfiles/formatting/issueThrowMethodChain.php", options,
false, true);
}
```
Expected output of `_01` (`_02` is the same with the chain lines indented by
8 instead of 4 relative to the statement):
```php
function throwMethodChain($factory): void {
throw Exception::create()
->message('a')
->code(1);
throw $factory->create()
->message('a')
->code(1);
$exception = Exception::create()
->message('a')
->code(1);
$factory->create()
->message('a')
->code(1);
}
```
I can open a pull request with the change and the test files if that is the
preferred way.
### What I did and did not verify
- Verified: `FormatVisitor.java` with the change compiles against the jars
of Apache NetBeans 31.
- Verified: the formatter test data of `master` has no multi-line `throw`
statement, so I expect no existing expected file to change. I only searched for
lines that start with `throw`.
- **Not verified:** I did not run the formatter tests or NetBeans with the
patch. The expected files are written by hand from the rules above, so they may
need a correction after a real run.
- Not verified, not touched by the patch: `visit(ReturnStatement)` adds a
continuation indent in the same way, so `return $x->a()->b()` over lines
probably has the same shape. I did not test it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists