add goto declaration support for synthetic android properties
This commit is contained in:
committed by
Yan Zhulanow
parent
934a3b0436
commit
996636aa62
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.codeInsight.navigation.actions.GotoDeclarationHandler
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.actionSystem.DataContext
|
||||
import org.jetbrains.jet.lang.psi.JetProperty
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import org.jetbrains.jet.lang.resolve.android.AndroidUIXmlParser
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.psi.PsiReferenceExpression
|
||||
import com.intellij.psi.PsiIdentifier
|
||||
import com.intellij.psi.impl.source.tree.LeafPsiElement
|
||||
|
||||
public class AndroidGotoDeclarationHandler: GotoDeclarationHandler {
|
||||
override fun getGotoDeclarationTargets(sourceElement: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
|
||||
if (sourceElement is LeafPsiElement) {
|
||||
val parser = ServiceManager.getService(sourceElement.getProject(), javaClass<AndroidUIXmlParser>())
|
||||
val psiElement = parser?.idToXmlAttribute(sourceElement.getText())
|
||||
if (psiElement != null) {
|
||||
return array(psiElement)
|
||||
}
|
||||
else return null
|
||||
} else return null
|
||||
}
|
||||
|
||||
override fun getActionText(context: DataContext?): String? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -29,15 +29,46 @@ import com.intellij.psi.xml.XmlFile
|
||||
import com.intellij.psi.XmlElementVisitor
|
||||
import com.intellij.psi.xml.XmlTag
|
||||
import com.intellij.psi.PsiElement
|
||||
import java.util.HashMap
|
||||
import com.intellij.psi.xml.XmlAttribute
|
||||
|
||||
class IDEAndroidUIXmlParser(val project: Project): AndroidUIXmlParser() {
|
||||
override val searchPath: String? = project.getBasePath() + "/res/layout/"
|
||||
override var androidAppPackage: String = ""
|
||||
|
||||
val idToXmlAttributeCache = HashMap<String, PsiElement>()
|
||||
|
||||
private fun setupElementCache() {
|
||||
for (file in getXmlLayouts(project)) {
|
||||
if (file is XmlFile) {
|
||||
file.accept(object : XmlElementVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
override fun visitXmlTag(tag: XmlTag?) {
|
||||
val idPrefix = "@+id/"
|
||||
val attribute = tag?.getAttribute("android:id")
|
||||
val s = attribute?.getValue()
|
||||
if (attribute != null && (s?.startsWith(idPrefix) ?: false)) {
|
||||
idToXmlAttributeCache[s!!.replace(idPrefix, "")] = attribute
|
||||
}
|
||||
tag?.acceptChildren(this)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun idToXmlAttribute(id: String): PsiElement? {
|
||||
return idToXmlAttributeCache[id]
|
||||
}
|
||||
|
||||
override protected fun lazySetup() {
|
||||
if (listenerSetUp) return
|
||||
androidAppPackage = readManifest()._package
|
||||
populateQueue(project)
|
||||
setupElementCache()
|
||||
listenerSetUp = true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user