KotlinIsReferenceToExtension extracted
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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 org.jetbrains.jet.plugin.references.JetSimpleNameReference
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
|
||||
public trait KotlinIsReferenceToExtension {
|
||||
class object {
|
||||
public val EP_NAME: ExtensionPointName<KotlinIsReferenceToExtension> = ExtensionPointName.create("org.jetbrains.kotlin.isReferenceToExtension")!!
|
||||
}
|
||||
|
||||
fun isReferenceTo(reference: JetSimpleNameReference, element: PsiElement): Boolean?
|
||||
fun handleElementRename(reference: JetSimpleNameReference, psiFactory: JetPsiFactory, newElementName: String): PsiElement?
|
||||
}
|
||||
+24
-20
@@ -36,28 +36,27 @@ import org.jetbrains.kotlin.resolve.dataClassUtils.isComponentLike
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import com.intellij.psi.impl.light.LightElement
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import org.jetbrains.jet.plugin.findUsages.handlers.KotlinIsReferenceToExtension
|
||||
|
||||
public class JetSimpleNameReference(
|
||||
jetSimpleNameExpression: JetSimpleNameExpression
|
||||
) : JetSimpleReference<JetSimpleNameExpression>(jetSimpleNameExpression) {
|
||||
|
||||
override fun isReferenceTo(element: PsiElement?): Boolean {
|
||||
val resolvedElement = resolve()
|
||||
if (resolvedElement == null || element == null) {
|
||||
return false
|
||||
}
|
||||
if (isAndroidSyntheticElement(resolvedElement)) {
|
||||
if (element is ValueResourceElementWrapper) {
|
||||
val resource = element.getValue()!!
|
||||
return (resolvedElement as JetProperty).getName() == resource.substring(resource.indexOf('/')+1)
|
||||
}
|
||||
if (element != null) {
|
||||
val extensions = Extensions.getArea(element.getProject()).getExtensionPoint(
|
||||
KotlinIsReferenceToExtension.EP_NAME).getExtensions()
|
||||
for (extension in extensions) {
|
||||
val value = extension.isReferenceTo(this, element)
|
||||
if (value != null) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if (resolvedElement is LightElement && element is JetProperty) {
|
||||
return resolvedElement.getName() == element.getName()
|
||||
}
|
||||
return super.isReferenceTo(element)
|
||||
}
|
||||
return super.isReferenceTo(element)
|
||||
}
|
||||
|
||||
override fun getRangeInElement(): TextRange = TextRange(0, getElement().getTextLength())
|
||||
|
||||
@@ -87,12 +86,17 @@ public class JetSimpleNameReference(
|
||||
JetTokens.FIELD_IDENTIFIER -> psiFactory.createFieldIdentifier(newElementName)
|
||||
JetTokens.LABEL_IDENTIFIER -> psiFactory.createClassLabel(newElementName)
|
||||
else -> {
|
||||
if (newElementName.startsWith("@+id/")) {
|
||||
psiFactory.createNameIdentifier(newElementName.substring(newElementName.indexOf('/')+1))
|
||||
}
|
||||
else {
|
||||
psiFactory.createNameIdentifier(newElementName)
|
||||
val extensions = Extensions.getArea(expression.getProject()).getExtensionPoint(
|
||||
KotlinIsReferenceToExtension.EP_NAME).getExtensions()
|
||||
|
||||
var handled: PsiElement? = null
|
||||
for (extension in extensions) {
|
||||
handled = extension.handleElementRename(this, psiFactory, newElementName)
|
||||
if (handled != null) {
|
||||
break
|
||||
}
|
||||
}
|
||||
handled ?: psiFactory.createNameIdentifier(newElementName)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,5 +17,8 @@
|
||||
<extensionPoint name="findUsagesHandlerDecorator"
|
||||
interface="org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindUsagesHandlerDecorator"
|
||||
area="IDEA_PROJECT"/>
|
||||
<extensionPoint name="isReferenceToExtension"
|
||||
interface="org.jetbrains.jet.plugin.findUsages.handlers.KotlinIsReferenceToExtension"
|
||||
area="IDEA_PROJECT"/>
|
||||
</extensionPoints>
|
||||
</idea-plugin>
|
||||
|
||||
Reference in New Issue
Block a user