Do not suggest to import something that is already imported
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.plugin.quickfix;
|
package org.jetbrains.jet.plugin.quickfix;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.collect.Collections2;
|
import com.google.common.collect.Collections2;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
@@ -46,6 +47,7 @@ import org.jetbrains.jet.lang.psi.JetFile;
|
|||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.FqName;
|
import org.jetbrains.jet.lang.resolve.FqName;
|
||||||
|
import org.jetbrains.jet.lang.resolve.ImportPath;
|
||||||
import org.jetbrains.jet.plugin.JetFileType;
|
import org.jetbrains.jet.plugin.JetFileType;
|
||||||
import org.jetbrains.jet.plugin.actions.JetAddImportAction;
|
import org.jetbrains.jet.plugin.actions.JetAddImportAction;
|
||||||
import org.jetbrains.jet.plugin.caches.JetCacheManager;
|
import org.jetbrains.jet.plugin.caches.JetCacheManager;
|
||||||
@@ -61,14 +63,14 @@ import java.util.*;
|
|||||||
public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression> implements HighPriorityAction {
|
public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression> implements HighPriorityAction {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<FqName> suggestions;
|
private final Collection<FqName> suggestions;
|
||||||
|
|
||||||
public ImportClassAndFunFix(@NotNull JetSimpleNameExpression element) {
|
public ImportClassAndFunFix(@NotNull JetSimpleNameExpression element) {
|
||||||
super(element);
|
super(element);
|
||||||
suggestions = computeSuggestions(element);
|
suggestions = computeSuggestions(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<FqName> computeSuggestions(@NotNull JetSimpleNameExpression element) {
|
private static Collection<FqName> computeSuggestions(@NotNull JetSimpleNameExpression element) {
|
||||||
final PsiFile file = element.getContainingFile();
|
final PsiFile file = element.getContainingFile();
|
||||||
if (!(file instanceof JetFile)) {
|
if (!(file instanceof JetFile)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -82,12 +84,18 @@ public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression>
|
|||||||
|
|
||||||
assert referenceName != null;
|
assert referenceName != null;
|
||||||
|
|
||||||
final ArrayList<FqName> result = Lists.newArrayList();
|
List<FqName> result = Lists.newArrayList();
|
||||||
result.addAll(getClassNames(referenceName, file.getProject()));
|
result.addAll(getClassNames(referenceName, file.getProject()));
|
||||||
result.addAll(getJetTopLevelFunctions(referenceName, element, file.getProject()));
|
result.addAll(getJetTopLevelFunctions(referenceName, element, file.getProject()));
|
||||||
result.addAll(getJetExtensionFunctions(referenceName, element, file.getProject()));
|
result.addAll(getJetExtensionFunctions(referenceName, element, file.getProject()));
|
||||||
|
|
||||||
return result;
|
return Collections2.filter(result, new Predicate<FqName>() {
|
||||||
|
@Override
|
||||||
|
public boolean apply(@Nullable FqName fqName) {
|
||||||
|
assert fqName != null;
|
||||||
|
return ImportInsertHelper.doNeedImport(new ImportPath(fqName, false), null, (JetFile) file);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Collection<FqName> getJetTopLevelFunctions(@NotNull String referenceName, JetSimpleNameExpression expression, @NotNull Project project) {
|
private static Collection<FqName> getJetTopLevelFunctions(@NotNull String referenceName, JetSimpleNameExpression expression, @NotNull Project project) {
|
||||||
@@ -180,7 +188,7 @@ public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression>
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!ApplicationManager.getApplication().isUnitTestMode()) {
|
if (!ApplicationManager.getApplication().isUnitTestMode()) {
|
||||||
String hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.get(0).getFqName());
|
String hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.iterator().next().getFqName());
|
||||||
|
|
||||||
HintManager.getInstance().showQuestionHint(
|
HintManager.getInstance().showQuestionHint(
|
||||||
editor, hintText,
|
editor, hintText,
|
||||||
|
|||||||
@@ -73,32 +73,14 @@ public class ImportInsertHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void addImportDirective(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) {
|
public static void addImportDirective(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) {
|
||||||
|
if (!doNeedImport(importPath, aliasName, file)) {
|
||||||
if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName()).equals(JavaDescriptorResolver.JAVA_ROOT)) {
|
|
||||||
FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart());
|
|
||||||
importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isImportedByDefault(importPath, aliasName, JetPsiUtil.getFQName(file))) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath, aliasName);
|
JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath, aliasName);
|
||||||
|
|
||||||
List<JetImportDirective> importDirectives = file.getImportDirectives();
|
List<JetImportDirective> importDirectives = file.getImportDirectives();
|
||||||
|
|
||||||
if (!importDirectives.isEmpty()) {
|
if (!importDirectives.isEmpty()) {
|
||||||
|
|
||||||
// Check if import is already present
|
|
||||||
for (JetImportDirective directive : importDirectives) {
|
|
||||||
ImportPath existentImportPath = JetPsiUtil.getImportPath(directive);
|
|
||||||
if (directive.getAliasName() == null && aliasName == null) {
|
|
||||||
if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
|
JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
|
||||||
lastDirective.getParent().addAfter(newDirective, lastDirective);
|
lastDirective.getParent().addAfter(newDirective, lastDirective);
|
||||||
}
|
}
|
||||||
@@ -110,7 +92,7 @@ public class ImportInsertHelper {
|
|||||||
/**
|
/**
|
||||||
* Check that import is useless.
|
* Check that import is useless.
|
||||||
*/
|
*/
|
||||||
private static boolean isImportedByDefault(@NotNull ImportPath importPath, String aliasName, @NotNull FqName filePackageFqn) {
|
private static boolean isImportedByDefault(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull FqName filePackageFqn) {
|
||||||
if (importPath.fqnPart().isRoot()) {
|
if (importPath.fqnPart().isRoot()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -143,4 +125,31 @@ public class ImportInsertHelper {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean doNeedImport(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) {
|
||||||
|
if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName()).equals(JavaDescriptorResolver.JAVA_ROOT)) {
|
||||||
|
FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart());
|
||||||
|
importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isImportedByDefault(importPath, null, JetPsiUtil.getFQName(file))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<JetImportDirective> importDirectives = file.getImportDirectives();
|
||||||
|
|
||||||
|
if (!importDirectives.isEmpty()) {
|
||||||
|
// Check if import is already present
|
||||||
|
for (JetImportDirective directive : importDirectives) {
|
||||||
|
ImportPath existentImportPath = JetPsiUtil.getImportPath(directive);
|
||||||
|
if (directive.getAliasName() == null && aliasName == null) {
|
||||||
|
if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Import Class" "false"
|
||||||
|
|
||||||
|
package Teting
|
||||||
|
|
||||||
|
import Teting.test.someFun
|
||||||
|
|
||||||
|
fun main(args : Array<String>) {
|
||||||
|
someFun
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Import Class" "false"
|
||||||
|
|
||||||
|
package Teting
|
||||||
|
|
||||||
|
import Teting.test.someFun
|
||||||
|
|
||||||
|
fun main(args : Array<String>) {
|
||||||
|
<caret>someFun
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user