Skip to content

Commit

Permalink
inheritor navigation works from Java to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
yole committed May 24, 2012
1 parent 783dbdd commit 39fe59d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
17 changes: 6 additions & 11 deletions compiler/frontend/src/org/jetbrains/jet/lang/psi/JetClass.java
Expand Up @@ -207,20 +207,15 @@ private String getQualifiedName() {
*/
@NotNull
public List<String> getSuperNames() {
final JetDelegationSpecifierList delegationSpecifierList = getDelegationSpecifierList();
if (delegationSpecifierList == null) return Collections.emptyList();
final List<JetDelegationSpecifier> specifiers = delegationSpecifierList.getDelegationSpecifiers();
final List<JetDelegationSpecifier> specifiers = 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);
}
final JetUserType superType = specifier.getTypeAsUserType();
if (superType != null) {
final String referencedName = superType.getReferencedName();
if (referencedName != null) {
addSuperName(result, referencedName);
}
}
}
Expand Down
Expand Up @@ -43,4 +43,16 @@ public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}

@Nullable
public JetUserType getTypeAsUserType() {
final JetTypeReference reference = getTypeReference();
if (reference != null) {
final JetTypeElement element = reference.getTypeElement();
if (element instanceof JetUserType) {
return ((JetUserType) element);
}
}
return null;
}
}
Expand Up @@ -82,16 +82,8 @@ public List<JetTypeProjection> getTypeArguments() {

@Override
public JetTypeArgumentList getTypeArgumentList() {
JetTypeReference typeReference = getTypeReference();
if (typeReference == null) {
return null;
}
JetTypeElement typeElement = typeReference.getTypeElement();
if (typeElement instanceof JetUserType) {
JetUserType userType = (JetUserType) typeElement;
return userType.getTypeArgumentList();
}
return null;
final JetUserType userType = getTypeAsUserType();
return userType != null ? userType.getTypeArgumentList() : null;
}

}
1 change: 1 addition & 0 deletions idea/src/META-INF/plugin.xml
Expand Up @@ -186,6 +186,7 @@
<psi.treeChangePreprocessor implementation="org.jetbrains.jet.asJava.JetCodeBlockModificationListener"/>

<referencesSearch implementation="org.jetbrains.jet.plugin.references.KotlinReferencesSearcher"/>
<directClassInheritorsSearch implementation="org.jetbrains.jet.plugin.references.KotlinDirectInheritorsSearcher"/>

<toolWindow id="Kotlin"
factoryClass="org.jetbrains.jet.plugin.internal.KotlinInternalToolWindowFactory"
Expand Down
@@ -0,0 +1,67 @@
/*
* 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.references;

import com.intellij.openapi.application.QueryExecutorBase;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.DirectClassInheritorsSearch;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.asJava.JetLightClass;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.stubindex.JetSuperClassIndex;

import java.util.Collection;
import java.util.List;

/**
* @author yole
*/
public class KotlinDirectInheritorsSearcher extends QueryExecutorBase<PsiClass, DirectClassInheritorsSearch.SearchParameters> {
public KotlinDirectInheritorsSearcher() {
super(true);
}

@Override
public void processQuery(@NotNull DirectClassInheritorsSearch.SearchParameters queryParameters, @NotNull Processor<PsiClass> consumer) {
final PsiClass clazz = queryParameters.getClassToProcess();
final String name = clazz.getName();
if (name == null || !(queryParameters.getScope() instanceof GlobalSearchScope)) return;
GlobalSearchScope scope = (GlobalSearchScope) queryParameters.getScope();
final Collection<JetClassOrObject> candidates = JetSuperClassIndex.getInstance().get(name, clazz.getProject(), scope);
for (JetClassOrObject candidate : candidates) {
if (!(candidate instanceof JetClass)) continue;
final List<JetDelegationSpecifier> specifiers = candidate.getDelegationSpecifiers();
for (JetDelegationSpecifier specifier : specifiers) {
final JetUserType type = specifier.getTypeAsUserType();
if (type != null) {
final JetSimpleNameExpression referenceExpression = type.getReferenceExpression();
if (referenceExpression != null) {
final PsiReference reference = referenceExpression.getReference();
final PsiElement resolved = reference != null ? reference.resolve() : null;
if (resolved instanceof PsiClass && resolved.isEquivalentTo(clazz)) {
consumer.process(JetLightClass.wrapDelegate((JetClass) candidate));
}
}
}
}
}
}
}

0 comments on commit 39fe59d

Please sign in to comment.