Don't add import if there's general import with .* exist

This commit is contained in:
Nikolay Krasko
2012-02-08 15:27:41 +04:00
parent e8ffaa28f3
commit 01e71cbd59
8 changed files with 80 additions and 2 deletions
@@ -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();
}
@@ -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);
}
}
@@ -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;
}
}
@@ -0,0 +1,6 @@
package some
import jettesting.data.*;
fun otherfun() {
}
@@ -0,0 +1,6 @@
package some
import jettesting.data.*;
fun otherfun() {
}
@@ -0,0 +1,6 @@
package some
import jettesting . data .*;
fun otherfun() {
}
@@ -0,0 +1,6 @@
package some
import jettesting . data .*;
fun otherfun() {
}
@@ -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/";
}
}