Add support of local declarations

This commit is contained in:
Alexey Sedunov
2013-10-23 17:44:04 +04:00
parent 46b14784e1
commit c3c0925de1
19 changed files with 196 additions and 14 deletions
@@ -71,9 +71,6 @@ public class KotlinFindClassUsagesHandler extends KotlinFindUsagesHandler<JetCla
KotlinClassFindUsagesOptions kotlinOptions = (KotlinClassFindUsagesOptions)options;
JetClass jetClass = (JetClass) element;
PsiClass lightClass = LightClassUtil.getPsiClass(getElement());
if (lightClass == null) return true;
if (kotlinOptions.isUsages || kotlinOptions.searchConstructorUsages) {
Collection<PsiReference> references = ReferencesSearch.search(
new ReferencesSearch.SearchParameters(jetClass, kotlinOptions.searchScope, false)
@@ -90,6 +87,9 @@ public class KotlinFindClassUsagesHandler extends KotlinFindUsagesHandler<JetCla
}
}
PsiClass lightClass = LightClassUtil.getPsiClass(getElement());
if (lightClass == null) return true;
if (!processInheritors(lightClass, processor, kotlinOptions)) return false;
if (!processDeclarationsUsages(jetClass, processor, kotlinOptions)) return false;
@@ -33,13 +33,18 @@ import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.asJava.LightClassUtil;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory;
import org.jetbrains.jet.plugin.findUsages.dialogs.KotlinFindMethodUsagesDialog;
import org.jetbrains.jet.plugin.hierarchy.calls.CalleeReferenceVisitorBase;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
import org.jetbrains.jet.plugin.findUsages.options.KotlinMethodFindUsagesOptions;
import org.jetbrains.jet.plugin.search.KotlinExtensionSearch;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class KotlinFindFunctionUsagesHandler extends KotlinFindUsagesHandler<JetNamedFunction> {
public KotlinFindFunctionUsagesHandler(
@@ -69,13 +74,48 @@ public class KotlinFindFunctionUsagesHandler extends KotlinFindUsagesHandler<Jet
@Override
public boolean searchReferences(
@NotNull PsiElement element,
@NotNull final PsiElement element,
@NotNull final Processor<UsageInfo> processor,
@NotNull FindUsagesOptions options
) {
final KotlinMethodFindUsagesOptions kotlinOptions = (KotlinMethodFindUsagesOptions)options;
final KotlinMethodFindUsagesOptions kotlinOptions = (KotlinMethodFindUsagesOptions) options;
SearchScope searchScope = kotlinOptions.searchScope;
JetElement blockForLocalDeclaration = JetPsiUtil.getEnclosingBlockForLocalDeclaration((JetNamedFunction) element);
if (blockForLocalDeclaration != null && kotlinOptions.isUsages) {
BindingContext bindingContext =
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) element.getContainingFile()).getBindingContext();
final List<PsiReference> result = new ArrayList<PsiReference>();
CalleeReferenceVisitorBase visitor = new CalleeReferenceVisitorBase(bindingContext, true) {
private boolean isAcceptable(PsiElement declaration) {
if (kotlinOptions.isIncludeOverloadUsages) {
//noinspection ConstantConditions
return declaration instanceof JetNamedFunction &&
((JetNamedFunction) element).getName()
.equals(((JetNamedFunction) declaration).getName());
}
return declaration.equals(element);
}
@Override
protected void processDeclaration(JetReferenceExpression reference, PsiElement declaration) {
if (isAcceptable(declaration)) {
result.add(reference.getReference());
}
}
};
blockForLocalDeclaration.accept(visitor);
for (PsiReference ref : result) {
if (!processUsage(processor, ref, kotlinOptions)) return false;
}
return true;
}
final PsiMethod lightMethod = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod>() {
@Override
public PsiMethod compute() {
@@ -114,18 +154,18 @@ public class KotlinFindFunctionUsagesHandler extends KotlinFindUsagesHandler<Jet
if (isAbstract && kotlinOptions.isImplementingMethods || kotlinOptions.isOverridingMethods) {
OverridingMethodsSearch.search(lightMethod, options.searchScope, kotlinOptions.isCheckDeepInheritance).forEach(
new PsiElementProcessorAdapter<PsiMethod>(
new PsiElementProcessor<PsiMethod>() {
@Override
public boolean execute(@NotNull PsiMethod element) {
return processUsage(processor, element.getNavigationElement(), kotlinOptions);
new PsiElementProcessor<PsiMethod>() {
@Override
public boolean execute(@NotNull PsiMethod element) {
return processUsage(processor, element.getNavigationElement(), kotlinOptions);
}
}
}
)
);
}
if (kotlinOptions.isIncludeOverloadUsages) {
String name = ((PsiNamedElement)element).getName();
String name = ((PsiNamedElement) element).getName();
if (name == null) return true;
for (PsiReference ref : KotlinExtensionSearch.search(lightMethod, searchScope).findAll()) {
@@ -147,6 +187,4 @@ public class KotlinFindFunctionUsagesHandler extends KotlinFindUsagesHandler<Jet
public FindUsagesOptions getFindUsagesOptions(@Nullable DataContext dataContext) {
return getFactory().getFindMethodOptions();
}
}
@@ -0,0 +1,9 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetClass
// OPTIONS: usages, constructorUsages
fun foo(): Any {
class <caret>Bar
return Bar()
}
val x = Bar()
@@ -0,0 +1,2 @@
Instantiation (6: 12) return Bar()
@@ -0,0 +1,13 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetClass
// OPTIONS: usages, constructorUsages
fun foo(): Any {
if (false) {
class <caret>Bar
return Bar()
}
return Bar()
}
val x = Bar()
@@ -0,0 +1,2 @@
Instantiation (7: 16) return Bar()
@@ -0,0 +1,11 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetNamedFunction
// OPTIONS: usages
fun foo() {
fun <caret>bar() {
}
bar()
}
bar()
@@ -0,0 +1,2 @@
Function call (8: 5) bar()
@@ -0,0 +1,15 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetNamedFunction
// OPTIONS: usages
fun foo() {
if (true) {
fun <caret>bar() {
}
bar()
}
bar()
}
bar()
@@ -0,0 +1,2 @@
Function call (9: 9) bar()
@@ -0,0 +1,9 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetObjectDeclarationName
// OPTIONS: usages
fun foo(): Any {
object <caret>Bar
return Bar
}
val x = Bar
@@ -0,0 +1 @@
Value read (6: 12) return Bar
@@ -0,0 +1,13 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetObjectDeclarationName
// OPTIONS: usages
fun foo(): Any {
if (true) {
object <caret>Bar
return Bar
}
return Bar
}
val x = Bar
@@ -0,0 +1 @@
Value read (7: 16) return Bar
@@ -0,0 +1,9 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetProperty
// OPTIONS: usages
fun foo(): String {
val <caret>bar = ""
return bar
}
val x = bar
@@ -0,0 +1 @@
Value read (6: 12) return bar
@@ -0,0 +1,13 @@
// PSI_ELEMENT: org.jetbrains.jet.lang.psi.JetProperty
// OPTIONS: usages
fun foo(): String {
if (true) {
val <caret>bar = ""
return bar
}
return bar
}
val x = bar
@@ -0,0 +1 @@
Value read (7: 16) return bar
@@ -163,6 +163,16 @@ public class JetFindUsagesTest extends AbstractJetFindUsagesTest {
doTest("idea/testData/findUsages/findClassUsages/kotlinDerivedInterfaceUsages2.0.kt");
}
@TestMetadata("kotlinLocalClassUsages1.0.kt")
public void testKotlinLocalClassUsages1() throws Exception {
doTest("idea/testData/findUsages/findClassUsages/kotlinLocalClassUsages1.0.kt");
}
@TestMetadata("kotlinLocalClassUsages2.0.kt")
public void testKotlinLocalClassUsages2() throws Exception {
doTest("idea/testData/findUsages/findClassUsages/kotlinLocalClassUsages2.0.kt");
}
@TestMetadata("kotlinPrivateClassUsages.0.kt")
public void testKotlinPrivateClassUsages() throws Exception {
doTest("idea/testData/findUsages/findClassUsages/kotlinPrivateClassUsages.0.kt");
@@ -186,6 +196,16 @@ public class JetFindUsagesTest extends AbstractJetFindUsagesTest {
doTest("idea/testData/findUsages/findMethodUsages/javaMethodUsages.0.kt");
}
@TestMetadata("kotlinLocalMethodUsages1.0.kt")
public void testKotlinLocalMethodUsages1() throws Exception {
doTest("idea/testData/findUsages/findMethodUsages/kotlinLocalMethodUsages1.0.kt");
}
@TestMetadata("kotlinLocalMethodUsages2.0.kt")
public void testKotlinLocalMethodUsages2() throws Exception {
doTest("idea/testData/findUsages/findMethodUsages/kotlinLocalMethodUsages2.0.kt");
}
@TestMetadata("kotlinMethodUsages.0.kt")
public void testKotlinMethodUsages() throws Exception {
doTest("idea/testData/findUsages/findMethodUsages/kotlinMethodUsages.0.kt");
@@ -219,6 +239,16 @@ public class JetFindUsagesTest extends AbstractJetFindUsagesTest {
doTest("idea/testData/findUsages/findObjectUsages/javaObjectUsages.0.kt");
}
@TestMetadata("kotlinLocalObjectUsages1.0.kt")
public void testKotlinLocalObjectUsages1() throws Exception {
doTest("idea/testData/findUsages/findObjectUsages/kotlinLocalObjectUsages1.0.kt");
}
@TestMetadata("kotlinLocalObjectUsages2.0.kt")
public void testKotlinLocalObjectUsages2() throws Exception {
doTest("idea/testData/findUsages/findObjectUsages/kotlinLocalObjectUsages2.0.kt");
}
@TestMetadata("kotlinObjectUsages.0.kt")
public void testKotlinObjectUsages() throws Exception {
doTest("idea/testData/findUsages/findObjectUsages/kotlinObjectUsages.0.kt");
@@ -242,6 +272,16 @@ public class JetFindUsagesTest extends AbstractJetFindUsagesTest {
doTest("idea/testData/findUsages/findPropertyUsages/javaPropertyUsages.0.kt");
}
@TestMetadata("kotlinLocalPropertyUsages1.0.kt")
public void testKotlinLocalPropertyUsages1() throws Exception {
doTest("idea/testData/findUsages/findPropertyUsages/kotlinLocalPropertyUsages1.0.kt");
}
@TestMetadata("kotlinLocalPropertyUsages2.0.kt")
public void testKotlinLocalPropertyUsages2() throws Exception {
doTest("idea/testData/findUsages/findPropertyUsages/kotlinLocalPropertyUsages2.0.kt");
}
@TestMetadata("kotlinPrivatePropertyUsages.0.kt")
public void testKotlinPrivatePropertyUsages() throws Exception {
doTest("idea/testData/findUsages/findPropertyUsages/kotlinPrivatePropertyUsages.0.kt");