58
58
import org .netbeans .modules .php .editor .parser .astnodes .CatchClause ;
59
59
import org .netbeans .modules .php .editor .parser .astnodes .ClassDeclaration ;
60
60
import org .netbeans .modules .php .editor .parser .astnodes .ClassInstanceCreation ;
61
+ import org .netbeans .modules .php .editor .parser .astnodes .ClassInstanceCreationVariable ;
61
62
import org .netbeans .modules .php .editor .parser .astnodes .ConditionalExpression ;
62
63
import org .netbeans .modules .php .editor .parser .astnodes .ConstantDeclaration ;
63
64
import org .netbeans .modules .php .editor .parser .astnodes .DeclareStatement ;
65
+ import org .netbeans .modules .php .editor .parser .astnodes .DereferencableVariable ;
66
+ import org .netbeans .modules .php .editor .parser .astnodes .DereferencedArrayAccess ;
67
+ import org .netbeans .modules .php .editor .parser .astnodes .Dispatch ;
64
68
import org .netbeans .modules .php .editor .parser .astnodes .DoStatement ;
65
69
import org .netbeans .modules .php .editor .parser .astnodes .EnumDeclaration ;
66
70
import org .netbeans .modules .php .editor .parser .astnodes .Expression ;
89
93
import org .netbeans .modules .php .editor .parser .astnodes .NamedArgument ;
90
94
import org .netbeans .modules .php .editor .parser .astnodes .NamespaceDeclaration ;
91
95
import org .netbeans .modules .php .editor .parser .astnodes .NullableType ;
96
+ import org .netbeans .modules .php .editor .parser .astnodes .ParenthesisExpression ;
92
97
import org .netbeans .modules .php .editor .parser .astnodes .Program ;
93
98
import org .netbeans .modules .php .editor .parser .astnodes .ReflectionVariable ;
94
99
import org .netbeans .modules .php .editor .parser .astnodes .ReturnStatement ;
95
100
import org .netbeans .modules .php .editor .parser .astnodes .SingleFieldDeclaration ;
96
101
import org .netbeans .modules .php .editor .parser .astnodes .SingleUseStatementPart ;
97
102
import org .netbeans .modules .php .editor .parser .astnodes .Statement ;
103
+ import org .netbeans .modules .php .editor .parser .astnodes .StaticDispatch ;
98
104
import org .netbeans .modules .php .editor .parser .astnodes .StaticFieldAccess ;
99
105
import org .netbeans .modules .php .editor .parser .astnodes .StaticMethodInvocation ;
100
106
import org .netbeans .modules .php .editor .parser .astnodes .StaticStatement ;
@@ -299,7 +305,7 @@ public void visit(StaticStatement node) {
299
305
@ Override
300
306
public void visit (AnonymousObjectVariable node ) {
301
307
// (new Test)->method();
302
- super . visit (node );
308
+ scan (node . getName () );
303
309
// avoid adding incorrect space for "within a method call"
304
310
// e.g. (new Test)->method(); -> (new Test )->method();
305
311
addAllUntilOffset (node .getEndOffset ());
@@ -876,7 +882,7 @@ public void visit(ClassInstanceCreation node) {
876
882
ts .movePrevious ();
877
883
scan (node .getBody ());
878
884
} else {
879
- if (node .ctorParams () != null && node .ctorParams ().size () > 0 ) {
885
+ if (node .ctorParams () != null && ! node .ctorParams ().isEmpty () ) {
880
886
boolean addIndentation = !(path .get (1 ) instanceof ReturnStatement
881
887
|| path .get (1 ) instanceof Assignment
882
888
|| path .get (1 ) instanceof ExpressionStatement )
@@ -890,7 +896,10 @@ public void visit(ClassInstanceCreation node) {
890
896
}
891
897
addAllUntilOffset (node .getEndOffset ());
892
898
} else {
893
- super .visit (node );
899
+ // e.g. new Example(); new $className();
900
+ // ctorParams() is empty, so, add tokens until the end offset
901
+ // to add WHITESPACE_WITHIN_METHOD_CALL_PARENS
902
+ addAllUntilOffset (node .getEndOffset ());
894
903
}
895
904
}
896
905
}
@@ -1259,7 +1268,9 @@ public void visit(ExpressionStatement node) {
1259
1268
if (moveNext () && lastIndex < ts .index ()) {
1260
1269
addFormatToken (formatTokens ); // add the first token of the expression and then add the indentation
1261
1270
Expression expression = node .getExpression ();
1262
- boolean addIndent = !(expression instanceof MethodInvocation || expression instanceof StaticMethodInvocation );
1271
+ boolean addIndent = !(expression instanceof MethodInvocation
1272
+ || expression instanceof StaticMethodInvocation
1273
+ || isAnonymousClass (expression ));
1263
1274
if (expression instanceof Assignment ) {
1264
1275
// anonymous classes
1265
1276
Assignment assignment = (Assignment ) expression ;
@@ -2681,6 +2692,18 @@ public void visit(ReflectionVariable node) {
2681
2692
ts .movePrevious ();
2682
2693
}
2683
2694
2695
+ @ Override
2696
+ public void visit (DereferencableVariable node ) {
2697
+ scan (node .getExpression ());
2698
+ addAllUntilOffset (node .getEndOffset ());
2699
+ }
2700
+
2701
+ @ Override
2702
+ public void visit (ParenthesisExpression node ) {
2703
+ scan (node .getExpression ());
2704
+ addAllUntilOffset (node .getEndOffset ());
2705
+ }
2706
+
2684
2707
private void processUnionOrIntersectionType (List <Expression > types ) {
2685
2708
assert !types .isEmpty ();
2686
2709
final Expression lastType = types .get (types .size () - 1 );
@@ -2896,6 +2919,12 @@ private void addFormatToken(List<FormatToken> tokens) {
2896
2919
} else if (parent instanceof UnionType ) {
2897
2920
tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2898
2921
tokens .add (new FormatToken (FormatToken .Kind .WHITESPACE_WITHIN_DNF_TYPE_PARENS , ts .offset () + ts .token ().length ()));
2922
+ } else if (isParenthesisExpression (parent )
2923
+ || parent instanceof DereferencableVariable
2924
+ || isAnonymousObjectVariable (parent )
2925
+ ) {
2926
+ tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2927
+ tokens .add (new FormatToken (FormatToken .Kind .WHITESPACE_WITHIN_OTHER_PARENS , ts .offset () + ts .token ().length ()));
2899
2928
} else {
2900
2929
tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2901
2930
}
@@ -2937,6 +2966,11 @@ private void addFormatToken(List<FormatToken> tokens) {
2937
2966
} else if (parent instanceof UnionType ) {
2938
2967
tokens .add (new FormatToken (FormatToken .Kind .WHITESPACE_WITHIN_DNF_TYPE_PARENS , ts .offset ()));
2939
2968
tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2969
+ } else if (parent instanceof ParenthesisExpression
2970
+ || parent instanceof DereferencableVariable
2971
+ || parent instanceof AnonymousObjectVariable ) {
2972
+ tokens .add (new FormatToken (FormatToken .Kind .WHITESPACE_WITHIN_OTHER_PARENS , ts .offset ()));
2973
+ tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2940
2974
} else {
2941
2975
tokens .add (new FormatToken (FormatToken .Kind .TEXT , ts .offset (), ts .token ().text ().toString ()));
2942
2976
}
@@ -3652,11 +3686,50 @@ private static boolean isCloseParen(Token<? extends PHPTokenId> token) {
3652
3686
return TokenUtilities .textEquals (")" , token .text ()); // NOI18N
3653
3687
}
3654
3688
3689
+ private static boolean isParenthesisExpression (ASTNode astNode ) {
3690
+ ASTNode node = astNode ;
3691
+ if (node instanceof ExpressionStatement ) {
3692
+ // ($example == 1);
3693
+ node = ((ExpressionStatement ) node ).getExpression ();
3694
+ }
3695
+ return node instanceof ParenthesisExpression ;
3696
+ }
3697
+
3698
+ private static boolean isAnonymousObjectVariable (ASTNode astNode ) {
3699
+ ASTNode node = astNode ;
3700
+ if (node instanceof ExpressionStatement ) {
3701
+ // (new $class());
3702
+ node = ((ExpressionStatement ) node ).getExpression ();
3703
+ if (node instanceof DereferencedArrayAccess ) {
3704
+ // (new $class())['key'];
3705
+ node = ((DereferencedArrayAccess ) node ).getMember ();
3706
+ }
3707
+ }
3708
+ return node instanceof AnonymousObjectVariable ;
3709
+ }
3710
+
3655
3711
private static boolean isAnonymousClass (ASTNode astNode ) {
3656
3712
ASTNode node = astNode ;
3657
3713
if (astNode instanceof NamedArgument ) {
3658
3714
node = ((NamedArgument ) astNode ).getExpression ();
3659
3715
}
3716
+
3717
+ // new class(){}['key'], new class(){}->method()
3718
+ while (node instanceof Dispatch
3719
+ || node instanceof StaticDispatch
3720
+ || node instanceof ClassInstanceCreationVariable
3721
+ || node instanceof FunctionInvocation ) {
3722
+ if (node instanceof Dispatch ) {
3723
+ node = ((Dispatch ) node ).getDispatcher ();
3724
+ } else if (node instanceof StaticDispatch ) {
3725
+ node = ((StaticDispatch ) node ).getDispatcher ();
3726
+ } else if (node instanceof ClassInstanceCreationVariable ) {
3727
+ node = ((ClassInstanceCreationVariable ) node ).getName ();
3728
+ } else if (node instanceof FunctionInvocation ) {
3729
+ // $c = new class{}();
3730
+ node = ((FunctionInvocation ) node ).getFunctionName ().getName ();
3731
+ }
3732
+ }
3660
3733
return node instanceof ClassInstanceCreation && ((ClassInstanceCreation ) node ).isAnonymous ();
3661
3734
}
3662
3735
0 commit comments