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/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 3ddcdc4 [NETBEANS-2917]: Support for yield in Switch-Expression for JDK-13 (#1409) 3ddcdc4 is described below commit 3ddcdc4d242293353d5a320d837759e8413644dd Author: Arunava Sinha <arunava.si...@oracle.com> AuthorDate: Tue Aug 27 17:12:34 2019 +0530 [NETBEANS-2917]: Support for yield in Switch-Expression for JDK-13 (#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 --- .../java/completion/JavaCompletionTask.java | 49 ++++++++++++++++--- .../1.8/SwitchExprYieldAutoCompletion.pass | 1 + .../10/SwitchExprYieldAutoCompletion.pass | 1 + .../11/SwitchExprYieldAutoCompletion.pass | 1 + .../12/SwitchExprYieldAutoCompletion.pass | 1 + .../13/SwitchExprYieldAutoCompletion.pass | 1 + .../13/ruleSwitchAutoCompleteCaseValues.pass | 1 + .../13/ruleSwitchEnumCaseValues.pass | 1 + .../13/switchEnumCaseValues.pass | 3 ++ .../13/switchEnumCaseValues2.pass | 2 + .../13/switchExprEnumCaseValues.pass | 2 + .../data/SwitchExprForYieldWithValue.java | 35 +++++++++++++ .../data/SwitchExprForYieldWithValue2.java | 36 ++++++++++++++ .../JavaCompletionTask113FeaturesTest.java | 57 ++++++++++++++++++++++ .../base/semantic/SemanticHighlighterBase.java | 13 ++++- .../java/editor/base/semantic/DetectorTest.java | 15 ++++++ .../java/editor/base/semantic/HighlightImpl.java | 3 ++ .../java/editor/base/semantic/TestBase.java | 35 ++++++++++++- .../hints/errors/DifferentCaseKindsFixTest.java | 44 ++++++++--------- .../org/netbeans/api/java/source/WorkingCopy.java | 6 +++ .../netbeans/modules/java/source/TreeShims.java | 16 ++++++ .../modules/java/source/pretty/VeryPretty.java | 17 ++++++- .../modules/java/source/save/Reformatter.java | 25 ++++++++-- .../api/java/source/gen/SwitchExpressionTest.java | 10 ++-- .../modules/java/source/save/FormatingTest.java | 36 +++++++------- .../modules/java/source/save/ReindenterTest.java | 16 +++--- 26 files changed, 361 insertions(+), 66 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 fb901e3..2722cf0 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 @@ -198,7 +198,8 @@ public final class JavaCompletionTask<T> extends BaseTask { 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 @@ -235,9 +236,10 @@ public final class JavaCompletionTask<T> extends BaseTask { 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"); @@ -249,9 +251,15 @@ public final class JavaCompletionTask<T> extends BaseTask { } 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; @@ -491,6 +499,11 @@ public final class JavaCompletionTask<T> extends BaseTask { case STRING_LITERAL: insideStringLiteral(env); break; + default: + if (path.getLeaf().getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) { + insideSwitch(env); + } + break; } } @@ -1498,6 +1511,15 @@ public final class JavaCompletionTask<T> extends BaseTask { } 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 { @@ -2279,12 +2301,21 @@ public final class JavaCompletionTask<T> extends BaseTask { 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; @@ -2324,8 +2355,14 @@ public final class JavaCompletionTask<T> extends BaseTask { } 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); diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/SwitchExprYieldAutoCompletion.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/SwitchExprYieldAutoCompletion.pass new file mode 100644 index 0000000..fcd1a85 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/1.8/SwitchExprYieldAutoCompletion.pass @@ -0,0 +1 @@ +yield diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/SwitchExprYieldAutoCompletion.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/SwitchExprYieldAutoCompletion.pass new file mode 100644 index 0000000..fcd1a85 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/SwitchExprYieldAutoCompletion.pass @@ -0,0 +1 @@ +yield diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/SwitchExprYieldAutoCompletion.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/SwitchExprYieldAutoCompletion.pass new file mode 100644 index 0000000..fcd1a85 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/11/SwitchExprYieldAutoCompletion.pass @@ -0,0 +1 @@ +yield diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/SwitchExprYieldAutoCompletion.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/SwitchExprYieldAutoCompletion.pass new file mode 100644 index 0000000..fcd1a85 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/12/SwitchExprYieldAutoCompletion.pass @@ -0,0 +1 @@ +yield diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/SwitchExprYieldAutoCompletion.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/SwitchExprYieldAutoCompletion.pass new file mode 100644 index 0000000..fcd1a85 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/SwitchExprYieldAutoCompletion.pass @@ -0,0 +1 @@ +yield diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/ruleSwitchAutoCompleteCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/ruleSwitchAutoCompleteCaseValues.pass new file mode 100644 index 0000000..9a33383 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/ruleSwitchAutoCompleteCaseValues.pass @@ -0,0 +1 @@ +case diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/ruleSwitchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/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/13/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/13/switchEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchEnumCaseValues.pass new file mode 100644 index 0000000..a29d018 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchEnumCaseValues.pass @@ -0,0 +1,3 @@ +public static final colors BLUE +public static final colors GREEN +public static final colors RED diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchEnumCaseValues2.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchEnumCaseValues2.pass new file mode 100644 index 0000000..08f4302 --- /dev/null +++ b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchEnumCaseValues2.pass @@ -0,0 +1,2 @@ +public static final colors BLUE +public static final colors GREEN \ No newline at end of file diff --git a/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/switchExprEnumCaseValues.pass b/java/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/13/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/13/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/SwitchExprForYieldWithValue.java b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprForYieldWithValue.java new file mode 100644 index 0000000..fe804a0 --- /dev/null +++ b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprForYieldWithValue.java @@ -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; + + + } + } +} diff --git a/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprForYieldWithValue2.java b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprForYieldWithValue2.java new file mode 100644 index 0000000..c379664 --- /dev/null +++ b/java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/SwitchExprForYieldWithValue2.java @@ -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; + } + + + }; + } +} diff --git a/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask113FeaturesTest.java b/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask113FeaturesTest.java new file mode 100644 index 0000000..2c3758a --- /dev/null +++ b/java/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask113FeaturesTest.java @@ -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() { + } + +} diff --git a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java index 5d6d51c..c5f07bd 100644 --- a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java +++ b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java @@ -1121,6 +1121,18 @@ public abstract class SemanticHighlighterBase extends JavaParserResultTask { 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; @@ -1133,7 +1145,6 @@ public abstract class SemanticHighlighterBase extends JavaParserResultTask { return indent; } - } public static interface ErrorDescriptionSetter { diff --git a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/DetectorTest.java b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/DetectorTest.java index 4dcece3..31d3937 100644 --- a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/DetectorTest.java +++ b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/DetectorTest.java @@ -446,6 +446,21 @@ public class DetectorTest extends TestBase { 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"); diff --git a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/HighlightImpl.java b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/HighlightImpl.java index 6ee1410..b9cc448 100644 --- a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/HighlightImpl.java +++ b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/HighlightImpl.java @@ -142,6 +142,9 @@ public final class HighlightImpl { ColoringAttributes.DECLARATION, ColoringAttributes.MARK_OCCURRENCES, + + ColoringAttributes.KEYWORD, + ColoringAttributes.UNINDENTED_TEXT_BLOCK, }); diff --git a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/TestBase.java b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/TestBase.java index 9d681b6..5ddf790 100644 --- a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/TestBase.java +++ b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/TestBase.java @@ -41,6 +41,8 @@ import java.util.Map.Entry; 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; @@ -55,6 +57,7 @@ import org.netbeans.api.lexer.Language; 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; @@ -163,7 +166,29 @@ public abstract class TestBase extends NbTestCase { } 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"); @@ -342,6 +367,14 @@ public abstract class TestBase extends NbTestCase { 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) { diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java index 9193cfb..b9cacff 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java @@ -47,7 +47,7 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { @Override protected void setUp() throws Exception { super.setUp(); - sourceLevel = "12"; + sourceLevel = "13"; JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; EXTRA_OPTIONS.add("--enable-preview"); } @@ -233,7 +233,7 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " var result = \n" + " switch (p) {\n" - + " case 1: break 1;\n" + + " case 1: yield 1;\n" + " case 2 -> 2;\n" + " default -> 3;\n" + " }\n" @@ -246,9 +246,9 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " var result =\n" + " switch (p) {\n" - + " case 1 -> { break 1; }\n" - + " case 2 -> { break 2; }\n" - + " default -> { break 3; }\n" + + " case 1 -> { yield 1; }\n" + + " case 2 -> { yield 2; }\n" + + " default -> { yield 3; }\n" + " }\n" + " }\n" + "}\n").replaceAll("[\\s]+", " ")); @@ -263,7 +263,7 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " switch (p) {\n" + " case 1 -> 1;\n" + " case 2 -> 2;\n" - + " default : break 3;\n" + + " default : yield 3;\n" + " }\n" + " }\n" + "}\n", @@ -274,9 +274,9 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " var result = \n" + " switch (p) {\n" - + " case 1 -> { break 1; }\n" - + " case 2 -> { break 2; }\n" - + " default -> { break 3; }\n" + + " case 1 -> { yield 1; }\n" + + " case 2 -> { yield 2; }\n" + + " default -> { yield 3; }\n" + " }\n" + " }\n" + "}\n").replaceAll("[\\s]+", " ")); @@ -289,9 +289,9 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " var result = \n" + " switch (p) {\n" - + " case 1: break 1; \n" - + " case 2: break getTest();\n" - + " case 3 -> { System.err.println(3); break 3;}\n" + + " case 1: yield 1; \n" + + " case 2: yield getTest();\n" + + " case 3 -> { System.err.println(3); yield 3;}\n" + " }\n" + " }\n" + " private int getTest() {\n" @@ -305,9 +305,9 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " var result = \n" + " switch (p) {\n" - + " case 1 -> { break 1; }\n" - + " case 2 -> { break getTest(); }\n" - + " case 3 -> { System.err.println(3); break 3;}\n" + + " case 1 -> { yield 1; }\n" + + " case 2 -> { yield getTest(); }\n" + + " case 3 -> { System.err.println(3); yield 3;}\n" + " }\n" + " }\n" + " private int getTest() {\n" @@ -324,7 +324,7 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " String result = \n" + " switch (p) {\n" + " case 0:\n" - + " case 1: break \"1\"; \n" + + " case 1: yield \"1\"; \n" + " case 2 -> \"2\";\n" + " }\n" + " }\n" @@ -336,8 +336,8 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " private void test(int p) {\n" + " String result = \n" + " switch (p) {\n" - + " case 0, 1 -> { break \"1\"; }\n" - + " case 2 -> { break \"2\"; }\n" + + " case 0, 1 -> { yield \"1\"; }\n" + + " case 2 -> { yield \"2\"; }\n" + " }\n" + " }\n" + "}\n").replaceAll("[\\s]+", " ")); @@ -352,10 +352,10 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " result = switch (p) {\n" + " case 1 : \n" + " int x = 1;\n" - + " break x;\n" + + " yield x;\n" + " default -> {\n" + " int y = 1;\n" - + " break 3;\n" + + " yield 3;\n" + " }\n" + " }\n" + " }\n" @@ -369,11 +369,11 @@ public class DifferentCaseKindsFixTest extends ErrorHintsTestBase { + " result = switch (p) {\n" + " case 1 -> {\n" + " int x = 1;\n" - + " break x;\n" + + " yield x;\n" + " }\n" + " default -> {\n" + " int y = 1;\n" - + " break 3;\n" + + " yield 3;\n" + " }\n" + " }\n" + " }\n" diff --git a/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java b/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java index 1cbeef7..a416ad7 100644 --- a/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java +++ b/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java @@ -867,6 +867,8 @@ public class WorkingCopy extends CompilationController { t = translate(translated); } else if (tree != null && tree.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) { t = visitSwitchExpression(tree, null); + } else if (tree != null && tree.getKind().toString().equals("YIELD")) { + t = visitYield(tree, null); } else { t = super.translate(tree); } @@ -893,6 +895,10 @@ public class WorkingCopy extends CompilationController { public Tree visitSwitchExpression(Tree set, Object p) { return rewriteChildren(set); } + + public Tree visitYield(Tree set, Object p) { + return set; + } }; Context c = impl.getJavacTask().getContext(); itt.attach(c, ia, tree2Tag); diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java index d6e4746..824ac75 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java @@ -35,6 +35,7 @@ import java.util.List; public class TreeShims { public static final String SWITCH_EXPRESSION = "SWITCH_EXPRESSION"; //NOI18N + public static final String YIELD = "YIELD"; //NOI18N public static List<? extends ExpressionTree> getExpressions(CaseTree node) { try { @@ -115,6 +116,21 @@ public class TreeShims { } } + public static ExpressionTree getYieldValue(Tree node) { + if (!node.getKind().toString().equals(YIELD)) { + return null; + } + try { + Class yieldTreeClass = Class.forName("com.sun.source.tree.YieldTree"); //NOI18N + Method getExpression = yieldTreeClass.getDeclaredMethod("getValue"); //NOI18N + return (ExpressionTree) getExpression.invoke(node); + } catch (NoSuchMethodException ex) { + return null; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) { + throw TreeShims.<RuntimeException>throwAny(ex); + } + } + public static Tree SwitchExpression(TreeMaker make, ExpressionTree selector, List<? extends CaseTree> caseList) throws SecurityException { ListBuffer<JCTree.JCCase> cases = new ListBuffer<JCTree.JCCase>(); for (CaseTree t : caseList) { diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java b/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java index 66c0043..85f00fe 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java @@ -69,6 +69,7 @@ import com.sun.source.doctree.UnknownInlineTagTree; import com.sun.source.doctree.UsesTree; import com.sun.source.doctree.ValueTree; import com.sun.source.doctree.VersionTree; +import com.sun.source.tree.ExpressionTree; import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.api.JavacTrees; @@ -410,7 +411,11 @@ public final class VeryPretty extends JCTree.Visitor implements DocTreeVisitor<V this.commentsEnabled = printComments; if (t.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) { visitSwitchExpression(t); - } else { + } + else if (t.getKind().toString().equals(TreeShims.YIELD)) { + visitYield(t); + } + else { t.accept(this); } this.commentsEnabled = saveComments; @@ -1498,6 +1503,16 @@ public final class VeryPretty extends JCTree.Visitor implements DocTreeVisitor<V print(';'); } + public void visitYield(Tree tree) { + print("yield"); + ExpressionTree expr = TreeShims.getYieldValue(tree); + if (expr != null) { + needSpace(); + print((JCTree) expr); + } + print(';'); + } + @Override public void visitContinue(JCContinue tree) { print("continue"); diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java index 36d5b64..d778a25 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java @@ -578,7 +578,7 @@ public class Reformatter implements ReformatTask { try { if (endPos < 0) return false; - Boolean ret = tokens.offset() <= endPos ? (tree.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) ? scanSwitchExpression(tree,p) : super.scan(tree, p) : null; + Boolean ret = tokens.offset() <= endPos ? (tree.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION)) ? scanSwitchExpression(tree, p) : (tree.getKind().toString().equals(TreeShims.YIELD)) ? scanYield(tree, p) : super.scan(tree, p) : null; return ret != null ? ret : true; } finally { @@ -2573,6 +2573,21 @@ public class Reformatter implements ReformatTask { return handleSwitch(node,p); } + private Boolean scanYield(Tree node, Void p) { + return handleYield(node, p); + } + + private Boolean handleYield(Tree node, Void p) { + ExpressionTree exprTree = TreeShims.getYieldValue(node); + if (exprTree != null) { + accept(IDENTIFIER); + space(); + scan(exprTree, p); + } + accept(SEMICOLON); + return true; + } + private boolean handleSwitch(Tree node, Void p) { accept(SWITCH); boolean oldContinuationIndent = continuationIndent; @@ -2684,11 +2699,13 @@ public class Reformatter implements ReformatTask { @Override public Boolean visitCase(CaseTree node, Void p) { - ExpressionTree exp = node.getExpression(); - if (exp != null) { + List<? extends ExpressionTree> exprs = TreeShims.getExpressions(node); + if (exprs.size() > 0) { accept(CASE); space(); - scan(exp, p); + for (ExpressionTree exp : exprs) { + scan(exp, p); + } } else { accept(DEFAULT); } diff --git a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/SwitchExpressionTest.java b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/SwitchExpressionTest.java index 344996e..84a1404 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/SwitchExpressionTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/SwitchExpressionTest.java @@ -72,7 +72,7 @@ public class SwitchExpressionTest extends TreeRewriteTestBase { @Override protected void setUp() throws Exception { super.setUp(); - sourceLevel = "1.12"; + sourceLevel = "1.13"; JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; EXTRA_OPTIONS.add("--enable-preview"); } @@ -113,7 +113,7 @@ public class SwitchExpressionTest extends TreeRewriteTestBase { + "public class Test {\n" + " private void test(int p) {\n" + " var v = switch (p) {\n" - + " case 1: break 1;\n" + + " case 1: yield 1;\n" + " case 2 -> 2;\n" + " default -> 3;\n" + " }\n" @@ -124,13 +124,13 @@ public class SwitchExpressionTest extends TreeRewriteTestBase { + " private void test(int p) {\n" + " var v = switch (p) {\n" + " case 1 -> {\n" - + " break 1;\n" + + " yield 1;\n" + " }\n" + " case 2 -> {\n" - + " break 2;\n" + + " yield 2;\n" + " }\n" + " default -> {\n" - + " break 3;\n" + + " yield 3;\n" + " }\n" + " }\n" + " }\n" diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java index 339c6b2..d28913b 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java @@ -2100,10 +2100,10 @@ public class FormatingTest extends NbTestCase { + "int i = switch(i){" + "case 0:" + "{System.out.println(i);" - + "break 5;}" + + "yield 5;}" + "default:" + "{System.out.println(\"DEFAULT\");" - + "break 6;}" + + "yield 6;}" + "}" + "}" + "}\n"; @@ -2115,11 +2115,11 @@ public class FormatingTest extends NbTestCase { + " int i = switch (i) {\n" + " case 0: {\n" + " System.out.println(i);\n" - + " break 5;\n" + + " yield 5;\n" + " }\n" + " default: {\n" + " System.out.println(\"DEFAULT\");\n" - + " break 6;\n" + + " yield 6;\n" + " }\n" + " }\n" + " }\n" @@ -2137,11 +2137,11 @@ public class FormatingTest extends NbTestCase { + " int i = switch( i ){\n" + " case 0: {\n" + " System.out.println(i);\n" - + " break 5;\n" + + " yield 5;\n" + " }\n" + " default: {\n" + " System.out.println(\"DEFAULT\");\n" - + " break 6;\n" + + " yield 6;\n" + " }\n" + " }\n" + " }\n" @@ -2167,12 +2167,12 @@ public class FormatingTest extends NbTestCase { + " case 0:\n" + " {\n" + " System.out.println(i);\n" - + " break 5;\n" + + " yield 5;\n" + " }\n" + " default:\n" + " {\n" + " System.out.println(\"DEFAULT\");\n" - + " break 6;\n" + + " yield 6;\n" + " }\n" + " }\n" + " }\n" @@ -2192,12 +2192,12 @@ public class FormatingTest extends NbTestCase { + " case 0:\n" + " {\n" + " System.out.println(i);\n" - + " break 5;\n" + + " yield 5;\n" + " }\n" + " default:\n" + " {\n" + " System.out.println(\"DEFAULT\");\n" - + " break 6;\n" + + " yield 6;\n" + " }\n" + " }\n" + " }\n" @@ -2231,10 +2231,10 @@ public class FormatingTest extends NbTestCase { + "int i = switch(i){" + "case 0->" + "{System.out.println(i);" - + "break 5;}" + + "yield 5;}" + "default->" + "{System.out.println(\"DEFAULT\");" - + "break 6;}" + + "yield 6;}" + "}" + "}" + "}\n"; @@ -2247,12 +2247,12 @@ public class FormatingTest extends NbTestCase { + " case 0->\n" + " {\n" + " System.out.println(i);\n" - + " break 5;\n" + + " yield 5;\n" + " }\n" + " default->\n" + " {\n" + " System.out.println(\"DEFAULT\");\n" - + " break 6;\n" + + " yield 6;\n" + " }\n" + " }\n" + " }\n" @@ -2274,7 +2274,7 @@ public class FormatingTest extends NbTestCase { + "case 0-> new Runnable(){public void run(){}};" + "default->" + "{System.out.println(\"DEFAULT\");" - + "break new Runnable(){public void run(){}};}" + + "yield new Runnable(){public void run(){}};}" + "}" + "}" + "}\n"; @@ -2290,7 +2290,7 @@ public class FormatingTest extends NbTestCase { + " };\n" + " default-> {\n" + " System.out.println(\"DEFAULT\");\n" - + " break new Runnable() {\n" + + " yield new Runnable() {\n" + " public void run() {\n" + " }\n" + " };\n" @@ -2308,7 +2308,7 @@ public class FormatingTest extends NbTestCase { + "case 0-> get();" + "default->" + "{System.out.println(\"DEFAULT\");" - + "break get();}" + + "yield get();}" + "}" + "}" + "}\n"; @@ -2324,7 +2324,7 @@ public class FormatingTest extends NbTestCase { + " get();\n" + " default-> {\n" + " System.out.println(\"DEFAULT\");\n" - + " break get();\n" + + " yield get();\n" + " }\n" + " }\n" + " }\n" diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/ReindenterTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/ReindenterTest.java index 3848fb4..93a059a 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/ReindenterTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/ReindenterTest.java @@ -1910,18 +1910,18 @@ public class ReindenterTest extends NbTestCase { } public void testNewLineIndentationInsideSwExpCase2() throws Exception { - performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:|\n break 5;\n }\n }\n}\n", - "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n \n break 5;\n }\n }\n}\n"); + performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:|\n yield 5;\n }\n }\n}\n", + "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n \n yield 5;\n }\n }\n}\n"); } public void testNewLineIndentationInsideSwExpCase3() throws Exception { - performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->|\n {break 5;}\n }\n }\n}\n", - "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->\n \n {break 5;}\n }\n }\n}\n"); + performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->|\n {yield 5;}\n }\n }\n}\n", + "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->\n \n {yield 5;}\n }\n }\n}\n"); } public void testNewLineIndentationInsideSwExpIllegalCase() throws Exception { - performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->|\n break 5;\n }\n }\n}\n", - "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->\n \n break 5;\n }\n }\n}\n"); + performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->|\n yield 5;\n }\n }\n}\n", + "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1->\n \n yield 5;\n }\n }\n}\n"); } public void testNewLineIndentationInsideSwExpIllegalCase2() throws Exception { @@ -1930,8 +1930,8 @@ public class ReindenterTest extends NbTestCase { } public void testNewLineIndentationAfterSwExprCase() throws Exception { - performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n break 5;|\n }\n }\n}\n", - "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n break 5;\n \n }\n }\n}\n"); + performNewLineIndentationTest("package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n yield 5;|\n }\n }\n}\n", + "package t;\npublic class T {\n public void op() {\n int a = switch(get()) {\n case 1:\n yield 5;\n \n }\n }\n}\n"); } public void testNewLineIndentationAfterSwExprRule() throws Exception { --------------------------------------------------------------------- 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