From 90f3996dd372e49c75da7b2b83b0d28221b41734 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 11 Jun 2014 15:07:58 +0400 Subject: [PATCH] Idea 138: Add custom kotlin search for kotlin properties --- .../intellij/psi/impl/search/annotations.xml | 10 ++++ idea/src/META-INF/plugin.xml | 1 + .../KotlinCallerMethodsTreeStructure.java | 4 +- ...linLightMethodTextOccurrenceProcessor.java | 4 +- ...LightPropertyAccessorsReferenceSearcher.kt | 56 +++++++++++++++++++ .../search/usagesSearch/usagesSearch.kt | 2 +- 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 annotations/com/intellij/psi/impl/search/annotations.xml create mode 100644 idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightPropertyAccessorsReferenceSearcher.kt diff --git a/annotations/com/intellij/psi/impl/search/annotations.xml b/annotations/com/intellij/psi/impl/search/annotations.xml new file mode 100644 index 00000000000..5584da4fd50 --- /dev/null +++ b/annotations/com/intellij/psi/impl/search/annotations.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 79c5bea046e..18d7b2a9484 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -387,6 +387,7 @@ + diff --git a/idea/src/org/jetbrains/jet/plugin/hierarchy/calls/KotlinCallerMethodsTreeStructure.java b/idea/src/org/jetbrains/jet/plugin/hierarchy/calls/KotlinCallerMethodsTreeStructure.java index 3e4740e5d5b..11b5e69992b 100644 --- a/idea/src/org/jetbrains/jet/plugin/hierarchy/calls/KotlinCallerMethodsTreeStructure.java +++ b/idea/src/org/jetbrains/jet/plugin/hierarchy/calls/KotlinCallerMethodsTreeStructure.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.plugin.hierarchy.calls; +import com.google.common.collect.Maps; import com.intellij.ide.hierarchy.HierarchyNodeDescriptor; import com.intellij.ide.hierarchy.call.CallerMethodsTreeStructure; import com.intellij.openapi.application.ReadActionProcessor; @@ -105,8 +106,7 @@ public abstract class KotlinCallerMethodsTreeStructure extends KotlinCallTreeStr PsiElement element = getTargetElement(descriptor); SearchScope searchScope = getSearchScope(scopeType, basePsiClass); - Map methodToDescriptorMap = - new HashMap(); + Map methodToDescriptorMap = Maps.newHashMap(); Object[] javaCallers = null; if (element instanceof PsiMethod) { diff --git a/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightMethodTextOccurrenceProcessor.java b/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightMethodTextOccurrenceProcessor.java index baf3729ef0f..8e0d60ad1a0 100644 --- a/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightMethodTextOccurrenceProcessor.java +++ b/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightMethodTextOccurrenceProcessor.java @@ -32,9 +32,7 @@ public class KotlinLightMethodTextOccurrenceProcessor extends MethodTextOccurren } @Override - protected boolean processInexactReference( - PsiReference ref, PsiElement refElement, PsiMethod method, Processor consumer - ) { + protected boolean processInexactReference(PsiReference ref, PsiElement refElement, PsiMethod method, Processor consumer) { if (refElement instanceof JetNamedFunction) { PsiMethod lightMethod = LightClassUtil.getLightClassMethod((JetNamedFunction) refElement); if (lightMethod != null) return super.processInexactReference(ref, lightMethod, method, consumer); diff --git a/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightPropertyAccessorsReferenceSearcher.kt b/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightPropertyAccessorsReferenceSearcher.kt new file mode 100644 index 00000000000..2cbcf2033c1 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/search/ideaExtensions/KotlinLightPropertyAccessorsReferenceSearcher.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2010-2014 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.search.ideaExtensions + +import com.intellij.openapi.application.QueryExecutorBase +import com.intellij.psi.PsiReference +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.search.SearchScope +import com.intellij.psi.search.UsageSearchContext +import com.intellij.psi.search.searches.MethodReferencesSearch +import com.intellij.util.Processor +import org.jetbrains.jet.plugin.JetFileType +import org.jetbrains.jet.asJava.namedUnwrappedElement +import org.jetbrains.jet.lang.psi.JetProperty + +public class KotlinLightPropertyAccessorsReferenceSearcher() : QueryExecutorBase(true) { + override fun processQuery(queryParameters: MethodReferencesSearch.SearchParameters, consumer: Processor) { + val method = queryParameters.getMethod() + val unwrapped = method.namedUnwrappedElement + + if (unwrapped !is JetProperty) return + + val propertyName = unwrapped.getName() + if (propertyName == null) return + + val onlyKotlinFiles = restrictToKotlinSources(queryParameters.getScope()) + + queryParameters.getOptimizer()!!.searchWord( + propertyName, + onlyKotlinFiles, + UsageSearchContext.IN_CODE, + true, + method) + } + + private fun restrictToKotlinSources(originalScope: SearchScope): SearchScope { + if (originalScope is GlobalSearchScope) { + return GlobalSearchScope.getScopeRestrictedByFileTypes((originalScope as GlobalSearchScope), JetFileType.INSTANCE) + } + return originalScope + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/search/usagesSearch/usagesSearch.kt b/idea/src/org/jetbrains/jet/plugin/search/usagesSearch/usagesSearch.kt index 3bc9422c270..12150a2bf07 100644 --- a/idea/src/org/jetbrains/jet/plugin/search/usagesSearch/usagesSearch.kt +++ b/idea/src/org/jetbrains/jet/plugin/search/usagesSearch/usagesSearch.kt @@ -150,7 +150,7 @@ public object UsagesSearch: QueryFactory() { fun UsagesSearchRequest.search(): Query = UsagesSearch.search(this) fun UsagesSearchTarget.retarget(element: B) = - UsagesSearchTarget(element, scope, location, restrictByTargetScope) + UsagesSearchTarget(element, scope, location, restrictByTargetScope) val UsagesSearchTarget.effectiveScope: SearchScope get() = if (restrictByTargetScope) scope and element.effectiveScope else scope