|
|
|
@@ -19,54 +19,33 @@ package org.jetbrains.jet.plugin.completion.handlers;
|
|
|
|
|
import com.intellij.codeInsight.completion.InsertHandler;
|
|
|
|
|
import com.intellij.codeInsight.completion.InsertionContext;
|
|
|
|
|
import com.intellij.codeInsight.lookup.LookupElement;
|
|
|
|
|
import com.intellij.openapi.application.ApplicationManager;
|
|
|
|
|
import com.intellij.psi.PsiDocumentManager;
|
|
|
|
|
import com.intellij.psi.PsiElement;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
|
|
|
|
import org.jetbrains.jet.lang.psi.JetFile;
|
|
|
|
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
|
|
|
|
import org.jetbrains.jet.lang.resolve.name.FqName;
|
|
|
|
|
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences;
|
|
|
|
|
import org.jetbrains.jet.plugin.completion.JetLookupObject;
|
|
|
|
|
import org.jetbrains.jet.plugin.project.ProjectStructureUtil;
|
|
|
|
|
import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper;
|
|
|
|
|
|
|
|
|
|
public class JetClassInsertHandler implements InsertHandler<LookupElement> {
|
|
|
|
|
|
|
|
|
|
public static final InsertHandler<LookupElement> INSTANCE = new JetClassInsertHandler();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handleInsert(final InsertionContext context, final LookupElement item) {
|
|
|
|
|
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
|
|
|
|
|
|
|
|
|
|
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) {
|
|
|
|
|
if (context.getFile() instanceof JetFile) {
|
|
|
|
|
final JetFile jetFile = (JetFile) context.getFile();
|
|
|
|
|
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (item.getObject() instanceof JetLookupObject) {
|
|
|
|
|
JetLookupObject lookupObject = (JetLookupObject)item.getObject();
|
|
|
|
|
DeclarationDescriptor descriptor = lookupObject.getDescriptor();
|
|
|
|
|
if (descriptor != null) {
|
|
|
|
|
String text = DescriptorUtils.getFqName(descriptor).asString();
|
|
|
|
|
int startOffset = context.getStartOffset();
|
|
|
|
|
PsiElement element = context.getFile().findElementAt(startOffset);
|
|
|
|
|
if (element == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
context.getDocument().replaceString(startOffset, context.getTailOffset(), text);
|
|
|
|
|
|
|
|
|
|
if (context.getFile() instanceof JetFile && item.getObject() instanceof JetLookupObject) {
|
|
|
|
|
JetLookupObject lookupObject = (JetLookupObject)item.getObject();
|
|
|
|
|
DeclarationDescriptor descriptor = lookupObject.getDescriptor();
|
|
|
|
|
PsiElement targetElement = lookupObject.getPsiElement();
|
|
|
|
|
if (descriptor != null) {
|
|
|
|
|
FqName fqn = DescriptorUtils.getFqNameSafe(descriptor);
|
|
|
|
|
|
|
|
|
|
// TODO: Find out the way for getting psi element for JS libs
|
|
|
|
|
if (targetElement != null) {
|
|
|
|
|
ImportInsertHelper.addImportDirectiveOrChangeToFqName(fqn, jetFile, context.getStartOffset(), targetElement);
|
|
|
|
|
}
|
|
|
|
|
else if (ProjectStructureUtil.isJsKotlinModule(jetFile)) {
|
|
|
|
|
ImportInsertHelper.addImportDirectiveIfNeeded(fqn, jetFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
|
|
|
|
|
ShortenReferences.instance$.process((JetFile) context.getFile(), startOffset, startOffset + text.length());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|