This is an automated email from the ASF dual-hosted git repository.

tmysik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e66bfc  [NETBEANS-5027] PHP - improved display of array parameter
     new 514e170  Merge pull request #2535 from 
KacerCZ/netbeans-5027-display-array-param
8e66bfc is described below

commit 8e66bfc02425317763717538e6c6ee092396b6e8
Author: Tomas Prochazka <ka...@razdva.cz>
AuthorDate: Sat Nov 14 16:58:31 2020 +0100

    [NETBEANS-5027] PHP - improved display of array parameter
    
    https://issues.apache.org/jira/browse/NETBEANS-5027
    
    Improves display of array used as default parameter value.
    Uses short array format, displays first item in array, indicates more 
elements in array.
---
 .../org/netbeans/modules/php/editor/CodeUtils.java | 27 +++++++++++++-
 .../netbeans/modules/php/editor/CodeUtilsTest.java | 43 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java 
b/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java
index 36123e9..71494f3 100644
--- a/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java
+++ b/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java
@@ -33,6 +33,7 @@ import org.netbeans.modules.php.editor.model.impl.Type;
 import org.netbeans.modules.php.editor.model.nodes.NamespaceDeclarationInfo;
 import org.netbeans.modules.php.editor.parser.astnodes.ArrayAccess;
 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.Assignment;
 import org.netbeans.modules.php.editor.parser.astnodes.CatchClause;
 import org.netbeans.modules.php.editor.parser.astnodes.ClassDeclaration;
@@ -568,6 +569,11 @@ public final class CodeUtils {
     @CheckForNull
     public static String getParamDefaultValue(FormalParameter param) {
         Expression expr = param.getDefaultValue();
+        return getParamDefaultValue(expr);
+    }
+
+    @CheckForNull
+    private static String getParamDefaultValue(Expression expr) {
         //TODO: can be improved
         Operator operator = null;
         if (expr instanceof UnaryOperation) {
@@ -582,7 +588,7 @@ public final class CodeUtils {
         } else if (expr instanceof NamespaceName) {
             return extractQualifiedName((NamespaceName) expr);
         } else if (expr instanceof ArrayCreation) {
-            return "array()"; //NOI18N
+            return getParamDefaultValue((ArrayCreation) expr);
         } else if (expr instanceof StaticConstantAccess) {
             StaticConstantAccess staticConstantAccess = (StaticConstantAccess) 
expr;
             Expression dispatcher = staticConstantAccess.getDispatcher();
@@ -597,6 +603,25 @@ public final class CodeUtils {
         }
         return expr == null ? null : " "; //NOI18N
     }
+    
+    private static String getParamDefaultValue(ArrayCreation param) {
+        StringBuilder sb = new StringBuilder("["); //NOI18N
+        List<ArrayElement> arrayElements = param.getElements();
+        if (arrayElements.size() > 0) {
+            ArrayElement firstElement = arrayElements.get(0);
+            Expression key = firstElement.getKey();
+            if (key != null) {
+                sb.append(getParamDefaultValue(key));
+                sb.append(" => "); //NOI18N
+            }
+            sb.append(getParamDefaultValue(firstElement.getValue()));
+        }
+        if (arrayElements.size() > 1) {
+            sb.append(",..."); //NOI18N
+        }
+        sb.append("]"); //NOI18N
+        return sb.toString();
+    }
 
     public static String getParamDisplayName(FormalParameter param) {
         Expression paramNameExpr = param.getParameterName();
diff --git 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/CodeUtilsTest.java
 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/CodeUtilsTest.java
index 3005b30..a5eeb0b 100644
--- 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/CodeUtilsTest.java
+++ 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/CodeUtilsTest.java
@@ -22,6 +22,10 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import org.netbeans.junit.NbTestCase;
+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.FormalParameter;
+import org.netbeans.modules.php.editor.parser.astnodes.Scalar;
 
 public class CodeUtilsTest extends NbTestCase {
 
@@ -179,4 +183,43 @@ public class CodeUtilsTest extends NbTestCase {
         assertEquals(0, prefixes.size());
     }
 
+    public void testGetParamDefaultValueEmptyArray() {
+        List<ArrayElement> emptyArray = Collections.emptyList();
+        FormalParameter param = 
createFormalParameterWithDefaultArray(emptyArray);
+        assertEquals("[]", CodeUtils.getParamDefaultValue(param));
+    }
+
+    public void testGetParamDefaultValueArrayOneItem() {
+        Scalar value = new Scalar(1, 1, "'a'", Scalar.Type.STRING);
+        ArrayElement item = new ArrayElement(1, 1, null, value);
+        List<ArrayElement> array = Arrays.asList(item);
+        FormalParameter param = createFormalParameterWithDefaultArray(array);
+        assertEquals("['a']", CodeUtils.getParamDefaultValue(param));
+    }
+
+    public void testGetParamDefaultValueArrayTwoItems() {
+        Scalar value1 = new Scalar(1, 1, "'a'", Scalar.Type.STRING);
+        ArrayElement item1 = new ArrayElement(1, 1, null, value1);
+        Scalar value2 = new Scalar(1, 1, "'b'", Scalar.Type.STRING);
+        ArrayElement item2 = new ArrayElement(1, 1, null, value2);
+        List<ArrayElement> array = Arrays.asList(item1, item2);
+        FormalParameter param = createFormalParameterWithDefaultArray(array);
+        assertEquals("['a',...]", CodeUtils.getParamDefaultValue(param));
+    }
+
+    public void testGetParamDefaultValueArrayItemWithKey() {
+        Scalar key = new Scalar(1, 1, "3", Scalar.Type.INT);
+        Scalar value = new Scalar(1, 1, "'a'", Scalar.Type.STRING);
+        ArrayElement item = new ArrayElement(1, 1, key, value);
+        List<ArrayElement> array = Arrays.asList(item);
+        FormalParameter param = createFormalParameterWithDefaultArray(array);
+        assertEquals("[3 => 'a']", CodeUtils.getParamDefaultValue(param));
+    }
+
+    private FormalParameter 
createFormalParameterWithDefaultArray(List<ArrayElement> arrayContent) {
+        ArrayCreation defaultValue = new ArrayCreation(1, 1, arrayContent, 
ArrayCreation.Type.NEW);
+        FormalParameter param = new FormalParameter(1, 1, null, null, 
defaultValue);
+        return param;
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to