diff options
author | Jay Arthanareeswaran | 2019-07-18 05:49:00 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2019-07-28 17:16:27 +0000 |
commit | ce369a4ecc8167f57467670086928858fe4bcf57 (patch) | |
tree | 602d083c459dfc88a82e05181d31a4ce48fc4c5f | |
parent | 1660166cbf7402140ac33c2b86e0e96c704f2ef4 (diff) | |
download | eclipse.jdt.core-Y20190730-0055.tar.gz eclipse.jdt.core-Y20190730-0055.tar.xz eclipse.jdt.core-Y20190730-0055.zip |
Bug 542560 - [13][completion] Code completion Support for SwitchY20190805-0900Y20190801-0900Y20190730-0135Y20190730-0055Y20190729-0900
Expressions
Change-Id: I82f86c5c029927d976e86cf8c3614f0a97921084
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
13 files changed, 825 insertions, 88 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionParserTest12.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionParserTest12.java index b7de23411d..7912c9e4b6 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionParserTest12.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionParserTest12.java @@ -500,11 +500,13 @@ public void test010() throws JavaModelException { " public X() {\n" + " }\n" + " public static void foo(Num num_) {\n" + - " {\n" + - " {\n" + - " int i_j;\n" + - " <SelectOnName:i_j>;\n" + - " }\n" + + " switch (num_) {\n" + + " case THREE ->\n" + + " {\n" + + " int i_j;\n" + + " System.out.println(<SelectOnName:i_j>);\n" + + " break;\n" + + " }\n" + " }\n" + " }\n" + "}\n"; diff --git a/org.eclipse.jdt.core.tests.model/JCL/jclMin13.jar b/org.eclipse.jdt.core.tests.model/JCL/jclMin13.jar Binary files differnew file mode 100644 index 0000000000..0f621d1524 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/JCL/jclMin13.jar diff --git a/org.eclipse.jdt.core.tests.model/JCL/jclMin13src.zip b/org.eclipse.jdt.core.tests.model/JCL/jclMin13src.zip Binary files differnew file mode 100644 index 0000000000..bf05b46301 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/JCL/jclMin13src.zip diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java index 85ad17c350..07a741c847 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java @@ -3318,7 +3318,7 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { } } else if ("13".equals(compliance)) { if (JavaCore.getClasspathVariable("JCL13_LIB") == null) { - setupExternalJCL("jclMin12"); // No need for an explicit jclmin13, just use the same old one. + setupExternalJCL("jclMin13"); // No need for an explicit jclmin13, just use the same old one. JavaCore.setClasspathVariables( new String[] {"JCL13_LIB", "JCL13_SRC", "JCL_SRCROOT"}, new IPath[] {getExternalJCLPath("13"), getExternalJCLSourcePath("13"), getExternalJCLRootSourcePath()}, diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/RunCompletionModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/RunCompletionModelTests.java index 348ad9ccb1..992ad6f1bb 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/RunCompletionModelTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/RunCompletionModelTests.java @@ -41,7 +41,6 @@ public class RunCompletionModelTests extends junit.framework.TestCase { COMPLETION_SUITES.add(CompletionTests9.class); COMPLETION_SUITES.add(CompletionTests10.class); COMPLETION_SUITES.add(CompletionTests11.class); - COMPLETION_SUITES.add(CompletionTests12.class); COMPLETION_SUITES.add(CompletionContextTests.class); COMPLETION_SUITES.add(CompletionContextTests_1_5.class); COMPLETION_SUITES.add(CompletionWithMissingTypesTests.class); diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java index 38eaad8ce3..3ae6696750 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java @@ -7,6 +7,10 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. * * Contributors: * IBM Corporation - initial API and implementation @@ -99,6 +103,7 @@ public class CompletionParser extends AssistParser { protected static final int K_AFTER_NAME_IN_PROVIDES_STATEMENT = COMPLETION_PARSER + 49; protected static final int K_AFTER_WITH_IN_PROVIDES_STATEMENT = COMPLETION_PARSER + 50; protected static final int K_INSIDE_OPENS_STATEMENT = COMPLETION_PARSER + 51; + protected static final int K_YIELD_KEYWORD = COMPLETION_PARSER + 52; public final static char[] FAKE_TYPE_NAME = new char[]{' '}; @@ -1675,6 +1680,14 @@ private boolean checkInstanceofKeyword() { } return false; } +private boolean checkYieldKeyword() { + // Clients to ensure that we are already inside a method + char[] id = this.scanner.getCurrentIdentifierSource(); + if(id.length > 0 && CharOperation.prefixEquals(id, Keywords.YIELD)) { + return true; + } + return false; +} /** * Checks if the completion is inside a method invocation or a constructor invocation. * Returns whether we found a completion node. @@ -3798,6 +3811,12 @@ protected void consumePushPosition() { } } @Override +protected void consumeSwitchLabeledBlock() { + popUntilElement(K_SWITCH_LABEL); + popElement(K_SWITCH_LABEL); + concatNodeLists(); +} +@Override protected void consumeToken(int token) { if(this.isFirst) { super.consumeToken(token); @@ -3826,6 +3845,10 @@ protected void consumeToken(int token) { break; case TokenNameLBRACE: popElement(K_BETWEEN_NEW_AND_LEFT_BRACKET); + if(topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) == K_SWITCH_LABEL + && previous == TokenNameARROW) { + pushOnElementStack(K_SWITCH_EXPRESSION_DELIMITTER); + } break; case TokenNameLBRACKET: if(topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) == K_BETWEEN_NEW_AND_LEFT_BRACKET) { @@ -3916,6 +3939,16 @@ protected void consumeToken(int token) { case TokenNameIdentifier: if (this.inReferenceExpression) break; + if (this.scanner.previewEnabled && isInsideSwitch() && checkYieldKeyword()) { + pushOnElementStack(K_YIELD_KEYWORD); + // Take the short cut here. + // Instead of injecting the TokenNameRestrictedIdentifierYield, totally ignore it + // and let completion take it course. We will not be constructing the + // YieldStatement and thus not producing accurate completion, but completion doesn't have + // enough information anyway about the LHS anyway. + token = this.currentToken = this.getNextToken(); + super.consumeToken(this.currentToken); + } if (previous == TokenNameDOT) { // e.g. foo().[fred]() if (this.invocationType != SUPER_RECEIVER // e.g. not super.[fred]() && this.invocationType != NAME_RECEIVER // e.g. not bar.[fred]() @@ -4321,6 +4354,7 @@ protected void consumeToken(int token) { pushOnElementStack(K_BETWEEN_FOR_AND_RIGHT_PAREN, this.bracketDepth); break; case TokenNameswitch: + popElement(K_LOCAL_INITIALIZER_DELIMITER); pushOnElementStack(K_BETWEEN_SWITCH_AND_RIGHT_PAREN, this.bracketDepth); break; case TokenNamesynchronized: @@ -4864,7 +4898,8 @@ public NameReference createSingleAssistNameReference(char[] assistName, long pos // https://bugs.eclipse.org/bugs/show_bug.cgi?id=269493: Keywords are not proposed in a for // loop without block. Completion while at K_CONTROL_STATEMENT_DELIMITER case needs to handled // similar to the K_BLOCK_DELIMITER with minor differences. - if(kind == K_BLOCK_DELIMITER || kind == K_CONTROL_STATEMENT_DELIMITER || kind == K_LAMBDA_EXPRESSION_DELIMITER) { + if(kind == K_BLOCK_DELIMITER || kind == K_CONTROL_STATEMENT_DELIMITER || kind == K_LAMBDA_EXPRESSION_DELIMITER + || kind == K_SWITCH_EXPRESSION_DELIMITTER) { if(this.canBeExplicitConstructor == YES) { canBeExplicitConstructorCall = true; } @@ -4906,6 +4941,9 @@ public NameReference createSingleAssistNameReference(char[] assistName, long pos if(isInsideBreakable()) { keywords[count++]= Keywords.BREAK; } + if(isInsideSwitch()) { + keywords[count++]= Keywords.YIELD; + } } else if (kind == K_BETWEEN_FOR_AND_RIGHT_PAREN) { if (this.options.complianceLevel >= ClassFileConstants.JDK10) { keywords[count++]= Keywords.VAR; @@ -4920,13 +4958,16 @@ public NameReference createSingleAssistNameReference(char[] assistName, long pos keywords[count++]= Keywords.TRUE; keywords[count++]= Keywords.FALSE; keywords[count++]= Keywords.NULL; - + if (kind == K_YIELD_KEYWORD) { + keywords[count++]= Keywords.YIELD; + } if(kind == K_SWITCH_LABEL) { if(topKnownElementInfo(COMPLETION_OR_ASSIST_PARSER) != DEFAULT) { keywords[count++]= Keywords.DEFAULT; } keywords[count++]= Keywords.BREAK; keywords[count++]= Keywords.CASE; + keywords[count++]= Keywords.YIELD; if (this.options.complianceLevel >= ClassFileConstants.JDK1_4) { keywords[count++]= Keywords.ASSERT; } @@ -5124,9 +5165,7 @@ protected NameReference getUnspecifiedReference(boolean rejectTypeAnnotations) { @Override protected void consumePostfixExpression() { // PostfixExpression ::= Name - if(this.topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) == K_INSIDE_BREAK_STATEMENT) { - // Do nothing, just let checkLabelStatement() do the job - } else { + if (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) != K_YIELD_KEYWORD) { super.consumePostfixExpression(); } } @@ -5231,24 +5270,11 @@ protected boolean isInsideAnnotation() { return false; } -protected boolean isIndirectlyInsideBlock(){ - int i = this.elementPtr; - while(i > -1) { - if(this.elementKindStack[i] == K_BLOCK_DELIMITER) - return true; - i--; - } - return false; -} - -protected boolean isInsideBlock(){ +protected boolean isInsideSwitch(){ int i = this.elementPtr; while(i > -1) { switch (this.elementKindStack[i]) { - case K_TYPE_DELIMITER : return false; - case K_METHOD_DELIMITER : return false; - case K_FIELD_INITIALIZER_DELIMITER : return false; - case K_BLOCK_DELIMITER : return true; + case K_SWITCH_LABEL : return true; } i--; } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/AssistParser.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/AssistParser.java index 99804638db..fa1531dedb 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/AssistParser.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/AssistParser.java @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -104,6 +108,7 @@ public abstract class AssistParser extends Parser { protected static final int K_ENUM_CONSTANT_DELIMITER = ASSIST_PARSER + 6; // whether we are inside a field initializer protected static final int K_LAMBDA_EXPRESSION_DELIMITER = ASSIST_PARSER + 7; // whether we are inside a lambda expression protected static final int K_MODULE_INFO_DELIMITER = ASSIST_PARSER + 8; // whether we are inside a module info declaration + protected static final int K_SWITCH_EXPRESSION_DELIMITTER = ASSIST_PARSER + 9; // whether we are inside a switch expression // selector constants protected static final int THIS_CONSTRUCTOR = -1; @@ -1281,11 +1286,8 @@ private void adjustBracket(int token) { break; } } -private boolean lastArrowAssociatedWithCase = false; @Override protected void consumeToken(int token) { - if (TokenNameARROW == token) - this.lastArrowAssociatedWithCase = this.caseFlagSet; // remember the arrow association before reset. super.consumeToken(token); if(this.isFirst) { @@ -1318,9 +1320,10 @@ protected void consumeToken(int token) { } break; case TokenNameLBRACE: - if (this.previousToken == TokenNameARROW && !this.lastArrowAssociatedWithCase) { + if (this.previousToken == TokenNameARROW) { popElement(K_LAMBDA_EXPRESSION_DELIMITER); - pushOnElementStack(K_LAMBDA_EXPRESSION_DELIMITER, BLOCK_BODY, this.previousObjectInfo); + if (topKnownElementKind(ASSIST_PARSER, 1) != K_SWITCH_EXPRESSION_DELIMITTER) + pushOnElementStack(K_LAMBDA_EXPRESSION_DELIMITER, BLOCK_BODY, this.previousObjectInfo); } break; } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/Keywords.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/Keywords.java index 2fb6ab568b..ba10f2c3d8 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/Keywords.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/impl/Keywords.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,7 +7,11 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Jesper Steen Møller - Contributions for @@ -22,6 +26,7 @@ public interface Keywords { char[] ASSERT = "assert".toCharArray(); //$NON-NLS-1$ char[] BREAK = "break".toCharArray(); //$NON-NLS-1$ char[] CASE = "case".toCharArray(); //$NON-NLS-1$ + char[] YIELD = "yield".toCharArray(); //$NON-NLS-1$ char[] CATCH = "catch".toCharArray(); //$NON-NLS-1$ char[] CLASS = "class".toCharArray(); //$NON-NLS-1$ char[] CONTINUE = "continue".toCharArray(); //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java index 8fd2e97c7b..0e8962bab7 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java @@ -32,8 +32,8 @@ public class YieldStatement extends BranchStatement { */ public boolean isImplicit; -public YieldStatement(Expression exp, int sourceStart, int e) { - super(null, sourceStart, e); +public YieldStatement(Expression exp, int sourceStart, int sourceEnd) { + super(null, sourceStart, sourceEnd); this.expression = exp; } @Override diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index 80d94bc51b..bda6400384 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -9794,7 +9794,7 @@ protected void consumeToken(int type) { //System.out.println(this.scanner.toStringAction(type));
switch (type) {
case TokenNameARROW:
- if (!this.caseFlagSet && this.scanner.lookBack[0] != TokenNamedefault)
+ if (!this.caseFlagSet && this.scanner.lookBack[0] != TokenNamedefault)
consumeLambdaHeader();
this.caseFlagSet = false;
break;
diff --git a/org.eclipse.jdt.tests.latestBREE/META-INF/MANIFEST.MF b/org.eclipse.jdt.tests.latestBREE/META-INF/MANIFEST.MF index 8ea3f60702..527baf4f5a 100644 --- a/org.eclipse.jdt.tests.latestBREE/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.tests.latestBREE/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Require-Bundle: org.junit;bundle-version="3.8.1", org.eclipse.core.resources;bundle-version="[3.2.0,4.0.0)", org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional, org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional, + org.eclipse.jdt.core.tests.model;bundle-version="[3.10.0,4.0.0)", org.eclipse.jdt.core.tests.compiler;bundle-version="[3.10.0,4.0.0)", javax.annotation Bundle-RequiredExecutionEnvironment: JavaSE-13 diff --git a/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/compiler/regression/latest/TestAll.java b/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/compiler/regression/latest/TestAll.java index e8443b284a..cdf4712cb7 100644 --- a/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/compiler/regression/latest/TestAll.java +++ b/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/compiler/regression/latest/TestAll.java @@ -3,6 +3,7 @@ package org.eclipse.jdt.core.tests.compiler.regression.latest; import java.util.ArrayList; import org.eclipse.jdt.core.tests.junit.extension.TestCase; +import org.eclipse.jdt.core.tests.model.CompletionTests13; import org.eclipse.jdt.core.tests.util.AbstractCompilerTest; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; @@ -20,6 +21,7 @@ public static Test suite() { // Common test suites ArrayList standardTests = new ArrayList(); standardTests.add(TextBlockTest.class); + standardTests.add(CompletionTests13.class); TestSuite all = new TestSuite(TestAll.class.getName()); int possibleComplianceLevels = AbstractCompilerTest.getPossibleComplianceLevels(); if ((possibleComplianceLevels & AbstractCompilerTest.F_13) != 0) { diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests12.java b/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/model/CompletionTests13.java index ea0370267a..844da06fe8 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests12.java +++ b/org.eclipse.jdt.tests.latestBREE/src/org/eclipse/jdt/core/tests/model/CompletionTests13.java @@ -14,28 +14,30 @@ package org.eclipse.jdt.core.tests.model; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.internal.codeassist.RelevanceConstants; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import junit.framework.Test; -public class CompletionTests12 extends AbstractJavaModelCompletionTests { +@SuppressWarnings("preview") +public class CompletionTests13 extends AbstractJavaModelCompletionTests { static { -// TESTS_NAMES = new String[]{"test018e"}; +// TESTS_NAMES = new String[]{"test034"}; } - public CompletionTests12(String name) { + public CompletionTests13(String name) { super(name); } public void setUpSuite() throws Exception { if (COMPLETION_PROJECT == null) { - COMPLETION_PROJECT = setUpJavaProject("Completion", "12"); + COMPLETION_PROJECT = setUpJavaProject("Completion", "13"); } else { - setUpProjectCompliance(COMPLETION_PROJECT, "12"); + setUpProjectCompliance(COMPLETION_PROJECT, "13"); } super.setUpSuite(); } public static Test suite() { - return buildModelTestSuite(CompletionTests12.class); + return buildModelTestSuite(CompletionTests13.class); } public void test001() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; @@ -223,18 +225,21 @@ public class CompletionTests12 extends AbstractJavaModelCompletionTests { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( "/Completion/src/Switch.java", - "public class Switch {\n" + - " public static void bar(MyDay day) {\n" + - " switch (day.toS) {\n" + - " case \"SATURDAY\" -> \n" + - " System.out.println(day.toString());\n" + - " }\n" + - " }\n" + - " public static void main(String[] args) {\n" + - " bar(MyDay.SUNDAY);\n" + - " }\n" + - "}\n" + - "enum MyDay { SATURDAY, SUNDAY}"); + """ + public class Switch { + public static void bar(MyDay day) { + switch (day.toS) { + case "SATURDAY" -> + System.out.println(day.toString()); + } + } + public static void main(String[] args) { + bar(MyDay.SUNDAY); + } + } + enum MyDay { SATURDAY, SUNDAY} + """ + ); CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); requestor.allowAllRequiredProposals(); String str = this.workingCopies[0].getSource(); @@ -449,30 +454,31 @@ public class CompletionTests12 extends AbstractJavaModelCompletionTests { "switch[KEYWORD]{switch, null, null, switch, null, 49}", requestor.getResults()); } - public void _test015() throws JavaModelException { + public void test015() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( "/Completion/src/X.java", - "public class Switch {\n" + - " public static void bar(int arg0) {\n" + - " foo(\n" + - " switch (0 + arg0) {\n" + - " case 1 -> {break ar;}\n" + - " default -> 0;\n" + - " }\n" + - " });\n" + - " public static void foo(int arg0) {\n" + - " bar(MyDay.SUNDAY);\n" + - " }\n" + - "}\n"); + """ + public class Switch { + public static void bar(int arg0) { + pointer: foo( + switch (0 + arg0) { + case 1 -> {break ;} + default -> 0; + } + }); + public static void foo(int arg0) { + bar(MyDay.SUNDAY); + } + }"""); CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); requestor.allowAllRequiredProposals(); String str = this.workingCopies[0].getSource(); - String completeBehind = "break ar"; + String completeBehind = "break "; int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); assertResults( - "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + "pointer[LABEL_REF]{pointer, null, null, pointer, null, 49}", requestor.getResults()); } public void test016() throws JavaModelException { @@ -501,34 +507,35 @@ public class CompletionTests12 extends AbstractJavaModelCompletionTests { "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", requestor.getResults()); } - public void _test017() throws JavaModelException { + public void test017() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( "/Completion/src/X.java", - "public class Switch {\n" + - " public static void bar(int arg0) {\n" + - " int arg1 = 0;\n" + - " foo(\n" + - " switch (0 + arg0) {\n" + - " case 1 -> 1;\n" + - " default -> {break ar;}\n" + - " }\n" + - " });\n" + - " public static void foo(int arg0) {\n" + - " bar(MyDay.SUNDAY);\n" + - " }\n" + - "}\n"); + """ + public class Switch { + public static void bar(int arg0) { + int arg1 = 0; + pointer: foo( + switch (0 + arg0) { + case 1 -> 1; + default -> {break p;} + } + }); + public static void foo(int arg0) { + bar(MyDay.SUNDAY); + } + }"""); CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); requestor.allowAllRequiredProposals(); String str = this.workingCopies[0].getSource(); - String completeBehind = "break ar"; + String completeBehind = "break p"; int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); assertResults( - "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}", + "pointer[LABEL_REF]{pointer, null, null, pointer, null, 49}", requestor.getResults()); } - public void _test017a() throws JavaModelException { + public void test017a() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( "/Completion/src/X.java", @@ -551,7 +558,6 @@ public class CompletionTests12 extends AbstractJavaModelCompletionTests { int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); assertResults( - "arg0[LOCAL_VARIABLE_REF]{arg0, null, I, arg0, null, 52}\n" + "argLabel[LABEL_REF]{argLabel, null, null, argLabel, null, 49}", requestor.getResults()); } @@ -853,4 +859,697 @@ public class CompletionTests12 extends AbstractJavaModelCompletionTests { "hashCode[METHOD_REF]{hashCode, Ljava.lang.Object;, ()I, hashCode, null, 90}", requestor.getResults()); } + /* + * Try completion for break keyword inside switch expression - negative + */ + public void test022() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " static final String MONDAY = \"MONDAY\";\n" + + " static final String TUESDAY = \"TUESDAY\";\n" + + " static final String WEDNESDAY = \"WEDNESDAY\";\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String[] args) {\n" + + " String day = \"MONDAY\";\n" + + " int k = switch (day) {\n" + + " case MONDAY -> throw new NullPointerException();\n" + + " case TUESDAY -> 1;\n" + + " case WEDNESDAY -> {br;}\n" + + " default -> {\n" + + " int g = day.hashCode();\n" + + " int result = f(g);\n" + + " yield result;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "case WEDNESDAY -> {br"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "break[KEYWORD]{break, null, null, break, null, 49}", + requestor.getResults()); + } + /* + * Try completion for yield keyword - positive + */ + public void test023() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String[] args) { + String day = "MONDAY"; + int k = switch (day) { + case "M" -> throw new NullPointerException(); + case "T" -> 1; + case "W" -> {yi ;} + default -> { + int g = day.hashCode(); + int result = f(g); + yield result; + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "case \"W\" -> {yi"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "yield[KEYWORD]{yield, null, null, yield, null, 49}", + requestor.getResults()); + } + /* + * Try completion for yield with identifier - positive + */ + public void test024() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String day) {\n" + + " int k = switch (day) {\n" + + " default -> {\n" + + " int g = day.hashCode();\n" + + " int result = f(g);\n" + + " yield res;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield res"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "result[LOCAL_VARIABLE_REF]{result, null, I, result, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion for yield with identifier with a preceding case with yield - positive + */ + public void test024a() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String day) {\n" + + " int k = switch (day) {\n" + + " case TUESDAY -> { yield 1;}\n" + + " default -> {\n" + + " int g = day.hashCode();\n" + + " int result = f(g);\n" + + " yield res;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield res"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "result[LOCAL_VARIABLE_REF]{result, null, I, result, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion for yield with identifier with a preceding case with yield - positive + */ + public void test024b() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String day) {\n" + + " int k = switch (day) {\n" + + " case TUESDAY -> { yield 1;}\n" + + " default -> {\n" + + " int g = day.hashCode();\n" + + " int result = f(g);\n" + + " yield 0 + res;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield 0 + res"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "result[LOCAL_VARIABLE_REF]{result, null, I, result, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion in a switch expression case block without any assist keyword + */ + public void test024c() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String day) {\n" + + " int k = switch (day) {\n" + + " case TUESDAY -> { yield 1;}\n" + + " default -> {\n" + + " int[] g = new int[0];\n" + + " yield g.;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield g."; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "length[FIELD_REF]{length, [I, I, length, null, 49}\n" + + "clone[METHOD_REF]{clone(), [I, ()[I, clone, null, 60}\n" + + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), 60}\n" + + "finalize[METHOD_REF]{finalize(), Ljava.lang.Object;, ()V, finalize, null, 60}\n" + + "getClass[METHOD_REF]{getClass(), Ljava.lang.Object;, ()Ljava.lang.Class<+Ljava.lang.Object;>;, getClass, null, 60}\n" + + "hashCode[METHOD_REF]{hashCode(), Ljava.lang.Object;, ()I, hashCode, null, 60}\n" + + "notify[METHOD_REF]{notify(), Ljava.lang.Object;, ()V, notify, null, 60}\n" + + "notifyAll[METHOD_REF]{notifyAll(), Ljava.lang.Object;, ()V, notifyAll, null, 60}\n" + + "toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, toString, null, 60}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, ()V, wait, null, 60}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, (J)V, wait, (millis), 60}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, (JI)V, wait, (millis, nanos), 60}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion in a switch expression case block without any assist keyword + */ + public void test024d() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String[] args) {\n" + + " String day = args[0];\n" + + " int k = switch (day) {\n" + + " case TUESDAY -> { yield 1;}\n" + + " default -> {\n" + + " int[] g = args.;\n" + + " yield g;\n" + + " }};\n" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "int[] g = args."; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "length[FIELD_REF]{length, [Ljava.lang.String;, I, length, null, 49}\n" + + "finalize[METHOD_REF]{finalize(), Ljava.lang.Object;, ()V, finalize, null, 55}\n" + + "notify[METHOD_REF]{notify(), Ljava.lang.Object;, ()V, notify, null, 55}\n" + + "notifyAll[METHOD_REF]{notifyAll(), Ljava.lang.Object;, ()V, notifyAll, null, 55}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, ()V, wait, null, 55}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, (J)V, wait, (millis), 55}\n" + + "wait[METHOD_REF]{wait(), Ljava.lang.Object;, (JI)V, wait, (millis, nanos), 55}\n" + + "clone[METHOD_REF]{clone(), [Ljava.lang.String;, ()[Ljava.lang.String;, clone, null, 60}\n" + + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), 60}\n" + + "getClass[METHOD_REF]{getClass(), Ljava.lang.Object;, ()Ljava.lang.Class<+Ljava.lang.Object;>;, getClass, null, 60}\n" + + "hashCode[METHOD_REF]{hashCode(), Ljava.lang.Object;, ()I, hashCode, null, 60}\n" + + "toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, toString, null, 60}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion for yield with value inside switch - positive + * + */ + public void test025() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + "public class X {\n" + + " static final String MONDAY = \"MONDAY\";\n" + + " static final String TUESDAY = \"TUESDAY\";\n" + + " static final String WEDNESDAY = \"WEDNESDAY\";\n" + + " @SuppressWarnings(\"preview\")\n" + + " public static void main(String[] args) {\n" + + " resolve: {" + + " String day = \"MONDAY\";\n" + + " int k = switch (day) {\n" + + " case MONDAY -> throw new NullPointerException();\n" + + " case TUESDAY -> 1;\n" + + " case WEDNESDAY -> {yield 10;}\n" + + " default -> {\n" + + " yield day.h;\n" + + " }};\n" + + " }" + + " }\n" + + " static int f(int k) {\n" + + " return k*k;\n" + + " }\n" + + "}"); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield day.h"; + int cursorLocation = str.indexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "hashCode[METHOD_REF]{hashCode(), Ljava.lang.Object;, ()I, hashCode, null, 60}", + requestor.getResults()); + } + /* + * Try completion for yield keyword - inside nested block + */ + public void test026() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String day) { + int k = switch (day) { + case "W" -> {yield 2;} + default -> { + int g = day.hashCode(); + int result = f(g); + { + yie; + } + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yie"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "yield[KEYWORD]{yield, null, null, yield, null, 49}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion for yield - inside nested block + */ + public void test027() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String day) { + int k = switch (day) { + case "W" -> {yield 2;} + default -> { + int g = day.hashCode(); + int result = f(g); + { + yield res; + } + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield res"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "result[LOCAL_VARIABLE_REF]{result, null, I, result, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion for yield - inside nested block + */ + public void test028() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String day) { + int k = switch (day) { + case "W" -> {yield 2;} + default -> { + int g = day.hashCode(); + int result = f(g); + { + for(int i = 0; i < 3; i++) { + if (i == 0) { + yield day.h + } + } + yield 0; + } + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield day.h"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "hashCode[METHOD_REF]{hashCode(), Ljava.lang.Object;, ()I, hashCode, null, 60}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion of other keywords inside switch expression nested block + */ + public void test029() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String day) { + int k = switch (day) { + case "W" -> {yield 2;} + default -> { + int g = day.hashCode(); + int result = f(g); + { + for(int i = 0; i < 3; i++) { + if (i == 0) { + thr + } + } + yield 0; + } + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "thr"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "Throwable[TYPE_REF]{Throwable, java.lang, Ljava.lang.Throwable;, null, null, 42}\n" + + "throw[KEYWORD]{throw, null, null, throw, null, 49}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion of other keywords inside switch expression nested block + */ + public void test030() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String day) { + int k = switch (day) { + case "W" -> {yield 2;} + default -> { + int g = day.hashCode(); + int result = f(g); + { + for(int i = 0; i < 3; i++) { + if (i == 0) { + throw Exc + } + } + yield 0; + } + }}; + } + static int f(int k) { + return k*k; + } + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "throw Exc"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "Exception[TYPE_REF]{Exception, java.lang, Ljava.lang.Exception;, null, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion inside a lambda nested inside a switch expression + */ + public void test031() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String[] args) { + String day = ""; + int i = switch (day) { + default -> { + for (int j = 0; j < 3; j++) { + if (j == 0) { + IntPredicate pre = (_value) -> !test(_v); + }; + } + } + yield 0; + } + }; + } + private static boolean test(int value) { + return false; + } + } + interface IntPredicate { + boolean test(int value); + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "test(_v"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "_value[LOCAL_VARIABLE_REF]{_value, null, I, _value, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion inside a lambda (block) nested inside a switch expression + */ + public void test032() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String[] args) { + String day = ""; + int i = switch (day) { + default -> { + for (int j = 0; j < 3; j++) { + if (j == 0) { + IntPredicate pre = (_value) -> { + return !test(_v); + }; + } + } + yield 0; + } + }; + } + private static boolean test(int value) { + return false; + } + } + interface IntPredicate { + boolean test(int value); + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "test(_v"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "_value[LOCAL_VARIABLE_REF]{_value, null, I, _value, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion inside a switch expression nested inside a lambda expression + */ + public void test033() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String[] args) { + IntPredicate pre = (_value) -> !test( + switch (_value) { + default -> { + yield _v; + } + } + ); + } + private static boolean test(int value) { + return false; + } + } + interface IntPredicate { + boolean test(int value); + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield _v"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "_value[LOCAL_VARIABLE_REF]{_value, null, I, _value, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } + /* + * Try completion inside a switch expression nested inside a lambda expression + */ + public void test034() throws JavaModelException { + String old = COMPLETION_PROJECT.getOption(CompilerOptions.OPTION_EnablePreviews, true); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/X.java", + """ + public class X { + @SuppressWarnings("preview") + public static void main(String[] args) { + IntPredicate pre = (_value) -> { + return !test( + switch (_value) { + default -> { + yield _v; + } + } + ); + }; + } + private static boolean test(int value) { + return false; + } + } + interface IntPredicate { + boolean test(int value); + } + """); + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "yield _v"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "_value[LOCAL_VARIABLE_REF]{_value, null, I, _value, null, 52}", + requestor.getResults()); + COMPLETION_PROJECT.setOption(CompilerOptions.OPTION_EnablePreviews, old); + } } |