KT-5876 Code completion should never auto-insert item which requires adding import
#KT-5876 Fixed
This commit is contained in:
@@ -20,23 +20,47 @@ import com.intellij.codeInsight.lookup.LookupElement
|
||||
import org.jetbrains.jet.plugin.project.TargetPlatform
|
||||
import org.junit.Assert
|
||||
import org.jetbrains.jet.completion.ExpectedCompletionUtils
|
||||
import com.intellij.codeInsight.CodeInsightSettings
|
||||
|
||||
fun testCompletion(fileText: String, platform: TargetPlatform?, complete: (Int) -> Array<LookupElement>?, defaultInvocationCount: Int = 0) {
|
||||
val invocationCount = ExpectedCompletionUtils.getInvocationCount(fileText) ?: defaultInvocationCount
|
||||
val items = complete(invocationCount) ?: array()
|
||||
testWithAutoCompleteSetting(fileText) {
|
||||
val invocationCount = ExpectedCompletionUtils.getInvocationCount(fileText) ?: defaultInvocationCount
|
||||
val items = complete(invocationCount) ?: array()
|
||||
|
||||
ExpectedCompletionUtils.assertDirectivesValid(fileText)
|
||||
ExpectedCompletionUtils.assertDirectivesValid(fileText)
|
||||
|
||||
val expected = ExpectedCompletionUtils.itemsShouldExist(fileText, platform)
|
||||
val unexpected = ExpectedCompletionUtils.itemsShouldAbsent(fileText, platform)
|
||||
val itemsNumber = ExpectedCompletionUtils.getExpectedNumber(fileText, platform)
|
||||
val expected = ExpectedCompletionUtils.itemsShouldExist(fileText, platform)
|
||||
val unexpected = ExpectedCompletionUtils.itemsShouldAbsent(fileText, platform)
|
||||
val itemsNumber = ExpectedCompletionUtils.getExpectedNumber(fileText, platform)
|
||||
|
||||
Assert.assertTrue("Should be some assertions about completion", expected.size != 0 || unexpected.size != 0 || itemsNumber != null)
|
||||
ExpectedCompletionUtils.assertContainsRenderedItems(expected, items, ExpectedCompletionUtils.isWithOrder(fileText))
|
||||
ExpectedCompletionUtils.assertNotContainsRenderedItems(unexpected, items)
|
||||
Assert.assertTrue("Should be some assertions about completion", expected.size != 0 || unexpected.size != 0 || itemsNumber != null)
|
||||
ExpectedCompletionUtils.assertContainsRenderedItems(expected, items, ExpectedCompletionUtils.isWithOrder(fileText))
|
||||
ExpectedCompletionUtils.assertNotContainsRenderedItems(unexpected, items)
|
||||
|
||||
if (itemsNumber != null) {
|
||||
val expectedItems = ExpectedCompletionUtils.listToString(ExpectedCompletionUtils.getItemsInformation(items))
|
||||
Assert.assertEquals("Invalid number of completion items: ${expectedItems}", itemsNumber, items.size)
|
||||
if (itemsNumber != null) {
|
||||
val expectedItems = ExpectedCompletionUtils.listToString(ExpectedCompletionUtils.getItemsInformation(items))
|
||||
Assert.assertEquals("Invalid number of completion items: ${expectedItems}", itemsNumber, items.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun testWithAutoCompleteSetting(fileText: String, doTest: () -> Unit) {
|
||||
val autoComplete = ExpectedCompletionUtils.getAutocompleteSetting(fileText)
|
||||
if (autoComplete == null) {
|
||||
doTest()
|
||||
return
|
||||
}
|
||||
|
||||
val settings = CodeInsightSettings.getInstance()
|
||||
val oldValue1 = settings.AUTOCOMPLETE_ON_CODE_COMPLETION
|
||||
val oldValue2 = settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION
|
||||
try {
|
||||
settings.AUTOCOMPLETE_ON_CODE_COMPLETION = autoComplete
|
||||
settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION = autoComplete
|
||||
doTest()
|
||||
}
|
||||
finally {
|
||||
settings.AUTOCOMPLETE_ON_CODE_COMPLETION = oldValue1
|
||||
settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION = oldValue2
|
||||
}
|
||||
}
|
||||
@@ -115,6 +115,7 @@ public class ExpectedCompletionUtils {
|
||||
|
||||
private static final String INVOCATION_COUNT_PREFIX = "INVOCATION_COUNT:";
|
||||
private static final String WITH_ORDER_PREFIX = "WITH_ORDER:";
|
||||
private static final String AUTOCOMPLETE_SETTING_PREFIX = "AUTOCOMPLETE_SETTING:";
|
||||
|
||||
public static final List<String> KNOWN_PREFIXES = ImmutableList.of(
|
||||
EXIST_LINE_PREFIX,
|
||||
@@ -128,6 +129,7 @@ public class ExpectedCompletionUtils {
|
||||
NUMBER_JAVA_LINE_PREFIX,
|
||||
INVOCATION_COUNT_PREFIX,
|
||||
WITH_ORDER_PREFIX,
|
||||
AUTOCOMPLETE_SETTING_PREFIX,
|
||||
AstAccessControl.INSTANCE$.getALLOW_AST_ACCESS_DIRECTIVE());
|
||||
|
||||
@NotNull
|
||||
@@ -204,6 +206,11 @@ public class ExpectedCompletionUtils {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, INVOCATION_COUNT_PREFIX);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Boolean getAutocompleteSetting(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedBoolean(fileText, AUTOCOMPLETE_SETTING_PREFIX);
|
||||
}
|
||||
|
||||
public static boolean isWithOrder(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, WITH_ORDER_PREFIX) != null;
|
||||
}
|
||||
|
||||
@@ -420,6 +420,12 @@ public class JSBasicCompletionTestGenerated extends AbstractJSBasicCompletionTes
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NoAutoInsertionOfNotImported.kt")
|
||||
public void testNoAutoInsertionOfNotImported() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/NoAutoInsertionOfNotImported.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NoAutoPopupAfterNumberLiteral.kt")
|
||||
public void testNoAutoPopupAfterNumberLiteral() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/NoAutoPopupAfterNumberLiteral.kt");
|
||||
|
||||
@@ -420,6 +420,12 @@ public class JvmBasicCompletionTestGenerated extends AbstractJvmBasicCompletionT
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NoAutoInsertionOfNotImported.kt")
|
||||
public void testNoAutoInsertionOfNotImported() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/NoAutoInsertionOfNotImported.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NoAutoPopupAfterNumberLiteral.kt")
|
||||
public void testNoAutoPopupAfterNumberLiteral() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/NoAutoPopupAfterNumberLiteral.kt");
|
||||
|
||||
@@ -112,6 +112,12 @@ public class MultiFileJvmBasicCompletionTestGenerated extends AbstractMultiFileJ
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NoAutoInsertionOfNotImported")
|
||||
public void testNoAutoInsertionOfNotImported() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/multifile/NoAutoInsertionOfNotImported/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NotImportedExtensionForImplicitReceiver")
|
||||
public void testNotImportedExtensionForImplicitReceiver() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/multifile/NotImportedExtensionForImplicitReceiver/");
|
||||
|
||||
@@ -67,6 +67,10 @@ public class CompletionMultifileHandlerTest extends KotlinCompletionTestCase {
|
||||
|
||||
configureByFiles(null, fileName + "-1.kt", fileName + "-2.kt");
|
||||
complete(2);
|
||||
if (myItems != null) {
|
||||
assertTrue("Multiple items in completion", myItems.length == 1);
|
||||
selectItem(myItems[0]);
|
||||
}
|
||||
checkResultByFile(fileName + ".kt.after");
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,13 @@
|
||||
package org.jetbrains.jet.plugin.quickfix;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.test.InnerTestClasses;
|
||||
import org.jetbrains.jet.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.jetbrains.jet.JUnit3RunnerWithInners;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
Reference in New Issue
Block a user