Skip to content

Commit a1b1bc7

Browse files
datho7561rgrunber
authored andcommitted
Keyword completion: differentiate between expressions and statements
- Add a bunch more statement-like keywords - Handle completion for `default` in switch statements and expressions Should fix around 43 cases Signed-off-by: David Thompson <[email protected]>
1 parent 2666050 commit a1b1bc7

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,9 +1842,24 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
18421842
suggestDefaultCompletions = false;
18431843
}
18441844
if (context instanceof SwitchStatement || context instanceof SwitchExpression) {
1845+
boolean hasDefault = false;
1846+
if (context instanceof SwitchStatement switchStatement) {
1847+
hasDefault = switchStatement.statements().stream().anyMatch(statement -> {
1848+
return statement instanceof SwitchCase switchCase && switchCase.isDefault();
1849+
});
1850+
} else {
1851+
hasDefault = ((SwitchExpression)context).statements().stream().anyMatch(statement -> {
1852+
return statement instanceof SwitchCase switchCase && switchCase.isDefault();
1853+
});
1854+
}
18451855
if (!this.isFailedMatch(this.prefix.toCharArray(), Keywords.CASE)) {
18461856
this.requestor.accept(createKeywordProposal(Keywords.CASE, -1, -1));
18471857
}
1858+
if (!hasDefault) {
1859+
if (!this.isFailedMatch(this.prefix.toCharArray(), Keywords.DEFAULT)) {
1860+
this.requestor.accept(createKeywordProposal(Keywords.DEFAULT, -1, -1));
1861+
}
1862+
}
18481863
}
18491864
if (context != null && context.getLocationInParent() == QualifiedType.NAME_PROPERTY && context.getParent() instanceof QualifiedType qType) {
18501865
Type qualifier = qType.getQualifier();
@@ -2845,20 +2860,39 @@ private void checkCancelled() {
28452860

28462861
private void statementLikeKeywords() {
28472862
List<char[]> keywords = new ArrayList<>();
2848-
keywords.add(Keywords.ASSERT);
2849-
keywords.add(Keywords.RETURN);
2863+
boolean isExpressionExpected = (this.toComplete.getParent() instanceof IfStatement ifStatement
2864+
&& (IfStatement.EXPRESSION_PROPERTY.getId().equals(this.toComplete.getLocationInParent().getId())
2865+
|| "$missing$".equals(ifStatement.getExpression().toString())));
2866+
if (!isExpressionExpected) {
2867+
keywords.add(Keywords.ASSERT);
2868+
keywords.add(Keywords.RETURN);
2869+
keywords.add(Keywords.DO);
2870+
keywords.add(Keywords.WHILE);
2871+
keywords.add(Keywords.FOR);
2872+
keywords.add(Keywords.IF);
2873+
keywords.add(Keywords.SWITCH);
2874+
keywords.add(Keywords.SYNCHRONIZED);
2875+
keywords.add(Keywords.THROW);
2876+
keywords.add(Keywords.TRY);
2877+
}
28502878
keywords.add(Keywords.SUPER);
28512879
if (DOMCompletionUtil.findParent(this.toComplete,
28522880
new int[] { ASTNode.WHILE_STATEMENT, ASTNode.DO_STATEMENT, ASTNode.FOR_STATEMENT }) != null) {
2853-
keywords.add(Keywords.BREAK);
2854-
keywords.add(Keywords.CONTINUE);
2881+
if (!isExpressionExpected) {
2882+
keywords.add(Keywords.BREAK);
2883+
keywords.add(Keywords.CONTINUE);
2884+
}
28552885
} else if (DOMCompletionUtil.findParent(this.toComplete,
28562886
new int[] { ASTNode.SWITCH_EXPRESSION, ASTNode.SWITCH_STATEMENT }) != null) {
2857-
keywords.add(Keywords.BREAK);
2887+
if (!isExpressionExpected) {
2888+
keywords.add(Keywords.BREAK);
2889+
}
28582890
}
28592891
if (DOMCompletionUtil.findParent(this.toComplete,
28602892
new int[] { ASTNode.SWITCH_EXPRESSION }) != null) {
2861-
keywords.add(Keywords.YIELD);
2893+
if (!isExpressionExpected) {
2894+
keywords.add(Keywords.YIELD);
2895+
}
28622896
}
28632897
Statement statement = (Statement) DOMCompletionUtil.findParent(this.toComplete, new int[] {ASTNode.EXPRESSION_STATEMENT, ASTNode.VARIABLE_DECLARATION_STATEMENT});
28642898
if (statement != null) {

0 commit comments

Comments
 (0)