Permalink
Browse files

index occurrences of superclass names for Kotlin classes

  • Loading branch information...
1 parent cc0a532 commit 783dbdd605a962e3902f243e47011e8d4b5165ca @yole yole committed May 23, 2012
@@ -39,7 +39,7 @@
* @author max
*/
public class JetClass extends JetTypeParameterListOwner
- implements JetClassOrObject, JetModifierListOwner, StubBasedPsiElement<PsiJetClassStub<?>> {
+ implements JetClassOrObject, JetModifierListOwner, StubBasedPsiElement<PsiJetClassStub> {
private PsiJetClassStub stub;
@@ -158,7 +158,7 @@ public IStubElementType getElementType() {
}
@Override
- public PsiJetClassStub<?> getStub() {
+ public PsiJetClassStub getStub() {
// TODO (stubs)
return null;
}
@@ -198,4 +198,48 @@ private String getQualifiedName() {
Collections.reverse(parts);
return StringUtil.join(parts, ".");
}
+
+ /**
+ * Returns the list of unqualified names that are indexed as the superclass names of this class. For the names that might be imported
+ * via an aliased import, includes both the original and the aliased name (reference resolution during inheritor search will sort this out).
+ *
+ * @return the list of possible superclass names
+ */
+ @NotNull
+ public List<String> getSuperNames() {
+ final JetDelegationSpecifierList delegationSpecifierList = getDelegationSpecifierList();
+ if (delegationSpecifierList == null) return Collections.emptyList();
+ final List<JetDelegationSpecifier> specifiers = delegationSpecifierList.getDelegationSpecifiers();
+ if (specifiers.size() == 0) return Collections.emptyList();
+ List<String> result = new ArrayList<String>();
+ for (JetDelegationSpecifier specifier : specifiers) {
+ final JetTypeReference typeReference = specifier.getTypeReference();
+ if (typeReference != null) {
+ final JetTypeElement typeElement = typeReference.getTypeElement();
+ if (typeElement instanceof JetUserType) {
+ final String referencedName = ((JetUserType) typeElement).getReferencedName();
+ if (referencedName != null) {
+ addSuperName(result, referencedName);
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ private void addSuperName(List<String> result, String referencedName) {
+ result.add(referencedName);
+ if (getContainingFile() instanceof JetFile) {
+ final JetImportDirective directive = ((JetFile) getContainingFile()).findImportByAlias(referencedName);
+ if (directive != null) {
+ JetExpression reference = directive.getImportedReference();
+ while (reference instanceof JetDotQualifiedExpression) {
+ reference = ((JetDotQualifiedExpression) reference).getSelectorExpression();
+ }
+ if (reference instanceof JetSimpleNameExpression) {
+ result.add(((JetSimpleNameExpression) reference).getReferencedName());
+ }
+ }
+ }
+ }
}
@@ -61,6 +61,16 @@ public String toString() {
return PsiTreeUtil.getChildrenOfTypeAsList(this, JetImportDirective.class);
}
+ @Nullable
+ public JetImportDirective findImportByAlias(@NotNull String name) {
+ for (JetImportDirective directive : getImportDirectives()) {
+ if (name.equals(directive.getAliasName())) {
+ return directive;
+ }
+ }
+ return null;
+ }
+
// scripts has no namespace header
@Nullable
public JetNamespaceHeader getNamespaceHeader() {
@@ -18,20 +18,26 @@
import com.intellij.psi.stubs.StubElement;
import org.jetbrains.annotations.NonNls;
+import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetClass;
+import java.util.List;
+
/**
* @author Nikolay Krasko
*/
-public interface PsiJetClassStub<T extends JetClass> extends StubElement<T> {
+public interface PsiJetClassStub extends StubElement<JetClass> {
@NonNls
@Nullable
String getQualifiedName();
@Nullable
String getName();
+ @NotNull
+ List<String> getSuperNames();
+
boolean isDeprecated();
boolean hasDeprecatedAnnotation();
}
@@ -62,7 +62,8 @@ public JetClass createPsiFromAst(@NotNull ASTNode node) {
@Override
public PsiJetClassStub createStub(@NotNull JetClass psi, StubElement parentStub) {
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());
}
@Override
@@ -75,9 +76,14 @@ public void serialize(PsiJetClassStub stub, StubOutputStream dataStream) throws
public PsiJetClassStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
final StringRef name = dataStream.readName();
final StringRef qualifiedName = dataStream.readName();
-
+ final int superCount = dataStream.readVarInt();
+ final StringRef[] superNames = StringRef.createArray(superCount);
+ for (int i = 0; i < superCount; i++) {
+ superNames[i] = dataStream.readName();
+ }
+
final JetClassElementType type = JetStubElementTypes.CLASS;
- final PsiJetClassStubImpl classStub = new PsiJetClassStubImpl(type, parentStub, qualifiedName, name);
+ final PsiJetClassStubImpl classStub = new PsiJetClassStubImpl(type, parentStub, qualifiedName, name, superNames);
return classStub;
}
@@ -16,12 +16,7 @@
package org.jetbrains.jet.lang.psi.stubs.elements;
-import com.intellij.lang.ASTNode;
-import com.intellij.lang.Language;
-import com.intellij.lang.LanguageParserDefinitions;
-import com.intellij.lang.PsiBuilder;
-import com.intellij.lang.PsiBuilderFactory;
-import com.intellij.lang.PsiParser;
+import com.intellij.lang.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.StubBuilder;
@@ -43,7 +38,7 @@
* @author Nikolay Krasko
*/
public class JetFileElementType extends IStubFileElementType<PsiJetFileStub> {
- public static final int STUB_VERSION = 3;
+ public static final int STUB_VERSION = 4;
public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE);
@@ -19,37 +19,53 @@
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
+import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.stubs.PsiJetClassStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetClassElementType;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* @author Nikolay Krasko
*/
-public class PsiJetClassStubImpl extends StubBase<JetClass> implements PsiJetClassStub<JetClass> {
+public class PsiJetClassStubImpl extends StubBase<JetClass> implements PsiJetClassStub {
private final StringRef qualifiedName;
private final StringRef name;
+ private final StringRef[] superNames;
public PsiJetClassStubImpl(
JetClassElementType type,
final StubElement parent,
@Nullable final String qualifiedName,
- final String name) {
+ final String name,
+ final List<String> superNames) {
+
+ this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), wrapStrings(superNames));
+ }
- this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name));
+ private static StringRef[] wrapStrings(List<String> names) {
+ StringRef[] refs = new StringRef[names.size()];
+ for (int i = 0; i < names.size(); i++) {
+ refs[i] = StringRef.fromString(names.get(i));
+ }
+ return refs;
}
public PsiJetClassStubImpl(
JetClassElementType type,
final StubElement parent,
final StringRef qualifiedName,
- final StringRef name) {
+ final StringRef name,
+ final StringRef[] superNames) {
super(parent, type);
this.qualifiedName = qualifiedName;
this.name = name;
+ this.superNames = superNames;
}
@Override
@@ -71,4 +87,14 @@ public boolean hasDeprecatedAnnotation() {
public String getName() {
return StringRef.toString(name);
}
+
+ @NotNull
+ @Override
+ public List<String> getSuperNames() {
+ List<String> result = new ArrayList<String>();
+ for (StringRef ref : superNames) {
+ result.add(ref.toString());
+ }
+ return result;
+ }
}
@@ -175,6 +175,7 @@
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetShortFunctionNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetExtensionFunctionNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetAllShortFunctionNameIndex"/>
+ <stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetSuperClassIndex"/>
<contentBasedClassFileProcessor implementation="org.jetbrains.jet.plugin.libraries.JetContentBasedFileSubstitutor" />
<psi.clsCustomNavigationPolicy implementation="org.jetbrains.jet.plugin.libraries.JetClsNavigationPolicy" />
@@ -25,6 +25,7 @@
*/
public interface JetIndexKeys {
StubIndexKey<String, JetClassOrObject> SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.class.shortName");
+ StubIndexKey<String, JetClassOrObject> SUPERCLASS_NAME_KEY = StubIndexKey.createIndexKey("jet.class.superClassName");
StubIndexKey<String, JetClassOrObject> FQN_KEY = StubIndexKey.createIndexKey("jet.fqn");
StubIndexKey<String, JetNamedFunction> TOP_LEVEL_FUNCTION_SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.top.level.function.short.name");
@@ -0,0 +1,47 @@
+/*
+ * 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.JetClassOrObject;
+
+import java.util.Collection;
+
+/**
+ * @author yole
+ */
+public class JetSuperClassIndex extends StringStubIndexExtension<JetClassOrObject> {
+ private static final JetSuperClassIndex ourInstance = new JetSuperClassIndex();
+ public static JetSuperClassIndex getInstance() {
+ return ourInstance;
+ }
+
+ @NotNull
+ @Override
+ public StubIndexKey<String, JetClassOrObject> getKey() {
+ return JetIndexKeys.SUPERCLASS_NAME_KEY;
+ }
+
+ @Override
+ public Collection<JetClassOrObject> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
+ return super.get(s, project, new JetSourceFilterScope(scope));
+ }
+}
@@ -37,6 +37,10 @@ public void indexClass(PsiJetClassStub stub, IndexSink sink) {
if (fqn != null) {
sink.occurrence(JetIndexKeys.FQN_KEY, fqn);
}
+
+ for (String superName : stub.getSuperNames()) {
+ sink.occurrence(JetIndexKeys.SUPERCLASS_NAME_KEY, superName);
+ }
}
@Override
@@ -0,0 +1,50 @@
+/*
+ * 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.stubs;
+
+import com.intellij.psi.PsiFile;
+import com.intellij.testFramework.LightProjectDescriptor;
+import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.jet.lang.psi.JetClass;
+import org.jetbrains.jet.lang.psi.JetDeclaration;
+import org.jetbrains.jet.lang.psi.JetFile;
+import org.jetbrains.jet.lang.psi.stubs.PsiJetClassStub;
+import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
+import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
+
+import java.util.List;
+
+/**
+ * @author yole
+ */
+public class JetStubsTest extends LightCodeInsightFixtureTestCase {
+ @NotNull
+ @Override
+ protected LightProjectDescriptor getProjectDescriptor() {
+ return JetLightProjectDescriptor.INSTANCE;
+ }
+
+ public void testSuperclassNames() {
+ final PsiFile psiFile = myFixture.configureByText("foo.kt", "import java.util.ArrayList as alist\nclass C(): alist() { }");
+ final List<JetDeclaration> declarations = ((JetFile) psiFile).getDeclarations();
+ final JetClass jetClass = (JetClass) declarations.get(0);
+ final PsiJetClassStub stub = JetStubElementTypes.CLASS.createStub(jetClass, null);
+ final List<String> names = stub.getSuperNames();
+ assertSameElements(names, "ArrayList", "alist");
+ }
+}

0 comments on commit 783dbdd

Please sign in to comment.