You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ar...@apache.org on 2019/04/03 04:28:51 UTC

[incubator-netbeans] branch master updated: [NETBEANS-2315] Switch expression multi cases should have Enum values in auto complete options

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 68f21ee  [NETBEANS-2315] Switch expression multi cases should have Enum values in auto complete options
     new 8fb84ee  Merge pull request #1175 from vikasprabhakar/auto_complete_switch_expression
68f21ee is described below

commit 68f21eeda6ffdc32f7dbab5fd27f15304ffbb0a3
Author: Vikas Prabhakar <vi...@oracle.com>
AuthorDate: Tue Mar 26 04:17:44 2019 -0700

    [NETBEANS-2315] Switch expression multi cases should have Enum values in auto complete options
---
 .../java/completion/JavaCompletionTask.java        | 43 ++++++++++++++++------
 .../1.7/ruleSwitchEnumCaseValues.pass              |  1 +
 .../1.7/switchExprEnumCaseValues.pass              |  2 +
 .../1.8/ruleSwitchEnumCaseValues.pass              |  1 +
 .../1.8/switchExprEnumCaseValues.pass              |  2 +
 .../10/ruleSwitchEnumCaseValues.pass               |  1 +
 .../10/switchExprEnumCaseValues.pass               |  2 +
 .../11/ruleSwitchEnumCaseValues.pass               |  1 +
 .../11/switchExprEnumCaseValues.pass               |  2 +
 .../12/ruleSwitchEnumCaseValues.pass               |  1 +
 .../12/switchExprEnumCaseValues.pass               |  2 +
 .../data/RuleSwitchWithMultiEnumValues.java        | 34 +++++++++++++++++
 .../data/SwitchExprWithMultiEnumValues.java        | 33 +++++++++++++++++
 .../data/SwitchStatementWithMultiEnumValues.java   | 33 +++++++++++++++++
 .../JavaCompletionTask112FeaturesTest.java         | 12 ++++++
 15 files changed, 159 insertions(+), 11 deletions(-)

diff --git a/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java b/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
index aa0b898..8e925dc 100644
--- a/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
+++ b/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
@@ -2334,20 +2334,37 @@ public final class JavaCompletionTask<T> extends BaseTask {
         SourcePositions sourcePositions = env.getSourcePositions();
         CompilationUnitTree root = env.getRoot();
         CompilationController controller = env.getController();
-        if (cst.getExpression() != null && ((sourcePositions.getStartPosition(root, cst.getExpression()) >= offset)
-                || (cst.getExpression().getKind() == Tree.Kind.ERRONEOUS && ((ErroneousTree) cst.getExpression()).getErrorTrees().isEmpty() && sourcePositions.getEndPosition(root, cst.getExpression()) >= offset))) {
-            TreePath path1 = path.getParentPath();
-            if (path1.getLeaf().getKind() == Tree.Kind.SWITCH || path1.getLeaf().getKind().toString().equals("SWITCH_EXPRESSION")) { //NOI18N
+        TreePath parentPath = path.getParentPath();
+        ExpressionTree caseExpressionTree = null;
+        ExpressionTree caseErroneousTree = null;
+        List<? extends ExpressionTree> caseTreeList = TreeShims.getExpressions(cst);
+        if (!caseTreeList.isEmpty() && caseTreeList.size() == 1) {
+            caseExpressionTree = caseTreeList.get(0);
+            caseErroneousTree = caseTreeList.get(0);
+        } else if (caseTreeList.size() > 1) {
+            caseExpressionTree = caseTreeList.get(0);
+            for (ExpressionTree et : caseTreeList) {
+                if (et != null && et.getKind() == Tree.Kind.ERRONEOUS) {
+                    caseErroneousTree = et;
+                    break;
+                }
+            }
+        }
+
+        if (caseExpressionTree != null && ((sourcePositions.getStartPosition(root, caseExpressionTree) >= offset)
+                || (caseErroneousTree != null && caseErroneousTree.getKind() == Tree.Kind.ERRONEOUS && ((ErroneousTree) caseErroneousTree).getErrorTrees().isEmpty() && sourcePositions.getEndPosition(root, caseErroneousTree) >= offset))) {
+
+            if (parentPath.getLeaf().getKind() == Tree.Kind.SWITCH || parentPath.getLeaf().getKind().toString().equals("SWITCH_EXPRESSION")) { //NOI18N
                 ExpressionTree exprTree = null;
-                if (path1.getLeaf().getKind() == Tree.Kind.SWITCH) {
-                    exprTree = ((SwitchTree) path1.getLeaf()).getExpression();
+                if (parentPath.getLeaf().getKind() == Tree.Kind.SWITCH) {
+                    exprTree = ((SwitchTree) parentPath.getLeaf()).getExpression();
                 } else {
-                    List<? extends ExpressionTree> exprTrees = TreeShims.getExpressions(path1.getLeaf());
+                    List<? extends ExpressionTree> exprTrees = TreeShims.getExpressions(parentPath.getLeaf());
                     if (!exprTrees.isEmpty()) {
                         exprTree = exprTrees.get(0);
                     }
                 }
-                TypeMirror tm = controller.getTrees().getTypeMirror(new TreePath(path1, exprTree));
+                TypeMirror tm = controller.getTrees().getTypeMirror(new TreePath(parentPath, exprTree));
                 if (tm.getKind() == TypeKind.DECLARED && ((DeclaredType) tm).asElement().getKind() == ENUM) {
                     addEnumConstants(env, (TypeElement) ((DeclaredType) tm).asElement());
                 } else {
@@ -3693,14 +3710,18 @@ public final class JavaCompletionTask<T> extends BaseTask {
         } else if (path != null && path.getLeaf().getKind().toString().equals("SWITCH_EXPRESSION")) { //NOI18N
             caseTrees = TreeShims.getCases(path.getLeaf());
         }
+
         if (caseTrees != null) {
             for (CaseTree ct : caseTrees) {
-                Element e = ct.getExpression() != null ? trees.getElement(new TreePath(path, ct.getExpression())) : null;
-                if (e != null && e.getKind() == ENUM_CONSTANT) {
-                    alreadyUsed.add(e);
+                for (ExpressionTree et : TreeShims.getExpressions(ct)) {
+                    Element e = et != null ? trees.getElement(new TreePath(path, et)) : null;
+                    if (e != null && e.getKind() == ENUM_CONSTANT) {
+                        alreadyUsed.add(e);
+                    }
                 }
             }
         }
+
         for (Element e : elem.getEnclosedElements()) {
             if (e.getKind() == ENUM_CONSTANT && !alreadyUsed.contains(e)) {
                 String name = e.getSimpleName().toString();
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/ruleSwitchEnumCaseValues.pass
new file mode 100644
index 0000000..5d20a0a
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/ruleSwitchEnumCaseValues.pass
@@ -0,0 +1 @@
+public static final colors BLUE
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/switchExprEnumCaseValues.pass
new file mode 100644
index 0000000..52e0a95
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.7/switchExprEnumCaseValues.pass
@@ -0,0 +1,2 @@
+public static final colors BLUE
+public static final colors GREEN
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/ruleSwitchEnumCaseValues.pass
new file mode 100644
index 0000000..5d20a0a
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/ruleSwitchEnumCaseValues.pass
@@ -0,0 +1 @@
+public static final colors BLUE
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/switchExprEnumCaseValues.pass
new file mode 100644
index 0000000..52e0a95
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/switchExprEnumCaseValues.pass
@@ -0,0 +1,2 @@
+public static final colors BLUE
+public static final colors GREEN
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/ruleSwitchEnumCaseValues.pass
new file mode 100644
index 0000000..5d20a0a
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/ruleSwitchEnumCaseValues.pass
@@ -0,0 +1 @@
+public static final colors BLUE
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/switchExprEnumCaseValues.pass
new file mode 100644
index 0000000..52e0a95
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/switchExprEnumCaseValues.pass
@@ -0,0 +1,2 @@
+public static final colors BLUE
+public static final colors GREEN
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/ruleSwitchEnumCaseValues.pass
new file mode 100644
index 0000000..5d20a0a
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/ruleSwitchEnumCaseValues.pass
@@ -0,0 +1 @@
+public static final colors BLUE
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/switchExprEnumCaseValues.pass
new file mode 100644
index 0000000..52e0a95
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/switchExprEnumCaseValues.pass
@@ -0,0 +1,2 @@
+public static final colors BLUE
+public static final colors GREEN
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/ruleSwitchEnumCaseValues.pass
new file mode 100644
index 0000000..5d20a0a
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/ruleSwitchEnumCaseValues.pass
@@ -0,0 +1 @@
+public static final colors BLUE
diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/switchExprEnumCaseValues.pass
new file mode 100644
index 0000000..52e0a95
--- /dev/null
+++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/switchExprEnumCaseValues.pass
@@ -0,0 +1,2 @@
+public static final colors BLUE
+public static final colors GREEN
diff --git a/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/RuleSwitchWithMultiEnumValues.java b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/RuleSwitchWithMultiEnumValues.java
new file mode 100644
index 0000000..039cb22
--- /dev/null
+++ b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/RuleSwitchWithMultiEnumValues.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package test;
+
+public class Test {
+
+    enum colors {RED, GREEN, BLUE}
+
+    public void op(int a) {
+        colors color = colors.RED;
+        switch (color) {
+            case RED -> a = 10;
+            case GREEN,
+                
+	}
+    }
+}
diff --git a/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprWithMultiEnumValues.java b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprWithMultiEnumValues.java
new file mode 100644
index 0000000..bded9df
--- /dev/null
+++ b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprWithMultiEnumValues.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package test;
+
+public class Test {
+
+    enum colors {RED, GREEN, BLUE}
+
+    public void op(int a) {
+        colors color = colors.RED;
+        a = switch (color) {
+            case RED, 
+                
+	}
+    }
+}
diff --git a/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchStatementWithMultiEnumValues.java b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchStatementWithMultiEnumValues.java
new file mode 100644
index 0000000..6bc88c3
--- /dev/null
+++ b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchStatementWithMultiEnumValues.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package test;
+
+public class Test {
+
+    enum colors {RED, GREEN, BLUE}
+
+    public void op(int a) {
+        colors color = colors.RED;
+        switch (color) {
+            case RED, 
+                
+	}
+    }
+}
diff --git a/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask112FeaturesTest.java b/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask112FeaturesTest.java
index 9a23500..6923635 100644
--- a/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask112FeaturesTest.java
+++ b/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask112FeaturesTest.java
@@ -61,6 +61,18 @@ public class JavaCompletionTask112FeaturesTest extends CompletionTestBase {
         performTest("SwitchExprWithEnumValues2", 1020, null, "switchEnumCaseValues2.pass");
     }
 
+    public void testSwitchExprMultiEnumCaseValue() throws Exception {
+        performTest("SwitchExprWithMultiEnumValues", 994, null, "switchExprEnumCaseValues.pass");
+    }
+
+    public void testSwitchStatementMultiEnumCaseValue() throws Exception {
+        performTest("SwitchStatementWithMultiEnumValues", 990, null, "switchExprEnumCaseValues.pass");
+    }
+
+    public void testRuleSwitchMultiEnumCaseValue() throws Exception {
+        performTest("RuleSwitchWithMultiEnumValues", 1024, null, "ruleSwitchEnumCaseValues.pass");
+    }
+
     public void noop() {
     }
 


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

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