diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java index 3f1121d9a19..800cd647185 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java @@ -138,6 +138,17 @@ public class JetPsiUtil { return jetClass.getName(); } + @Nullable @JetElement.IfNotParsed + public static String getImportPath(JetImportDirective importDirective) { + final JetExpression importedReference = importDirective.getImportedReference(); + if (importedReference == null) { + return null; + } + + final String text = importedReference.getText(); + return text.replaceAll(" ", "") + (importDirective.isAllUnder() ? ".*" : ""); + } + private static String makeFQName(String prefix, JetClassOrObject jetClass) { return ((prefix == null || prefix.length() == 0) ? "" : prefix + ".") + jetClass.getName(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java b/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java index db62e45c1d3..8d263ec272d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java @@ -100,4 +100,19 @@ public final class QualifiedNamesUtil { return null; } + + /** + * Check that given fqn could be imported with import. + * + * @param importPath path from the import. Could contain .* part + * @param fqn + * @return + */ + public static boolean isImported(@NotNull String importPath, @NotNull String fqn) { + if (importPath.endsWith("*")) { + return withoutLastSegment(importPath).equals(withoutLastSegment(fqn)); + } + + return importPath.equals(fqn); + } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java index 98b30a5375f..19c202ec5ab 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java @@ -52,10 +52,11 @@ public class ImportClassHelper { JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importString); if (!importDirectives.isEmpty()) { - + // Check if import is already present for (JetImportDirective directive : importDirectives) { - if (directive.getText().endsWith(importString) || directive.getText().endsWith(importString + ";")) { + String importPath = JetPsiUtil.getImportPath(directive); + if (importPath != null && QualifiedNamesUtil.isImported(importPath, importString)) { return; } } diff --git a/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt new file mode 100644 index 00000000000..f7e6dce4946 --- /dev/null +++ b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt @@ -0,0 +1,6 @@ +package some + +import jettesting.data.*; + +fun otherfun() { +} diff --git a/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt.after b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt.after new file mode 100644 index 00000000000..f7e6dce4946 --- /dev/null +++ b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralExist.kt.after @@ -0,0 +1,6 @@ +package some + +import jettesting.data.*; + +fun otherfun() { +} diff --git a/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt new file mode 100644 index 00000000000..963032725e6 --- /dev/null +++ b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt @@ -0,0 +1,6 @@ +package some + +import jettesting . data .*; + +fun otherfun() { +} diff --git a/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt.after b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt.after new file mode 100644 index 00000000000..963032725e6 --- /dev/null +++ b/idea/testData/quickfix/importHelper/DoNotImportIfGeneralSpaceExist.kt.after @@ -0,0 +1,6 @@ +package some + +import jettesting . data .*; + +fun otherfun() { +} diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/ImportClassHelperTest.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/ImportClassHelperTest.java new file mode 100644 index 00000000000..a1212528ed4 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/ImportClassHelperTest.java @@ -0,0 +1,27 @@ +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.plugin.PluginTestCaseBase; + +/** + * @author Nikolay Krasko + */ +public class ImportClassHelperTest extends LightDaemonAnalyzerTestCase { + public void testDoNotImportIfGeneralExist() { + configureByFile(getTestName(false) + ".kt"); + ImportClassHelper.addImportDirective("jettesting.data.testFunction", (JetFile) getFile()); + checkResultByFile(getTestName(false) + ".kt.after"); + } + + public void testDoNotImportIfGeneralSpaceExist() { + configureByFile(getTestName(false) + ".kt"); + ImportClassHelper.addImportDirective("jettesting.data.testFunction", (JetFile) getFile()); + checkResultByFile(getTestName(false) + ".kt.after"); + } + + @Override + protected String getTestDataPath() { + return PluginTestCaseBase.getTestDataPathBase() + "/quickfix/importHelper/"; + } +}