Skip to content

Commit 387d5ff

Browse files
committed
Added call to ImportFilter in import quickfix (KT-14439)
#KT-14439 Fixed
1 parent fd6d4c3 commit 387d5ff

File tree

9 files changed

+156
-0
lines changed

9 files changed

+156
-0
lines changed

generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.intellij.lang.annotations.Language
2121
import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
2222
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
2323
import org.jetbrains.kotlin.android.*
24+
import org.jetbrains.kotlin.android.quickfixes.AbstractAndroidQuickFixMultiFileTest
2425
import org.jetbrains.kotlin.android.configure.AbstractConfigureProjectTest
2526
import org.jetbrains.kotlin.android.intentions.AbstractAndroidResourceIntentionTest
2627
import org.jetbrains.kotlin.annotation.AbstractAnnotationProcessorBoxTest
@@ -1158,6 +1159,10 @@ fun main(args: Array<String>) {
11581159
testClass<AbstractAndroidResourceIntentionTest> {
11591160
model("android/resourceIntentions", extension = "test", singleClass = true)
11601161
}
1162+
1163+
testClass<AbstractAndroidQuickFixMultiFileTest>() {
1164+
model("android/quickfix", pattern = """^(\w+)\.((before\.Main\.\w+)|(test))$""", testMethod = "doTestWithExtraFile")
1165+
}
11611166
}
11621167

11631168
testGroup("plugins/plugins-tests/tests", "plugins/android-extensions/android-extensions-jps/testData") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.android.quickfixes;
18+
19+
import com.intellij.facet.FacetManager;
20+
import com.intellij.facet.ModifiableFacetModel;
21+
import com.intellij.openapi.application.ApplicationManager;
22+
import org.jetbrains.android.facet.AndroidFacet;
23+
import org.jetbrains.kotlin.idea.quickfix.AbstractQuickFixMultiFileTest;
24+
25+
public abstract class AbstractAndroidQuickFixMultiFileTest extends AbstractQuickFixMultiFileTest {
26+
27+
@Override
28+
protected void setUp() throws Exception {
29+
super.setUp();
30+
addAndroidFacet();
31+
}
32+
33+
@Override
34+
protected void doTestWithExtraFile(String beforeFileName) throws Exception {
35+
addManifest();
36+
super.doTestWithExtraFile(beforeFileName);
37+
}
38+
39+
@Override
40+
protected void doTestWithoutExtraFile(String beforeFileName) throws Exception {
41+
addManifest();
42+
super.doTestWithoutExtraFile(beforeFileName);
43+
}
44+
45+
private void addAndroidFacet() {
46+
FacetManager facetManager = FacetManager.getInstance(myModule);
47+
AndroidFacet facet = facetManager.createFacet(AndroidFacet.getFacetType(), "Android", null);
48+
49+
final ModifiableFacetModel facetModel = facetManager.createModifiableModel();
50+
facetModel.addFacet(facet);
51+
ApplicationManager.getApplication().runWriteAction(new Runnable() {
52+
@Override
53+
public void run() {
54+
facetModel.commit();
55+
}
56+
});
57+
}
58+
59+
private void addManifest() throws Exception {
60+
configureByFile("idea/testData/android/AndroidManifest.xml");
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.android.quickfixes;
18+
19+
import com.intellij.testFramework.TestDataPath;
20+
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
21+
import org.jetbrains.kotlin.test.KotlinTestUtils;
22+
import org.jetbrains.kotlin.test.TestMetadata;
23+
import org.junit.runner.RunWith;
24+
25+
import java.io.File;
26+
import java.util.regex.Pattern;
27+
28+
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
29+
@SuppressWarnings("all")
30+
@TestMetadata("idea/testData/android/quickfix")
31+
@TestDataPath("$PROJECT_ROOT")
32+
@RunWith(JUnit3RunnerWithInners.class)
33+
public class AndroidQuickFixMultiFileTestGenerated extends AbstractAndroidQuickFixMultiFileTest {
34+
public void testAllFilesPresentInQuickfix() throws Exception {
35+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/quickfix"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
36+
}
37+
38+
@TestMetadata("idea/testData/android/quickfix/autoImports")
39+
@TestDataPath("$PROJECT_ROOT")
40+
@RunWith(JUnit3RunnerWithInners.class)
41+
public static class AutoImports extends AbstractAndroidQuickFixMultiFileTest {
42+
public void testAllFilesPresentInAutoImports() throws Exception {
43+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/quickfix/autoImports"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
44+
}
45+
46+
@TestMetadata("androidRImport.before.Main.kt")
47+
public void testAndroidRImport() throws Exception {
48+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/quickfix/autoImports/androidRImport.before.Main.kt");
49+
doTestWithExtraFile(fileName);
50+
}
51+
}
52+
}

idea/src/org/jetbrains/kotlin/idea/quickfix/ImportFix.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.jetbrains.kotlin.idea.quickfix
1818

19+
import com.intellij.codeInsight.ImportFilter
1920
import com.intellij.codeInsight.hint.HintManager
2021
import com.intellij.codeInsight.intention.HighPriorityAction
2122
import com.intellij.codeInsight.intention.IntentionAction
@@ -66,6 +67,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
6667
import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfo
6768
import org.jetbrains.kotlin.resolve.calls.callUtil.getParentCall
6869
import org.jetbrains.kotlin.resolve.calls.context.ContextDependency
70+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
6971
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
7072
import org.jetbrains.kotlin.resolve.scopes.ExplicitImportsScope
7173
import org.jetbrains.kotlin.resolve.scopes.utils.addImportingScope
@@ -176,6 +178,8 @@ internal abstract class ImportFixBase<T : KtExpression> protected constructor(
176178
}
177179
}
178180

181+
result = result.filter { ImportFilter.shouldImport(file, it.fqNameSafe.asString()) }
182+
179183
return if (result.size > 1)
180184
reduceCandidatesBasedOnDependencyRuleViolation(result, file)
181185
else
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// "Import" "true"
2+
// ERROR: Unresolved reference: R
3+
4+
package com.myapp.activity
5+
6+
import com.myapp.R
7+
8+
fun test() {
9+
val a = <caret>R.layout.activity_test_kotlin
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.myapp
2+
3+
public class R {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package android
2+
3+
public class R {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package android.support.v7.appcompat
2+
3+
public class R {
4+
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// "Import" "true"
2+
// ERROR: Unresolved reference: R
3+
4+
package com.myapp.activity
5+
6+
fun test() {
7+
val a = <caret>R.layout.activity_test_kotlin
8+
}

0 commit comments

Comments
 (0)