Using indexes to find packages

JetLightPackage does not depend on PSI elements any more
This commit is contained in:
Andrey Breslav
2012-12-19 14:27:56 +04:00
parent 941e47503e
commit 29dd3821cc
5 changed files with 28 additions and 12 deletions
@@ -132,4 +132,11 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport
}
return result;
}
@Override
public boolean packageExists(
@NotNull FqName fqName, @NotNull GlobalSearchScope scope
) {
return trace.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, fqName) != null;
}
}
@@ -205,12 +205,10 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade
FqName fqName = new FqName(qualifiedNameString);
final Collection<JetFile> psiFiles = collectProjectJetFiles(project, GlobalSearchScope.allScope(project));
for (JetFile psiFile : psiFiles) {
if (QualifiedNamesUtil.isSubpackageOf(JetPsiUtil.getFQName(psiFile), fqName)) {
return new JetLightPackage(psiManager, fqName, psiFile.getNamespaceHeader());
}
// allScope() because the contract says that the whole project
GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
if (lightClassGenerationSupport.packageExists(fqName, allScope)) {
return new JetLightPackage(psiManager, fqName, allScope);
}
return null;
@@ -228,7 +226,7 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade
final FqName subPackageFQN = QualifiedNamesUtil.plusOneSegment(new FqName(psiPackage.getQualifiedName()), jetRootNamespace);
if (subPackageFQN != null) {
answer.add(new JetLightPackage(psiManager, subPackageFQN, psiFile.getNamespaceHeader()));
answer.add(new JetLightPackage(psiManager, subPackageFQN, scope));
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.jet.asJava;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.file.PsiPackageImpl;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.jet.lang.resolve.name.FqName;
/**
@@ -26,22 +27,22 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
*/
public class JetLightPackage extends PsiPackageImpl {
private final PsiElement namespaceElement;
private final FqName fqName;
private final GlobalSearchScope scope;
public JetLightPackage(PsiManager manager, FqName qualifiedName, PsiElement namespaceElement) {
public JetLightPackage(PsiManager manager, FqName qualifiedName, GlobalSearchScope scope) {
super(manager, qualifiedName.getFqName());
this.fqName = qualifiedName;
this.namespaceElement = namespaceElement;
this.scope = scope;
}
@Override
public PsiElement copy() {
return new JetLightPackage(getManager(), fqName, namespaceElement);
return new JetLightPackage(getManager(), fqName, scope);
}
@Override
public boolean isValid() {
return namespaceElement.isValid();
return LightClassGenerationSupport.getInstance(getProject()).packageExists(fqName, scope);
}
}
@@ -51,4 +51,6 @@ public abstract class LightClassGenerationSupport {
@NotNull FqName packageFqName,
@NotNull GlobalSearchScope searchScope
);
public abstract boolean packageExists(@NotNull FqName fqName, @NotNull GlobalSearchScope scope);
}
@@ -24,6 +24,7 @@ import org.jetbrains.jet.asJava.LightClassGenerationSupport;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.plugin.stubindex.JetAllPackagesIndex;
import org.jetbrains.jet.plugin.stubindex.JetClassByPackageIndex;
import org.jetbrains.jet.plugin.stubindex.JetFullClassNameIndex;
import org.jetbrains.jet.plugin.stubindex.JetPackageDeclarationIndex;
@@ -65,4 +66,11 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
) {
return JetClassByPackageIndex.getInstance().get(packageFqName.getFqName(), project, searchScope);
}
@Override
public boolean packageExists(
@NotNull FqName fqName, @NotNull GlobalSearchScope scope
) {
return !JetAllPackagesIndex.getInstance().get(fqName.getFqName(), project, scope).isEmpty();
}
}