Changed completion for classes, so if they have the same name as already imported, new import is not added. Qualified name is inserted instead.

This commit is contained in:
Evgeny Gerashchenko
2012-04-24 18:24:40 +04:00
parent 4e617094ce
commit 97d96b480e
3 changed files with 34 additions and 11 deletions
@@ -49,15 +49,12 @@ public class JetClassInsertHandler implements InsertHandler<LookupElement> {
} }
if (context.getFile() instanceof JetFile && item.getObject() instanceof JetLookupObject) { if (context.getFile() instanceof JetFile && item.getObject() instanceof JetLookupObject) {
final DeclarationDescriptor descriptor = ((JetLookupObject) item.getObject()).getDescriptor(); JetLookupObject lookupObject = (JetLookupObject)item.getObject();
if (descriptor != null) { final DeclarationDescriptor descriptor = lookupObject.getDescriptor();
ApplicationManager.getApplication().runWriteAction(new Runnable() { PsiElement targetElement = lookupObject.getPsiElement();
@Override if (descriptor != null && targetElement != null) {
public void run() { final FqName fqn = DescriptorUtils.getFQName(descriptor).toSafe();
final FqName fqn = DescriptorUtils.getFQName(descriptor).toSafe(); ImportInsertHelper.addImportDirectiveOrChangeToFqName(fqn, jetFile, context.getStartOffset(), targetElement);
ImportInsertHelper.addImportDirective(fqn, jetFile);
}
});
} }
} }
} }
@@ -35,8 +35,10 @@ public class JetJavaClassInsertHandler implements InsertHandler<JavaPsiClassRefe
@Override @Override
public void handleInsert(final InsertionContext context, final JavaPsiClassReferenceElement item) { public void handleInsert(final InsertionContext context, final JavaPsiClassReferenceElement item) {
if (context.getFile() instanceof JetFile) { if (context.getFile() instanceof JetFile) {
final JetFile jetFile = (JetFile) context.getFile(); ImportInsertHelper.addImportDirectiveOrChangeToFqName(new FqName(item.getQualifiedName()),
ImportInsertHelper.addImportDirective(new FqName(item.getQualifiedName()), jetFile); (JetFile) context.getFile(),
context.getStartOffset(),
item.getObject());
} }
// check annotation // check annotation
@@ -16,7 +16,12 @@
package org.jetbrains.jet.plugin.quickfix; package org.jetbrains.jet.plugin.quickfix;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.DefaultModuleConfiguration; import org.jetbrains.jet.lang.DefaultModuleConfiguration;
@@ -33,6 +38,7 @@ import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils; import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.plugin.JetPluginUtil; import org.jetbrains.jet.plugin.JetPluginUtil;
import org.jetbrains.jet.plugin.references.JetPsiReference;
import org.jetbrains.jet.util.QualifiedNamesUtil; import org.jetbrains.jet.util.QualifiedNamesUtil;
import java.util.List; import java.util.List;
@@ -76,6 +82,24 @@ public class ImportInsertHelper {
addImportDirective(new ImportPath(importFqn, false), null, file); addImportDirective(new ImportPath(importFqn, false), null, file);
} }
public static void addImportDirectiveOrChangeToFqName(@NotNull FqName importFqn, @NotNull JetFile file, int refOffset, @NotNull PsiElement targetElement) {
PsiReference reference = file.findReferenceAt(refOffset);
if (reference instanceof JetPsiReference) {
PsiElement target = reference.resolve();
if (target != null) {
boolean same = file.getManager().areElementsEquivalent(target, targetElement);
same |= target instanceof PsiClass && importFqn.getFqName().equals(((PsiClass)target).getQualifiedName());
if (!same) {
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
TextRange refRange = reference.getElement().getTextRange();
document.replaceString(refRange.getStartOffset(), refRange.getEndOffset(), importFqn.getFqName());
}
return;
}
}
addImportDirective(new ImportPath(importFqn, false), null, file);
}
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 (!doNeedImport(importPath, aliasName, file)) {
return; return;