Skip to content

Commit

Permalink
Code completion after yield in switch expressions fixed. (#4769)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalek authored Oct 12, 2022
1 parent 0087cff commit 2662296
Show file tree
Hide file tree
Showing 12 changed files with 1,437 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1551,13 +1551,6 @@ private void insideBlock(Env env) throws IOException {
}
localResult(env);
addKeywordsForBlock(env);

if (env.getController().getSourceVersion().compareTo(RELEASE_13) >= 0) {
TreePath parentPath = env.getPath().getParentPath();
if (parentPath.getLeaf().getKind() == Tree.Kind.CASE && parentPath.getParentPath().getLeaf().getKind() == Kind.SWITCH_EXPRESSION) {
addKeyword(env, YIELD_KEYWORD, SPACE, false);
}
}
}

@SuppressWarnings("fallthrough")
Expand Down Expand Up @@ -2400,9 +2393,6 @@ private void insideSwitch(Env env) throws IOException {
}
localResult(env);
addKeywordsForBlock(env);
if (env.getController().getSourceVersion().compareTo(SourceVersion.RELEASE_13) >= 0 && path.getLeaf().getKind() == Kind.SWITCH_EXPRESSION) {
addKeyword(env, YIELD_KEYWORD, SPACE, false);
}
}
} else {
TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, path.getLeaf(), offset);
Expand Down Expand Up @@ -2782,6 +2772,12 @@ private void insideExpressionStatement(Env env) throws IOException {
TreePath tPath = new TreePath(path, t);
if (t.getKind() == Tree.Kind.MODIFIERS) {
insideModifiers(env, tPath);
} else if (t.getKind() == Tree.Kind.IDENTIFIER && YIELD_KEYWORD.contentEquals(((IdentifierTree) t).getName())) {
TreePath sPath = controller.getTreeUtilities().getPathElementOfKind(Tree.Kind.SWITCH_EXPRESSION, path);
if (sPath != null) {
localResult(env);
addValueKeywords(env);
}
} else if (t.getKind() == Tree.Kind.MEMBER_SELECT && ERROR.contentEquals(((MemberSelectTree) t).getIdentifier())) {
controller.toPhase(Phase.ELEMENTS_RESOLVED);
TreePath expPath = new TreePath(tPath, ((MemberSelectTree) t).getExpression());
Expand Down Expand Up @@ -4745,6 +4741,7 @@ private void addKeywordsForBlock(Env env) {
boolean caseAdded = false;
boolean breakAdded = false;
boolean continueAdded = false;
boolean yieldAdded = false;
TreePath tp = env.getPath();
while (tp != null) {
switch (tp.getLeaf().getKind()) {
Expand Down Expand Up @@ -4772,6 +4769,30 @@ private void addKeywordsForBlock(Env env) {
results.add(itemFactory.createKeywordItem(BREAK_KEYWORD, withinLabeledStatement(env) ? null : SEMI, anchorOffset, false));
}
break;
case SWITCH_EXPRESSION:
lastCase = null;
root = env.getRoot();
sourcePositions = env.getSourcePositions();
for (CaseTree t : ((SwitchExpressionTree) tp.getLeaf()).getCases()) {
if (sourcePositions.getStartPosition(root, t) >= env.getOffset()) {
break;
}
lastCase = t;
}
if (!caseAdded && (lastCase == null || lastCase.getExpression() != null)) {
caseAdded = true;
if (Utilities.startsWith(CASE_KEYWORD, prefix)) {
results.add(itemFactory.createKeywordItem(CASE_KEYWORD, SPACE, anchorOffset, false));
}
if (Utilities.startsWith(DEFAULT_KEYWORD, prefix)) {
results.add(itemFactory.createKeywordItem(DEFAULT_KEYWORD, COLON, anchorOffset, false));
}
}
if (!yieldAdded && Utilities.startsWith(YIELD_KEYWORD, prefix)) {
yieldAdded = true;
results.add(itemFactory.createKeywordItem(YIELD_KEYWORD, SPACE, anchorOffset, false));
}
break;
case DO_WHILE_LOOP:
case ENHANCED_FOR_LOOP:
case FOR_LOOP:
Expand Down Expand Up @@ -4830,6 +4851,7 @@ private void addKeywordsForStatement(Env env) {
TreePath tp = env.getPath();
boolean cAdded = false;
boolean bAdded = false;
boolean yAdded = false;
while (tp != null && !(cAdded && bAdded)) {
switch (tp.getLeaf().getKind()) {
case DO_WHILE_LOOP:
Expand All @@ -4846,6 +4868,12 @@ private void addKeywordsForStatement(Env env) {
bAdded = true;
}
break;
case SWITCH_EXPRESSION:
if (!yAdded && Utilities.startsWith(YIELD_KEYWORD, prefix)) {
results.add(itemFactory.createKeywordItem(YIELD_KEYWORD, SPACE, anchorOffset, false));
yAdded = true;
}
break;
}
tp = tp.getParentPath();
}
Expand Down Expand Up @@ -5590,6 +5618,25 @@ private Set<? extends TypeMirror> getSmartTypesImpl(Env env) throws IOException
}
}
return ret;
case SWITCH_EXPRESSION:
SwitchExpressionTree sew = (SwitchExpressionTree) tree;
if (sew.getExpression() != lastTree && sew.getExpression().getKind() != Tree.Kind.ERRONEOUS) {
return null;
}
ret = new HashSet<>();
types = controller.getTypes();
ret.add(controller.getTypes().getPrimitiveType(TypeKind.INT));
te = controller.getElements().getTypeElement("java.lang.Enum"); //NOI18N
if (te != null) {
ret.add(types.getDeclaredType(te));
}
if (controller.getSourceVersion().compareTo(SourceVersion.RELEASE_7) >= 0) {
te = controller.getElements().getTypeElement("java.lang.String"); //NOI18N
if (te != null) {
ret.add(types.getDeclaredType(te));
}
}
return ret;
case METHOD_INVOCATION:
MethodInvocationTree mi = (MethodInvocationTree) tree;
sourcePositions = env.getSourcePositions();
Expand Down Expand Up @@ -5967,6 +6014,23 @@ public boolean accept(Element e, TypeMirror t) {
{
return null;
}
} else if (exp.getKind() == Tree.Kind.ERRONEOUS) {
Iterator<? extends Tree> it = ((ErroneousTree) exp).getErrorTrees().iterator();
if (it.hasNext()) {
Tree t = it.next();
if (t.getKind() == Tree.Kind.IDENTIFIER && YIELD_KEYWORD.contentEquals(((IdentifierTree) t).getName())) {
TreePath sPath = tu.getPathElementOfKind(Tree.Kind.SWITCH_EXPRESSION, path);
if (sPath != null) {
path = sPath;
}
}
}
}
break;
case YIELD:
TreePath sPath = tu.getPathElementOfKind(Tree.Kind.SWITCH_EXPRESSION, path);
if (sPath != null) {
path = sPath;
}
break;
case BLOCK:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
int a
public native int hashCode()
public static final int Integer.BYTES
public static final int Integer.MAX_VALUE
public static final int Integer.MIN_VALUE
public static final int Integer.SIZE
public static int Integer.bitCount(int arg0)
public static int Integer.compare(int arg0, int arg1)
public static int Integer.compareUnsigned(int arg0, int arg1)
public static Integer Integer.decode(String arg0)
public static int Integer.divideUnsigned(int arg0, int arg1)
public static Integer Integer.getInteger(String arg0)
public static Integer Integer.getInteger(String arg0, Integer arg1)
public static Integer Integer.getInteger(String arg0, int arg1)
public static int Integer.hashCode(int arg0)
public static int Integer.highestOneBit(int arg0)
public static int Integer.lowestOneBit(int arg0)
public static int Integer.max(int arg0, int arg1)
public static int Integer.min(int arg0, int arg1)
public static int Integer.numberOfLeadingZeros(int arg0)
public static int Integer.numberOfTrailingZeros(int arg0)
public static int Integer.parseInt(String arg0)
public static int Integer.parseInt(String arg0, int arg1)
public static int Integer.parseUnsignedInt(String arg0)
public static int Integer.parseUnsignedInt(String arg0, int arg1)
public static int Integer.remainderUnsigned(int arg0, int arg1)
public static int Integer.reverse(int arg0)
public static int Integer.reverseBytes(int arg0)
public static int Integer.rotateLeft(int arg0, int arg1)
public static int Integer.rotateRight(int arg0, int arg1)
public static int Integer.signum(int arg0)
public static int Integer.sum(int arg0, int arg1)
public static Integer Integer.valueOf(String arg0)
public static Integer Integer.valueOf(int arg0)
public static Integer Integer.valueOf(String arg0, int arg1)
colors color
protected native Object clone()
public boolean equals(Object arg0)
protected void finalize()
public final native Class<?> getClass()
public final native void notify()
public final native void notifyAll()
public void op(int a)
public String toString()
public final void wait()
public final native void wait(long arg0)
public final void wait(long arg0, int arg1)
boolean
byte
char
double
false
float
int
long
new
null
short
super
this
true
AbstractMethodError
Appendable
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
AssertionError
AutoCloseable
Boolean
BootstrapMethodError
Byte
CharSequence
Character
Class
ClassCastException
ClassCircularityError
ClassFormatError
ClassLoader
ClassNotFoundException
ClassValue
CloneNotSupportedException
Cloneable
Comparable
Compiler
Deprecated
Double
Enum
EnumConstantNotPresentException
Error
Exception
ExceptionInInitializerError
Float
FunctionalInterface
IllegalAccessError
IllegalAccessException
IllegalArgumentException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IncompatibleClassChangeError
IndexOutOfBoundsException
InheritableThreadLocal
InstantiationError
InstantiationException
Integer
InternalError
InterruptedException
Iterable
LinkageError
Long
Math
NegativeArraySizeException
NoClassDefFoundError
NoSuchFieldError
NoSuchFieldException
NoSuchMethodError
NoSuchMethodException
NullPointerException
Number
NumberFormatException
Object
OutOfMemoryError
Override
Package
Process
ProcessBuilder
Readable
ReflectiveOperationException
Runnable
Runtime
RuntimeException
RuntimePermission
SafeVarargs
SecurityException
SecurityManager
Short
StackOverflowError
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
StringIndexOutOfBoundsException
SuppressWarnings
System
Test
Thread
ThreadDeath
ThreadGroup
ThreadLocal
Throwable
TypeNotPresentException
UnknownError
UnsatisfiedLinkError
UnsupportedClassVersionError
UnsupportedOperationException
VerifyError
VirtualMachineError
Void
colors
java
Loading

0 comments on commit 2662296

Please sign in to comment.