Create descriptors directly in Android Extensions (IDEA plugin)

This commit is contained in:
Yan Zhulanow
2015-11-06 18:35:29 +03:00
parent d88c2249b8
commit c7f1fd74a1
16 changed files with 131 additions and 948 deletions
@@ -1,38 +0,0 @@
<idea-plugin version="2" url="http://kotlinlang.org">
<id>org.jetbrains.kotlin.android</id>
<name>Kotlin Extensions For Android</name>
<description>Various extensions facilitating Android development in Kotlin</description>
<version>@snapshot@</version>
<vendor url="http://www.jetbrains.com">JetBrains s.r.o.</vendor>
<!-- IDEA VERSION -->
<depends optional="false">org.jetbrains.kotlin</depends>
<depends optional="false">org.jetbrains.android</depends>
<extensions defaultExtensionNs="com.intellij">
<moduleService serviceInterface="org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator"
serviceImplementation="org.jetbrains.kotlin.android.synthetic.idea.res.IDESyntheticFileGenerator"/>
<moduleService serviceInterface="org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager"
serviceImplementation="org.jetbrains.kotlin.android.synthetic.idea.res.IDEAndroidLayoutXmlFileManager"/>
<compileServer.plugin classpath="jps/kotlin-android-extensions-jps.jar;android-compiler-plugin.jar"/>
<gotoDeclarationHandler implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidGotoDeclarationHandler"/>
<psi.treeChangePreprocessor implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidPsiTreeChangePreprocessor"/>
<renamePsiElementProcessor id="KotlinAndroidSyntheticProperty"
implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidRenameProcessor"
order="first"/>
<errorHandler implementation="org.jetbrains.kotlin.plugin.android.AndroidExtensionsReportSubmitter"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<externalDeclarationsProvider implementation="org.jetbrains.kotlin.android.synthetic.idea.IDEAndroidExternalDeclarationsProvider"/>
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.synthetic.codegen.AndroidExpressionCodegenExtension"/>
<findUsagesHandlerDecorator implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidFindUsageHandlerDecorator"/>
<simpleNameReferenceExtension implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidSimpleNameReferenceExtension"/>
<defaultErrorMessages implementation="org.jetbrains.kotlin.android.synthetic.diagnostic.DefaultErrorMessagesAndroid"/>
<storageComponentContainerContributor implementation="org.jetbrains.kotlin.android.synthetic.AndroidExtensionPropertiesComponentContainerContributor"/>
<classBuilderFactoryInterceptorExtension implementation="org.jetbrains.kotlin.android.synthetic.codegen.AndroidOnDestroyClassBuilderInterceptorExtension"/>
</extensions>
</idea-plugin>
@@ -1,112 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.find.findUsages.FindUsagesOptions
import com.intellij.find.findUsages.JavaVariableFindUsagesOptions
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.module.ModuleServiceManager
import com.intellij.psi.PsiElement
import com.intellij.psi.xml.XmlAttribute
import com.intellij.usageView.UsageInfo
import com.intellij.util.Processor
import org.jetbrains.android.util.AndroidResourceUtil
import org.jetbrains.kotlin.android.synthetic.isAndroidSyntheticElement
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import org.jetbrains.kotlin.plugin.findUsages.handlers.KotlinFindUsagesHandlerDecorator
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtProperty
import java.util.ArrayList
class AndroidFindUsageHandlerDecorator : KotlinFindUsagesHandlerDecorator {
override fun decorateHandler(element: PsiElement, forHighlightUsages: Boolean, delegate: FindUsagesHandler): FindUsagesHandler {
if (element !is KtNamedDeclaration) return delegate
if (!isAndroidSyntheticElement(element)) return delegate
return AndroidFindMemberUsagesHandler(element, delegate)
}
}
class AndroidFindMemberUsagesHandler(
private val declaration: KtNamedDeclaration,
private val delegate: FindUsagesHandler? = null
) : FindUsagesHandler(declaration) {
override fun getPrimaryElements(): Array<PsiElement> {
assert(isAndroidSyntheticElement(declaration))
val property = declaration as KtProperty
val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements()
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<SyntheticFileGenerator>())
val psiElements = parser?.layoutXmlFileManager?.propertyToXmlAttributes(property)
val valueElements = psiElements?.map { (it as? XmlAttribute)?.getValueElement() as? PsiElement }?.filterNotNull()
if (valueElements != null && valueElements.isNotEmpty()) return valueElements.toTypedArray()
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 property = declaration as KtProperty
val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements()
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<SyntheticFileGenerator>())
val psiElements = parser?.layoutXmlFileManager?.propertyToXmlAttributes(property) ?: listOf()
val res = ArrayList<PsiElement>()
for (psiElement in psiElements) {
if (psiElement is XmlAttribute) {
val fields = AndroidResourceUtil.findIdFields(psiElement)
for (field in fields) {
res.add(field)
}
res.add(declaration)
}
}
if (res.isNotEmpty()) return res.toTypedArray()
return super.getSecondaryElements()
}
override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
assert(isAndroidSyntheticElement(declaration))
val findUsagesOptions = JavaVariableFindUsagesOptions(runReadAction { element.project })
findUsagesOptions.isSearchForTextOccurrences = false
findUsagesOptions.isSkipImportStatements = true
findUsagesOptions.isUsages = true
findUsagesOptions.isReadAccess = true
findUsagesOptions.isWriteAccess = true
return super.processElementUsages(element, processor, findUsagesOptions)
}
// Android extensions plugin has it's own runtime -> different function classes
private fun runReadAction<T>(action: () -> T): T {
return ApplicationManager.getApplication().runReadAction<T>(action)
}
}
@@ -23,31 +23,44 @@ import com.intellij.openapi.module.ModuleServiceManager
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.xml.XmlAttribute
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
import org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
public class AndroidGotoDeclarationHandler : GotoDeclarationHandler {
override fun getGotoDeclarationTargets(sourceElement: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
if (sourceElement is LeafPsiElement && sourceElement.parent is KtSimpleNameExpression) {
val resolved = KtSimpleNameReference(sourceElement.parent as KtSimpleNameExpression).resolve()
val property = resolved as? KtProperty ?: return null
val simpleNameExpression = sourceElement.parent as? KtSimpleNameExpression ?: return null
val layoutManager = getLayoutManager(sourceElement) ?: return null
val propertyDescriptor = resolvePropertyDescriptor(simpleNameExpression) ?: return null
val moduleInfo = sourceElement.getModuleInfo()
if (moduleInfo !is ModuleSourceInfo) return null
val parser = ModuleServiceManager.getService(moduleInfo.module, SyntheticFileGenerator::class.java)!!
val psiElements = parser.layoutXmlFileManager.propertyToXmlAttributes(property)
val psiElements = layoutManager.propertyToXmlAttributes(propertyDescriptor)
val valueElements = psiElements.map { (it as? XmlAttribute)?.valueElement as? PsiElement }.filterNotNull()
if (valueElements.isNotEmpty()) return valueElements.toTypedArray()
}
return null
}
private fun resolvePropertyDescriptor(simpleNameExpression: KtSimpleNameExpression): PropertyDescriptor? {
val bindingContext = simpleNameExpression.analyze(BodyResolveMode.PARTIAL)
val call = bindingContext[BindingContext.CALL, simpleNameExpression]
val resolvedCall = bindingContext[BindingContext.RESOLVED_CALL, call]
return resolvedCall?.resultingDescriptor as? PropertyDescriptor
}
private fun getLayoutManager(sourceElement: PsiElement): AndroidLayoutXmlFileManager? {
val moduleInfo = sourceElement.getModuleInfo()
if (moduleInfo !is ModuleSourceInfo) return null
return ModuleServiceManager.getService(moduleInfo.module, AndroidLayoutXmlFileManager::class.java)
}
override fun getActionText(context: DataContext?): String? {
return null
}
@@ -1,152 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleServiceManager
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiField
import com.intellij.psi.impl.light.LightElement
import com.intellij.psi.search.SearchScope
import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.xml.XmlAttributeValue
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper
import org.jetbrains.android.util.AndroidResourceUtil
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idToName
import org.jetbrains.kotlin.android.synthetic.isAndroidSyntheticElement
import org.jetbrains.kotlin.android.synthetic.nameToIdDeclaration
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import org.jetbrains.kotlin.psi.KtProperty
public class AndroidRenameProcessor : RenamePsiElementProcessor() {
override fun canProcessElement(element: PsiElement): Boolean {
// Either renaming synthetic property, or value in layout xml, or R class field
return (element.namedUnwrappedElement is KtProperty &&
isAndroidSyntheticElement(element.namedUnwrappedElement)) || element is XmlAttributeValue ||
isRClassField(element)
}
private fun isRClassField(element: PsiElement): Boolean {
return if (element is PsiField) {
val outerClass = element.getParent()?.getParent()
if (outerClass !is PsiClass) return false
if (outerClass.getQualifiedName()?.startsWith(AndroidConst.SYNTHETIC_PACKAGE) ?: false)
true else false
}
else false
}
private fun PsiElement.getModule(): Module? {
val moduleInfo = getModuleInfo()
return if (moduleInfo is ModuleSourceInfo) moduleInfo.module else null
}
override fun prepareRenaming(
element: PsiElement?,
newName: String,
allRenames: MutableMap<PsiElement, String>,
scope: SearchScope
) {
if (element != null && element.namedUnwrappedElement is KtProperty) {
renameSyntheticProperty(element.namedUnwrappedElement as KtProperty, newName, allRenames)
}
else if (element is XmlAttributeValue) {
renameAttributeValue(element, newName, allRenames)
}
else if (element is LightElement) {
renameLightClassField(element, newName, allRenames)
}
}
private fun renameSyntheticProperty(
jetProperty: KtProperty,
newName: String,
allRenames: MutableMap<PsiElement, String>
) {
val module = jetProperty.getModule() ?: return
val processor = ModuleServiceManager.getService(module, javaClass<SyntheticFileGenerator>())!!
val resourceManager = processor.layoutXmlFileManager
val psiElements = resourceManager.propertyToXmlAttributes(jetProperty).map { it as? XmlAttribute }.filterNotNull()
for (psiElement in psiElements) {
val valueElement = psiElement.getValueElement()
if (valueElement != null) {
allRenames[XmlAttributeValueWrapper(valueElement)] = nameToIdDeclaration(newName)
val name = AndroidResourceUtil.getResourceNameByReferenceText(newName) ?: return
for (resField in AndroidResourceUtil.findIdFields(psiElement)) {
allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name))
}
}
}
}
private fun renameAttributeValue(
attribute: XmlAttributeValue,
newName: String,
allRenames: MutableMap<PsiElement, String>
) {
val element = LazyValueResourceElementWrapper.computeLazyElement(attribute)
val module = attribute.getModule() ?: ModuleUtilCore.findModuleForFile(
attribute.getContainingFile().getVirtualFile(), attribute.getProject()) ?: return
val processor = ModuleServiceManager.getService(module, javaClass<SyntheticFileGenerator>())!!
if (element == null) return
val oldPropName = AndroidResourceUtil.getResourceNameByReferenceText(attribute.getValue())
val newPropName = idToName(newName)
if (oldPropName != null && newPropName != null) {
renameSyntheticProperties(allRenames, newPropName, oldPropName, processor)
}
}
private fun renameSyntheticProperties(
allRenames: MutableMap<PsiElement, String>,
newPropName: String,
oldPropName: String,
processor: SyntheticFileGenerator
) {
val props = processor.getSyntheticFiles().flatMap { it.findChildrenByClass(javaClass<KtProperty>()).toList() }
val matchedProps = props.filter { it.getName() == oldPropName } ?: listOf()
for (prop in matchedProps) {
allRenames[prop] = newPropName
}
}
private fun renameLightClassField(
field: LightElement,
newName: String,
allRenames: MutableMap<PsiElement, String>
) {
val oldName = field.getName()!!
val processor = ServiceManager.getService(field.getProject(), javaClass<SyntheticFileGenerator>())
renameSyntheticProperties(allRenames, newName, oldName, processor)
}
}
@@ -17,30 +17,25 @@
package org.jetbrains.kotlin.android.synthetic.idea
import com.intellij.psi.PsiElement
import com.intellij.psi.xml.XmlAttributeValue
import org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper
import org.jetbrains.kotlin.android.synthetic.isAndroidSyntheticElement
import org.jetbrains.android.util.AndroidResourceUtil
import org.jetbrains.kotlin.android.synthetic.androidIdToName
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.plugin.references.SimpleNameReferenceExtension
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPsiFactory
public class AndroidSimpleNameReferenceExtension : SimpleNameReferenceExtension {
override fun isReferenceTo(reference: KtSimpleNameReference, element: PsiElement): Boolean? {
val resolvedElement = reference.resolve() as? KtProperty ?: return null
class AndroidSimpleNameReferenceExtension : SimpleNameReferenceExtension {
if (isAndroidSyntheticElement(resolvedElement)) {
if (element is ValueResourceElementWrapper) {
val resource = element.value
return resolvedElement.name == resource.substring(resource.indexOf('/') + 1)
}
}
return null
override fun isReferenceTo(reference: KtSimpleNameReference, element: PsiElement): Boolean {
return element is ValueResourceElementWrapper && AndroidResourceUtil.isIdDeclaration(element)
}
override fun handleElementRename(reference: KtSimpleNameReference, psiFactory: KtPsiFactory, newElementName: String): PsiElement? {
return if (newElementName.startsWith("@+id/"))
psiFactory.createNameIdentifier(newElementName.substring(newElementName.indexOf('/') + 1))
else
null
val resolvedElement = reference.resolve()
if (resolvedElement !is XmlAttributeValue || !AndroidResourceUtil.isIdDeclaration(resolvedElement)) return null
val newSyntheticPropertyName = androidIdToName(newElementName) ?: return null
return psiFactory.createNameIdentifier(newSyntheticPropertyName)
}
}
@@ -22,7 +22,7 @@ import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.xml.XmlElement
import com.intellij.psi.xml.XmlTag
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idToName
import org.jetbrains.kotlin.android.synthetic.androidIdToName
import org.jetbrains.kotlin.android.synthetic.isWidgetTypeIgnored
class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> Unit) : XmlElementVisitor() {
@@ -47,7 +47,7 @@ class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> U
val idAttributeValue = idAttribute.value
if (idAttributeValue != null) {
val xmlType = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.value ?: localName
val name = idToName(idAttributeValue)
val name = androidIdToName(idAttributeValue)
if (name != null) elementCallback(name, xmlType, idAttribute)
}
}
@@ -1,40 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea
import com.intellij.openapi.module.ModuleServiceManager
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.android.synthetic.idea.res.IDESyntheticFileGenerator
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
import org.jetbrains.kotlin.extensions.ExternalDeclarationsProvider
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.moduleInfo
public class IDEAndroidExternalDeclarationsProvider(private val project: Project) : ExternalDeclarationsProvider {
override fun getExternalDeclarations(moduleInfo: ModuleInfo?): Collection<KtFile> {
if (moduleInfo !is ModuleSourceInfo) return listOf()
val module = moduleInfo.module
val parser = ModuleServiceManager.getService(module, SyntheticFileGenerator::class.java) as? IDESyntheticFileGenerator
val syntheticFiles = parser?.getSyntheticFiles()
syntheticFiles?.forEach { it.moduleInfo = moduleInfo }
return syntheticFiles ?: listOf()
}
}
@@ -1,23 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea;
import com.intellij.openapi.util.Key;
public class TestConst {
public static final Key<String> TESTDATA_PATH = Key.create("");
}
@@ -1,459 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.navigation.PsiElementNavigationItem;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.meta.PsiMetaOwner;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiElementProcessor;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.io.File;
/**
* Created by IntelliJ IDEA.
* User: Eugene.Kudelevsky
* Date: Aug 6, 2009
* Time: 7:08:02 PM
* To change this template use File | Settings | File Templates.
*/
public class XmlAttributeValueWrapper implements XmlAttributeValue, PsiNamedElement, PsiElementNavigationItem {
private final XmlAttributeValue myWrappee;
private final String myFileName;
private final String myDirName;
public XmlAttributeValueWrapper(@NotNull XmlAttributeValue wrappeeElement) {
if (!(wrappeeElement instanceof NavigationItem)) {
throw new IllegalArgumentException();
}
if (!(wrappeeElement instanceof PsiMetaOwner)) {
throw new IllegalArgumentException();
}
myWrappee = wrappeeElement;
final PsiFile file = getContainingFile();
myFileName = file != null ? file.getName() : null;
final PsiDirectory dir = file != null ? file.getContainingDirectory() : null;
myDirName = dir != null ? dir.getName() : null;
}
@Override
@NotNull
public Project getProject() throws PsiInvalidElementAccessException {
return myWrappee.getProject();
}
@Override
@NotNull
public Language getLanguage() {
return myWrappee.getLanguage();
}
@Override
public PsiManager getManager() {
return myWrappee.getManager();
}
@Override
@NotNull
public PsiElement[] getChildren() {
return myWrappee.getChildren();
}
@Override
public PsiElement getParent() {
return myWrappee.getParent();
}
@Override
@Nullable
public PsiElement getFirstChild() {
return myWrappee.getFirstChild();
}
@Override
@Nullable
public PsiElement getLastChild() {
return myWrappee.getLastChild();
}
@Override
@Nullable
public PsiElement getNextSibling() {
return myWrappee.getNextSibling();
}
@Override
@Nullable
public PsiElement getPrevSibling() {
return myWrappee.getPrevSibling();
}
@Override
public PsiFile getContainingFile() throws PsiInvalidElementAccessException {
return myWrappee.getContainingFile();
}
@Override
public TextRange getTextRange() {
return myWrappee.getTextRange();
}
@Override
public int getStartOffsetInParent() {
return myWrappee.getStartOffsetInParent();
}
@Override
public int getTextLength() {
return myWrappee.getTextLength();
}
@Override
@Nullable
public PsiElement findElementAt(int offset) {
return myWrappee.findElementAt(offset);
}
@Override
@Nullable
public PsiReference findReferenceAt(int offset) {
return myWrappee.findReferenceAt(offset);
}
@Override
public int getTextOffset() {
return myWrappee.getTextOffset();
}
@Override
@NonNls
public String getText() {
return myWrappee.getText();
}
@Override
@NotNull
public char[] textToCharArray() {
return myWrappee.textToCharArray();
}
@Override
public PsiElement getNavigationElement() {
return myWrappee.getNavigationElement();
}
@Override
public PsiElement getOriginalElement() {
return myWrappee.getOriginalElement();
}
@Override
public boolean textMatches(@NotNull @NonNls CharSequence text) {
return myWrappee.textMatches(text);
}
@Override
public boolean textMatches(@NotNull PsiElement element) {
return myWrappee.textMatches(element);
}
@Override
public boolean textContains(char c) {
return myWrappee.textContains(c);
}
@Override
public void accept(@NotNull PsiElementVisitor visitor) {
myWrappee.accept(visitor);
}
@Override
public void acceptChildren(@NotNull PsiElementVisitor visitor) {
myWrappee.acceptChildren(visitor);
}
@NotNull
@Override
public PsiElement copy() {
return myWrappee.copy();
}
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
return myWrappee.add(element);
}
@Override
public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
return myWrappee.addBefore(element, anchor);
}
@Override
public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
return myWrappee.addAfter(element, anchor);
}
@Override
public void checkAdd(@NotNull PsiElement element) throws IncorrectOperationException {
//noinspection deprecation
myWrappee.checkAdd(element);
}
@Override
public PsiElement addRange(PsiElement first, PsiElement last) throws IncorrectOperationException {
return myWrappee.addRange(first, last);
}
@Override
public PsiElement addRangeBefore(@NotNull PsiElement first, @NotNull PsiElement last, PsiElement anchor)
throws IncorrectOperationException {
return myWrappee.addRangeBefore(first, last, anchor);
}
@Override
public PsiElement addRangeAfter(PsiElement first, PsiElement last, PsiElement anchor) throws IncorrectOperationException {
return myWrappee.addRangeAfter(first, last, anchor);
}
@Override
public void delete() throws IncorrectOperationException {
myWrappee.delete();
}
@Override
public void checkDelete() throws IncorrectOperationException {
//noinspection deprecation
myWrappee.checkDelete();
}
@Override
public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException {
myWrappee.deleteChildRange(first, last);
}
@NotNull
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
return myWrappee.replace(newElement);
}
@Override
public boolean isValid() {
return myWrappee.isValid();
}
@Override
public boolean isWritable() {
return myWrappee.isWritable();
}
@Override
@Nullable
public PsiReference getReference() {
return myWrappee.getReference();
}
@Override
@NotNull
public PsiReference[] getReferences() {
return myWrappee.getReferences();
}
@Override
@Nullable
public <T> T getCopyableUserData(Key<T> key) {
return myWrappee.getCopyableUserData(key);
}
@Override
public <T> void putCopyableUserData(Key<T> key, T value) {
myWrappee.putCopyableUserData(key, value);
}
@Override
public boolean processDeclarations(
@NotNull PsiScopeProcessor processor,
@NotNull ResolveState state,
@Nullable PsiElement lastParent,
@NotNull PsiElement place
) {
return myWrappee.processDeclarations(processor, state, lastParent, place);
}
@Override
@Nullable
public PsiElement getContext() {
return myWrappee.getContext();
}
@Override
public boolean isPhysical() {
return myWrappee.isPhysical();
}
@Override
@NotNull
public GlobalSearchScope getResolveScope() {
return myWrappee.getResolveScope();
}
@Override
@NotNull
public SearchScope getUseScope() {
return myWrappee.getUseScope();
}
@Override
@Nullable
public ASTNode getNode() {
return myWrappee.getNode();
}
@NonNls
public String toString() {
return myWrappee.toString();
}
@Override
public boolean isEquivalentTo(PsiElement another) {
return myWrappee == another || myWrappee.isEquivalentTo(another);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
XmlAttributeValueWrapper that = (XmlAttributeValueWrapper) o;
if (!myWrappee.equals(that.myWrappee)) return false;
return true;
}
@Override
public int hashCode() {
return myWrappee.hashCode();
}
@Override
public <T> T getUserData(@NotNull Key<T> key) {
return myWrappee.getUserData(key);
}
@Override
public <T> void putUserData(@NotNull Key<T> key, @Nullable T value) {
myWrappee.putUserData(key, value);
}
@Override
public Icon getIcon(int flags) {
return myWrappee.getIcon(flags);
}
@Override
public String getName() {
return ((NavigationItem) myWrappee).getName();
}
@Override
@Nullable
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
XmlAttribute attribute = (XmlAttribute) myWrappee.getParent();
attribute.setValue(name);
return null;
}
@Override
public ItemPresentation getPresentation() {
return new ItemPresentation() {
@Override
@Nullable
public String getPresentableText() {
String name = ((NavigationItem) myWrappee).getName();
if (myDirName == null || myFileName == null) {
return name;
}
return name + " (..." + File.separatorChar + myDirName +
File.separatorChar + myFileName + ')';
}
@Override
public String getLocationString() {
return null;
}
@Override
public Icon getIcon(boolean open) {
return null;
}
};
}
@Override
public void navigate(boolean requestFocus) {
((NavigationItem) myWrappee).navigate(requestFocus);
}
@Override
public boolean canNavigate() {
return ((NavigationItem) myWrappee).canNavigate();
}
@Override
public boolean canNavigateToSource() {
return ((NavigationItem) myWrappee).canNavigateToSource();
}
@Override
public String getValue() {
return myWrappee.getValue();
}
@Override
public TextRange getValueTextRange() {
return myWrappee.getValueTextRange();
}
@Override
public boolean processElements(PsiElementProcessor processor, PsiElement place) {
return myWrappee.processElements(processor, place);
}
@Override
public PsiElement getTargetElement() {
return myWrappee;
}
}
@@ -19,22 +19,47 @@ package org.jetbrains.kotlin.android.synthetic.idea.res
import com.android.builder.model.SourceProvider
import com.intellij.openapi.module.Module
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.impl.PsiTreeChangePreprocessor
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import org.jetbrains.android.facet.AndroidFacet
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idea.AndroidXmlVisitor
import org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager
import org.jetbrains.kotlin.android.synthetic.res.AndroidModule
import org.jetbrains.kotlin.android.synthetic.res.AndroidVariant
import org.jetbrains.kotlin.android.synthetic.res.AndroidVariantData
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.android.synthetic.AndroidConst.SYNTHETIC_PACKAGE_PATH_LENGTH
import org.jetbrains.kotlin.android.synthetic.idea.AndroidPsiTreeChangePreprocessor
import org.jetbrains.kotlin.android.synthetic.res.*
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
public class IDEAndroidLayoutXmlFileManager(val module: Module) : AndroidLayoutXmlFileManager(module.project) {
override val androidModule: AndroidModule? by lazy { module.androidFacet?.toAndroidModuleInfo() }
override fun propertyToXmlAttributes(property: KtProperty): List<PsiElement> {
val fqPath = property.fqName?.pathSegments() ?: return listOf()
private val moduleData: CachedValue<AndroidModuleData> by lazy {
cachedValue(project) {
CachedValueProvider.Result.create(super.getModuleData(), getPsiTreeChangePreprocessor())
}
}
override fun getModuleData() = moduleData.value
private fun getPsiTreeChangePreprocessor(): PsiTreeChangePreprocessor {
return project.getExtensions(PsiTreeChangePreprocessor.EP_NAME).first { it is AndroidPsiTreeChangePreprocessor }
}
override fun doExtractResources(files: List<PsiFile>, module: ModuleDescriptor): List<AndroidResource> {
val widgets = arrayListOf<AndroidResource>()
val visitor = AndroidXmlVisitor { id, widgetType, attribute ->
widgets += parseAndroidResource(id, widgetType, attribute.valueElement)
}
files.forEach { it.accept(visitor) }
return widgets
}
override fun propertyToXmlAttributes(propertyDescriptor: PropertyDescriptor): List<PsiElement> {
val fqPath = propertyDescriptor.fqNameUnsafe.pathSegments()
if (fqPath.size <= SYNTHETIC_PACKAGE_PATH_LENGTH) return listOf()
fun handle(variantData: AndroidVariantData, defaultVariant: Boolean = false): List<PsiElement>? {
@@ -44,7 +69,7 @@ public class IDEAndroidLayoutXmlFileManager(val module: Module) : AndroidLayoutX
val layoutFiles = variantData[layoutName] ?: return null
if (layoutFiles.isEmpty()) return null
val propertyName = property.name
val propertyName = propertyDescriptor.name.asString()
val attributes = arrayListOf<PsiElement>()
val visitor = AndroidXmlVisitor { retId, wClass, valueElement ->
@@ -55,7 +80,7 @@ public class IDEAndroidLayoutXmlFileManager(val module: Module) : AndroidLayoutX
return attributes
}
for (variantData in getLayoutXmlFiles()) {
for (variantData in getModuleData()) {
if (variantData.variant.isMainVariant && fqPath.size == SYNTHETIC_PACKAGE_PATH_LENGTH + 2) {
handle(variantData, true)?.let { return it }
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea.res
import com.intellij.openapi.module.ModuleServiceManager
import com.intellij.openapi.project.Project
import org.jetbrains.android.facet.AndroidFacet
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager
import org.jetbrains.kotlin.android.synthetic.res.AndroidPackageFragmentProviderExtension
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
class IDEAndroidPackageFragmentProviderExtension : AndroidPackageFragmentProviderExtension() {
override fun getLayoutXmlFileManager(project: Project, moduleInfo: ModuleInfo?): AndroidLayoutXmlFileManager? {
val moduleSourceInfo = moduleInfo as? ModuleSourceInfo ?: return null
val module = moduleSourceInfo.module
if (AndroidFacet.getInstance(module) == null) return null
return ModuleServiceManager.getService(module, AndroidLayoutXmlFileManager::class.java)
}
}
@@ -1,75 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.android.synthetic.idea.res
import com.intellij.openapi.module.Module
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiFile
import com.intellij.psi.impl.PsiTreeChangePreprocessor
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider.Result
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idea.AndroidPsiTreeChangePreprocessor
import org.jetbrains.kotlin.android.synthetic.idea.AndroidXmlVisitor
import org.jetbrains.kotlin.android.synthetic.res.AndroidResource
import org.jetbrains.kotlin.android.synthetic.res.AndroidWidget
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
import org.jetbrains.kotlin.psi.KtFile
class IDESyntheticFileGenerator(val module: Module) : SyntheticFileGenerator(module.project) {
private val cachedJetFiles: CachedValue<List<KtFile>> by lazy {
cachedValue {
val supportV4 = supportV4Available()
Result.create(generateSyntheticJetFiles(generateSyntheticFiles(true, supportV4)), psiTreeChangePreprocessor)
}
}
override val layoutXmlFileManager: IDEAndroidLayoutXmlFileManager = IDEAndroidLayoutXmlFileManager(module)
private val psiTreeChangePreprocessor by lazy {
module.project.getExtensions(PsiTreeChangePreprocessor.EP_NAME).first { it is AndroidPsiTreeChangePreprocessor }
}
public override fun getSyntheticFiles(): List<KtFile> {
if (!checkIfClassExist(AndroidConst.VIEW_FQNAME)) return listOf()
return cachedJetFiles.value
}
override fun extractLayoutResources(files: List<PsiFile>): List<AndroidResource> {
val widgets = arrayListOf<AndroidResource>()
val visitor = AndroidXmlVisitor { id, widgetType, attribute ->
widgets += parseAndroidResource(id, widgetType) { tag ->
resolveFqClassNameForView(tag)
}
}
files.forEach { it.accept(visitor) }
return filterDuplicates(widgets)
}
override fun parseAndroidWidget(id: String, tag: String, fqNameResolver: (String) -> String?): AndroidResource {
val fqName = fqNameResolver(tag)
val invalidType = if (fqName != null) null else tag
return AndroidWidget(id, fqName ?: AndroidConst.VIEW_FQNAME, invalidType)
}
override fun checkIfClassExist(fqName: String): Boolean {
val moduleScope = module.getModuleWithDependenciesAndLibrariesScope(false)
return JavaPsiFacade.getInstance(module.project).findClass(fqName, moduleScope) != null
}
}