diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt index f66331389cd..0dba554ed58 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.core +import com.intellij.codeInsight.CodeInsightSettings import com.intellij.openapi.project.Project import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.stubs.StringStubIndexExtension @@ -198,3 +199,10 @@ public class KotlinIndicesHelper( .filter { it.getExtensionReceiverParameter() == null } } } + +public fun isInExcludedPackage(descriptor: DeclarationDescriptor): Boolean { + val fqName = DescriptorUtils.getFqName(descriptor).asString() + + return CodeInsightSettings.getInstance().EXCLUDED_PACKAGES + .any { excluded -> fqName == excluded || fqName.startsWith(excluded + ".") } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AutoImportFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AutoImportFix.kt index d9ae0e1215a..e19b1d325f9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AutoImportFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AutoImportFix.kt @@ -48,6 +48,8 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.utils.CachedValueProperty import java.util.ArrayList +import com.intellij.codeInsight.* +import org.jetbrains.kotlin.idea.core.isInExcludedPackage /** * Check possibility and perform fix for unresolved references. @@ -152,7 +154,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction !isInExcludedPackage(it) } } private fun getClasses(name: String, file: JetFile, searchScope: GlobalSearchScope): Collection diff --git a/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt new file mode 100644 index 00000000000..92f2354e6c0 --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt @@ -0,0 +1,6 @@ +// "class com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase" "false" +// ACTION: Create class 'SomeClass' +// ACTION: Create function 'SomeClass' +// ERROR: Unresolved reference: SomeClass + +val x = SomeClass() \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.data.Sample.kt b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.data.Sample.kt new file mode 100644 index 00000000000..00f79598da5 --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.data.Sample.kt @@ -0,0 +1,3 @@ +package excludedPackage + +class SomeClass \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt new file mode 100644 index 00000000000..1f3fc10ef1e --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt @@ -0,0 +1,6 @@ +// "class com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase" "false" +// ACTION: Create class 'ExcludedClass' +// ACTION: Create function 'ExcludedClass' +// ERROR: Unresolved reference: ExcludedClass + +val x = ExcludedClass() \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.data.Sample.kt b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.data.Sample.kt new file mode 100644 index 00000000000..59299eb163c --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.data.Sample.kt @@ -0,0 +1,3 @@ +package somePackage + +class ExcludedClass \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt new file mode 100644 index 00000000000..081b55d7691 --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt @@ -0,0 +1,5 @@ +// "class com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase" "false" +// ACTION: Create function 'someFunction' +// ERROR: Unresolved reference: someFunction + +val x = someFunction() \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.data.Sample.kt b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.data.Sample.kt new file mode 100644 index 00000000000..c6bd1606f7f --- /dev/null +++ b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.data.Sample.kt @@ -0,0 +1,5 @@ +package excludedPackage + +fun someFunction() { + +} \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/notExcludedClass.after.kt b/idea/testData/quickfix/autoImports/notExcludedClass.after.kt new file mode 100644 index 00000000000..b34d2bb4756 --- /dev/null +++ b/idea/testData/quickfix/autoImports/notExcludedClass.after.kt @@ -0,0 +1,6 @@ +import somePackage.NotExcludedClass + +// "Import" "true" +// ERROR: Unresolved reference: NotExcludedClass + +val x = NotExcludedClass() \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/notExcludedClass.before.Main.kt b/idea/testData/quickfix/autoImports/notExcludedClass.before.Main.kt new file mode 100644 index 00000000000..860bc03f0cf --- /dev/null +++ b/idea/testData/quickfix/autoImports/notExcludedClass.before.Main.kt @@ -0,0 +1,4 @@ +// "Import" "true" +// ERROR: Unresolved reference: NotExcludedClass + +val x = NotExcludedClass() \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/notExcludedClass.before.data.Sample.kt b/idea/testData/quickfix/autoImports/notExcludedClass.before.data.Sample.kt new file mode 100644 index 00000000000..55afe2de2a1 --- /dev/null +++ b/idea/testData/quickfix/autoImports/notExcludedClass.before.data.Sample.kt @@ -0,0 +1,3 @@ +package somePackage + +class NotExcludedClass \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java index 694977bce05..ca35d7439d4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.quickfix; +import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase; import com.intellij.codeInsight.intention.IntentionAction; @@ -67,6 +68,18 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer doTest(beforeFileName, true); } + @Override + protected void setUp() throws Exception { + super.setUp(); + CodeInsightSettings.getInstance().EXCLUDED_PACKAGES = new String[]{"excludedPackage", "somePackage.ExcludedClass"}; + } + + @Override + protected void tearDown() throws Exception { + CodeInsightSettings.getInstance().EXCLUDED_PACKAGES = ArrayUtil.EMPTY_STRING_ARRAY; + super.tearDown(); + } + private void doTest(final String beforeFileName, boolean withExtraFile) throws Exception { String testDataPath = getTestDataPath(); File mainFile = new File(testDataPath + beforeFileName); diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 8ea878b8edd..638f8cd5a74 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -185,6 +185,30 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes doTestWithExtraFile(fileName); } + @TestMetadata("noImportsForClassInExcludedPackage.before.Main.kt") + public void testNoImportsForClassInExcludedPackage() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt"); + doTestWithExtraFile(fileName); + } + + @TestMetadata("noImportsForExcludedClass.before.Main.kt") + public void testNoImportsForExcludedClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt"); + doTestWithExtraFile(fileName); + } + + @TestMetadata("noImportsForFunctionInExcludedPackage.before.Main.kt") + public void testNoImportsForFunctionInExcludedPackage() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt"); + doTestWithExtraFile(fileName); + } + + @TestMetadata("notExcludedClass.before.Main.kt") + public void testNotExcludedClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/notExcludedClass.before.Main.kt"); + doTestWithExtraFile(fileName); + } + @TestMetadata("objectImport.before.Main.kt") public void testObjectImport() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/objectImport.before.Main.kt");