Android Extensions refactoring

This commit is contained in:
Yan Zhulanow
2015-08-18 16:31:26 +03:00
parent 2de4de4a1e
commit ad10340bd7
29 changed files with 254 additions and 241 deletions
@@ -12,22 +12,22 @@
<depends optional="false">org.jetbrains.android</depends>
<extensions defaultExtensionNs="com.intellij">
<moduleService serviceInterface="org.jetbrains.kotlin.lang.resolve.android.AndroidUIXmlProcessor"
serviceImplementation="org.jetbrains.kotlin.plugin.android.IDEAndroidUIXmlProcessor"/>
<moduleService serviceInterface="org.jetbrains.kotlin.lang.resolve.android.AndroidResourceManager"
serviceImplementation="org.jetbrains.kotlin.plugin.android.IDEAndroidResourceManager"/>
<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.plugin.android.AndroidGotoDeclarationHandler"/>
<psi.treeChangePreprocessor implementation="org.jetbrains.kotlin.plugin.android.AndroidPsiTreeChangePreprocessor"/>
<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.plugin.android.AndroidRenameProcessor"
implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidRenameProcessor"
order="first"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<externalDeclarationsProvider implementation="org.jetbrains.kotlin.plugin.android.IDEAndroidExternalDeclarationsProvider"/>
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.AndroidExpressionCodegenExtension"/>
<findUsagesHandlerDecorator implementation="org.jetbrains.kotlin.plugin.android.AndroidFindUsageHandlerDecorator"/>
<simpleNameReferenceExtension implementation="org.jetbrains.kotlin.plugin.android.AndroidSimpleNameReferenceExtension"/>
<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"/>
</extensions>
</idea-plugin>
@@ -14,28 +14,27 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
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 com.intellij.find.findUsages.FindUsagesOptions
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.components.ServiceManager
import com.intellij.psi.xml.XmlAttribute
import org.jetbrains.android.util.AndroidResourceUtil
import java.util.ArrayList
import com.intellij.find.findUsages.JavaVariableFindUsagesOptions
import com.intellij.openapi.module.ModuleServiceManager
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.JetNamedDeclaration
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.lang.resolve.android.isAndroidSyntheticElement
import org.jetbrains.kotlin.lang.resolve.android.AndroidUIXmlProcessor
import java.util.ArrayList
class AndroidFindUsageHandlerDecorator : KotlinFindUsagesHandlerDecorator {
override fun decorateHandler(element: PsiElement, forHighlightUsages: Boolean, delegate: FindUsagesHandler): FindUsagesHandler {
@@ -56,9 +55,9 @@ class AndroidFindMemberUsagesHandler(
val property = declaration as JetProperty
val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements()
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<AndroidUIXmlProcessor>())
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<SyntheticFileGenerator>())
val psiElements = parser?.resourceManager?.propertyToXmlAttributes(property)
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()
@@ -74,9 +73,9 @@ class AndroidFindMemberUsagesHandler(
val property = declaration as JetProperty
val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements()
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<AndroidUIXmlProcessor>())
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<SyntheticFileGenerator>())
val psiElements = parser?.resourceManager?.propertyToXmlAttributes(property) ?: listOf()
val psiElements = parser?.layoutXmlFileManager?.propertyToXmlAttributes(property) ?: listOf()
val res = ArrayList<PsiElement>()
for (psiElement in psiElements) {
@@ -97,7 +96,7 @@ class AndroidFindMemberUsagesHandler(
override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
assert(isAndroidSyntheticElement(declaration))
val findUsagesOptions = JavaVariableFindUsagesOptions(runReadAction { element.getProject() })
val findUsagesOptions = JavaVariableFindUsagesOptions(runReadAction { element.project })
findUsagesOptions.isSearchForTextOccurrences = false
findUsagesOptions.isSkipImportStatements = true
findUsagesOptions.isUsages = true
@@ -105,4 +104,9 @@ class AndroidFindMemberUsagesHandler(
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)
}
}
@@ -14,21 +14,21 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
import com.intellij.psi.PsiElement
import com.intellij.openapi.editor.Editor
import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import com.intellij.psi.xml.XmlAttribute
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.kotlin.lang.resolve.android.AndroidUIXmlProcessor
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.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
public class AndroidGotoDeclarationHandler : GotoDeclarationHandler {
@@ -40,8 +40,8 @@ public class AndroidGotoDeclarationHandler : GotoDeclarationHandler {
val moduleInfo = sourceElement.getModuleInfo()
if (moduleInfo !is ModuleSourceInfo) return null
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<AndroidUIXmlProcessor>())!!
val psiElements = parser.resourceManager.propertyToXmlAttributes(property)
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.isNotEmpty()) return valueElements.toTypedArray()
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.openapi.roots.ProjectRootManager
@@ -25,7 +25,7 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.impl.PsiTreeChangeEventImpl
import com.intellij.psi.impl.PsiTreeChangePreprocessor
import com.intellij.psi.xml.XmlFile
import org.jetbrains.kotlin.lang.resolve.android.AndroidResourceManager
import org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager
public class AndroidPsiTreeChangePreprocessor : PsiTreeChangePreprocessor, SimpleModificationTracker() {
@@ -47,7 +47,7 @@ public class AndroidPsiTreeChangePreprocessor : PsiTreeChangePreprocessor, Simpl
val projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex()
val module = projectFileIndex.getModuleForFile(file.getVirtualFile())
if (module != null) {
val resourceManager = AndroidResourceManager.getInstance(module)
val resourceManager = AndroidLayoutXmlFileManager.getInstance(module)
val resDirectories = resourceManager.getModuleResDirectories()
val baseDirectory = file.getParent()?.getParent()?.getVirtualFile()
@@ -60,7 +60,7 @@ public class AndroidPsiTreeChangePreprocessor : PsiTreeChangePreprocessor, Simpl
}
}
private fun AndroidResourceManager.getModuleResDirectories(): List<VirtualFile> {
private fun AndroidLayoutXmlFileManager.getModuleResDirectories(): List<VirtualFile> {
val info = androidModuleInfo ?: return listOf()
val fileManager = VirtualFileManager.getInstance()
@@ -14,34 +14,31 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
import org.jetbrains.kotlin.psi.moduleInfo
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.JetProperty
import com.intellij.psi.xml.XmlAttributeValue
import com.intellij.psi.PsiField
import com.intellij.psi.PsiClass
import com.intellij.openapi.module.Module
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import org.jetbrains.kotlin.psi.JetFile
import com.intellij.psi.search.SearchScope
import com.intellij.psi.impl.light.LightElement
import org.jetbrains.kotlin.lang.resolve.android.AndroidUIXmlProcessor
import com.intellij.psi.xml.XmlAttribute
import org.jetbrains.kotlin.lang.resolve.android
import org.jetbrains.kotlin.lang.resolve.android.AndroidConst
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.android.util.AndroidResourceUtil
import org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.kotlin.lang.resolve.android.isAndroidSyntheticElement
import org.jetbrains.kotlin.lang.resolve.android.nameToIdDeclaration
import org.jetbrains.kotlin.lang.resolve.android.idToName
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.JetProperty
public class AndroidRenameProcessor : RenamePsiElementProcessor() {
@@ -93,8 +90,8 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
) {
val module = jetProperty.getModule() ?: return
val processor = ModuleServiceManager.getService(module, javaClass<AndroidUIXmlProcessor>())!!
val resourceManager = processor.resourceManager
val processor = ModuleServiceManager.getService(module, javaClass<SyntheticFileGenerator>())!!
val resourceManager = processor.layoutXmlFileManager
val psiElements = resourceManager.propertyToXmlAttributes(jetProperty).map { it as? XmlAttribute }.filterNotNull()
@@ -120,7 +117,7 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
val module = attribute.getModule() ?: ModuleUtilCore.findModuleForFile(
attribute.getContainingFile().getVirtualFile(), attribute.getProject()) ?: return
val processor = ModuleServiceManager.getService(module, javaClass<AndroidUIXmlProcessor>())!!
val processor = ModuleServiceManager.getService(module, javaClass<SyntheticFileGenerator>())!!
if (element == null) return
val oldPropName = AndroidResourceUtil.getResourceNameByReferenceText(attribute.getValue())
val newPropName = idToName(newName)
@@ -133,9 +130,9 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
allRenames: MutableMap<PsiElement, String>,
newPropName: String,
oldPropName: String,
processor: AndroidUIXmlProcessor
processor: SyntheticFileGenerator
) {
val props = processor.parseToPsi()?.flatMap { it.findChildrenByClass(javaClass<JetProperty>()).toList() }
val props = processor.getSyntheticFiles()?.flatMap { it.findChildrenByClass(javaClass<JetProperty>()).toList() }
val matchedProps = props?.filter { it.getName() == oldPropName } ?: listOf()
for (prop in matchedProps) {
allRenames[prop] = newPropName
@@ -148,7 +145,7 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
allRenames: MutableMap<PsiElement, String>
) {
val oldName = field.getName()!!
val processor = ServiceManager.getService(field.getProject(), javaClass<AndroidUIXmlProcessor>())
val processor = ServiceManager.getService(field.getProject(), javaClass<SyntheticFileGenerator>())
renameSyntheticProperties(allRenames, newName, oldName, processor)
}
@@ -14,24 +14,24 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import org.jetbrains.kotlin.plugin.references.SimpleNameReferenceExtension
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import com.intellij.psi.PsiElement
import org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper
import org.jetbrains.kotlin.android.synthetic.isAndroidSyntheticElement
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import org.jetbrains.kotlin.plugin.references.SimpleNameReferenceExtension
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.lang.resolve.android.isAndroidSyntheticElement
public class AndroidSimpleNameReferenceExtension : SimpleNameReferenceExtension {
override fun isReferenceTo(reference: JetSimpleNameReference, element: PsiElement): Boolean? {
val resolvedElement = reference.resolve() ?: return null
val resolvedElement = reference.resolve() as? JetProperty ?: return null
if (isAndroidSyntheticElement(resolvedElement)) {
if (element is ValueResourceElementWrapper) {
val resource = element.getValue()
return (resolvedElement as JetProperty).getName() == resource.substring(resource.indexOf('/') + 1)
val resource = element.value
return resolvedElement.name == resource.substring(resource.indexOf('/') + 1)
}
}
return null
@@ -14,15 +14,16 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.XmlElementVisitor
import com.intellij.psi.PsiElement
import com.intellij.psi.XmlElementVisitor
import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.xml.XmlElement
import com.intellij.psi.xml.XmlTag
import org.jetbrains.kotlin.lang.resolve.android
import org.jetbrains.kotlin.lang.resolve.android.*
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idToName
import org.jetbrains.kotlin.android.synthetic.isWidgetTypeIgnored
class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> Unit) : XmlElementVisitor() {
@@ -35,7 +36,7 @@ class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> U
}
override fun visitXmlTag(tag: XmlTag?) {
val localName = tag?.getLocalName() ?: ""
val localName = tag?.localName ?: ""
if (isWidgetTypeIgnored(localName)) {
tag?.acceptChildren(this)
return
@@ -43,9 +44,9 @@ class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> U
val idAttribute = tag?.getAttribute(AndroidConst.ID_ATTRIBUTE)
if (idAttribute != null) {
val idAttributeValue = idAttribute.getValue()
val idAttributeValue = idAttribute.value
if (idAttributeValue != null) {
val xmlType = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.getValue() ?: localName
val xmlType = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.value ?: localName
val name = idToName(idAttributeValue)
if (name != null) elementCallback(name, xmlType, idAttribute)
}
@@ -14,15 +14,15 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.extensions.ExternalDeclarationsProvider
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.idea.caches.resolve.ModuleSourceInfo
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.kotlin.lang.resolve.android.AndroidUIXmlProcessor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.ModuleInfo
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.JetFile
import org.jetbrains.kotlin.psi.moduleInfo
public class IDEAndroidExternalDeclarationsProvider(private val project: Project) : ExternalDeclarationsProvider {
@@ -30,8 +30,8 @@ public class IDEAndroidExternalDeclarationsProvider(private val project: Project
if (moduleInfo !is ModuleSourceInfo) return listOf()
val module = moduleInfo.module
val parser = ModuleServiceManager.getService(module, javaClass<AndroidUIXmlProcessor>())!!
val syntheticFiles = parser.parseToPsi()
val parser = ModuleServiceManager.getService(module, javaClass<SyntheticFileGenerator>())!!
val syntheticFiles = parser.getSyntheticFiles()
syntheticFiles?.forEach { it.moduleInfo = moduleInfo }
return syntheticFiles ?: listOf()
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android;
package org.jetbrains.kotlin.android.synthetic.idea;
import com.intellij.openapi.util.Key;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android;
package org.jetbrains.kotlin.android.synthetic.idea;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
@@ -14,21 +14,20 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea.res
import com.intellij.openapi.module.Module
import com.intellij.psi.PsiElement
import org.jetbrains.android.facet.AndroidFacet
import org.jetbrains.kotlin.lang.resolve.android.AndroidConst
import kotlin.properties.Delegates
import org.jetbrains.kotlin.plugin.android.AndroidXmlVisitor
import org.jetbrains.kotlin.lang.resolve.android.AndroidResourceManager
import org.jetbrains.kotlin.lang.resolve.android.AndroidModuleInfo
import org.jetbrains.kotlin.android.synthetic.AndroidConst
import org.jetbrains.kotlin.android.synthetic.idea.AndroidXmlVisitor
import org.jetbrains.kotlin.android.synthetic.res.AndroidModuleInfo
import org.jetbrains.kotlin.android.synthetic.res.AndroidLayoutXmlFileManager
import org.jetbrains.kotlin.psi.JetProperty
public class IDEAndroidResourceManager(val module: Module) : AndroidResourceManager(module.getProject()) {
public class IDEAndroidLayoutXmlFileManager(val module: Module) : AndroidLayoutXmlFileManager(module.getProject()) {
override val androidModuleInfo: AndroidModuleInfo? by Delegates.lazy { module.androidFacet?.toAndroidModuleInfo() }
override val androidModuleInfo: AndroidModuleInfo? by lazy { module.androidFacet?.toAndroidModuleInfo() }
override fun propertyToXmlAttributes(property: JetProperty): List<PsiElement> {
val fqPath = property.getFqName()?.pathSegments() ?: return listOf()
@@ -53,7 +52,7 @@ public class IDEAndroidResourceManager(val module: Module) : AndroidResourceMana
get() = AndroidFacet.getInstance(this)
private fun AndroidFacet.toAndroidModuleInfo(): AndroidModuleInfo? {
val applicationPackage = getManifest()?.getPackage()?.toString()
val applicationPackage = manifest?.getPackage()?.toString()
val mainResDirectories = getAllResourceDirectories().map { it.getPath() }
return if (applicationPackage != null) {
@@ -14,21 +14,25 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.plugin.android
package org.jetbrains.kotlin.android.synthetic.idea.res
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.module.Module
import com.intellij.psi.JavaPsiFacade
import org.jetbrains.kotlin.plugin.android.IDEAndroidResourceManager
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.plugin.android.AndroidXmlVisitor
import com.intellij.psi.impl.*
import kotlin.properties.*
import org.jetbrains.kotlin.lang.resolve.android.*
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.parseAndroidResource
import org.jetbrains.kotlin.android.synthetic.res.AndroidResource
import org.jetbrains.kotlin.android.synthetic.res.AndroidSyntheticFile
import org.jetbrains.kotlin.android.synthetic.res.SyntheticFileGenerator
class IDEAndroidUIXmlProcessor(val module: Module) : AndroidUIXmlProcessor(module.getProject()) {
class IDESyntheticFileGenerator(val module: Module) : SyntheticFileGenerator(module.project) {
private val supportV4: Boolean
init {
val scope = module.getModuleWithDependenciesAndLibrariesScope(false)
@@ -36,26 +40,28 @@ class IDEAndroidUIXmlProcessor(val module: Module) : AndroidUIXmlProcessor(modul
.findClasses(AndroidConst.SUPPORT_FRAGMENT_FQNAME, scope).isNotEmpty()
}
override val resourceManager: IDEAndroidResourceManager = IDEAndroidResourceManager(module)
override fun supportV4() = supportV4
private val psiTreeChangePreprocessor by Delegates.lazy {
module.getProject().getExtensions(PsiTreeChangePreprocessor.EP_NAME).first { it is AndroidPsiTreeChangePreprocessor }
override val layoutXmlFileManager: IDEAndroidLayoutXmlFileManager = IDEAndroidLayoutXmlFileManager(module)
private val psiTreeChangePreprocessor by lazy {
module.project.getExtensions(PsiTreeChangePreprocessor.EP_NAME).first { it is AndroidPsiTreeChangePreprocessor }
}
override val cachedSources: CachedValue<List<AndroidSyntheticFile>> by Delegates.lazy {
override val cachedSources: CachedValue<List<AndroidSyntheticFile>> by lazy {
cachedValue {
Result.create(parse(), psiTreeChangePreprocessor)
Result.create(generateSyntheticFiles(), psiTreeChangePreprocessor)
}
}
override fun parseLayout(files: List<PsiFile>): List<AndroidResource> {
override fun extractLayoutResources(files: List<PsiFile>): List<AndroidResource> {
val widgets = arrayListOf<AndroidResource>()
val visitor = AndroidXmlVisitor { id, widgetType, attribute ->
widgets.add(parseAndroidResource(id, widgetType))
}
files.forEach { it.accept(visitor) }
return removeDuplicates(widgets)
return filterDuplicates(widgets)
}
}