From 6342196c311975110811b0a0c0b1e5647faf9009 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 20 Mar 2019 10:11:59 +0100 Subject: [PATCH] IDEA-209230 No `new` keyword completion inside negation --- .../completion/JavaKeywordCompletion.java | 31 +++++++++---------- .../completion/keywords/newInNegation.java | 6 ++++ .../completion/KeywordCompletionTest.java | 3 +- 3 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/keywords/newInNegation.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java index 31cabc7c9369..2ad9a7dc1ce6 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java @@ -95,19 +95,6 @@ public class JavaKeywordCompletion { private static final ElementPattern CLASS_REFERENCE = psiElement().withParent(psiReferenceExpression().referencing(psiClass().andNot(psiElement(PsiTypeParameter.class)))); - private static final ElementPattern EXPR_KEYWORDS = and( - psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent( - not( - or( - psiElement(PsiExpressionStatement.class), - psiElement(PsiPrefixExpression.class) - ) - ) - )), - not(psiElement().afterLeaf(".")), - not(JavaCompletionContributor.IN_SWITCH_LABEL) - ); - private final CompletionParameters myParameters; private final JavaCompletionSession mySession; private final PsiElement myPosition; @@ -429,6 +416,9 @@ public class JavaKeywordCompletion { } if (isExpressionPosition(myPosition)) { + PsiElement parent = myPosition.getParent(); + PsiElement grandParent = parent == null ? null : parent.getParent(); + boolean allowExprKeywords = !(grandParent instanceof PsiExpressionStatement) && !(grandParent instanceof PsiUnaryExpression); if (PsiTreeUtil.getParentOfType(myPosition, PsiAnnotation.class) == null) { if (!statementPosition) { addKeyword(TailTypeDecorator.withTail(createKeyword(PsiKeyword.NEW), TailType.INSERT_SPACE)); @@ -436,9 +426,11 @@ public class JavaKeywordCompletion { addKeyword(new OverridableSpace(createKeyword(PsiKeyword.SWITCH), TailTypes.SWITCH_LPARENTH)); } } - addKeyword(createKeyword(PsiKeyword.NULL)); + if (allowExprKeywords) { + addKeyword(createKeyword(PsiKeyword.NULL)); + } } - if (mayExpectBoolean(myParameters)) { + if (allowExprKeywords && mayExpectBoolean(myParameters)) { addKeyword(createKeyword(PsiKeyword.TRUE)); addKeyword(createKeyword(PsiKeyword.FALSE)); } @@ -597,8 +589,13 @@ public class JavaKeywordCompletion { } private static boolean isExpressionPosition(PsiElement position) { - return EXPR_KEYWORDS.accepts(position) && LabelReferenceCompletion.isBreakValueOrLabelPosition(position) != Boolean.FALSE || - psiElement().insideStarting(psiElement(PsiClassObjectAccessExpression.class)).accepts(position); + if (psiElement().insideStarting(psiElement(PsiClassObjectAccessExpression.class)).accepts(position)) return true; + + PsiElement parent = position.getParent(); + return parent instanceof PsiReferenceExpression && + !((PsiReferenceExpression)parent).isQualified() && + !JavaCompletionContributor.IN_SWITCH_LABEL.accepts(position) && + LabelReferenceCompletion.isBreakValueOrLabelPosition(position) != Boolean.FALSE; } public static boolean isInstanceofPlace(PsiElement position) { diff --git a/java/java-tests/testData/codeInsight/completion/keywords/newInNegation.java b/java/java-tests/testData/codeInsight/completion/keywords/newInNegation.java new file mode 100644 index 000000000000..c9ee9f9a9346 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/newInNegation.java @@ -0,0 +1,6 @@ +class Foo { + + Object test() { + return !n + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/KeywordCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/KeywordCompletionTest.java index 4288e7806491..a744e8a3151e 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/KeywordCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/KeywordCompletionTest.java @@ -29,8 +29,6 @@ import org.jetbrains.annotations.NotNull; public class KeywordCompletionTest extends LightCompletionTestCase { private static final String BASE_PATH = "/codeInsight/completion/keywords/"; - private static final String[] FILE_SCOPE_KEYWORDS = { - "package", "public", "private", "import", "final", "class", "interface", "abstract", "enum", "default", null}; private static final String[] CLASS_SCOPE_KEYWORDS = { "public", "private", "protected", "import", "final", "class", "interface", "abstract", "enum", "default", null}; private static final String[] CLASS_SCOPE_KEYWORDS_2 = { @@ -114,6 +112,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase { public void testNullInMethodCall2() { doTest(); } public void testNewInMethodRefs() { doTest(1, "new", "null", "true", "false"); } public void testNewInCast() { doTest(2, "new", "null", "true", "false"); } + public void testNewInNegation() { doTest(1, "new", "null", "true", "false"); } public void testSpaceAfterInstanceof() { doTest(); } public void testInstanceofAfterUnresolved() { doTest(1, "instanceof"); } public void testInstanceofAfterStatementStart() { doTest(1, "instanceof"); }