Support multi-module configuration in Android plugin

This commit is contained in:
Yan Zhulanow
2014-12-29 14:58:05 +03:00
parent 438d2ee5fa
commit 4f9aba2cdc
48 changed files with 150 additions and 69 deletions
@@ -12,7 +12,7 @@
<depends optional="false">org.jetbrains.android</depends>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.jet.lang.resolve.android.AndroidUIXmlProcessor"
<moduleService serviceInterface="org.jetbrains.jet.lang.resolve.android.AndroidUIXmlProcessor"
serviceImplementation="org.jetbrains.jet.plugin.android.IDEAndroidUIXmlProcessor"/>
<compileServer.plugin classpath="jps/kotlin-android-extensions-jps.jar;android-compiler-plugin.jar"/>
<gotoDeclarationHandler implementation="org.jetbrains.jet.plugin.android.AndroidGotoDeclarationHandler"/>
@@ -22,7 +22,7 @@
</extensions>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<externalDeclarationsProvider implementation="org.jetbrains.kotlin.android.AndroidDeclarationsProvider"/>
<externalDeclarationsProvider implementation="org.jetbrains.jet.plugin.android.IDEAndroidExternalDeclarationsProvider"/>
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.AndroidExpressionCodegen"/>
<findUsagesHandlerDecorator implementation="org.jetbrains.jet.plugin.android.AndroidFindUsageHandlerDecorator"/>
<simpleNameReferenceExtension implementation="org.jetbrains.jet.plugin.android.AndroidSimpleNameReferenceExtension"/>
@@ -30,6 +30,9 @@ import com.intellij.psi.impl.light.LightElement
import com.intellij.psi.xml.XmlAttribute
import org.jetbrains.jet.lang.resolve.android.isRClassField
import com.intellij.psi.PsiField
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.jet.plugin.caches.resolve.getModuleInfo
import org.jetbrains.jet.plugin.caches.resolve.ModuleSourceInfo
public class AndroidGotoDeclarationHandler : GotoDeclarationHandler {
override fun getGotoDeclarationTargets(sourceElement: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
@@ -41,10 +44,13 @@ public class AndroidGotoDeclarationHandler : GotoDeclarationHandler {
}
else null
if (name != null) {
val parser = ServiceManager.getService(sourceElement.getProject(), javaClass<AndroidUIXmlProcessor>())
val psiElement = parser?.resourceManager?.idToXmlAttribute(name) as? XmlAttribute
val moduleInfo = sourceElement.getModuleInfo()
if (moduleInfo !is ModuleSourceInfo) return null
val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass<AndroidUIXmlProcessor>())
val psiElement = parser.resourceManager.idToXmlAttribute(name) as? XmlAttribute
if (psiElement != null) {
return array(psiElement.getValueElement()!!)
return array(psiElement.getValueElement())
}
}
}
@@ -30,6 +30,12 @@ import org.jetbrains.android.util.AndroidResourceUtil
import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.impl.light.LightElement
import org.jetbrains.jet.lang.resolve.android.isRClassField
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.jet.plugin.caches.resolve.getModuleInfo
import org.jetbrains.jet.plugin.caches.resolve.ModuleSourceInfo
import com.intellij.openapi.module.Module
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.moduleInfo
public class AndroidRenameProcessor : RenamePsiElementProcessor() {
override fun canProcessElement(element: PsiElement): Boolean {
@@ -39,9 +45,22 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
isRClassField(element)
}
private fun PsiElement.getModule(): Module? {
val moduleInfo = getModuleInfo()
if (moduleInfo is ModuleSourceInfo) return moduleInfo.module
val file = getContainingFile() as? JetFile
if (file != null) {
val moduleInfo = file.moduleInfo
if (moduleInfo is ModuleSourceInfo) return moduleInfo.module
}
return null
}
override fun prepareRenaming(element: PsiElement?, newName: String?, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
if (element?.namedUnwrappedElement is JetProperty) {
renameSyntheticProperty(element!!.namedUnwrappedElement as JetProperty, newName, allRenames, scope)
if (element != null && element.namedUnwrappedElement is JetProperty) {
renameSyntheticProperty(element.namedUnwrappedElement as JetProperty, newName, allRenames, scope)
}
else if (element is XmlAttributeValue) {
renameAttributeValue(element, newName, allRenames, scope)
@@ -52,8 +71,11 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
}
private fun renameSyntheticProperty(jetProperty: JetProperty, newName: String?, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
val oldName = jetProperty.getName()!!
val processor = ServiceManager.getService(jetProperty.getProject(), javaClass<AndroidUIXmlProcessor>())
val oldName = jetProperty.getName()
val module = jetProperty.getModule()
if (module == null) return
val processor = ModuleServiceManager.getService(module, javaClass<AndroidUIXmlProcessor>())
val resourceManager = processor!!.resourceManager
val attr = resourceManager.idToXmlAttribute(oldName) as XmlAttribute
allRenames[XmlAttributeValueWrapper(attr.getValueElement()!!)] = resourceManager.nameToIdDeclaration(newName!!)
@@ -64,8 +86,11 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
}
private fun renameAttributeValue(attribute: XmlAttributeValue, newName: String?, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
val element1 = LazyValueResourceElementWrapper.computeLazyElement(attribute);
val processor = ServiceManager.getService(attribute.getProject(), javaClass<AndroidUIXmlProcessor>())
val element1 = LazyValueResourceElementWrapper.computeLazyElement(attribute)
val module = attribute.getModule()
if (module == null) return
val processor = ModuleServiceManager.getService(module, javaClass<AndroidUIXmlProcessor>())
if (element1 == null) return
val oldPropName = AndroidResourceUtil.getResourceNameByReferenceText(attribute.getValue()!!)
val newPropName = processor!!.resourceManager.idToName(newName!!)
@@ -81,7 +106,7 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
}
private fun renameLightClassField(field: LightElement, newName: String?, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
val oldName = field.getName()!!
val oldName = field.getName()
val processor = ServiceManager.getService(field.getProject(), javaClass<AndroidUIXmlProcessor>())
renameSyntheticProperties(allRenames, newName!!, oldName, processor!!)
}
@@ -0,0 +1,40 @@
/*
* 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.openapi.project.Project
import org.jetbrains.jet.extensions.ExternalDeclarationsProvider
import org.jetbrains.jet.analyzer.ModuleInfo
import org.jetbrains.jet.lang.psi.JetFile
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.jet.lang.resolve.android.AndroidUIXmlProcessor
import org.jetbrains.jet.plugin.caches.resolve.ModuleSourceInfo
import com.intellij.openapi.module.ModuleServiceManager
import org.jetbrains.jet.lang.psi.moduleInfo
public class IDEAndroidExternalDeclarationsProvider(private val project: Project) : ExternalDeclarationsProvider {
override fun getExternalDeclarations(moduleInfo: ModuleInfo?): Collection<JetFile> {
if (moduleInfo !is ModuleSourceInfo) return listOf()
val module = moduleInfo.module
val parser = ModuleServiceManager.getService<AndroidUIXmlProcessor>(module, javaClass<AndroidUIXmlProcessor>())
val syntheticFile = parser.parseToPsi(project)
syntheticFile?.moduleInfo = moduleInfo
return if (syntheticFile != null) listOf(syntheticFile) else listOf()
}
}
@@ -27,8 +27,9 @@ import org.jetbrains.jet.lang.resolve.android.AndroidManifest
import com.intellij.openapi.module.ModuleManager
import java.util.ArrayList
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.module.Module
public class IDEAndroidResourceManager(project: Project, searchPath: String?) : AndroidResourceManagerBase(project, searchPath) {
public class IDEAndroidResourceManager(val module: Module, searchPath: String?) : AndroidResourceManagerBase(module.getProject(), searchPath) {
override fun getLayoutXmlFiles(): Collection<PsiFile> {
val directories = getAndroidFacet()?.getAllResourceDirectories() ?: listOf()
@@ -38,17 +39,13 @@ public class IDEAndroidResourceManager(project: Project, searchPath: String?) :
}
private fun getAndroidFacet(): AndroidFacet? {
for (module in ModuleManager.getInstance(project).getModules()) {
val facet = AndroidFacet.getInstance(module)
if (facet != null) return facet
}
return null
return AndroidFacet.getInstance(module)
}
override fun readManifest(): AndroidManifest {
val facet = getAndroidFacet()
val attributeValue = facet?.getManifest()!!.getPackage()
return AndroidManifest(attributeValue!!.getRawText()!!)
return AndroidManifest(attributeValue.getRawText())
}
override fun idToXmlAttribute(id: String): PsiElement? {
@@ -25,13 +25,14 @@ import org.jetbrains.jet.lang.resolve.android.KotlinStringWriter
import com.intellij.openapi.application.ApplicationManager
import org.jetbrains.jet.lang.resolve.android.CliAndroidResourceManager
import org.jetbrains.jet.lang.resolve.android.AndroidResourceManager
import com.intellij.openapi.module.Module
class IDEAndroidUIXmlProcessor(project: Project) : AndroidUIXmlProcessor(project) {
class IDEAndroidUIXmlProcessor(val module: Module) : AndroidUIXmlProcessor(module.getProject()) {
override val searchPath: String? = project.getBasePath() + "/res/layout/"
override var androidAppPackage: String = ""
get() = resourceManager.readManifest()._package
override val resourceManager: IDEAndroidResourceManager = IDEAndroidResourceManager(project, searchPath)
override val resourceManager: IDEAndroidResourceManager = IDEAndroidResourceManager(module, searchPath)
override fun parseSingleFileImpl(file: PsiFile): String {
val ids: MutableCollection<AndroidWidget> = ArrayList()