Class import fix and completion for JS from lib

This commit is contained in:
Nikolay Krasko
2012-06-08 12:27:15 +04:00
parent 1fe272d592
commit bd068b750c
4 changed files with 60 additions and 17 deletions
@@ -80,7 +80,7 @@ public class JetShortNamesCache extends PsiShortNamesCache {
}
/**
* Return class names form jet sources in given scope which should be visible as java classes.
* Return class names form jet sources in given scope which should be visible as Java classes.
*/
@NotNull
@Override
@@ -265,6 +265,26 @@ public class JetShortNamesCache extends PsiShortNamesCache {
return resultDescriptors;
}
public Collection<ClassDescriptor> getJetClassesDescriptors(
@NotNull Condition<String> acceptedShortNameCondition,
@NotNull JetFile jetFile
) {
BindingContext context = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(jetFile).getBindingContext();
Collection<ClassDescriptor> classDescriptors = new ArrayList<ClassDescriptor>();
for (String fqName : JetFullClassNameIndex.getInstance().getAllKeys(project)) {
FqName classFQName = new FqName(fqName);
if (acceptedShortNameCondition.value(classFQName.shortName().getName())) {
ClassDescriptor descriptor = context.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, classFQName);
if (descriptor != null) {
classDescriptors.add(descriptor);
}
}
}
return classDescriptors;
}
@NotNull
@Override
public PsiMethod[] getMethodsByName(@NonNls @NotNull String name, @NotNull GlobalSearchScope scope) {
@@ -18,12 +18,12 @@ package org.jetbrains.jet.plugin.completion;
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.util.Consumer;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
@@ -118,18 +118,19 @@ public class JetClassCompletionContributor extends CompletionContributor {
});
}
else {
GlobalSearchScope globalSearchScope = GlobalSearchScope.allScope(parameters.getOriginalFile().getProject());
PsiShortNamesCache cache = JetCacheManager.getInstance(parameters.getOriginalFile().getProject()).getShortNamesCache(
Project project = parameters.getOriginalFile().getProject();
JetShortNamesCache namesCache = JetCacheManager.getInstance(project).getNamesCache();
Collection<ClassDescriptor> descriptors = namesCache.getJetClassesDescriptors(
new Condition<String>() {
@Override
public boolean value(String shortName) {
return result.getPrefixMatcher().prefixMatches(shortName);
}
},
(JetFile) parameters.getOriginalFile());
for (String className : cache.getAllClassNames()) {
if (result.getPrefixMatcher().prefixMatches(className)) {
for (PsiClass aClass : cache.getClassesByName(className, globalSearchScope)) {
if (!addAsJetLookupElement(aClass, bindingContext, consumer)) {
assert false : "All classes should be possible to add as kotlin classes in JS project";
}
}
}
for (ClassDescriptor descriptor : descriptors) {
consumer.consume(DescriptorLookupConverter.createLookupElement(bindingContext, descriptor));
}
}
}
@@ -55,6 +55,7 @@ import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.actions.JetAddImportAction;
import org.jetbrains.jet.plugin.caches.JetCacheManager;
import org.jetbrains.jet.plugin.caches.JetShortNamesCache;
import org.jetbrains.jet.plugin.project.JsModuleDetector;
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
import java.util.Collection;
@@ -152,10 +153,14 @@ public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression>
* Searches for possible class names in kotlin context and java facade.
*/
public static Collection<FqName> getClassNames(@NotNull String referenceName, @NotNull JetFile file) {
final GlobalSearchScope scope = GlobalSearchScope.allScope(file.getProject());
Set<FqName> possibleResolveNames = Sets.newHashSet();
possibleResolveNames.addAll(getClassesFromCache(referenceName, file));
if (!JsModuleDetector.isJsModule(file)) {
possibleResolveNames.addAll(getClassesFromCache(referenceName, file));
}
else {
possibleResolveNames.addAll(getJetClasses(referenceName, file));
}
// TODO: Do appropriate sorting
return Lists.newArrayList(possibleResolveNames);
@@ -186,6 +191,23 @@ public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression>
});
}
private static Collection<FqName> getJetClasses(@NotNull final String typeName, @NotNull JetFile file) {
JetShortNamesCache cache = JetCacheManager.getInstance(file.getProject()).getNamesCache();
Collection<ClassDescriptor> descriptors = cache.getJetClassesDescriptors(new Condition<String>() {
@Override
public boolean value(String s) {
return typeName.equals(s);
}
}, file);
return Collections2.transform(descriptors, new Function<ClassDescriptor, FqName>() {
@Override
public FqName apply(ClassDescriptor descriptor) {
return DescriptorUtils.getFQName(descriptor).toSafe();
}
});
}
private static boolean isAccessible(PsiMember member) {
if (member instanceof JetLightClass) {
// TODO: Now light classes losing accessibility information
@@ -42,7 +42,7 @@ public class JetFullClassNameIndex extends StringStubIndexExtension<JetClassOrOb
}
@Override
public Collection<JetClassOrObject> get(final String integer, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(integer, project, new JetSourceFilterScope(scope));
public Collection<JetClassOrObject> get(final String fqName, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(fqName, project, new JetSourceFilterScope(scope));
}
}