[java-completion] IDEA-269952 Code completion results in invalid code

GitOrigin-RevId: 5cc2df6d2be5315d0520d44d766a89c45b9e974d
This commit is contained in:
Tagir Valeev
2021-05-28 10:51:13 +00:00
committed by intellij-monorepo-bot
parent 0be177e19e
commit 090b19388c
2 changed files with 70 additions and 0 deletions
@@ -815,6 +815,9 @@ public final class JavaCompletionUtil {
private static boolean insertTail(InsertionContext context, LookupElement item, TailType tailType, boolean hasTail) {
TailType toInsert = tailType;
LookupItem<?> lookupItem = item.as(LookupItem.CLASS_CONDITION_KEY);
if (toInsert == EqTailType.INSTANCE) {
toInsert = TailType.UNKNOWN;
}
if (lookupItem == null || lookupItem.getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN) {
if (!hasTail && item.getObject() instanceof PsiMethod && PsiType.VOID.equals(((PsiMethod)item.getObject()).getReturnType())) {
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
@@ -218,6 +218,7 @@ class NormalCompletionTest extends NormalCompletionTestCase {
protected void tearDown() throws Exception {
CodeInsightSettings.instance.AUTOCOMPLETE_ON_CODE_COMPLETION = true
CodeInsightSettings.instance.setCompletionCaseSensitive(CodeInsightSettings.FIRST_LETTER)
CodeInsightSettings.instance.setSelectAutopopupSuggestionsByChars(false)
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = true
super.tearDown()
}
@@ -2443,4 +2444,70 @@ class Abc {
myFixture.completeBasic()
assert myFixture.getLookupElementStrings() == ["demoEntity", "demo", "entity"]
}
@NeedsIndex.Full
void testCompleteByEqualsAssignment() {
CodeInsightSettings.instance.setSelectAutopopupSuggestionsByChars(true)
myFixture.configureByText("Test.java", "public class Test {\n" +
" public static void main(final String[] args) {\n" +
" Test test = new Test();\n" +
" Test test2 = new Test();\n" +
" tes<caret>\n" +
" }\n" +
"}")
myFixture.completeBasic()
myFixture.type('=')
myFixture.checkResult("public class Test {\n" +
" public static void main(final String[] args) {\n" +
" Test test = new Test();\n" +
" Test test2 = new Test();\n" +
" test = <caret>\n" +
" }\n" +
"}")
}
@NeedsIndex.Full
void testCompleteByEqualsDeclaration() {
CodeInsightSettings.instance.setSelectAutopopupSuggestionsByChars(true)
CodeInsightSettings.instance.AUTOCOMPLETE_ON_CODE_COMPLETION = false
myFixture.configureByText("Test.java", "public class Test {\n" +
" public static void main(final String[] args) {\n" +
" String str<caret>\n" +
" }\n" +
"}")
myFixture.completeBasic()
myFixture.type('=')
myFixture.checkResult("public class Test {\n" +
" public static void main(final String[] args) {\n" +
" String string = <caret>\n" +
" }\n" +
"}")
}
@NeedsIndex.Full
void testCompleteByEquals() {
CodeInsightSettings.instance.setSelectAutopopupSuggestionsByChars(true)
myFixture.configureByText("Test.java", "public class Test {\n" +
" public static void main(final String[] args) {\n" +
" final Test test = new Test();\n" +
" if (test.get<caret>)\n" +
" }\n" +
"\n" +
" public String getFoo() {\n" +
" return \"\";\n" +
" }\n" +
"}")
myFixture.completeBasic()
myFixture.type('=')
myFixture.checkResult("public class Test {\n" +
" public static void main(final String[] args) {\n" +
" final Test test = new Test();\n" +
" if (test.getFoo()=)\n" +
" }\n" +
"\n" +
" public String getFoo() {\n" +
" return \"\";\n" +
" }\n" +
"}")
}
}