Skip to content

Commit

Permalink
[NETBEANS-5028] PHP - improved display of constant in code completion
Browse files Browse the repository at this point in the history
https://issues.apache.org/jira/browse/NETBEANS-5028

Improves display of array constant in code completion.
Uses short array format, displays first item in array, indicates more elements in array.
  • Loading branch information
KacerCZ committed Nov 15, 2020
1 parent c9d7ae8 commit c36a4fa
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.netbeans.modules.php.editor.model.nodes;

import java.util.ArrayList;
import java.util.List;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.modules.php.editor.model.nodes.ASTNodeInfo.Kind;
import org.netbeans.modules.php.editor.parser.astnodes.ArrayCreation;
import org.netbeans.modules.php.editor.parser.astnodes.ArrayElement;
import org.netbeans.modules.php.editor.parser.astnodes.ConstantDeclaration;
import org.netbeans.modules.php.editor.parser.astnodes.Expression;
import org.netbeans.modules.php.editor.parser.astnodes.Identifier;
Expand All @@ -32,6 +34,7 @@
* @author Radek Matous
*/
public class ConstantDeclarationInfo extends ClassConstantDeclarationInfo {

ConstantDeclarationInfo(final Identifier node, final String value, final ConstantDeclaration constantDeclaration) {
super(node, value, constantDeclaration);
}
Expand All @@ -42,6 +45,11 @@ public static List<? extends ConstantDeclarationInfo> create(ConstantDeclaration
for (Identifier identifier : names) {
String value = null;
for (final Expression expression : constantDeclaration.getInitializers()) {
value = getConstantValue(expression);
if (value != null) {
break;
}
/*
if (expression instanceof Scalar) {
value = ((Scalar) expression).getStringValue();
break;
Expand All @@ -54,6 +62,7 @@ public static List<? extends ConstantDeclarationInfo> create(ConstantDeclaration
break;
}
}
*/
}
retval.add(new ConstantDeclarationInfo(identifier, value, constantDeclaration));
}
Expand All @@ -64,4 +73,53 @@ public static List<? extends ConstantDeclarationInfo> create(ConstantDeclaration
public Kind getKind() {
return Kind.CONSTANT;
}

@CheckForNull
private static String getConstantValue(Expression expr) {
if (expr instanceof Scalar) {
return ((Scalar) expr).getStringValue();
}
if (expr instanceof UnaryOperation) {
UnaryOperation up = (UnaryOperation) expr;
if (up.getOperator() == UnaryOperation.Operator.MINUS
&& up.getExpression() instanceof Scalar) {
return "-" + ((Scalar) up.getExpression()).getStringValue();
}
}
if (expr instanceof ArrayCreation) {
return getConstantValue((ArrayCreation) expr);
}
return null;
}

private static String getConstantValue(ArrayCreation expr) {
StringBuilder sb = new StringBuilder("["); //NOI18N
boolean itemAdded = false;
List<ArrayElement> elements = expr.getElements();
if (elements.size() > 0) {
ArrayElement firstElement = elements.get(0);
Expression key = firstElement.getKey();
if (key != null) {
String convertedKey = getConstantValue(key);
if (convertedKey != null) {
sb.append(convertedKey);
sb.append(" => "); //NOI18N
}
}
String convertedValue = getConstantValue(firstElement.getValue());
if (convertedValue != null) {
sb.append(convertedValue);
itemAdded = true;
} else {
// Case when element exist but value was not converted to output.
sb.append("..."); //NOI18N
}
}
if (itemAdded && elements.size() > 1) {
sb.append(",..."); //NOI18N
}
sb.append("]"); //NOI18N
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ VARIABLE array $array1 [PUBLIC] spreadOperatorInArrayExpre
VARIABLE array $array2 [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array2_a [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array3 [PUBLIC] spreadOperatorInArrayExpression.php
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
VARIABLE $GLOBALS PHP Platform
VARIABLE $HTTP_RAW_POST_DATA PHP Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ VARIABLE array $array2 [PUBLIC] spreadOperatorInArrayExpre
VARIABLE array $array2_a [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array3 [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array3_a [PUBLIC] spreadOperatorInArrayExpression.php
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
VARIABLE $GLOBALS PHP Platform
VARIABLE $HTTP_RAW_POST_DATA PHP Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ VARIABLE array $array2_a [PUBLIC] spreadOperatorInArrayExpre
VARIABLE array $array3 [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array3_a [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array4 [PUBLIC] spreadOperatorInArrayExpression.php
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
VARIABLE $GLOBALS PHP Platform
VARIABLE $HTTP_RAW_POST_DATA PHP Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ VARIABLE array $array2_a [PUBLIC] spreadOperatorInArrayExpre
VARIABLE array $array3 [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array3_a [PUBLIC] spreadOperatorInArrayExpression.php
VARIABLE array $array4 [PUBLIC] spreadOperatorInArrayExpression.php
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
VARIABLE $GLOBALS PHP Platform
VARIABLE $HTTP_RAW_POST_DATA PHP Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const CONSTANT1 = [...|CONSTANT];
PACKAGE Bar [PUBLIC] null
PACKAGE Foo [PUBLIC] null
CLASS Qux [PUBLIC] Bar
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
KEYWORD array null
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const CONSTANT2 = [100, ...CONSTANT, ...|CONSTANT1,];
PACKAGE Bar [PUBLIC] null
PACKAGE Foo [PUBLIC] null
CLASS Qux [PUBLIC] Bar
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
KEYWORD array null
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Code completion result for source line:
const CONSTANT3 = [...CONSTANT2, 100 => 0, ...CONSTANT|];
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Code completion result for source line:
const CONSTANT4 = [...CONSTANT2, 100 => 0, ...\Bar\|BAR_CONSTANT];
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
CLASS Qux [PUBLIC] Bar
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const BAR_CONSTANT = |[];
PACKAGE Bar [PUBLIC] null
PACKAGE Foo [PUBLIC] null
CLASS Qux [PUBLIC] Bar
CONSTANT BAR_CONSTANT ? [PUBLIC] Bar
CONSTANT CONSTANT ? [PUBLIC] Foo
CONSTANT CONSTANT1 ? [PUBLIC] Foo
CONSTANT CONSTANT1a ? [PUBLIC] Foo
CONSTANT CONSTANT2 ? [PUBLIC] Foo
CONSTANT CONSTANT3 ? [PUBLIC] Foo
CONSTANT CONSTANT4 ? [PUBLIC] Foo
CONSTANT CONSTANT5 ? [PUBLIC] Foo
CONSTANT BAR_CONSTANT [] [PUBLIC] Bar
CONSTANT CONSTANT [0,...] [PUBLIC] Foo
CONSTANT CONSTANT1 [...] [PUBLIC] Foo
CONSTANT CONSTANT1a [[0,...]] [PUBLIC] Foo
CONSTANT CONSTANT2 [100,...] [PUBLIC] Foo
CONSTANT CONSTANT3 [...] [PUBLIC] Foo
CONSTANT CONSTANT4 [...] [PUBLIC] Foo
CONSTANT CONSTANT5 [...] [PUBLIC] Foo
------------------------------------
KEYWORD array null

0 comments on commit c36a4fa

Please sign in to comment.