fix some missing keywords in java completion (IDEA-175673, IDEA-175669)

This commit is contained in:
peter
2017-07-11 21:52:24 +02:00
parent 4f7634cae6
commit e44da4387a
4 changed files with 18 additions and 3 deletions
@@ -38,6 +38,7 @@ import com.intellij.util.Consumer;
import com.intellij.util.ObjectUtils;
import com.intellij.util.ProcessingContext;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.JBIterable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -152,7 +153,7 @@ public class JavaKeywordCompletion {
private static final ElementPattern<PsiElement> EXPR_KEYWORDS = and(
psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(
not(
or(psiElement(PsiTypeCastExpression.class),
or(
psiElement(PsiSwitchLabelStatement.class),
psiElement(PsiExpressionStatement.class),
psiElement(PsiPrefixExpression.class)
@@ -193,12 +194,16 @@ public class JavaKeywordCompletion {
mySession = session;
myPrefix = session.getMatcher().getPrefix();
myPosition = parameters.getPosition();
myPrevLeaf = PsiTreeUtil.prevVisibleLeaf(myPosition);
myPrevLeaf = prevSignificantLeaf(myPosition);
addKeywords();
addEnumCases();
}
private static PsiElement prevSignificantLeaf(PsiElement position) {
return JBIterable.generate(position, PsiTreeUtil::prevVisibleLeaf).skip(1).skipWhile(e -> e instanceof PsiComment).first();
}
private void addKeyword(LookupElement element) {
if (element.getLookupString().startsWith(myPrefix)) {
myResults.add(element);
@@ -617,7 +622,7 @@ public class JavaKeywordCompletion {
return false;
}
PsiElement prev = PsiTreeUtil.prevVisibleLeaf(position);
PsiElement prev = prevSignificantLeaf(position);
if (prev == null) {
return true;
}
@@ -0,0 +1,2 @@
//comment
<caret>package
@@ -0,0 +1,6 @@
class Foo {
Object test() {
return (CharSequence) n<caret>
}
}
@@ -45,6 +45,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
}
public void testFileScope1() { doTest(8, FILE_SCOPE_KEYWORDS); }
public void testFileScopeAfterComment() { doTest(4, "package", "class", "import", "public", "private"); }
public void testFileScope2() { doTest(7, CLASS_SCOPE_KEYWORDS); }
public void testClassScope1() { doTest(5, CLASS_SCOPE_KEYWORDS); }
public void testClassScope2() { doTest(4, CLASS_SCOPE_KEYWORDS); }
@@ -104,6 +105,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testNullInMethodCall() { doTest(); }
public void testNullInMethodCall2() { doTest(); }
public void testNewInMethodRefs() { doTest(1, "new", "null", "true", "false"); }
public void testNewInCast() { doTest(2, "new", "null", "true", "false"); }
public void testSpaceAfterInstanceof() { doTest(); }
public void testInstanceofAfterUnresolved() { doTest(1, "instanceof"); }
public void testInstanceofAfterStatementStart() { doTest(1, "instanceof"); }