find usages and rename work for Kotlin classes called from Java code (KT-1641)
#KT-1641 fixed
This commit is contained in:
@@ -47,6 +47,7 @@ import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderMode;
|
||||
import org.jetbrains.jet.codegen.CompilationErrorHandler;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.FqName;
|
||||
@@ -238,4 +239,8 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
|
||||
return JetLightClass.class.getSimpleName() + ":" + e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static JetLightClass wrapDelegate(JetClass jetClass) {
|
||||
return new JetLightClass(jetClass.getManager(), (JetFile) jetClass.getContainingFile(), JetPsiUtil.getFQName(jetClass));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,7 @@
|
||||
<iconProvider implementation="org.jetbrains.jet.plugin.JetIconProvider"/>
|
||||
<itemPresentationProvider implementationClass="org.jetbrains.jet.plugin.presentation.JetFunctionPresenter" forClass="org.jetbrains.jet.lang.psi.JetNamedFunction"/>
|
||||
<elementDescriptionProvider implementation="org.jetbrains.jet.plugin.findUsages.JetElementDescriptionProvider"/>
|
||||
<findUsagesHandlerFactory implementation="org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory"/>
|
||||
<debugger.positionManagerFactory implementation="org.jetbrains.jet.plugin.debugger.JetPositionManagerFactory"/>
|
||||
<codeInsight.implementMethod language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.ImplementMethodsHandler"/>
|
||||
<codeInsight.overrideMethod language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.OverrideMethodsHandler"/>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.findUsages;
|
||||
|
||||
import com.intellij.find.findUsages.FindUsagesHandler;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.asJava.JetLightClass;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class KotlinFindClassUsagesHandler extends FindUsagesHandler {
|
||||
public KotlinFindClassUsagesHandler(@NotNull PsiElement psiElement) {
|
||||
super(psiElement);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElement[] getSecondaryElements() {
|
||||
return new PsiElement[] {JetLightClass.wrapDelegate((JetClass) getPsiElement()) };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.findUsages;
|
||||
|
||||
import com.intellij.find.findUsages.FindUsagesHandler;
|
||||
import com.intellij.find.findUsages.FindUsagesHandlerFactory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class KotlinFindUsagesHandlerFactory extends FindUsagesHandlerFactory {
|
||||
@Override
|
||||
public boolean canFindUsages(@NotNull PsiElement element) {
|
||||
return element instanceof JetClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
|
||||
return new KotlinFindClassUsagesHandler(element);
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,20 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.refactoring.rename;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.asJava.JetLightClass;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -37,6 +42,18 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
|
||||
return element instanceof JetClassOrObject;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<PsiReference> findReferences(PsiElement element) {
|
||||
if (element instanceof JetClass) {
|
||||
List<PsiReference> references = new ArrayList<PsiReference>();
|
||||
references.addAll(ReferencesSearch.search(element).findAll());
|
||||
references.addAll(ReferencesSearch.search(JetLightClass.wrapDelegate((JetClass) element)).findAll());
|
||||
return references;
|
||||
}
|
||||
return super.findReferences(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {
|
||||
JetClassOrObject clazz = (JetClassOrObject) element;
|
||||
|
||||
@@ -67,7 +67,7 @@ public class JetJUnitConfigurationProducer extends RuntimeConfigurationProducer
|
||||
JetElement owner = PsiTreeUtil.getParentOfType(function, JetFunction.class, JetClass.class);
|
||||
|
||||
if (owner instanceof JetClass) {
|
||||
JetLightClass delegate = wrapDelegate((JetClass) owner);
|
||||
JetLightClass delegate = JetLightClass.wrapDelegate((JetClass) owner);
|
||||
for (PsiMethod method : delegate.getMethods()) {
|
||||
if (method.getNavigationElement() == function) {
|
||||
Location<PsiMethod> methodLocation = PsiLocation.fromPsiElement(method);
|
||||
@@ -97,7 +97,7 @@ public class JetJUnitConfigurationProducer extends RuntimeConfigurationProducer
|
||||
|
||||
if (jetClass != null) {
|
||||
myElement = jetClass;
|
||||
PsiClass delegate = wrapDelegate(jetClass);
|
||||
PsiClass delegate = JetLightClass.wrapDelegate(jetClass);
|
||||
|
||||
if (JUnitUtil.isTestClass(delegate)) {
|
||||
RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(context.getProject(), context);
|
||||
@@ -136,10 +136,6 @@ public class JetJUnitConfigurationProducer extends RuntimeConfigurationProducer
|
||||
return tempSingleDeclaration;
|
||||
}
|
||||
|
||||
private static JetLightClass wrapDelegate(JetClass jetClass) {
|
||||
return new JetLightClass(jetClass.getManager(), (JetFile) jetClass.getContainingFile(), JetPsiUtil.getFQName(jetClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import testing.*;
|
||||
|
||||
class Client {
|
||||
private Server myServer;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package testing;
|
||||
|
||||
class <caret>Server() {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.Third;
|
||||
|
||||
class JavaClient {
|
||||
private Third myField;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.First;
|
||||
|
||||
class JavaClient {
|
||||
private First myField;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.findUsages;
|
||||
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class JetFindUsagesTest extends LightCodeInsightFixtureTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JetLightProjectDescriptor.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/findUsages");
|
||||
}
|
||||
|
||||
public void testFindClassUsages() {
|
||||
myFixture.configureByFiles("findClassUsages/Server.kt", "findClassUsages/Client.java");
|
||||
JetClass cls = PsiTreeUtil.getParentOfType(myFixture.getElementAtCaret(), JetClass.class, false);
|
||||
final Collection<UsageInfo> usages = myFixture.findUsages(cls);
|
||||
assertEquals(1, usages.size());
|
||||
UsageInfo first = usages.iterator().next();
|
||||
final PsiField field = PsiTreeUtil.getParentOfType(first.getElement(), PsiField.class);
|
||||
assertEquals("private Server myServer;", field.getText());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user