JetTypeParameter stub

This commit is contained in:
Nikolay Krasko
2012-06-19 21:30:41 +04:00
parent 8df1f1b125
commit a3649c47c0
15 changed files with 261 additions and 51 deletions
@@ -43,7 +43,7 @@ public interface JetNodeTypes {
JetNodeType ANONYMOUS_INITIALIZER = new JetNodeType("ANONYMOUS_INITIALIZER", JetClassInitializer.class); JetNodeType ANONYMOUS_INITIALIZER = new JetNodeType("ANONYMOUS_INITIALIZER", JetClassInitializer.class);
JetNodeType TYPE_PARAMETER_LIST = new JetNodeType("TYPE_PARAMETER_LIST", JetTypeParameterList.class); JetNodeType TYPE_PARAMETER_LIST = new JetNodeType("TYPE_PARAMETER_LIST", JetTypeParameterList.class);
JetNodeType TYPE_PARAMETER = new JetNodeType("TYPE_PARAMETER", JetTypeParameter.class); IElementType TYPE_PARAMETER = JetStubElementTypes.TYPE_PARAMETER;
JetNodeType DELEGATION_SPECIFIER_LIST = new JetNodeType("DELEGATION_SPECIFIER_LIST", JetDelegationSpecifierList.class); JetNodeType DELEGATION_SPECIFIER_LIST = new JetNodeType("DELEGATION_SPECIFIER_LIST", JetDelegationSpecifierList.class);
JetNodeType DELEGATOR_BY = new JetNodeType("DELEGATOR_BY", JetDelegatorByExpressionSpecifier.class); JetNodeType DELEGATOR_BY = new JetNodeType("DELEGATOR_BY", JetDelegatorByExpressionSpecifier.class);
JetNodeType DELEGATOR_SUPER_CALL = new JetNodeType("DELEGATOR_SUPER_CALL", JetDelegatorToSuperCall.class); JetNodeType DELEGATOR_SUPER_CALL = new JetNodeType("DELEGATOR_SUPER_CALL", JetDelegatorToSuperCall.class);
@@ -145,6 +145,11 @@ public class JetClass extends JetTypeParameterListOwnerStub<PsiJetClassStub> imp
} }
public boolean isTrait() { public boolean isTrait() {
PsiJetClassStub stub = getStub();
if (stub != null) {
return stub.isTrait();
}
return findChildByType(JetTokens.TRAIT_KEYWORD) != null; return findChildByType(JetTokens.TRAIT_KEYWORD) != null;
} }
@@ -17,20 +17,27 @@
package org.jetbrains.jet.lang.psi; package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.stubs.IStubElementType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes; import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
import org.jetbrains.jet.lang.types.Variance; import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
/** /**
* @author max * @author max
*/ */
public class JetTypeParameter extends JetNamedDeclarationNotStubbed { public class JetTypeParameter extends JetNamedDeclarationStub<PsiJetTypeParameterStub> {
public JetTypeParameter(@NotNull ASTNode node) { public JetTypeParameter(@NotNull ASTNode node) {
super(node); super(node);
} }
public JetTypeParameter(@NotNull PsiJetTypeParameterStub stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
}
@Override @Override
public void accept(@NotNull JetVisitorVoid visitor) { public void accept(@NotNull JetVisitorVoid visitor) {
visitor.visitTypeParameter(this); visitor.visitTypeParameter(this);
@@ -43,6 +50,13 @@ public class JetTypeParameter extends JetNamedDeclarationNotStubbed {
@NotNull @NotNull
public Variance getVariance() { public Variance getVariance() {
PsiJetTypeParameterStub stub = getStub();
if (stub != null) {
if (stub.isOutVariance()) return Variance.OUT_VARIANCE;
if (stub.isInVariance()) return Variance.IN_VARIANCE;
return Variance.INVARIANT;
}
JetModifierList modifierList = getModifierList(); JetModifierList modifierList = getModifierList();
if (modifierList == null) return Variance.INVARIANT; if (modifierList == null) return Variance.INVARIANT;
@@ -55,4 +69,10 @@ public class JetTypeParameter extends JetNamedDeclarationNotStubbed {
public JetTypeReference getExtendsBound() { public JetTypeReference getExtendsBound() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
} }
@NotNull
@Override
public IStubElementType getElementType() {
return JetStubElementTypes.TYPE_PARAMETER;
}
} }
@@ -17,7 +17,6 @@
package org.jetbrains.jet.lang.psi; package org.jetbrains.jet.lang.psi;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
@@ -25,12 +24,6 @@ import java.util.List;
* @author Nikolay Krasko * @author Nikolay Krasko
*/ */
public interface JetTypeParameterListOwner extends JetNamedDeclaration { public interface JetTypeParameterListOwner extends JetNamedDeclaration {
@Nullable
JetTypeParameterList getTypeParameterList();
@Nullable
JetTypeConstraintList getTypeConstraintList();
@NotNull @NotNull
List<JetTypeConstraint> getTypeConstraints(); List<JetTypeConstraint> getTypeConstraints();
@@ -34,15 +34,13 @@ abstract class JetTypeParameterListOwnerNotStubbed extends JetNamedDeclarationNo
super(node); super(node);
} }
@Override
@Nullable @Nullable
public JetTypeParameterList getTypeParameterList() { JetTypeParameterList getTypeParameterList() {
return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST); return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST);
} }
@Override
@Nullable @Nullable
public JetTypeConstraintList getTypeConstraintList() { JetTypeConstraintList getTypeConstraintList() {
return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST); return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST);
} }
@@ -18,7 +18,7 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement; import com.intellij.psi.stubs.NamedStub;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes; import org.jetbrains.jet.JetNodeTypes;
@@ -29,7 +29,7 @@ import java.util.List;
/** /**
* @author Nikolay Krasko * @author Nikolay Krasko
*/ */
abstract class JetTypeParameterListOwnerStub<T extends StubElement> extends JetNamedDeclarationStub<T> implements JetTypeParameterListOwner { abstract class JetTypeParameterListOwnerStub<T extends NamedStub> extends JetNamedDeclarationStub<T> implements JetTypeParameterListOwner {
public JetTypeParameterListOwnerStub(@NotNull T stub, @NotNull IStubElementType nodeType) { public JetTypeParameterListOwnerStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType); super(stub, nodeType);
} }
@@ -38,15 +38,13 @@ abstract class JetTypeParameterListOwnerStub<T extends StubElement> extends JetN
super(node); super(node);
} }
@Override
@Nullable @Nullable
public JetTypeParameterList getTypeParameterList() { JetTypeParameterList getTypeParameterList() {
return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST); return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST);
} }
@Override
@Nullable @Nullable
public JetTypeConstraintList getTypeConstraintList() { JetTypeConstraintList getTypeConstraintList() {
return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST); return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST);
} }
@@ -28,15 +28,12 @@ import java.util.List;
* @author Nikolay Krasko * @author Nikolay Krasko
*/ */
public interface PsiJetClassStub extends NamedStub<JetClass> { public interface PsiJetClassStub extends NamedStub<JetClass> {
@NonNls @NonNls
@Nullable @Nullable
String getQualifiedName(); String getQualifiedName();
boolean isTrait();
@NotNull @NotNull
List<String> getSuperNames(); List<String> getSuperNames();
boolean isDeprecated();
boolean hasDeprecatedAnnotation();
} }
@@ -0,0 +1,29 @@
/*
* 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.psi.stubs;
import com.intellij.psi.stubs.NamedStub;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
/**
* @author Nikolay Krasko
*/
public interface PsiJetTypeParameterStub extends NamedStub<JetTypeParameter> {
String getExtendBoundTypeText();
boolean isInVariance();
boolean isOutVariance();
}
@@ -63,14 +63,16 @@ public class JetClassElementType extends JetStubElementType<PsiJetClassStub, Jet
public PsiJetClassStub createStub(@NotNull JetClass psi, StubElement parentStub) { public PsiJetClassStub createStub(@NotNull JetClass psi, StubElement parentStub) {
FqName fqName = JetPsiUtil.getFQName(psi); FqName fqName = JetPsiUtil.getFQName(psi);
return new PsiJetClassStubImpl(JetStubElementTypes.CLASS, parentStub, fqName != null ? fqName.getFqName() : null, psi.getName(), return new PsiJetClassStubImpl(JetStubElementTypes.CLASS, parentStub, fqName != null ? fqName.getFqName() : null, psi.getName(),
psi.getSuperNames()); psi.getSuperNames(), psi.isTrait());
} }
@Override @Override
public void serialize(PsiJetClassStub stub, StubOutputStream dataStream) throws IOException { public void serialize(PsiJetClassStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName()); dataStream.writeName(stub.getName());
dataStream.writeName(stub.getQualifiedName()); dataStream.writeName(stub.getQualifiedName());
final List<String> superNames = stub.getSuperNames(); dataStream.writeBoolean(stub.isTrait());
List<String> superNames = stub.getSuperNames();
dataStream.writeVarInt(superNames.size()); dataStream.writeVarInt(superNames.size());
for (String name : superNames) { for (String name : superNames) {
dataStream.writeName(name); dataStream.writeName(name);
@@ -79,18 +81,17 @@ public class JetClassElementType extends JetStubElementType<PsiJetClassStub, Jet
@Override @Override
public PsiJetClassStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException { public PsiJetClassStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
final StringRef name = dataStream.readName(); StringRef name = dataStream.readName();
final StringRef qualifiedName = dataStream.readName(); StringRef qualifiedName = dataStream.readName();
final int superCount = dataStream.readVarInt(); boolean isTrait = dataStream.readBoolean();
final StringRef[] superNames = StringRef.createArray(superCount);
int superCount = dataStream.readVarInt();
StringRef[] superNames = StringRef.createArray(superCount);
for (int i = 0; i < superCount; i++) { for (int i = 0; i < superCount; i++) {
superNames[i] = dataStream.readName(); superNames[i] = dataStream.readName();
} }
final JetClassElementType type = JetStubElementTypes.CLASS; return new PsiJetClassStubImpl(JetStubElementTypes.CLASS, parentStub, qualifiedName, name, superNames, isTrait);
final PsiJetClassStubImpl classStub = new PsiJetClassStubImpl(type, parentStub, qualifiedName, name, superNames);
return classStub;
} }
@Override @Override
@@ -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 = 5; public static final int STUB_VERSION = 6;
public JetFileElementType() { public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE); super("jet.FILE", JetLanguage.INSTANCE);
@@ -24,4 +24,6 @@ public interface JetStubElementTypes {
JetClassElementType CLASS = new JetClassElementType("CLASS"); JetClassElementType CLASS = new JetClassElementType("CLASS");
JetFunctionElementType FUNCTION = new JetFunctionElementType("FUN"); JetFunctionElementType FUNCTION = new JetFunctionElementType("FUN");
JetTypeParameterElementType TYPE_PARAMETER = new JetTypeParameterElementType("TYPE_PARAMETER");
} }
@@ -0,0 +1,93 @@
/*
* 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.psi.stubs.elements;
import com.intellij.lang.ASTNode;
import com.intellij.lang.LighterAST;
import com.intellij.lang.LighterASTNode;
import com.intellij.psi.stubs.IndexSink;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub;
import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetTypeParameterStubImpl;
import org.jetbrains.jet.lang.types.Variance;
import java.io.IOException;
/**
* @author Nikolay Krasko
*/
public class JetTypeParameterElementType extends JetStubElementType<PsiJetTypeParameterStub, JetTypeParameter> {
public JetTypeParameterElementType(@NotNull @NonNls String debugName) {
super(debugName);
}
@Override
public JetTypeParameter createPsiFromAst(@NotNull ASTNode node) {
return new JetTypeParameter(node);
}
@Override
public PsiJetTypeParameterStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
return null;
}
@Override
public JetTypeParameter createPsi(@NotNull PsiJetTypeParameterStub stub) {
return new JetTypeParameter(stub, JetStubElementTypes.TYPE_PARAMETER);
}
@Override
public PsiJetTypeParameterStub createStub(@NotNull JetTypeParameter psi, StubElement parentStub) {
JetTypeReference extendsBound = psi.getExtendsBound();
return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub,
psi.getName(),
extendsBound != null ? extendsBound.getText() : null,
psi.getVariance() == Variance.IN_VARIANCE,
psi.getVariance() == Variance.OUT_VARIANCE);
}
@Override
public void serialize(PsiJetTypeParameterStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName());
dataStream.writeName(stub.getExtendBoundTypeText());
dataStream.writeBoolean(stub.isInVariance());
dataStream.writeBoolean(stub.isOutVariance());
}
@Override
public PsiJetTypeParameterStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
StringRef name = dataStream.readName();
StringRef extendBoundTypeText = dataStream.readName();
boolean isInVariance = dataStream.readBoolean();
boolean isOutVariance = dataStream.readBoolean();
return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub,
name, extendBoundTypeText, isInVariance, isOutVariance);
}
@Override
public void indexStub(PsiJetTypeParameterStub stub, IndexSink sink) {
// No index
}
}
@@ -36,15 +36,16 @@ public class PsiJetClassStubImpl extends StubBase<JetClass> implements PsiJetCla
private final StringRef qualifiedName; private final StringRef qualifiedName;
private final StringRef name; private final StringRef name;
private final StringRef[] superNames; private final StringRef[] superNames;
private final boolean isTrait;
public PsiJetClassStubImpl( public PsiJetClassStubImpl(
JetClassElementType type, JetClassElementType type,
final StubElement parent, StubElement parent,
@Nullable final String qualifiedName, @Nullable final String qualifiedName,
final String name, String name,
final List<String> superNames) { List<String> superNames,
boolean isTrait) {
this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), wrapStrings(superNames)); this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), wrapStrings(superNames), isTrait);
} }
private static StringRef[] wrapStrings(List<String> names) { private static StringRef[] wrapStrings(List<String> names) {
@@ -57,15 +58,17 @@ public class PsiJetClassStubImpl extends StubBase<JetClass> implements PsiJetCla
public PsiJetClassStubImpl( public PsiJetClassStubImpl(
JetClassElementType type, JetClassElementType type,
final StubElement parent, StubElement parent,
final StringRef qualifiedName, StringRef qualifiedName,
final StringRef name, StringRef name,
final StringRef[] superNames) { StringRef[] superNames,
boolean isTrait) {
super(parent, type); super(parent, type);
this.qualifiedName = qualifiedName; this.qualifiedName = qualifiedName;
this.name = name; this.name = name;
this.superNames = superNames; this.superNames = superNames;
this.isTrait = isTrait;
} }
@Override @Override
@@ -74,13 +77,8 @@ public class PsiJetClassStubImpl extends StubBase<JetClass> implements PsiJetCla
} }
@Override @Override
public boolean isDeprecated() { public boolean isTrait() {
return false; return isTrait;
}
@Override
public boolean hasDeprecatedAnnotation() {
return false;
} }
@Override @Override
@@ -0,0 +1,68 @@
/*
* 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.psi.stubs.impl;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetTypeParameterElementType;
/**
* @author Nikolay Krasko
*/
public class PsiJetTypeParameterStubImpl extends StubBase<JetTypeParameter> implements PsiJetTypeParameterStub {
private final StringRef name;
private final StringRef extendBoundTypeText;
private final boolean isInVariance;
private final boolean isOutVariance;
public PsiJetTypeParameterStubImpl(JetTypeParameterElementType type, final StubElement parent,
StringRef name, StringRef extendBoundTypeText, boolean isInVariance, boolean isOutVariance) {
super(parent, type);
this.name = name;
this.extendBoundTypeText = extendBoundTypeText;
this.isInVariance = isInVariance;
this.isOutVariance = isOutVariance;
}
public PsiJetTypeParameterStubImpl(JetTypeParameterElementType type, final StubElement parent,
String name, String extendBoundTypeText, boolean isInVariance, boolean isOutVariance) {
this(type, parent, StringRef.fromString(name), StringRef.fromString(extendBoundTypeText), isInVariance, isOutVariance);
}
@Override
public String getExtendBoundTypeText() {
return StringRef.toString(extendBoundTypeText);
}
@Override
public boolean isInVariance() {
return isInVariance;
}
@Override
public boolean isOutVariance() {
return isOutVariance;
}
@Override
public String getName() {
return StringRef.toString(name);
}
}
@@ -47,4 +47,12 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase {
final List<String> names = stub.getSuperNames(); final List<String> names = stub.getSuperNames();
assertSameElements(names, "ArrayList", "alist"); assertSameElements(names, "ArrayList", "alist");
} }
public void testClassIsTrait() {
PsiFile psiFile = myFixture.configureByText("foo.kt", "trait Test { }");
final List<JetDeclaration> declarations = ((JetFile) psiFile).getDeclarations();
final JetClass jetClass = (JetClass) declarations.get(0);
final PsiJetClassStub stub = JetStubElementTypes.CLASS.createStub(jetClass, null);
assertEquals(true, stub.isTrait());
}
} }