Skip to content

Commit

Permalink
[NETBEANS-2917]: Support for yield in Switch-Expression for JDK-13 (#…
Browse files Browse the repository at this point in the history
…1409)

* Adding support for yield.

* Adding forgotten test API.

* [NETBEANS-2917]: AutoCompletion of Yield keyword inside Switch Expression for JDK-13

* [NETBEANS-2917]: Corrected formatting for Switch Expression using yield with value

* [NETBEANS-2917]: Added autocomplete for Switch-Expr Yield keyword inside Block

* [NETBEANS-2917]: Modified Test-Cases for supporting Yield with value inside Switch Expression

* [NETBEANS-2917]: Refactored code for Yield Support in Switch Expression

* [NETBEANS-2917]:Syncing golden file with master
  • Loading branch information
Arunava Sinha committed Aug 27, 2019
1 parent 8f82dfc commit 3ddcdc4
Show file tree
Hide file tree
Showing 26 changed files with 361 additions and 66 deletions.
Expand Up @@ -198,7 +198,8 @@ public static enum Options {
private static final String VOLATILE_KEYWORD = "volatile"; //NOI18N
private static final String WHILE_KEYWORD = "while"; //NOI18N
private static final String WITH_KEYWORD = "with"; //NOI18N

private static final String YIELD_KEYWORD = "yield"; //NOI18N

private static final String JAVA_LANG_CLASS = "java.lang.Class"; //NOI18N
private static final String JAVA_LANG_OBJECT = "java.lang.Object"; //NOI18N
private static final String JAVA_LANG_ITERABLE = "java.lang.Iterable"; //NOI18N
Expand Down Expand Up @@ -235,9 +236,10 @@ public static enum Options {

private static final SourceVersion SOURCE_VERSION_RELEASE_10;
private static final SourceVersion SOURCE_VERSION_RELEASE_11;
private static final SourceVersion SOURCE_VERSION_RELEASE_13;

static {
SourceVersion r10, r11;
SourceVersion r10, r11, r13;

try {
r10 = SourceVersion.valueOf("RELEASE_10");
Expand All @@ -249,9 +251,15 @@ public static enum Options {
} catch (IllegalArgumentException ex) {
r11 = null;
}
try {
r13 = SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
r13 = null;
}

SOURCE_VERSION_RELEASE_10 = r10;
SOURCE_VERSION_RELEASE_11 = r11;
SOURCE_VERSION_RELEASE_13 = r13;
}

private final ItemFactory<T> itemFactory;
Expand Down Expand Up @@ -491,6 +499,11 @@ protected void resolve(CompilationController controller) throws IOException {
case STRING_LITERAL:
insideStringLiteral(env);
break;
default:
if (path.getLeaf().getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) {
insideSwitch(env);
}
break;
}
}

Expand Down Expand Up @@ -1498,6 +1511,15 @@ private void insideBlock(Env env) throws IOException {
}
localResult(env);
addKeywordsForBlock(env);

String prefix = env.getPrefix();
if (SOURCE_VERSION_RELEASE_13 != null && env.getController().getSourceVersion().compareTo(SOURCE_VERSION_RELEASE_13) >= 0
&& Utilities.startsWith(YIELD_KEYWORD, prefix)) {
TreePath parentPath = env.getPath().getParentPath();
if (parentPath.getLeaf().getKind() == Tree.Kind.CASE && parentPath.getParentPath().getLeaf().getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) {
addKeyword(env, YIELD_KEYWORD, null, false);
}
}
}

private void insideMemberSelect(Env env) throws IOException {
Expand Down Expand Up @@ -2279,12 +2301,21 @@ private void insideForEach(Env env) throws IOException {
private void insideSwitch(Env env) throws IOException {
int offset = env.getOffset();
TreePath path = env.getPath();
SwitchTree st = (SwitchTree) path.getLeaf();
ExpressionTree exprTree = null;
if (path.getLeaf().getKind() == Tree.Kind.SWITCH) {
exprTree = ((SwitchTree) path.getLeaf()).getExpression();

} else {
List<? extends ExpressionTree> exprTrees = TreeShims.getExpressions(path.getLeaf());
if (!exprTrees.isEmpty()) {
exprTree = exprTrees.get(0);
}
}
SourcePositions sourcePositions = env.getSourcePositions();
CompilationUnitTree root = env.getRoot();
if (sourcePositions.getStartPosition(root, st.getExpression()) < offset) {
if (sourcePositions.getStartPosition(root, exprTree) < offset) {
CaseTree lastCase = null;
for (CaseTree t : st.getCases()) {
for (CaseTree t : TreeShims.getCases(path.getLeaf())) {
int pos = (int) sourcePositions.getStartPosition(root, t);
if (pos == Diagnostic.NOPOS || offset <= pos) {
break;
Expand Down Expand Up @@ -2324,8 +2355,14 @@ private void insideSwitch(Env env) throws IOException {
}
localResult(env);
addKeywordsForBlock(env);
String prefix = env.getPrefix();
if (SOURCE_VERSION_RELEASE_13 != null && (env.getController().getSourceVersion().compareTo(SOURCE_VERSION_RELEASE_13) >= 0
&& path.getLeaf().getKind().toString().equals(TreeShims.SWITCH_EXPRESSION) && Utilities.startsWith(YIELD_KEYWORD, prefix))) {
addKeyword(env, YIELD_KEYWORD, null, false);
}

} else {
TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, st, offset);
TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, path.getLeaf(), offset);
if (ts != null && ts.token().id() == JavaTokenId.LBRACE) {
addKeyword(env, CASE_KEYWORD, SPACE, false);
addKeyword(env, DEFAULT_KEYWORD, COLON, false);
Expand Down
@@ -0,0 +1 @@
yield
@@ -0,0 +1 @@
yield
@@ -0,0 +1 @@
yield
@@ -0,0 +1 @@
yield
@@ -0,0 +1 @@
yield
@@ -0,0 +1 @@
case
@@ -0,0 +1 @@
public static final colors BLUE
@@ -0,0 +1,3 @@
public static final colors BLUE
public static final colors GREEN
public static final colors RED
@@ -0,0 +1,2 @@
public static final colors BLUE
public static final colors GREEN
@@ -0,0 +1,2 @@
public static final colors BLUE
public static final colors GREEN
@@ -0,0 +1,35 @@
/*
* 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:
a=20;


}
}
}
@@ -0,0 +1,36 @@
/*
* 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-> {
a = 20;
}


};
}
}
@@ -0,0 +1,57 @@
/*
* 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 org.netbeans.modules.java.completion;

import javax.lang.model.SourceVersion;
import org.netbeans.junit.NbTestSuite;

/**
*
* @author arusinha
*/
public class JavaCompletionTask113FeaturesTest extends CompletionTestBase {

public JavaCompletionTask113FeaturesTest(String testName) {
super(testName);
}

public static NbTestSuite suite() {
NbTestSuite suite = new NbTestSuite();
try {
SourceVersion.valueOf("RELEASE_13"); //NOI18N
suite.addTestSuite(JavaCompletionTask113FeaturesTest.class);
} catch (IllegalArgumentException ex) {
//OK, no RELEASE_13, skip tests
suite.addTest(new JavaCompletionTask113FeaturesTest("noop")); //NOI18N
}
return suite;
}

public void testSwitchExprAutoCompleteYieldValue() throws Exception {
performTest("SwitchExprForYieldWithValue", 1019, "yi", "SwitchExprYieldAutoCompletion.pass");
}

public void testSwitchExprAutoCompleteYieldValue2() throws Exception {
performTest("SwitchExprForYieldWithValue2", 1023, "yi", "SwitchExprYieldAutoCompletion.pass");
}

public void noop() {
}

}
Expand Up @@ -1121,6 +1121,18 @@ public Void visitLiteral(LiteralTree node, Void p) {
return super.visitLiteral(node, p);
}

@Override
public Void scan(Tree tree, Void p) {
if (tree != null && "YIELD".equals(tree.getKind().name())) {
tl.moveToOffset(sourcePositions.getStartPosition(info.getCompilationUnit(), tree));
Token t = firstIdentifierToken("yield"); //NOI18N
if (t != null) {
contextKeywords.add(t);
}
}
return super.scan(tree, p);
}

private int leadingIndent(String line) {
int indent = 0;

Expand All @@ -1133,7 +1145,6 @@ private int leadingIndent(String line) {

return indent;
}

}

public static interface ErrorDescriptionSetter {
Expand Down
Expand Up @@ -446,6 +446,21 @@ public void testIncDecReading230408() throws Exception {
performTest("IncDecReading230408");
}

public void testYield() throws Exception {
enablePreview();
performTest("YieldTest.java",
"public class YieldTest {\n" +
" private int map(int i) {\n" +
" return switch (i) { default -> { yield 0; } };\n" +
" }\n" +
"}\n",
"[PUBLIC, CLASS, DECLARATION], 0:13-0:22\n" +
"[PRIVATE, METHOD, UNUSED, DECLARATION], 1:16-1:19\n" +
"[PARAMETER, DECLARATION], 1:24-1:25\n" +
"[PARAMETER], 2:23-2:24\n" +
"[KEYWORD], 2:41-2:46\n");
}

public void testRawStringLiteral() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
Expand Down
Expand Up @@ -142,6 +142,9 @@ private String coloringsToString() {
ColoringAttributes.DECLARATION,

ColoringAttributes.MARK_OCCURRENCES,

ColoringAttributes.KEYWORD,

ColoringAttributes.UNINDENTED_TEXT_BLOCK,
});

Expand Down
Expand Up @@ -41,6 +41,8 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import javax.lang.model.SourceVersion;
import javax.swing.event.ChangeListener;
import java.util.stream.Collectors;
import javax.swing.text.Document;
import org.netbeans.api.java.lexer.JavaTokenId;
Expand All @@ -55,6 +57,7 @@
import org.netbeans.api.lexer.Token;
import org.netbeans.junit.NbTestCase;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.java.queries.CompilerOptionsQueryImplementation;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -163,7 +166,29 @@ protected void performTest(String filename, String content, final Performer perf
}

protected void performTest(Input input, final Performer performer, boolean doCompileRecursively, Validator validator) throws Exception {
SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[] {new MIMEResolverImpl()});
SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[] {
new MIMEResolverImpl(),
new CompilerOptionsQueryImplementation() {
@Override
public CompilerOptionsQueryImplementation.Result getOptions(FileObject file) {
if (testSourceFO == file) {
return new CompilerOptionsQueryImplementation.Result() {
@Override
public List<? extends String> getArguments() {
return extraOptions;
}

@Override
public void addChangeListener(ChangeListener listener) {}

@Override
public void removeChangeListener(ChangeListener listener) {}
};
}
return null;
}
}
});

FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this);
FileObject cache = scratch.createFolder("cache");
Expand Down Expand Up @@ -342,6 +367,14 @@ protected final void setSourceLevel(String sourceLevel) {
this.sourceLevel = sourceLevel;
}

private List<String> extraOptions = new ArrayList<>();

protected final void enablePreview() {
String svName = SourceVersion.latest().name();
setSourceLevel(svName.substring(svName.indexOf('_') + 1));
extraOptions.add("--enable-preview");
}

private boolean showPrependedText;

protected final void setShowPrependedText(boolean showPrependedText) {
Expand Down

0 comments on commit 3ddcdc4

Please sign in to comment.