Add qualified name to top level functions stub

This commit is contained in:
Nikolay Krasko
2012-06-28 18:45:11 +04:00
parent 2f64000917
commit fed9df9858
6 changed files with 75 additions and 23 deletions
@@ -85,15 +85,15 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
} }
/** /**
* Returns full qualified name for function "package_fqn.function_name" * Returns full qualified name for function "package_fqn.function_name"
* Not null for top level functions. * Not null for top level functions.
* @return * @return
*/ */
@Nullable @Nullable
public FqName getQualifiedName() { public FqName getQualifiedName() {
final PsiJetFunctionStub stub = getStub(); final PsiJetFunctionStub stub = getStub();
if (stub != null) { if (stub != null) {
// TODO (stubs): return stub.getQualifiedName(); return stub.getTopFQName();
} }
PsiElement parent = getParent(); PsiElement parent = getParent();
@@ -18,12 +18,17 @@ package org.jetbrains.jet.lang.psi.stubs;
import com.intellij.psi.stubs.NamedStub; import com.intellij.psi.stubs.NamedStub;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetNamedFunction; import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.resolve.name.FqName;
/** /**
* @author Nikolay Krasko * @author Nikolay Krasko
*/ */
public interface PsiJetFunctionStub extends NamedStub<JetNamedFunction> { public interface PsiJetFunctionStub extends NamedStub<JetNamedFunction> {
@Nullable
FqName getTopFQName();
/** /**
* Is function defined in directly in package. * Is function defined in directly in package.
* @return * @return
@@ -38,7 +38,7 @@ import java.io.IOException;
* @author Nikolay Krasko * @author Nikolay Krasko
*/ */
public class JetFileElementType extends IStubFileElementType<PsiJetFileStub> { public class JetFileElementType extends IStubFileElementType<PsiJetFileStub> {
public static final int STUB_VERSION = 15; public static final int STUB_VERSION = 16;
public JetFileElementType() { public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE); super("jet.FILE", JetLanguage.INSTANCE);
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.psi.stubs.elements; package org.jetbrains.jet.lang.psi.stubs.elements;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.IndexSink; import com.intellij.psi.stubs.IndexSink;
import com.intellij.psi.stubs.StubElement; import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream; import com.intellij.psi.stubs.StubInputStream;
@@ -28,6 +29,7 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.stubs.PsiJetFileStub; import org.jetbrains.jet.lang.psi.stubs.PsiJetFileStub;
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub; import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetFunctionStubImpl; import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetFunctionStubImpl;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.io.IOException; import java.io.IOException;
@@ -50,30 +52,51 @@ public class JetFunctionElementType extends JetStubElementType<PsiJetFunctionStu
return new JetNamedFunction(stub); return new JetNamedFunction(stub);
} }
@Override
public boolean shouldCreateStub(ASTNode node) {
if (super.shouldCreateStub(node)) {
PsiElement psi = node.getPsi();
if (psi instanceof JetNamedFunction) {
JetNamedFunction function = (JetNamedFunction) psi;
return function.getName() != null;
}
}
return false;
}
@Override @Override
public PsiJetFunctionStub createStub(@NotNull JetNamedFunction psi, @NotNull StubElement parentStub) { public PsiJetFunctionStub createStub(@NotNull JetNamedFunction psi, @NotNull StubElement parentStub) {
final boolean isTopLevel = parentStub instanceof PsiJetFileStub; final boolean isTopLevel = parentStub instanceof PsiJetFileStub;
final boolean isExtension = psi.getReceiverTypeRef() != null; final boolean isExtension = psi.getReceiverTypeRef() != null;
return new PsiJetFunctionStubImpl( FqName qualifiedName = psi.getQualifiedName();
JetStubElementTypes.FUNCTION, parentStub, psi.getName(),
isTopLevel, isExtension); return new PsiJetFunctionStubImpl(JetStubElementTypes.FUNCTION, parentStub, psi.getName(), isTopLevel, qualifiedName, isExtension);
} }
@Override @Override
public void serialize(PsiJetFunctionStub stub, StubOutputStream dataStream) throws IOException { public void serialize(PsiJetFunctionStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName()); dataStream.writeName(stub.getName());
dataStream.writeBoolean(stub.isTopLevel()); dataStream.writeBoolean(stub.isTopLevel());
FqName topFQName = stub.getTopFQName();
dataStream.writeName(topFQName != null ? topFQName.toString() : null);
dataStream.writeBoolean(stub.isExtension()); dataStream.writeBoolean(stub.isExtension());
} }
@Override @Override
public PsiJetFunctionStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException { public PsiJetFunctionStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
final StringRef name = dataStream.readName(); StringRef name = dataStream.readName();
final boolean isTopLevel = dataStream.readBoolean(); boolean isTopLevel = dataStream.readBoolean();
final boolean isExtension = dataStream.readBoolean();
return new PsiJetFunctionStubImpl(JetStubElementTypes.FUNCTION, parentStub, name, isTopLevel, isExtension); StringRef topFQNameStr = dataStream.readName();
FqName fqName = topFQNameStr != null ? new FqName(topFQNameStr.toString()) : null;
boolean isExtension = dataStream.readBoolean();
return new PsiJetFunctionStubImpl(JetStubElementTypes.FUNCTION, parentStub, name, isTopLevel, fqName, isExtension);
} }
@Override @Override
@@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetNamedFunction; import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub; import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
import org.jetbrains.jet.lang.resolve.name.FqName;
/** /**
* @author Nikolay Krasko * @author Nikolay Krasko
@@ -34,17 +35,33 @@ public class PsiJetFunctionStubImpl extends StubBase<JetNamedFunction> implement
private final StringRef nameRef; private final StringRef nameRef;
private final boolean isTopLevel; private final boolean isTopLevel;
private final boolean isExtension; private final boolean isExtension;
private final FqName topFQName;
public PsiJetFunctionStubImpl(@NotNull IStubElementType elementType, @NotNull StubElement parent,
@Nullable String name, boolean isTopLevel, boolean isExtension) { public PsiJetFunctionStubImpl(
this(elementType, parent, StringRef.fromString(name), isTopLevel, isExtension); @NotNull IStubElementType elementType,
@NotNull StubElement parent,
@Nullable String name,
boolean isTopLevel,
@Nullable FqName topFQName,
boolean isExtension) {
this(elementType, parent, StringRef.fromString(name), isTopLevel, topFQName, isExtension);
} }
public PsiJetFunctionStubImpl(@NotNull IStubElementType elementType, @NotNull StubElement parent, public PsiJetFunctionStubImpl(
@Nullable StringRef nameRef, boolean isTopLevel, boolean isExtension) { @NotNull IStubElementType elementType,
@NotNull StubElement parent,
@Nullable StringRef nameRef,
boolean isTopLevel,
@Nullable FqName topFQName,
boolean isExtension) {
super(parent, elementType); super(parent, elementType);
if (isTopLevel && topFQName == null) {
throw new IllegalArgumentException("topFQName shouldn't be null for top level functions");
}
this.nameRef = nameRef; this.nameRef = nameRef;
this.topFQName = topFQName;
this.isTopLevel = isTopLevel; this.isTopLevel = isTopLevel;
this.isExtension = isExtension; this.isExtension = isExtension;
} }
@@ -54,6 +71,12 @@ public class PsiJetFunctionStubImpl extends StubBase<JetNamedFunction> implement
return StringRef.toString(nameRef); return StringRef.toString(nameRef);
} }
@Nullable
@Override
public FqName getTopFQName() {
return topFQName;
}
@Override @Override
public boolean isTopLevel() { public boolean isTopLevel() {
return isTopLevel; return isTopLevel;
@@ -77,7 +100,8 @@ public class PsiJetFunctionStubImpl extends StubBase<JetNamedFunction> implement
builder.append("PsiJetFunctionStubImpl["); builder.append("PsiJetFunctionStubImpl[");
if (isTopLevel()) { if (isTopLevel()) {
builder.append("top "); assert topFQName != null;
builder.append("top ").append("topFQName=").append(topFQName.toString()).append(" ");
} }
if (isExtension()) { if (isExtension()) {
@@ -78,7 +78,7 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase {
public void testFunctionParameters() { public void testFunctionParameters() {
doBuildTest("fun some(t: Int, other: String = \"hello\") { }", doBuildTest("fun some(t: Int, other: String = \"hello\") { }",
"PsiJetFileStubImpl[package=]\n" + "PsiJetFileStubImpl[package=]\n" +
" FUN:PsiJetFunctionStubImpl[top name=some]\n" + " FUN:PsiJetFunctionStubImpl[top topFQName=some name=some]\n" +
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n" + " VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n" +
" VALUE_PARAMETER:PsiJetParameterStubImpl[val name=t typeText=Int defaultValue=null]\n" + " VALUE_PARAMETER:PsiJetParameterStubImpl[val name=t typeText=Int defaultValue=null]\n" +
" VALUE_PARAMETER:PsiJetParameterStubImpl[val name=other typeText=String defaultValue=\"hello\"]\n"); " VALUE_PARAMETER:PsiJetParameterStubImpl[val name=other typeText=String defaultValue=\"hello\"]\n");
@@ -87,14 +87,14 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase {
public void testNotStoreInFunction() { public void testNotStoreInFunction() {
doBuildTest("fun some() { val test = 12;\n fun fake() {}\n class FakeClass\n }", doBuildTest("fun some() { val test = 12;\n fun fake() {}\n class FakeClass\n }",
"PsiJetFileStubImpl[package=]\n" + "PsiJetFileStubImpl[package=]\n" +
" FUN:PsiJetFunctionStubImpl[top name=some]\n" + " FUN:PsiJetFunctionStubImpl[top topFQName=some name=some]\n" +
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n"); " VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n");
} }
public void testNotStorePropertyFromInitializer() { public void testNotStorePropertyFromInitializer() {
doBuildTest("fun DoubleArray.some() = for (element in this) println(element)", doBuildTest("fun DoubleArray.some() = for (element in this) println(element)",
"PsiJetFileStubImpl[package=]\n" + "PsiJetFileStubImpl[package=]\n" +
" FUN:PsiJetFunctionStubImpl[top ext name=some]\n" + " FUN:PsiJetFunctionStubImpl[top topFQName=some ext name=some]\n" +
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n"); " VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n");
} }