PSI-based declaration providers
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.lazy;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||
import org.jetbrains.jet.lang.psi.JetProperty;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public abstract class AbstractPsiBasedDeclarationProvider implements DeclarationProvider {
|
||||
private final List<JetDeclaration> allDeclarations = Lists.newArrayList();
|
||||
private final Multimap<Name, JetNamedFunction> functions = HashMultimap.create();
|
||||
private final Multimap<Name, JetProperty> properties = HashMultimap.create();
|
||||
private final Map<Name, JetClassOrObject> classesAndObjects = Maps.newHashMap();
|
||||
|
||||
private boolean indexCreated = false;
|
||||
|
||||
private void createIndex() {
|
||||
if (indexCreated) return;
|
||||
indexCreated = true;
|
||||
|
||||
doCreateIndex();
|
||||
}
|
||||
|
||||
protected abstract void doCreateIndex();
|
||||
|
||||
protected void putToIndex(JetDeclaration declaration) {
|
||||
allDeclarations.add(declaration);
|
||||
if (declaration instanceof JetNamedFunction) {
|
||||
JetNamedFunction namedFunction = (JetNamedFunction) declaration;
|
||||
functions.put(namedFunction.getNameAsName(), namedFunction);
|
||||
}
|
||||
else if (declaration instanceof JetProperty) {
|
||||
JetProperty property = (JetProperty) declaration;
|
||||
properties.put(property.getNameAsName(), property);
|
||||
}
|
||||
else if (declaration instanceof JetClassOrObject) {
|
||||
JetClassOrObject classOrObject = (JetClassOrObject) declaration;
|
||||
classesAndObjects.put(classOrObject.getNameAsName(), classOrObject);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unknown declaration: " + declaration);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JetDeclaration> getAllDeclarations() {
|
||||
createIndex();
|
||||
return allDeclarations;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<JetNamedFunction> getFunctionDeclarations(@NotNull Name name) {
|
||||
createIndex();
|
||||
return Lists.newArrayList(functions.get(name));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<JetProperty> getPropertyDeclarations(@NotNull Name name) {
|
||||
createIndex();
|
||||
return Lists.newArrayList(properties.get(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name) {
|
||||
createIndex();
|
||||
return classesAndObjects.get(name);
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.lazy;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespaceHeader;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class FileBasedDeclarationProviderFactory implements DeclarationProviderFactory {
|
||||
|
||||
private final Collection<JetFile> allFiles;
|
||||
|
||||
private final Multimap<FqName, JetFile> filesByPackage = HashMultimap.create();
|
||||
private final Set<FqName> definedPackages = Sets.newHashSet();
|
||||
private final Map<FqName, DeclarationProvider> packageDeclarationProviders = Maps.newHashMap();
|
||||
private final Map<JetClassOrObject, ClassMemberDeclarationProvider> classMemberDeclarationProviders = Maps.newHashMap();
|
||||
|
||||
private boolean indexed = false;
|
||||
|
||||
public FileBasedDeclarationProviderFactory(@NotNull Collection<JetFile> files) {
|
||||
this.allFiles = files;
|
||||
}
|
||||
|
||||
private void createIndex() {
|
||||
if (indexed) return;
|
||||
indexed = true;
|
||||
|
||||
for (JetFile file : allFiles) {
|
||||
JetNamespaceHeader header = file.getNamespaceHeader();
|
||||
if (header == null) {
|
||||
throw new IllegalArgumentException("Scripts are not supported");
|
||||
}
|
||||
|
||||
FqName packageFqName = new FqName(header.getQualifiedName());
|
||||
addMeAndParentPackages(packageFqName);
|
||||
filesByPackage.put(packageFqName, file);
|
||||
}
|
||||
}
|
||||
|
||||
private void addMeAndParentPackages(@NotNull FqName name) {
|
||||
definedPackages.add(name);
|
||||
if (!name.isRoot()) {
|
||||
addMeAndParentPackages(name.parent());
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ boolean isPackageDeclared(@NotNull FqName packageFqName) {
|
||||
createIndex();
|
||||
return definedPackages.contains(packageFqName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeclarationProvider getPackageMemberDeclarationProvider(@NotNull FqName packageFqName) {
|
||||
createIndex();
|
||||
|
||||
DeclarationProvider declarationProvider = packageDeclarationProviders.get(packageFqName);
|
||||
if (declarationProvider != null) {
|
||||
return declarationProvider;
|
||||
}
|
||||
|
||||
if (!isPackageDeclared(packageFqName)) return null;
|
||||
|
||||
FileBasedPackageMemberDeclarationProvider provider =
|
||||
new FileBasedPackageMemberDeclarationProvider(packageFqName, this, filesByPackage.get(packageFqName));
|
||||
packageDeclarationProviders.put(packageFqName, provider);
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ClassMemberDeclarationProvider getClassMemberDeclarationProvider(@NotNull JetClassOrObject jetClassOrObject) {
|
||||
createIndex();
|
||||
|
||||
ClassMemberDeclarationProvider declarationProvider = classMemberDeclarationProviders.get(jetClassOrObject);
|
||||
if (declarationProvider != null) {
|
||||
return declarationProvider;
|
||||
}
|
||||
|
||||
JetFile file = (JetFile) jetClassOrObject.getContainingFile();
|
||||
|
||||
if (!filesByPackage.containsKey(new FqName(file.getNamespaceHeader().getQualifiedName()))) {
|
||||
throw new IllegalStateException("This factory doesn't know about this class: " + jetClassOrObject);
|
||||
}
|
||||
|
||||
ClassMemberDeclarationProvider provider = new PsiBasedClassMemberDeclarationProvider(jetClassOrObject);
|
||||
classMemberDeclarationProviders.put(jetClassOrObject, provider);
|
||||
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.lazy;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class FileBasedPackageMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider {
|
||||
|
||||
private final FqName fqName;
|
||||
private final FileBasedDeclarationProviderFactory factory;
|
||||
private final Collection<JetFile> allFiles;
|
||||
|
||||
|
||||
/*package*/ FileBasedPackageMemberDeclarationProvider(
|
||||
@NotNull FqName fqName,
|
||||
@NotNull FileBasedDeclarationProviderFactory factory,
|
||||
@NotNull Collection<JetFile> allFiles
|
||||
) {
|
||||
this.fqName = fqName;
|
||||
this.factory = factory;
|
||||
this.allFiles = allFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doCreateIndex() {
|
||||
for (JetFile file : allFiles) {
|
||||
for (JetDeclaration declaration : file.getDeclarations()) {
|
||||
putToIndex(declaration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPackageDeclared(@NotNull Name name) {
|
||||
return factory.isPackageDeclared(fqName.child(name));
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.lazy;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class PsiBasedClassMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider implements ClassMemberDeclarationProvider {
|
||||
|
||||
private final JetClassOrObject classOrObject;
|
||||
|
||||
public PsiBasedClassMemberDeclarationProvider(@NotNull JetClassOrObject classOrObject) {
|
||||
this.classOrObject = classOrObject;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JetClassOrObject getOwnerClassOrObject() {
|
||||
return classOrObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doCreateIndex() {
|
||||
for (JetDeclaration declaration : classOrObject.getDeclarations()) {
|
||||
putToIndex(declaration);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPackageDeclared(@NotNull Name name) {
|
||||
throw new UnsupportedOperationException("Shouldn't be called on a ClassMemberDeclarationProvider");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user