Index package-level properties

This commit is contained in:
Nikolay Krasko
2012-07-02 15:13:23 +04:00
parent 3a196d6718
commit de4e302578
19 changed files with 209 additions and 52 deletions
@@ -74,15 +74,19 @@ public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStu
}
public boolean isLocal() {
PsiJetPropertyStub stub = getStub();
if (stub != null) {
return stub.isLocal();
}
PsiElement parent = getParent();
return !(parent instanceof JetFile || parent instanceof JetClassBody || parent instanceof JetNamespaceBody);
}
public boolean isTopLevel() {
PsiJetPropertyStub stub = getStub();
if (stub != null) {
return stub.isTopLevel();
}
return getParent() instanceof JetFile;
}
@NotNull
@Override
public SearchScope getUseScope() {
@@ -165,8 +165,8 @@ public class JetPsiUtil {
return getFQName(objectDeclaration);
}
Name functionName = namedDeclaration.getNameAsName();
if (functionName == null) {
Name name = namedDeclaration.getNameAsName();
if (name == null) {
return null;
}
@@ -180,7 +180,7 @@ public class JetPsiUtil {
if (parent instanceof JetFile) {
firstPart = getFQName((JetFile) parent);
}
else if (parent instanceof JetNamedDeclaration) {
else if (parent instanceof JetNamedFunction || parent instanceof JetClass || parent instanceof JetObjectDeclaration) {
firstPart = getFQName((JetNamedDeclaration) parent);
}
@@ -188,7 +188,7 @@ public class JetPsiUtil {
return null;
}
return firstPart.child(functionName);
return firstPart.child(name);
}
@Nullable @IfNotParsed
@@ -17,14 +17,21 @@
package org.jetbrains.jet.lang.psi.stubs;
import com.intellij.psi.stubs.NamedStub;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.resolve.name.FqName;
/**
* @author Nikolay Krasko
*/
public interface PsiJetPropertyStub extends NamedStub<JetProperty> {
boolean isVar();
boolean isLocal();
boolean isTopLevel();
@Nullable
FqName getTopFQName();
String getTypeText();
String getInferenceBodyText();
}
@@ -38,7 +38,7 @@ import java.io.IOException;
* @author Nikolay Krasko
*/
public class JetFileElementType extends IStubFileElementType<PsiJetFileStub> {
public static final int STUB_VERSION = 16;
public static final int STUB_VERSION = 17;
public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE);
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.psi.stubs.elements;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.IndexSink;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
@@ -26,9 +27,11 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetPropertyStubImpl;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.io.IOException;
@@ -50,13 +53,28 @@ public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStu
return new JetProperty(stub, JetStubElementTypes.PROPERTY);
}
@Override
public boolean shouldCreateStub(ASTNode node) {
if (super.shouldCreateStub(node)) {
PsiElement psi = node.getPsi();
if (psi instanceof JetProperty) {
JetProperty property = (JetProperty) psi;
return property.getName() != null;
}
}
return false;
}
@Override
public PsiJetPropertyStub createStub(@NotNull JetProperty psi, StubElement parentStub) {
JetTypeReference typeRef = psi.getPropertyTypeRef();
JetExpression expression = psi.getInitializer();
assert !psi.isLocal() : "Should not store local property";
return new PsiJetPropertyStubImpl(JetStubElementTypes.PROPERTY, parentStub,
psi.getName(), psi.isVar(), psi.isLocal(),
psi.getName(), psi.isVar(), psi.isTopLevel(), JetPsiUtil.getFQName(psi),
typeRef != null ? typeRef.getText() : null,
expression != null ? expression.getText() : null);
}
@@ -65,7 +83,11 @@ public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStu
public void serialize(PsiJetPropertyStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName());
dataStream.writeBoolean(stub.isVar());
dataStream.writeBoolean(stub.isLocal());
dataStream.writeBoolean(stub.isTopLevel());
FqName topFQName = stub.getTopFQName();
dataStream.writeName(topFQName != null ? topFQName.toString() : null);
dataStream.writeName(stub.getTypeText());
dataStream.writeName(stub.getInferenceBodyText());
}
@@ -74,16 +96,20 @@ public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStu
public PsiJetPropertyStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
StringRef name = dataStream.readName();
boolean isVar = dataStream.readBoolean();
boolean isLocal = dataStream.readBoolean();
boolean isTopLevel = dataStream.readBoolean();
StringRef topFQNameStr = dataStream.readName();
FqName fqName = topFQNameStr != null ? new FqName(topFQNameStr.toString()) : null;
StringRef typeText = dataStream.readName();
StringRef inferenceBodyText = dataStream.readName();
return new PsiJetPropertyStubImpl(JetStubElementTypes.PROPERTY, parentStub,
name, isVar, isLocal, typeText, inferenceBodyText);
name, isVar, isTopLevel, fqName, typeText, inferenceBodyText);
}
@Override
public void indexStub(PsiJetPropertyStub stub, IndexSink sink) {
// No index
StubIndexServiceFactory.getInstance().indexProperty(stub, sink);
}
}
@@ -23,7 +23,10 @@ import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.JetBlockExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFunctionLiteral;
import org.jetbrains.jet.lang.psi.JetWithExpressionInitializer;
import org.jetbrains.jet.plugin.JetLanguage;
/**
@@ -53,10 +56,7 @@ public abstract class JetStubElementType<StubT extends StubElement, PsiT extends
// Don't create stubs if declaration is inside function or property accessor with block
JetBlockExpression blockExpression = PsiTreeUtil.getParentOfType(psi, JetBlockExpression.class);
@SuppressWarnings("unchecked") JetDeclarationWithBody stubStopElement =
PsiTreeUtil.getParentOfType(blockExpression, JetFunction.class, JetPropertyAccessor.class);
if (stubStopElement != null) {
if (blockExpression != null) {
return false;
}
@@ -20,6 +20,7 @@ import com.intellij.psi.stubs.IndexSink;
import org.jetbrains.jet.lang.psi.stubs.PsiJetClassStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetObjectStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
/**
* @author Nikolay Krasko
@@ -41,9 +42,14 @@ public interface StubIndexService {
@Override
public void indexObject(PsiJetObjectStub stub, IndexSink sink) {
}
@Override
public void indexProperty(PsiJetPropertyStub stub, IndexSink sink) {
}
};
void indexClass(PsiJetClassStub stub, IndexSink sink);
void indexFunction(PsiJetFunctionStub stub, IndexSink sink);
void indexObject(PsiJetObjectStub stub, IndexSink sink);
void indexProperty(PsiJetPropertyStub stub, IndexSink sink);
}
@@ -20,8 +20,10 @@ import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
import org.jetbrains.jet.lang.resolve.name.FqName;
/**
* @author Nikolay Krasko
@@ -29,26 +31,33 @@ import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
public class PsiJetPropertyStubImpl extends StubBase<JetProperty> implements PsiJetPropertyStub {
private final StringRef name;
private final boolean isVar;
private final boolean isLocal;
private final boolean isTopLevel;
private final FqName topFQName;
private final StringRef typeText;
private final StringRef inferenceBodyText;
public PsiJetPropertyStubImpl(IStubElementType elementType, StubElement parent, StringRef name,
boolean isVar, boolean isLocal, StringRef typeText, StringRef inferenceBodyText) {
boolean isVar, boolean isTopLevel, @Nullable FqName topFQName, StringRef typeText, StringRef inferenceBodyText) {
super(parent, elementType);
if (isTopLevel && topFQName == null) {
throw new IllegalArgumentException("topFQName shouldn't be null for top level properties");
}
this.name = name;
this.isVar = isVar;
this.isLocal = isLocal;
this.isTopLevel = isTopLevel;
this.topFQName = topFQName;
this.typeText = typeText;
this.inferenceBodyText = inferenceBodyText;
}
public PsiJetPropertyStubImpl(IStubElementType elementType, StubElement parent, String name,
boolean isVal, boolean isLocal,
boolean isVal, boolean isTopLevel, @Nullable FqName topFQName,
String typeText, String inferenceBodyText
) {
this(elementType, parent, StringRef.fromString(name),
isVal, isLocal, StringRef.fromString(typeText), StringRef.fromString(inferenceBodyText));
isVal, isTopLevel, topFQName, StringRef.fromString(typeText), StringRef.fromString(inferenceBodyText));
}
@Override
@@ -57,8 +66,14 @@ public class PsiJetPropertyStubImpl extends StubBase<JetProperty> implements Psi
}
@Override
public boolean isLocal() {
return isLocal;
public boolean isTopLevel() {
return isTopLevel;
}
@Nullable
@Override
public FqName getTopFQName() {
return topFQName;
}
@Override
@@ -84,8 +99,9 @@ public class PsiJetPropertyStubImpl extends StubBase<JetProperty> implements Psi
builder.append(isVar() ? "var " : "val ");
if (isLocal()) {
builder.append("local ");
if (isTopLevel()) {
assert topFQName != null;
builder.append("top ").append("topFQName=").append(topFQName.toString()).append(" ");
}
builder.append("name=").append(getName());
@@ -133,7 +133,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
Set<VariableDescriptor> result = Sets.newLinkedHashSet();
List<JetProperty> declarations = declarationProvider.getPropertyDeclarations(name);
Collection<JetProperty> declarations = declarationProvider.getPropertyDeclarations(name);
for (JetProperty propertyDeclaration : declarations) {
JetScope resolutionScope = getScopeForMemberDeclarationResolution(propertyDeclaration);
result.add(resolveSession.getInjector().getDescriptorResolver().resolvePropertyDescriptor(thisDescriptor, resolutionScope,
@@ -37,7 +37,7 @@ public interface DeclarationProvider {
Collection<JetNamedFunction> getFunctionDeclarations(@NotNull Name name);
@NotNull
List<JetProperty> getPropertyDeclarations(@NotNull Name name);
Collection<JetProperty> getPropertyDeclarations(@NotNull Name name);
@Nullable
JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name);
+1
View File
@@ -185,6 +185,7 @@
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetAllShortFunctionNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetSuperClassIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetTopLevelFunctionsFqnNameIndex" />
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetTopLevelPropertiesFqnNameIndex" />
<contentBasedClassFileProcessor implementation="org.jetbrains.jet.plugin.libraries.JetContentBasedFileSubstitutor" />
<psi.clsCustomNavigationPolicy implementation="org.jetbrains.jet.plugin.libraries.JetClsNavigationPolicy" />
@@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.stubindex;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
/**
* @author Nikolay Krasko
@@ -38,6 +39,9 @@ public interface JetIndexKeys {
StubIndexKey<String, JetNamedFunction> TOP_LEVEL_FUNCTIONS_FQN_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.functions.fqn.name");
StubIndexKey<String, JetProperty> TOP_LEVEL_PROPERTY_FQN_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.property.fqn.name");
StubIndexKey<String, JetNamedFunction> FUNCTIONS_SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.functions.short.name");
}
@@ -0,0 +1,48 @@
/*
* 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.plugin.stubindex;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetProperty;
import java.util.Collection;
/**
* @author Nikolay Krasko
*/
public class JetTopLevelPropertiesFqnNameIndex extends StringStubIndexExtension<JetProperty> {
private static final JetTopLevelPropertiesFqnNameIndex INSTANCE = new JetTopLevelPropertiesFqnNameIndex();
public static JetTopLevelPropertiesFqnNameIndex getInstance() {
return INSTANCE;
}
@NotNull
@Override
public StubIndexKey<String, JetProperty> getKey() {
return JetIndexKeys.TOP_LEVEL_PROPERTY_FQN_NAME_KEY;
}
@Override
public Collection<JetProperty> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(s, project, new JetSourceFilterScope(scope));
}
}
@@ -20,6 +20,7 @@ import com.intellij.psi.stubs.IndexSink;
import org.jetbrains.jet.lang.psi.stubs.PsiJetClassStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetObjectStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
import org.jetbrains.jet.lang.psi.stubs.elements.StubIndexService;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -76,4 +77,14 @@ public class StubIndexServiceImpl implements StubIndexService {
sink.occurrence(JetIndexKeys.FUNCTIONS_SHORT_NAME_KEY, name);
}
}
@Override
public void indexProperty(PsiJetPropertyStub stub, IndexSink sink) {
if (stub.isTopLevel()) {
FqName topFQName = stub.getTopFQName();
if (topFQName != null) {
sink.occurrence(JetIndexKeys.TOP_LEVEL_PROPERTY_FQN_NAME_KEY, topFQName.toString());
}
}
}
}
@@ -19,12 +19,9 @@ package org.jetbrains.jet.plugin.stubindex.resolve;
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.lazy.DeclarationProvider;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import java.util.List;
/**
@@ -37,20 +34,6 @@ public abstract class AbstractStubDeclarationProvider implements DeclarationProv
return null;
}
@NotNull
@Override
public Collection<JetNamedFunction> getFunctionDeclarations(@NotNull Name name) {
// TODO:
return null;
}
@NotNull
@Override
public List<JetProperty> getPropertyDeclarations(@NotNull Name name) {
// TODO:
return null;
}
@Override
public JetClassOrObject getClassOrObjectDeclaration(@NotNull Name name) {
// TODO:
@@ -17,8 +17,13 @@
package org.jetbrains.jet.plugin.stubindex.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.resolve.lazy.ClassMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
/**
* @author Nikolay Krasko
@@ -30,4 +35,18 @@ public class StubClassMemberDeclarationProvider extends AbstractStubDeclarationP
// TODO:
return null;
}
@NotNull
@Override
public Collection<JetNamedFunction> getFunctionDeclarations(@NotNull Name name) {
// TODO:
return null;
}
@NotNull
@Override
public Collection<JetProperty> getPropertyDeclarations(@NotNull Name name) {
// TODO:
return null;
}
}
@@ -30,10 +30,10 @@ import org.jetbrains.jet.lang.resolve.lazy.PackageMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.plugin.stubindex.JetTopLevelFunctionsFqnNameIndex;
import org.jetbrains.jet.plugin.stubindex.JetTopLevelPropertiesFqnNameIndex;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
/**
* @author Nikolay Krasko
@@ -68,8 +68,8 @@ public class StubPackageMemberDeclarationProvider extends AbstractStubDeclaratio
@NotNull
@Override
public List<JetProperty> getPropertyDeclarations(@NotNull Name name) {
return super.getPropertyDeclarations(name); //To change body of overridden methods use File | Settings | File Templates.
public Collection<JetProperty> getPropertyDeclarations(@NotNull Name name) {
return JetTopLevelPropertiesFqnNameIndex.getInstance().get(fqName.child(name).toString(), project, GlobalSearchScope.allScope(project));
}
@Override
@@ -91,6 +91,23 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase {
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n");
}
public void testPackageProperty() {
doBuildTest("package test.testing\n" +
"val some = 12",
"PsiJetFileStubImpl[package=test.testing]\n" +
" PROPERTY:PsiJetPropertyStubImpl[val top topFQName=test.testing.some name=some typeText=null bodyText=12]\n");
}
public void testClassProperty() {
doBuildTest("class More { \n" +
" private val test : Int = 11\n" +
"}",
"PsiJetFileStubImpl[package=]\n" +
" CLASS:PsiJetClassStubImpl[name=More fqn=More superNames=[]]\n" +
" TYPE_PARAMETER_LIST:PsiJetTypeParameterListStubImpl\n" +
" PROPERTY:PsiJetPropertyStubImpl[val name=test typeText=Int bodyText=11]\n");
}
public void testNotStorePropertyFromInitializer() {
doBuildTest("fun DoubleArray.some() = for (element in this) println(element)",
"PsiJetFileStubImpl[package=]\n" +
@@ -21,6 +21,7 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.plugin.JetFileType;
@@ -67,4 +68,18 @@ public class JetStubResolveTest extends LightCodeInsightFixtureTestCase {
assertTrue(functionTexts.contains("fun other(v : Int) = 12"));
assertTrue(functionTexts.contains("fun other(v : String) {}"));
}
public void testPackageProperty() {
myFixture.configureByText(JetFileType.INSTANCE,
"package test.testing\n" +
"val test = 12\n");
StubPackageMemberDeclarationProvider provider =
new StubPackageMemberDeclarationProvider(new FqName("test.testing"), getProject());
List<JetProperty> testProperties = Lists.newArrayList(provider.getPropertyDeclarations(Name.identifier("test")));
assertSize(1, testProperties);
assertEquals("val test = 12", testProperties.get(0).getText());
}
}