Find Usages extension extracted

This commit is contained in:
Andrey Breslav
2014-09-30 10:44:11 +04:00
committed by Yan Zhulanow
parent 5811b13ce7
commit 8844ecdbc4
8 changed files with 148 additions and 44 deletions
+3
View File
@@ -14,5 +14,8 @@
<extensionPoint name="expressionCodegenExtension"
interface="org.jetbrains.jet.codegen.extensions.ExpressionCodegenExtension"
area="IDEA_PROJECT"/>
<extensionPoint name="findUsagesHandlerDecorator"
interface="org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindUsagesHandlerDecorator"
area="IDEA_PROJECT"/>
</extensionPoints>
</idea-plugin>
@@ -0,0 +1,29 @@
/*
* 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.findUsages.handlers
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.psi.PsiElement
import com.intellij.find.findUsages.FindUsagesHandler
public trait KotlinFindUsagesHandlerDecorator {
class object {
public val EP_NAME: ExtensionPointName<KotlinFindUsagesHandlerDecorator> = ExtensionPointName.create("org.jetbrains.kotlin.findUsagesHandlerDecorator")!!
}
public fun decorateHandler(element: PsiElement, forHighlightUsages: Boolean, delegate: FindUsagesHandler): FindUsagesHandler
}
@@ -33,11 +33,13 @@ import org.jetbrains.kotlin.psi.JetNamedDeclaration
import org.jetbrains.kotlin.psi.JetClassOrObject
import com.intellij.find.findUsages.JavaFindUsagesHandlerFactory
import org.jetbrains.kotlin.idea.findUsages.handlers.DelegatingFindMemberUsagesHandler
import org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindUsagesHandlerDecorator
import com.intellij.openapi.extensions.Extensions
public class KotlinFindUsagesHandlerFactory(project: Project) : FindUsagesHandlerFactory() {
val javaHandlerFactory = JavaFindUsagesHandlerFactory(project)
val findFunctionOptions = KotlinFunctionFindUsagesOptions(project)
public val findFunctionOptions: KotlinFunctionFindUsagesOptions = KotlinFunctionFindUsagesOptions(project)
val findPropertyOptions = KotlinPropertyFindUsagesOptions(project)
val findClassOptions = KotlinClassFindUsagesOptions(project)
val defaultOptions = FindUsagesOptions(project)
@@ -50,25 +52,31 @@ public class KotlinFindUsagesHandlerFactory(project: Project) : FindUsagesHandle
element is JetTypeParameter
public override fun createFindUsagesHandler(element: PsiElement, forHighlightUsages: Boolean): FindUsagesHandler? {
when(element) {
val handler = when (element) {
is JetClassOrObject ->
return KotlinFindClassUsagesHandler(element, this)
KotlinFindClassUsagesHandler(element, this)
is JetNamedFunction, is JetProperty, is JetParameter -> {
val declaration = element as JetNamedDeclaration
if (forHighlightUsages) return KotlinFindMemberUsagesHandler.getInstance(declaration, this)
return JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
JetRefactoringUtil.checkSuperMethods(declaration, null, "super.methods.action.key.find.usages")?.let { callables ->
if (callables.empty) FindUsagesHandler.NULL_HANDLER else DelegatingFindMemberUsagesHandler(declaration, callables, this)
}
}
is JetTypeParameter ->
return KotlinTypeParameterFindUsagesHandler(element, this)
KotlinTypeParameterFindUsagesHandler(element, this)
else ->
throw IllegalArgumentException("unexpected element type: " + element)
}
for (decorator in Extensions.getArea(element.getProject()).getExtensionPoint(KotlinFindUsagesHandlerDecorator.EP_NAME).getExtensions()) {
val decorated = decorator.decorateHandler(element, forHighlightUsages, handler!!)
if (decorated != handler) return decorated
}
return handler
}
}
@@ -48,7 +48,7 @@ class DelegatingFindMemberUsagesHandler(
JavaFindUsagesHandler(declaration, elementsToSearch.copyToArray(), factory.javaHandlerFactory)
}
fun getHandler(element: PsiElement): FindUsagesHandler? =
private fun getHandler(element: PsiElement): FindUsagesHandler? =
when (element) {
is JetNamedDeclaration ->
KotlinFindMemberUsagesHandler.getInstance(element, elementsToSearch, factory)
@@ -60,51 +60,16 @@ class DelegatingFindMemberUsagesHandler(
}
override fun getPrimaryElements(): Array<PsiElement> {
val primaryElements = kotlinHandler.getPrimaryElements()
if (isAndroidSyntheticElement(declaration)) {
val name = (declaration as JetProperty).getName()!!
val parser = ServiceManager.getService(declaration.getProject(), javaClass<AndroidUIXmlProcessor>())
val psiElement = parser?.resourceManager?.idToXmlAttribute(name) as? XmlAttribute
if (psiElement != null && psiElement.getValueElement() != null) {
return array(psiElement.getValueElement()!!)
}
}
return primaryElements
return kotlinHandler.getPrimaryElements()
}
override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions {
return kotlinHandler.getFindUsagesOptions(dataContext)
}
override fun getSecondaryElements(): Array<PsiElement> {
if (isAndroidSyntheticElement(declaration)) {
val name = (declaration as JetProperty).getName()!!
val parser = ServiceManager.getService(declaration.getProject(), javaClass<AndroidUIXmlProcessor>())
val psiElement = parser?.resourceManager?.idToXmlAttribute(name) as? XmlAttribute
if (psiElement != null) {
val res = ArrayList<PsiElement>()
val fields = AndroidResourceUtil.findIdFields(psiElement)
for (field in fields) {
res.add(field)
}
res.add(declaration)
return res.copyToArray()
}
}
return array()
}
override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
val handler = getHandler(element)
if (handler == null || isAndroidSyntheticElement(element)) {
val findUsagesOptions = JavaVariableFindUsagesOptions(element.getProject())
findUsagesOptions.isSearchForTextOccurrences = false
findUsagesOptions.isSkipImportStatements = true
findUsagesOptions.isUsages = true
findUsagesOptions.isReadAccess = true
findUsagesOptions.isWriteAccess = true
return super.processElementUsages(element, processor, findUsagesOptions)
}
if (handler == null) return true
val handlerOptions = when (handler) {
/* Can't have KotlinPropertyFindUsagesOptions here since Kotlin properties do not override java methods, so
@@ -8,7 +8,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="plugin-api" exported="" />
<orderEntry type="module" module-name="frontend" />
<orderEntry type="module" module-name="frontend" exported="" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="cli" />
<orderEntry type="module" module-name="util" />
@@ -12,6 +12,7 @@
<orderEntry type="library" exported="" scope="PROVIDED" name="android" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="android_tests" level="project" />
<orderEntry type="library" scope="PROVIDED" name="idea-full" level="project" />
<orderEntry type="module" module-name="idea" scope="PROVIDED" />
</component>
</module>
@@ -19,5 +19,6 @@
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<externalDeclarationsProvider implementation="org.jetbrains.kotlin.android.AndroidDeclarationsProvider"/>
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.AndroidExpressionCodegen"/>
<findUsagesHandlerDecorator implementation="org.jetbrains.jet.plugin.android.AndroidFindUsageHandlerDecorator"/>
</extensions>
</idea-plugin>
@@ -0,0 +1,97 @@
/*
* 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.android
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.psi.PsiElement
import com.intellij.usageView.UsageInfo
import com.intellij.util.Processor
import com.intellij.find.findUsages.FindUsagesOptions
import org.jetbrains.jet.lang.psi.JetNamedDeclaration
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.jet.lang.resolve.android.isAndroidSyntheticElement
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.jet.lang.resolve.android.AndroidUIXmlProcessor
import com.intellij.psi.xml.XmlAttribute
import org.jetbrains.jet.lang.psi.JetProperty
import org.jetbrains.android.util.AndroidResourceUtil
import java.util.ArrayList
import com.intellij.find.findUsages.JavaVariableFindUsagesOptions
import org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindUsagesHandlerDecorator
class AndroidFindUsageHandlerDecorator : KotlinFindUsagesHandlerDecorator {
override fun decorateHandler(element: PsiElement, forHighlightUsages: Boolean, delegate: FindUsagesHandler): FindUsagesHandler {
if (element !is JetNamedDeclaration) return delegate
if (!isAndroidSyntheticElement(element)) return delegate
return AndroidFindMemberUsagesHandler(element, delegate)
}
}
class AndroidFindMemberUsagesHandler(
private val declaration: JetNamedDeclaration,
private val delegate: FindUsagesHandler? = null
) : FindUsagesHandler(declaration) {
override fun getPrimaryElements(): Array<PsiElement> {
assert(isAndroidSyntheticElement(declaration))
val name = (declaration as JetProperty).getName()!!
val parser = ServiceManager.getService(declaration.getProject(), javaClass<AndroidUIXmlProcessor>())
val psiElement = parser?.resourceManager?.idToXmlAttribute(name) as? XmlAttribute
if (psiElement != null && psiElement.getValueElement() != null) {
return array(psiElement.getValueElement()!!)
}
return super.getPrimaryElements()
}
override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions {
return delegate?.getFindUsagesOptions(dataContext) ?: super.getFindUsagesOptions(dataContext)
}
override fun getSecondaryElements(): Array<PsiElement> {
assert(isAndroidSyntheticElement(declaration))
val name = (declaration as JetProperty).getName()!!
val parser = ServiceManager.getService(declaration.getProject(), javaClass<AndroidUIXmlProcessor>())
val psiElement = parser?.resourceManager?.idToXmlAttribute(name) as? XmlAttribute
if (psiElement != null) {
val res = ArrayList<PsiElement>()
val fields = AndroidResourceUtil.findIdFields(psiElement)
for (field in fields) {
res.add(field)
}
res.add(declaration)
return res.copyToArray()
}
return super.getSecondaryElements()
}
override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
assert(isAndroidSyntheticElement(declaration))
val findUsagesOptions = JavaVariableFindUsagesOptions(element.getProject())
findUsagesOptions.isSearchForTextOccurrences = false
findUsagesOptions.isSkipImportStatements = true
findUsagesOptions.isUsages = true
findUsagesOptions.isReadAccess = true
findUsagesOptions.isWriteAccess = true
return super.processElementUsages(element, processor, findUsagesOptions)
}
}