Remove as32 bunch files
This commit is contained in:
-81
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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
|
||||
|
||||
import com.android.SdkConstants
|
||||
import com.android.resources.ResourceType.*
|
||||
import com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType.FRAMEWORK
|
||||
import com.android.tools.idea.rendering.GutterIconRenderer
|
||||
import com.android.tools.idea.res.resolveColor
|
||||
import com.android.tools.idea.res.resolveDrawable
|
||||
import com.intellij.lang.annotation.AnnotationHolder
|
||||
import com.intellij.lang.annotation.Annotator
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.android.AndroidColorAnnotator
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.kotlin.android.ResourceReferenceAnnotatorUtil.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||
|
||||
|
||||
class AndroidResourceReferenceAnnotator : Annotator {
|
||||
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
|
||||
val reference = element as? KtReferenceExpression ?: return
|
||||
val androidFacet = AndroidFacet.getInstance(element) ?: return
|
||||
val referenceTarget = reference.getResourceReferenceTargetDescriptor() ?: return
|
||||
val resourceType = referenceTarget.getAndroidResourceType() ?: return
|
||||
|
||||
if (resourceType != COLOR && resourceType != DRAWABLE && resourceType != MIPMAP) {
|
||||
return
|
||||
}
|
||||
|
||||
val referenceType = referenceTarget.getResourceReferenceType()
|
||||
val configuration = pickConfiguration(androidFacet, androidFacet.module, element.containingFile) ?: return
|
||||
val resourceValue = findResourceValue(
|
||||
resourceType,
|
||||
reference.text,
|
||||
referenceType == FRAMEWORK,
|
||||
androidFacet.module,
|
||||
configuration
|
||||
) ?: return
|
||||
|
||||
val resourceResolver = configuration.resourceResolver ?: return
|
||||
|
||||
if (resourceType == COLOR) {
|
||||
val color = resourceResolver.resolveColor(resourceValue, element.project)
|
||||
if (color != null) {
|
||||
val annotation = holder.createInfoAnnotation(element, null)
|
||||
annotation.gutterIconRenderer = ColorRenderer(element, color)
|
||||
}
|
||||
}
|
||||
else {
|
||||
var file = resourceResolver.resolveDrawable(resourceValue, element.project)
|
||||
if (file != null && file.path.endsWith(SdkConstants.DOT_XML)) {
|
||||
file = pickBitmapFromXml(file, resourceResolver, element.project)
|
||||
}
|
||||
val iconFile = AndroidColorAnnotator.pickBestBitmap(file)
|
||||
if (iconFile != null) {
|
||||
val annotation = holder.createInfoAnnotation(element, null)
|
||||
annotation.gutterIconRenderer = GutterIconRenderer(resourceResolver, element, iconFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtReferenceExpression.getResourceReferenceTargetDescriptor(): JavaPropertyDescriptor? =
|
||||
resolveToCall()?.resultingDescriptor as? JavaPropertyDescriptor
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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
|
||||
|
||||
import com.android.SdkConstants
|
||||
import com.android.SdkConstants.ANDROID_PKG
|
||||
import com.android.SdkConstants.R_CLASS
|
||||
import com.android.resources.ResourceType
|
||||
import com.android.tools.idea.AndroidPsiUtils
|
||||
import com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType.*
|
||||
import com.android.tools.idea.res.AndroidInternalRClassFinder
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.android.dom.AndroidAttributeValue
|
||||
import org.jetbrains.android.dom.manifest.Manifest
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.android.util.AndroidResourceUtil
|
||||
import org.jetbrains.android.util.AndroidResourceUtil.isManifestJavaFile
|
||||
import org.jetbrains.android.util.AndroidResourceUtil.isRJavaFile
|
||||
import org.jetbrains.android.util.AndroidUtils
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceFile
|
||||
|
||||
internal fun KtClass.findComponentDeclarationInManifest(manifest: Manifest): AndroidAttributeValue<PsiClass>? {
|
||||
val application = manifest.application ?: return null
|
||||
val type = (unsafeResolveToDescriptor(BodyResolveMode.PARTIAL) as? ClassDescriptor)?.defaultType ?: return null
|
||||
|
||||
return when {
|
||||
type.isSubclassOf(AndroidUtils.ACTIVITY_BASE_CLASS_NAME) ->
|
||||
application.activities?.find { it.activityClass.value?.qualifiedName == fqName?.asString() }?.activityClass
|
||||
type.isSubclassOf(AndroidUtils.SERVICE_CLASS_NAME) ->
|
||||
application.services?.find { it.serviceClass.value?.qualifiedName == fqName?.asString() }?.serviceClass
|
||||
type.isSubclassOf(AndroidUtils.RECEIVER_CLASS_NAME) ->
|
||||
application.receivers?.find { it.receiverClass.value?.qualifiedName == fqName?.asString() }?.receiverClass
|
||||
type.isSubclassOf(AndroidUtils.PROVIDER_CLASS_NAME) ->
|
||||
application.providers?.find { it.providerClass.value?.qualifiedName == fqName?.asString() }?.providerClass
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
internal fun PsiElement.getAndroidFacetForFile(): AndroidFacet? {
|
||||
val file = containingFile ?: return null
|
||||
return AndroidFacet.getInstance(file)
|
||||
}
|
||||
|
||||
internal fun JavaPropertyDescriptor.getAndroidResourceType(): ResourceType? {
|
||||
if (getResourceReferenceType() == NONE) {
|
||||
return null
|
||||
}
|
||||
|
||||
val containingClass = containingDeclaration as? JavaClassDescriptor ?: return null
|
||||
return ResourceType.getEnum(containingClass.name.asString())
|
||||
}
|
||||
|
||||
internal fun JavaPropertyDescriptor.getResourceReferenceType(): AndroidPsiUtils.ResourceReferenceType {
|
||||
val containingClass = containingDeclaration as? JavaClassDescriptor ?: return NONE
|
||||
val rClass = containingClass.containingDeclaration as? JavaClassDescriptor ?: return NONE
|
||||
|
||||
if (R_CLASS == rClass.name.asString()) {
|
||||
return if ((rClass.containingDeclaration as? PackageFragmentDescriptor)?.fqName?.asString() == ANDROID_PKG) {
|
||||
FRAMEWORK
|
||||
}
|
||||
else {
|
||||
APP
|
||||
}
|
||||
}
|
||||
|
||||
return NONE
|
||||
}
|
||||
|
||||
internal fun getReferredResourceOrManifestField(facet: AndroidFacet, expression: KtSimpleNameExpression, localOnly: Boolean)
|
||||
= getReferredResourceOrManifestField(facet, expression, null, localOnly)
|
||||
|
||||
internal fun getReferredResourceOrManifestField(facet: AndroidFacet, expression: KtSimpleNameExpression,
|
||||
className: String?, localOnly: Boolean): AndroidResourceUtil.MyReferredResourceFieldInfo? {
|
||||
val resFieldName = expression.getReferencedName()
|
||||
val resClassReference = expression.getPreviousInQualifiedChain() as? KtSimpleNameExpression ?: return null
|
||||
val resClassName = resClassReference.getReferencedName()
|
||||
|
||||
if (resClassName.isEmpty() || className != null && className != resClassName) {
|
||||
return null
|
||||
}
|
||||
|
||||
val rClassReference = resClassReference.getPreviousInQualifiedChain() as? KtSimpleNameExpression ?: return null
|
||||
val rClassDescriptor = rClassReference.analyze(BodyResolveMode.PARTIAL)
|
||||
.get(BindingContext.REFERENCE_TARGET, rClassReference) as? ClassDescriptor ?: return null
|
||||
|
||||
val rClassShortName = rClassDescriptor.name.asString()
|
||||
val fromManifest = AndroidUtils.MANIFEST_CLASS_NAME == rClassShortName
|
||||
|
||||
if (!fromManifest && AndroidUtils.R_CLASS_NAME != rClassShortName) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!localOnly) {
|
||||
val qName = rClassDescriptor.fqNameSafe.asString()
|
||||
|
||||
if (SdkConstants.CLASS_R == qName || AndroidInternalRClassFinder.INTERNAL_R_CLASS_QNAME == qName) {
|
||||
return AndroidResourceUtil.MyReferredResourceFieldInfo(resClassName, resFieldName, facet.module, true, false)
|
||||
}
|
||||
}
|
||||
|
||||
val containingFile = (rClassDescriptor.source.containingFile as? PsiSourceFile)?.psiFile ?: return null
|
||||
if (if (fromManifest) !isManifestJavaFile(facet, containingFile) else !isRJavaFile(facet, containingFile)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return AndroidResourceUtil.MyReferredResourceFieldInfo(resClassName, resFieldName, facet.module, false, false)
|
||||
}
|
||||
|
||||
private fun KtExpression.getPreviousInQualifiedChain(): KtExpression? {
|
||||
val receiverExpression = getQualifiedExpressionForSelector()?.receiverExpression
|
||||
return (receiverExpression as? KtQualifiedExpression)?.selectorExpression ?: receiverExpression
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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
|
||||
|
||||
import com.android.tools.idea.npw.template.ConvertJavaToKotlinProvider
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import org.jetbrains.kotlin.android.configure.KotlinAndroidGradleModuleConfigurator
|
||||
import org.jetbrains.kotlin.idea.actions.JavaToKotlinAction
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinProjectConfigurator
|
||||
import org.jetbrains.kotlin.idea.configuration.getCanBeConfiguredModules
|
||||
import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion
|
||||
|
||||
class ConvertJavaToKotlinProviderImpl : ConvertJavaToKotlinProvider {
|
||||
override fun configureKotlin(project: Project) {
|
||||
val configurator = KotlinProjectConfigurator.EP_NAME.findExtension(KotlinAndroidGradleModuleConfigurator::class.java)
|
||||
val nonConfiguredModules = getCanBeConfiguredModules(project, configurator)
|
||||
configurator.configureSilently(project, nonConfiguredModules, bundledRuntimeVersion())
|
||||
}
|
||||
|
||||
override fun getKotlinVersion(): String {
|
||||
return bundledRuntimeVersion()
|
||||
}
|
||||
|
||||
override fun convertToKotlin(project: Project, files: List<PsiJavaFile>): List<PsiFile> {
|
||||
return JavaToKotlinAction.convertFiles(files, project, askExternalCodeProcessing = false)
|
||||
}
|
||||
}
|
||||
-138
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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
|
||||
|
||||
import com.android.SdkConstants
|
||||
import com.android.resources.ResourceType
|
||||
import com.intellij.codeHighlighting.Pass
|
||||
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler
|
||||
import com.intellij.codeInsight.daemon.LineMarkerInfo
|
||||
import com.intellij.codeInsight.daemon.LineMarkerProvider
|
||||
import com.intellij.codeInsight.navigation.NavigationUtil
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.ide.highlighter.XmlFileType
|
||||
import com.intellij.navigation.GotoRelatedItem
|
||||
import com.intellij.openapi.editor.markup.GutterIconRenderer
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.xml.XmlAttributeValue
|
||||
import com.intellij.ui.awt.RelativePoint
|
||||
import com.intellij.util.Function
|
||||
import org.jetbrains.android.dom.manifest.Manifest
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.android.resourceManagers.ModuleResourceManagers
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import java.awt.event.MouseEvent
|
||||
import javax.swing.Icon
|
||||
|
||||
|
||||
class KotlinAndroidLineMarkerProvider : LineMarkerProvider {
|
||||
override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<*>? = null
|
||||
|
||||
override fun collectSlowLineMarkers(elements: List<PsiElement>, result: MutableCollection<LineMarkerInfo<PsiElement>>) {
|
||||
elements.forEach {
|
||||
(it as? KtClass)?.getLineMarkerInfo()?.let { marker -> result.add(marker) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtClass.getLineMarkerInfo(): LineMarkerInfo<PsiElement>? {
|
||||
val nameIdentifier = nameIdentifier ?: return null
|
||||
val androidFacet = AndroidFacet.getInstance(this) ?: return null
|
||||
val manifest = androidFacet.manifest ?: return null
|
||||
val manifestItems = collectGoToRelatedManifestItems(manifest)
|
||||
if (manifestItems.isEmpty() && !isClassWithLayoutXml()) {
|
||||
return null
|
||||
}
|
||||
|
||||
return LineMarkerInfo(
|
||||
nameIdentifier,
|
||||
nameIdentifier.textRange,
|
||||
AllIcons.FileTypes.Xml,
|
||||
Pass.LINE_MARKERS,
|
||||
Function { "Related XML file" },
|
||||
GutterIconNavigationHandler { e: MouseEvent, _: PsiElement ->
|
||||
NavigationUtil
|
||||
.getRelatedItemsPopup(
|
||||
manifestItems + collectGoToRelatedLayoutItems(androidFacet),
|
||||
"Go to Related Files")
|
||||
.show(RelativePoint(e))
|
||||
},
|
||||
GutterIconRenderer.Alignment.RIGHT)
|
||||
}
|
||||
|
||||
private fun KtClass.collectGoToRelatedLayoutItems(androidFacet: AndroidFacet): List<GotoRelatedItem> {
|
||||
val resources = mutableSetOf<PsiFile>()
|
||||
accept(object: KtVisitorVoid() {
|
||||
override fun visitKtElement(element: KtElement) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
|
||||
override fun visitReferenceExpression(expression: KtReferenceExpression) {
|
||||
super.visitReferenceExpression(expression)
|
||||
|
||||
val resClassName = ResourceType.LAYOUT.getName()
|
||||
val info = (expression as? KtSimpleNameExpression)?.let {
|
||||
getReferredResourceOrManifestField(androidFacet, it, resClassName, true)
|
||||
}
|
||||
|
||||
if (info == null || info.isFromManifest) {
|
||||
return
|
||||
}
|
||||
|
||||
val files = ModuleResourceManagers
|
||||
.getInstance(androidFacet)
|
||||
.localResourceManager
|
||||
.findResourcesByFieldName(resClassName, info.fieldName)
|
||||
.filterIsInstance<PsiFile>()
|
||||
|
||||
resources.addAll(files)
|
||||
}
|
||||
})
|
||||
|
||||
return resources.map { GotoRelatedLayoutItem(it) }
|
||||
}
|
||||
|
||||
private fun KtClass.collectGoToRelatedManifestItems(manifest: Manifest): List<GotoRelatedItem> =
|
||||
findComponentDeclarationInManifest(manifest)?.xmlAttributeValue?.let { listOf(GotoManifestItem(it)) } ?: emptyList()
|
||||
|
||||
private class GotoManifestItem(attributeValue: XmlAttributeValue) : GotoRelatedItem(attributeValue) {
|
||||
override fun getCustomName(): String? = "AndroidManifest.xml"
|
||||
override fun getCustomContainerName(): String? = ""
|
||||
override fun getCustomIcon(): Icon? = XmlFileType.INSTANCE.icon
|
||||
}
|
||||
|
||||
private class GotoRelatedLayoutItem(private val file: PsiFile) : GotoRelatedItem(file, "Layout Files") {
|
||||
override fun getCustomContainerName(): String? = "(${file.containingDirectory.name})"
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val CLASSES_WITH_LAYOUT_XML = arrayOf(
|
||||
SdkConstants.CLASS_ACTIVITY,
|
||||
SdkConstants.CLASS_FRAGMENT,
|
||||
SdkConstants.CLASS_V4_FRAGMENT.oldName(),
|
||||
SdkConstants.CLASS_V4_FRAGMENT.newName(),
|
||||
"android.widget.Adapter")
|
||||
|
||||
private fun KtClass.isClassWithLayoutXml(): Boolean {
|
||||
val type = (unsafeResolveToDescriptor(BodyResolveMode.PARTIAL) as? ClassDescriptor)?.defaultType ?: return false
|
||||
return CLASSES_WITH_LAYOUT_XML.any { type.isSubclassOf(it, true) }
|
||||
}
|
||||
}
|
||||
}
|
||||
-275
@@ -1,275 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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;
|
||||
|
||||
import com.android.ide.common.rendering.api.ResourceNamespace;
|
||||
import com.android.ide.common.rendering.api.ResourceValue;
|
||||
import com.android.ide.common.resources.ResourceResolver;
|
||||
import com.android.ide.common.resources.AbstractResourceRepository;
|
||||
import com.android.ide.common.resources.ResourceItem;
|
||||
import com.android.resources.ResourceType;
|
||||
import com.android.tools.idea.configurations.Configuration;
|
||||
import com.android.tools.idea.configurations.ConfigurationManager;
|
||||
import com.android.tools.idea.res.LocalResourceRepository;
|
||||
import com.android.tools.idea.res.ResourceHelper;
|
||||
import com.android.tools.idea.res.ResourceRepositoryManager;
|
||||
import com.android.tools.idea.ui.resourcechooser.ColorPicker;
|
||||
import com.android.utils.XmlUtils;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.markup.GutterIconRenderer;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlAttributeValue;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.ui.ColorIcon;
|
||||
import com.intellij.util.ui.EmptyIcon;
|
||||
import com.intellij.util.ui.JBUI;
|
||||
import org.jetbrains.android.facet.AndroidFacet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import static com.android.SdkConstants.*;
|
||||
|
||||
/**
|
||||
* Contains copied privates from AndroidColorAnnotator, so we could use them for Kotlin AndroidResourceReferenceAnnotator
|
||||
*/
|
||||
public class ResourceReferenceAnnotatorUtil {
|
||||
|
||||
private static final int ICON_SIZE = 8;
|
||||
|
||||
@Nullable
|
||||
public static File pickBitmapFromXml(@NotNull File file, @NotNull ResourceResolver resourceResolver, @NotNull Project project) {
|
||||
try {
|
||||
String xml = Files.toString(file, Charsets.UTF_8);
|
||||
Document document = XmlUtils.parseDocumentSilently(xml, true);
|
||||
if (document != null && document.getDocumentElement() != null) {
|
||||
Element root = document.getDocumentElement();
|
||||
String tag = root.getTagName();
|
||||
Element target = null;
|
||||
String attribute = null;
|
||||
if ("vector".equals(tag)) {
|
||||
// Vectors are handled in the icon cache
|
||||
return file;
|
||||
}
|
||||
else if ("bitmap".equals(tag) || "nine-patch".equals(tag)) {
|
||||
target = root;
|
||||
attribute = ATTR_SRC;
|
||||
}
|
||||
else if ("selector".equals(tag) ||
|
||||
"level-list".equals(tag) ||
|
||||
"layer-list".equals(tag) ||
|
||||
"transition".equals(tag)) {
|
||||
NodeList children = root.getChildNodes();
|
||||
for (int i = children.getLength() - 1; i >= 0; i--) {
|
||||
Node item = children.item(i);
|
||||
if (item.getNodeType() == Node.ELEMENT_NODE && TAG_ITEM.equals(item.getNodeName())) {
|
||||
target = (Element)item;
|
||||
if (target.hasAttributeNS(ANDROID_URI, ATTR_DRAWABLE)) {
|
||||
attribute = ATTR_DRAWABLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ("clip".equals(tag) || "inset".equals(tag) || "scale".equals(tag)) {
|
||||
target = root;
|
||||
attribute = ATTR_DRAWABLE;
|
||||
} else {
|
||||
// <shape> etc - no bitmap to be found
|
||||
return null;
|
||||
}
|
||||
if (attribute != null && target.hasAttributeNS(ANDROID_URI, attribute)) {
|
||||
String src = target.getAttributeNS(ANDROID_URI, attribute);
|
||||
ResourceValue value = resourceResolver.findResValue(src, false);
|
||||
if (value != null) {
|
||||
return ResourceHelper.resolveDrawable(resourceResolver, value, project);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignore) {
|
||||
// Not logging for now; afraid to risk unexpected crashes in upcoming preview. TODO: Re-enable.
|
||||
//Logger.getInstance(AndroidColorAnnotator.class).warn(String.format("Could not read/render icon image %1$s", file), e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Looks up the resource item of the given type and name for the given configuration, if any */
|
||||
@Nullable
|
||||
public static ResourceValue findResourceValue(ResourceType type,
|
||||
String name,
|
||||
boolean isFramework,
|
||||
Module module,
|
||||
Configuration configuration) {
|
||||
if (isFramework) {
|
||||
AbstractResourceRepository frameworkResources = configuration.getFrameworkResources();
|
||||
if (frameworkResources == null) {
|
||||
return null;
|
||||
}
|
||||
List<ResourceItem> items = frameworkResources.getResourceItems(ResourceNamespace.ANDROID, type, name);
|
||||
if (items.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return items.get(0).getResourceValue();
|
||||
} else {
|
||||
ResourceRepositoryManager repoManager = ResourceRepositoryManager.getOrCreateInstance(module);
|
||||
if (repoManager == null) {
|
||||
return null;
|
||||
}
|
||||
LocalResourceRepository appResources = repoManager.getAppResources(true); if (appResources == null) {
|
||||
return null;
|
||||
}
|
||||
if (!appResources.hasResourceItem(type, name)) {
|
||||
return null;
|
||||
}
|
||||
return appResources.getConfiguredValue(type, name, configuration.getFullConfig());
|
||||
}
|
||||
}
|
||||
|
||||
/** Picks a suitable configuration to use for resource resolution */
|
||||
@Nullable
|
||||
public static Configuration pickConfiguration(AndroidFacet facet, Module module, PsiFile file) {
|
||||
VirtualFile virtualFile = file.getVirtualFile();
|
||||
if(virtualFile == null) {
|
||||
return null;
|
||||
} else {
|
||||
VirtualFile parent = virtualFile.getParent();
|
||||
if(parent == null) {
|
||||
return null;
|
||||
} else {
|
||||
String parentName = parent.getName();
|
||||
VirtualFile layout;
|
||||
if(!parentName.startsWith("layout")) {
|
||||
layout = ResourceHelper.pickAnyLayoutFile(module, facet);
|
||||
if(layout == null) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
layout = virtualFile;
|
||||
}
|
||||
|
||||
return ConfigurationManager.getOrCreateInstance(module).getConfiguration(layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ColorRenderer extends GutterIconRenderer {
|
||||
private final PsiElement myElement;
|
||||
private final Color myColor;
|
||||
|
||||
ColorRenderer(@NotNull PsiElement element, @Nullable Color color) {
|
||||
myElement = element;
|
||||
myColor = color;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Icon getIcon() {
|
||||
Color color = getCurrentColor();
|
||||
return JBUI.scale(color == null ? EmptyIcon.create(ICON_SIZE) : new ColorIcon(ICON_SIZE, color));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Color getCurrentColor() {
|
||||
if (myColor != null) {
|
||||
return myColor;
|
||||
} else if (myElement instanceof XmlTag) {
|
||||
return ResourceHelper.parseColor(((XmlTag)myElement).getValue().getText());
|
||||
} else if (myElement instanceof XmlAttributeValue) {
|
||||
return ResourceHelper.parseColor(((XmlAttributeValue)myElement).getValue());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnAction getClickAction() {
|
||||
if (myColor != null) { // Cannot set colors that were derived
|
||||
return null;
|
||||
}
|
||||
return new AnAction() {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
|
||||
if (editor != null) {
|
||||
// Need ARGB support in platform color chooser; see
|
||||
// https://youtrack.jetbrains.com/issue/IDEA-123498
|
||||
//final Color color =
|
||||
// ColorChooser.chooseColor(editor.getComponent(), AndroidBundle.message("android.choose.color"), getCurrentColor());
|
||||
final Color color = ColorPicker.showDialog(editor.getComponent(), "Choose Color", getCurrentColor(), true, null, false);
|
||||
if (color != null) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (myElement instanceof XmlTag) {
|
||||
((XmlTag)myElement).getValue().setText(ResourceHelper.colorToString(color));
|
||||
} else if (myElement instanceof XmlAttributeValue) {
|
||||
XmlAttribute attribute = PsiTreeUtil.getParentOfType(myElement, XmlAttribute.class);
|
||||
if (attribute != null) {
|
||||
attribute.setValue(ResourceHelper.colorToString(color));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ColorRenderer that = (ColorRenderer)o;
|
||||
// TODO: Compare with modification count in app resources (if not framework)
|
||||
if (myColor != null ? !myColor.equals(that.myColor) : that.myColor != null) return false;
|
||||
if (!myElement.equals(that.myElement)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = myElement.hashCode();
|
||||
result = 31 * result + (myColor != null ? myColor.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
-65
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.configure
|
||||
|
||||
import com.android.tools.idea.gradle.project.model.JavaModuleModel
|
||||
import com.android.tools.idea.gradle.project.sync.idea.data.service.AndroidProjectKeys
|
||||
import com.android.tools.idea.io.FilePaths
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.project.ModuleData
|
||||
import com.intellij.openapi.externalSystem.model.project.ProjectData
|
||||
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider
|
||||
import com.intellij.openapi.externalSystem.service.project.manage.AbstractProjectDataService
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.roots.impl.libraries.LibraryEx
|
||||
import com.intellij.openapi.roots.libraries.Library
|
||||
import org.jetbrains.kotlin.idea.configuration.detectPlatformKindByPlugin
|
||||
import org.jetbrains.kotlin.idea.framework.detectLibraryKind
|
||||
import org.jetbrains.kotlin.idea.platform.tooling
|
||||
import java.io.File
|
||||
|
||||
class KotlinAndroidGradleLibraryDataService : AbstractProjectDataService<JavaModuleModel, Void>() {
|
||||
override fun getTargetDataKey() = AndroidProjectKeys.JAVA_MODULE_MODEL
|
||||
|
||||
override fun postProcess(
|
||||
toImport: MutableCollection<DataNode<JavaModuleModel>>,
|
||||
projectData: ProjectData?,
|
||||
project: Project,
|
||||
modelsProvider: IdeModifiableModelsProvider
|
||||
) {
|
||||
for (dataNode in toImport) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val targetLibraryKind = detectPlatformKindByPlugin(dataNode.parent as DataNode<ModuleData>)?.tooling?.libraryKind
|
||||
if (targetLibraryKind != null) {
|
||||
for (dep in dataNode.data.jarLibraryDependencies) {
|
||||
val library = modelsProvider.findLibraryByBinaryPath(dep.binaryPath) as LibraryEx? ?: continue
|
||||
if (library.kind == null) {
|
||||
val model = modelsProvider.getModifiableLibraryModel(library) as LibraryEx.ModifiableModelEx
|
||||
detectLibraryKind(model.getFiles(OrderRootType.CLASSES))?.let { model.kind = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun IdeModifiableModelsProvider.findLibraryByBinaryPath(path: File?): Library? {
|
||||
if (path == null) return null
|
||||
val url = FilePaths.pathToIdeaUrl(path)
|
||||
return allLibraries.firstOrNull { url in getModifiableLibraryModel(it).getUrls(OrderRootType.CLASSES) }
|
||||
}
|
||||
}
|
||||
-236
@@ -1,236 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.android.configure
|
||||
|
||||
import com.android.tools.idea.gradle.project.sync.idea.data.service.AndroidProjectKeys
|
||||
import com.android.tools.idea.gradle.util.ContentEntries.findParentContentEntry
|
||||
import com.android.tools.idea.io.FilePaths
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.ProjectKeys
|
||||
import com.intellij.openapi.externalSystem.model.project.ModuleData
|
||||
import com.intellij.openapi.externalSystem.model.project.ProjectData
|
||||
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider
|
||||
import com.intellij.openapi.externalSystem.service.project.manage.AbstractProjectDataService
|
||||
import com.intellij.openapi.externalSystem.service.project.manage.ContentRootDataService.CREATE_EMPTY_DIRECTORIES
|
||||
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.DependencyScope
|
||||
import com.intellij.openapi.roots.ModifiableRootModel
|
||||
import com.intellij.openapi.vfs.VfsUtil
|
||||
import com.intellij.util.SmartList
|
||||
import com.intellij.util.containers.stream
|
||||
import org.jetbrains.jps.model.java.JavaResourceRootType
|
||||
import org.jetbrains.jps.model.java.JavaSourceRootType
|
||||
import org.jetbrains.jps.model.module.JpsModuleSourceRootType
|
||||
import org.jetbrains.kotlin.gradle.KotlinCompilation
|
||||
import org.jetbrains.kotlin.gradle.KotlinPlatform
|
||||
import org.jetbrains.kotlin.gradle.KotlinSourceSet
|
||||
import org.jetbrains.kotlin.idea.addModuleDependencyIfNeeded
|
||||
import org.jetbrains.kotlin.idea.configuration.*
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import org.jetbrains.kotlin.idea.configuration.kotlinSourceSet
|
||||
|
||||
class KotlinAndroidGradleMPPModuleDataService : AbstractProjectDataService<ModuleData, Void>() {
|
||||
override fun getTargetDataKey() = ProjectKeys.MODULE
|
||||
|
||||
private fun shouldCreateEmptySourceRoots(
|
||||
moduleDataNode: DataNode<ModuleData>,
|
||||
module: Module
|
||||
): Boolean {
|
||||
val projectDataNode = ExternalSystemApiUtil.findParent(moduleDataNode, ProjectKeys.PROJECT) ?: return false
|
||||
if (projectDataNode.getUserData<Boolean>(CREATE_EMPTY_DIRECTORIES) == true) return true
|
||||
|
||||
val projectSystemId = projectDataNode.data.owner
|
||||
val externalSystemSettings = ExternalSystemApiUtil.getSettings(module.project, projectSystemId)
|
||||
|
||||
val path = ExternalSystemModulePropertyManager.getInstance(module).getRootProjectPath() ?: return false
|
||||
return externalSystemSettings.getLinkedProjectSettings(path)?.isCreateEmptyContentRootDirectories ?: false
|
||||
}
|
||||
|
||||
override fun postProcess(
|
||||
toImport: MutableCollection<DataNode<ModuleData>>,
|
||||
projectData: ProjectData?,
|
||||
project: Project,
|
||||
modelsProvider: IdeModifiableModelsProvider
|
||||
) {
|
||||
for (nodeToImport in toImport) {
|
||||
val projectNode = ExternalSystemApiUtil.findParent(nodeToImport, ProjectKeys.PROJECT) ?: continue
|
||||
val moduleData = nodeToImport.data
|
||||
val module = modelsProvider.findIdeModule(moduleData) ?: continue
|
||||
val shouldCreateEmptySourceRoots = shouldCreateEmptySourceRoots(nodeToImport, module)
|
||||
val rootModel = modelsProvider.getModifiableRootModel(module)
|
||||
val kotlinAndroidSourceSets = nodeToImport.kotlinAndroidSourceSets ?: emptyList()
|
||||
for (sourceSetInfo in kotlinAndroidSourceSets) {
|
||||
val compilation = sourceSetInfo.kotlinModule as? KotlinCompilation ?: continue
|
||||
for (sourceSet in compilation.sourceSets) {
|
||||
if (sourceSet.platform == KotlinPlatform.ANDROID) {
|
||||
val sourceType = if (sourceSet.isTestModule) JavaSourceRootType.TEST_SOURCE else JavaSourceRootType.SOURCE
|
||||
val resourceType = if (sourceSet.isTestModule) JavaResourceRootType.TEST_RESOURCE else JavaResourceRootType.RESOURCE
|
||||
sourceSet.sourceDirs.forEach { addSourceRoot(it, sourceType, rootModel, shouldCreateEmptySourceRoots) }
|
||||
sourceSet.resourceDirs.forEach { addSourceRoot(it, resourceType, rootModel, shouldCreateEmptySourceRoots) }
|
||||
}
|
||||
}
|
||||
}
|
||||
addExtraDependeeModules(nodeToImport, projectNode, modelsProvider, rootModel, false)
|
||||
addExtraDependeeModules(nodeToImport, projectNode, modelsProvider, rootModel, true)
|
||||
|
||||
if (nodeToImport.kotlinAndroidSourceSets == null) {
|
||||
continue
|
||||
}
|
||||
|
||||
val androidModel = getAndroidModuleModel(nodeToImport) ?: continue
|
||||
val variantName = androidModel.selectedVariant.name
|
||||
val activeSourceSetInfos = nodeToImport.kotlinAndroidSourceSets?.filter { it.kotlinModule.name.startsWith(variantName) } ?: emptyList()
|
||||
for (activeSourceSetInfo in activeSourceSetInfos) {
|
||||
val activeCompilation = activeSourceSetInfo.kotlinModule as? KotlinCompilation ?: continue
|
||||
for (sourceSet in activeCompilation.sourceSets) {
|
||||
if (sourceSet.platform != KotlinPlatform.ANDROID) {
|
||||
val sourceSetId = activeSourceSetInfo.sourceSetIdsByName[sourceSet.name] ?: continue
|
||||
val sourceSetNode = ExternalSystemApiUtil.findFirstRecursively(projectNode) {
|
||||
(it.data as? ModuleData)?.id == sourceSetId
|
||||
} as? DataNode<out ModuleData>? ?: continue
|
||||
val sourceSetData = sourceSetNode.data as? ModuleData ?: continue
|
||||
val sourceSetModule = modelsProvider.findIdeModule(sourceSetData) ?: continue
|
||||
addModuleDependencyIfNeeded(
|
||||
rootModel,
|
||||
sourceSetModule,
|
||||
activeSourceSetInfo.isTestModule,
|
||||
sourceSetNode.kotlinSourceSet?.isTestModule ?: false
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val mainSourceSetInfo = activeSourceSetInfos.firstOrNull { it.kotlinModule.name == variantName }
|
||||
if (mainSourceSetInfo != null) {
|
||||
KotlinSourceSetDataService.configureFacet(moduleData, mainSourceSetInfo, nodeToImport, module, modelsProvider)
|
||||
}
|
||||
|
||||
val kotlinFacet = KotlinFacet.get(module)
|
||||
if (kotlinFacet != null) {
|
||||
GradleProjectImportHandler.getInstances(project).forEach { it.importByModule(kotlinFacet, nodeToImport) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDependeeModuleNodes(
|
||||
moduleNode: DataNode<ModuleData>,
|
||||
projectNode: DataNode<ProjectData>,
|
||||
modelsProvider: IdeModifiableModelsProvider,
|
||||
testScope: Boolean
|
||||
): List<DataNode<out ModuleData>> {
|
||||
val androidModel = getAndroidModuleModel(moduleNode)
|
||||
if (androidModel != null) {
|
||||
val dependencies = if (testScope) {
|
||||
androidModel.selectedAndroidTestCompileDependencies
|
||||
} else {
|
||||
androidModel.selectedMainCompileLevel2Dependencies
|
||||
} ?: return emptyList()
|
||||
return dependencies
|
||||
.moduleDependencies
|
||||
.mapNotNull { projectNode.findChildModuleById(it.projectPath) }
|
||||
}
|
||||
|
||||
val javaModel = getJavaModuleModel(moduleNode)
|
||||
if (javaModel != null) {
|
||||
val scope = if (testScope) DependencyScope.TEST.name else DependencyScope.COMPILE.name
|
||||
val moduleNames = javaModel
|
||||
.javaModuleDependencies
|
||||
.filter { scope == it.scope ?: DependencyScope.COMPILE.name }
|
||||
.mapTo(HashSet()) { it.moduleName }
|
||||
return ExternalSystemApiUtil
|
||||
.getChildren(projectNode, ProjectKeys.MODULE)
|
||||
.filter { modelsProvider.findIdeModule(it.data)?.name in moduleNames }
|
||||
}
|
||||
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
private fun List<DataNode<GradleSourceSetData>>.firstByPlatformOrNull(platform: KotlinPlatform) = firstOrNull {
|
||||
it.kotlinSourceSet?.platform == platform
|
||||
}
|
||||
|
||||
private fun addExtraDependeeModules(
|
||||
moduleNode: DataNode<ModuleData>,
|
||||
projectNode: DataNode<ProjectData>,
|
||||
modelsProvider: IdeModifiableModelsProvider,
|
||||
rootModel: ModifiableRootModel,
|
||||
testScope: Boolean
|
||||
) {
|
||||
val dependeeModuleNodes = getDependeeModuleNodes(moduleNode, projectNode, modelsProvider, testScope)
|
||||
val relevantNodes = dependeeModuleNodes
|
||||
.flatMap { ExternalSystemApiUtil.getChildren(it, GradleSourceSetData.KEY) }
|
||||
.filter {
|
||||
val ktModule = it.kotlinSourceSet?.kotlinModule
|
||||
ktModule != null && ktModule.isTestModule == testScope
|
||||
}
|
||||
val commonSourceSetName = KotlinSourceSet.commonName(testScope)
|
||||
val isAndroidModule = getAndroidModuleModel(moduleNode) != null
|
||||
|
||||
val gradleSourceSetDataNodes = SmartList<DataNode<GradleSourceSetData>>()
|
||||
.apply {
|
||||
addIfNotNull(
|
||||
(if (isAndroidModule) relevantNodes.firstByPlatformOrNull(KotlinPlatform.ANDROID) else null)
|
||||
?: relevantNodes.firstByPlatformOrNull(KotlinPlatform.JVM)
|
||||
)
|
||||
addIfNotNull(
|
||||
relevantNodes.firstOrNull {
|
||||
it.kotlinSourceSet?.platform == KotlinPlatform.COMMON && it.kotlinSourceSet?.kotlinModule?.name == commonSourceSetName
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
val testKotlinModules =
|
||||
gradleSourceSetDataNodes.filter { it.kotlinSourceSet?.isTestModule ?: false }.mapNotNull { modelsProvider.findIdeModule(it.data) }
|
||||
.toSet()
|
||||
|
||||
gradleSourceSetDataNodes.forEach { node ->
|
||||
val module = modelsProvider.findIdeModule(node.data) ?: return
|
||||
addModuleDependencyIfNeeded(rootModel, module, testScope, node.kotlinSourceSet?.isTestModule ?: false)
|
||||
val dependeeRootModel = modelsProvider.getModifiableRootModel(module)
|
||||
dependeeRootModel.getModuleDependencies(testScope).forEach { transitiveDependee ->
|
||||
addModuleDependencyIfNeeded(rootModel, transitiveDependee, testScope, testKotlinModules.contains(transitiveDependee))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAndroidModuleModel(moduleNode: DataNode<ModuleData>) =
|
||||
ExternalSystemApiUtil.getChildren(moduleNode, AndroidProjectKeys.ANDROID_MODEL).firstOrNull()?.data
|
||||
|
||||
private fun getJavaModuleModel(moduleNode: DataNode<ModuleData>) =
|
||||
ExternalSystemApiUtil.getChildren(moduleNode, AndroidProjectKeys.JAVA_MODULE_MODEL).firstOrNull()?.data
|
||||
|
||||
private fun addSourceRoot(
|
||||
sourceRoot: File,
|
||||
type: JpsModuleSourceRootType<*>,
|
||||
rootModel: ModifiableRootModel,
|
||||
shouldCreateEmptySourceRoots: Boolean
|
||||
) {
|
||||
val parent = findParentContentEntry(sourceRoot, rootModel.contentEntries.stream()) ?: return
|
||||
val url = FilePaths.pathToIdeaUrl(sourceRoot)
|
||||
parent.addSourceFolder(url, type)
|
||||
if (shouldCreateEmptySourceRoots) {
|
||||
ExternalSystemApiUtil.doWriteAction {
|
||||
try {
|
||||
VfsUtil.createDirectoryIfMissing(sourceRoot.path)
|
||||
} catch (e: IOException) {
|
||||
LOG.warn(String.format("Unable to create directory for the path: %s", sourceRoot.path), e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val LOG = Logger.getInstance(KotlinAndroidGradleMPPModuleDataService::class.java)
|
||||
}
|
||||
}
|
||||
-82
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.android.configure
|
||||
|
||||
import com.android.tools.idea.gradle.project.sync.GradleSyncInvoker
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleUtil
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.idea.configuration.AndroidGradle
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator
|
||||
import org.jetbrains.kotlin.idea.configuration.getBuildSystemType
|
||||
import org.jetbrains.kotlin.idea.util.projectStructure.version
|
||||
import org.jetbrains.kotlin.idea.versions.MAVEN_STDLIB_ID_JDK7
|
||||
import org.jetbrains.kotlin.idea.versions.hasJreSpecificRuntime
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
|
||||
class KotlinAndroidGradleModuleConfigurator internal constructor() : KotlinWithGradleConfigurator() {
|
||||
|
||||
override val name: String = NAME
|
||||
|
||||
override val targetPlatform: TargetPlatform = JvmPlatform
|
||||
|
||||
override val presentableText: String = "Android with Gradle"
|
||||
|
||||
public override fun isApplicable(module: Module): Boolean = module.getBuildSystemType() == AndroidGradle
|
||||
|
||||
override val kotlinPluginName: String = KOTLIN_ANDROID
|
||||
|
||||
override fun getKotlinPluginExpression(forKotlinDsl: Boolean): String =
|
||||
if (forKotlinDsl) "kotlin(\"android\")" else "id 'org.jetbrains.kotlin.android' "
|
||||
|
||||
override fun addElementsToFile(file: PsiFile, isTopLevelProjectFile: Boolean, version: String): Boolean {
|
||||
val manipulator = getManipulator(file, false)
|
||||
val sdk = ModuleUtil.findModuleForPsiElement(file)?.let { ModuleRootManager.getInstance(it).sdk }
|
||||
val jvmTarget = getJvmTarget(sdk, version)
|
||||
return if (isTopLevelProjectFile) {
|
||||
manipulator.configureProjectBuildScript(kotlinPluginName, version)
|
||||
}
|
||||
else {
|
||||
manipulator.configureModuleBuildScript(
|
||||
kotlinPluginName,
|
||||
getKotlinPluginExpression(file.isKtDsl()),
|
||||
getStdlibArtifactName(sdk, version),
|
||||
version,
|
||||
jvmTarget
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getStdlibArtifactName(sdk: Sdk?, version: String): String {
|
||||
if (sdk != null && hasJreSpecificRuntime(version)) {
|
||||
val sdkVersion = sdk.version
|
||||
if (sdkVersion != null && sdkVersion.isAtLeast(JavaSdkVersion.JDK_1_8)) {
|
||||
// Android dex can't convert our kotlin-stdlib-jre8 artifact, so use jre7 instead (KT-16530)
|
||||
return MAVEN_STDLIB_ID_JDK7
|
||||
}
|
||||
}
|
||||
|
||||
return super.getStdlibArtifactName(sdk, version)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards
|
||||
override fun configure(project: Project, excludeModules: Collection<Module>) {
|
||||
super.configure(project, excludeModules)
|
||||
// Sync after changing build scripts
|
||||
GradleSyncInvoker.getInstance().requestProjectSync(project, GradleSyncInvoker.Request.projectModified())
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val NAME = "android-gradle"
|
||||
|
||||
private val KOTLIN_ANDROID = "kotlin-android"
|
||||
}
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.configure
|
||||
|
||||
import com.android.tools.idea.gradle.project.model.AndroidModuleModel
|
||||
import com.android.tools.idea.gradle.project.sync.setup.post.ModuleSetupStep
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.progress.ProgressIndicator
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.kotlin.idea.configuration.compilerArgumentsBySourceSet
|
||||
import org.jetbrains.kotlin.idea.configuration.configureFacetByCompilerArguments
|
||||
import org.jetbrains.kotlin.idea.configuration.sourceSetName
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
|
||||
class KotlinAndroidModuleSetupStep : ModuleSetupStep() {
|
||||
override fun setUpModule(module: Module, progressIndicator: ProgressIndicator?) {
|
||||
val facet = AndroidFacet.getInstance(module) ?: return
|
||||
val androidModel = AndroidModuleModel.get(facet) ?: return
|
||||
val sourceSetName = androidModel.selectedVariant.name
|
||||
if (module.sourceSetName == sourceSetName) return
|
||||
val argsInfo = module.compilerArgumentsBySourceSet?.get(sourceSetName) ?: return
|
||||
val kotlinFacet = KotlinFacet.get(module) ?: return
|
||||
module.sourceSetName = sourceSetName
|
||||
configureFacetByCompilerArguments(kotlinFacet, argsInfo, null)
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.configure
|
||||
|
||||
import com.android.tools.idea.gradle.project.model.AndroidModuleModel
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.Key
|
||||
import com.intellij.openapi.externalSystem.model.ProjectKeys
|
||||
import com.intellij.openapi.externalSystem.model.project.ProjectData
|
||||
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider
|
||||
import com.intellij.openapi.externalSystem.service.project.manage.AbstractProjectDataService
|
||||
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler
|
||||
import org.jetbrains.kotlin.idea.configuration.configureFacetByGradleModule
|
||||
|
||||
class KotlinGradleAndroidModuleModelProjectDataService : AbstractProjectDataService<AndroidModuleModel, Void>() {
|
||||
companion object {
|
||||
val KEY = Key<AndroidModuleModel>(AndroidModuleModel::class.qualifiedName!!, 0)
|
||||
}
|
||||
|
||||
override fun getTargetDataKey() = KEY
|
||||
|
||||
override fun postProcess(
|
||||
toImport: MutableCollection<DataNode<AndroidModuleModel>>,
|
||||
projectData: ProjectData?,
|
||||
project: Project,
|
||||
modelsProvider: IdeModifiableModelsProvider
|
||||
) {
|
||||
super.postProcess(toImport, projectData, project, modelsProvider)
|
||||
for (moduleModelNode in toImport) {
|
||||
val moduleNode = ExternalSystemApiUtil.findParent(moduleModelNode, ProjectKeys.MODULE) ?: continue
|
||||
val moduleData = moduleNode.data
|
||||
val ideModule = modelsProvider.findIdeModule(moduleData) ?: continue
|
||||
val sourceSetName = moduleModelNode.data.selectedVariant.name
|
||||
val kotlinFacet = configureFacetByGradleModule(moduleNode, sourceSetName, ideModule, modelsProvider) ?: continue
|
||||
GradleProjectImportHandler.getInstances(project).forEach { it.importByModule(kotlinFacet, moduleNode) }
|
||||
}
|
||||
}
|
||||
}
|
||||
-258
@@ -1,258 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.folding
|
||||
|
||||
import com.android.SdkConstants.*
|
||||
import com.android.ide.common.resources.configuration.FolderConfiguration
|
||||
import com.android.ide.common.resources.configuration.LocaleQualifier
|
||||
import com.android.resources.ResourceType
|
||||
import com.android.tools.idea.folding.AndroidFoldingSettings
|
||||
import com.android.tools.idea.res.LocalResourceRepository
|
||||
import com.android.tools.idea.res.ResourceRepositoryManager
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.lang.folding.FoldingBuilderEx
|
||||
import com.intellij.lang.folding.FoldingDescriptor
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.impl.source.SourceTreeToPsiMap
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.uast.*
|
||||
import org.jetbrains.uast.visitor.AbstractUastVisitor
|
||||
import java.util.regex.Pattern
|
||||
|
||||
|
||||
class ResourceFoldingBuilder : FoldingBuilderEx() {
|
||||
|
||||
companion object {
|
||||
// See lint's StringFormatDetector
|
||||
private val FORMAT = Pattern.compile("%(\\d+\\$)?([-+#, 0(<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])")
|
||||
private val FOLD_MAX_LENGTH = 60
|
||||
private val UNIT_TEST_MODE: Boolean = ApplicationManager.getApplication().isUnitTestMode
|
||||
private val RESOURCE_TYPES = listOf(ResourceType.STRING,
|
||||
ResourceType.DIMEN,
|
||||
ResourceType.INTEGER,
|
||||
ResourceType.PLURALS)
|
||||
}
|
||||
|
||||
private val isFoldingEnabled = AndroidFoldingSettings.getInstance().isCollapseAndroidStrings
|
||||
|
||||
override fun getPlaceholderText(node: ASTNode): String? {
|
||||
|
||||
tailrec fun UElement.unwrapReferenceAndGetValue(resources: LocalResourceRepository): String? = when (this) {
|
||||
is UQualifiedReferenceExpression -> selector.unwrapReferenceAndGetValue(resources)
|
||||
is UCallExpression -> (valueArguments.firstOrNull() as? UReferenceExpression)?.getAndroidResourceValue(resources, this)
|
||||
else -> (this as? UReferenceExpression)?.getAndroidResourceValue(resources)
|
||||
}
|
||||
|
||||
val element = SourceTreeToPsiMap.treeElementToPsi(node) ?: return null
|
||||
val appResources = getAppResources(element) ?: return null
|
||||
val uastContext = ServiceManager.getService(element.project, UastContext::class.java) ?: return null
|
||||
return uastContext.convertElement(element, null, null)?.unwrapReferenceAndGetValue(appResources)
|
||||
}
|
||||
|
||||
override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
|
||||
if (root !is KtFile || quick && !UNIT_TEST_MODE || !isFoldingEnabled || AndroidFacet.getInstance(root) == null) {
|
||||
return emptyArray()
|
||||
}
|
||||
|
||||
val file = root.toUElement()
|
||||
val result = arrayListOf<FoldingDescriptor>()
|
||||
file?.accept(object : AbstractUastVisitor() {
|
||||
override fun visitSimpleNameReferenceExpression(node: USimpleNameReferenceExpression): Boolean {
|
||||
node.getFoldingDescriptor()?.let { result.add(it) }
|
||||
return super.visitSimpleNameReferenceExpression(node)
|
||||
}
|
||||
})
|
||||
|
||||
return result.toTypedArray()
|
||||
}
|
||||
|
||||
override fun isCollapsedByDefault(node: ASTNode): Boolean = isFoldingEnabled
|
||||
|
||||
private fun UReferenceExpression.getFoldingDescriptor(): FoldingDescriptor? {
|
||||
val resolved = resolve() ?: return null
|
||||
val resourceType = resolved.getAndroidResourceType() ?: return null
|
||||
if (resourceType !in RESOURCE_TYPES) return null
|
||||
|
||||
fun UElement.createFoldingDescriptor() = psi?.let { psi ->
|
||||
val dependencies: Set<PsiElement> = setOf(psi)
|
||||
FoldingDescriptor(psi.node, psi.textRange, null, dependencies)
|
||||
}
|
||||
|
||||
val element = uastParent as? UQualifiedReferenceExpression ?: this
|
||||
val getResourceValueCall = (element.uastParent as? UCallExpression)?.takeIf { it.isFoldableGetResourceValueCall() }
|
||||
if (getResourceValueCall != null) {
|
||||
val qualifiedCall = getResourceValueCall.uastParent as? UQualifiedReferenceExpression
|
||||
if (qualifiedCall?.selector == getResourceValueCall) {
|
||||
return qualifiedCall.createFoldingDescriptor()
|
||||
}
|
||||
|
||||
return getResourceValueCall.createFoldingDescriptor()
|
||||
}
|
||||
|
||||
return element.createFoldingDescriptor()
|
||||
}
|
||||
|
||||
private fun UCallExpression.isFoldableGetResourceValueCall(): Boolean {
|
||||
return methodName == "getString" ||
|
||||
methodName == "getText" ||
|
||||
methodName == "getInteger" ||
|
||||
methodName?.startsWith("getDimension") ?: false ||
|
||||
methodName?.startsWith("getQuantityString") ?: false
|
||||
}
|
||||
|
||||
private fun PsiElement.getAndroidResourceType(): ResourceType? {
|
||||
val elementType = parent as? PsiClass ?: return null
|
||||
val elementPackage = elementType.parent as? PsiClass ?: return null
|
||||
if (R_CLASS != elementPackage.name) return null
|
||||
if (elementPackage.qualifiedName != "$ANDROID_PKG.$R_CLASS") {
|
||||
return ResourceType.getEnum(elementType.name)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun UReferenceExpression.getAndroidResourceValue(resources: LocalResourceRepository, call: UCallExpression? = null): String? {
|
||||
val resourceType = resolve()?.getAndroidResourceType() ?: return null
|
||||
val referenceConfig = FolderConfiguration().apply { localeQualifier = LocaleQualifier("xx") }
|
||||
val key = resolvedName ?: return null
|
||||
val resourceValue = resources.getResourceValue(resourceType, key, referenceConfig) ?: return null
|
||||
val text = if (call != null) formatArguments(call, resourceValue) else resourceValue
|
||||
|
||||
if (resourceType == ResourceType.STRING || resourceType == ResourceType.PLURALS) {
|
||||
return '"' + StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH - 2, 0) + '"'
|
||||
}
|
||||
else if (text.length <= 1) {
|
||||
// Don't just inline empty or one-character replacements: they can't be expanded by a mouse click
|
||||
// so are hard to use without knowing about the folding keyboard shortcut to toggle folding.
|
||||
// This is similar to how IntelliJ 14 handles call parameters
|
||||
return key + ": " + text
|
||||
}
|
||||
|
||||
return StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH, 0)
|
||||
}
|
||||
|
||||
private tailrec fun LocalResourceRepository.getResourceValue(
|
||||
type: ResourceType,
|
||||
name: String,
|
||||
referenceConfig: FolderConfiguration): String? {
|
||||
val value = getConfiguredValue(type, name, referenceConfig)?.value ?: return null
|
||||
if (!value.startsWith('@')) {
|
||||
return value
|
||||
}
|
||||
|
||||
val (referencedTypeName, referencedName) = value.substring(1).split('/').takeIf { it.size == 2 } ?: return value
|
||||
val referencedType = ResourceType.getEnum(referencedTypeName) ?: return value
|
||||
return getResourceValue(referencedType, referencedName, referenceConfig)
|
||||
}
|
||||
|
||||
// Converted from com.android.tools.idea.folding.InlinedResource#insertArguments
|
||||
private fun formatArguments(callExpression: UCallExpression, formatString: String): String {
|
||||
if (!formatString.contains('%')) {
|
||||
return formatString
|
||||
}
|
||||
|
||||
val args = callExpression.valueArguments
|
||||
if (args.isEmpty() || !args.first().isPsiValid) {
|
||||
return formatString
|
||||
}
|
||||
|
||||
val matcher = FORMAT.matcher(formatString)
|
||||
var index = 0
|
||||
var prevIndex = 0
|
||||
var nextNumber = 1
|
||||
var start = 0
|
||||
val sb = StringBuilder(2 * formatString.length)
|
||||
while (true) {
|
||||
if (matcher.find(index)) {
|
||||
if ("%" == matcher.group(6)) {
|
||||
index = matcher.end()
|
||||
continue
|
||||
}
|
||||
val matchStart = matcher.start()
|
||||
// Make sure this is not an escaped '%'
|
||||
while (prevIndex < matchStart) {
|
||||
val c = formatString[prevIndex]
|
||||
if (c == '\\') {
|
||||
prevIndex++
|
||||
}
|
||||
prevIndex++
|
||||
}
|
||||
if (prevIndex > matchStart) {
|
||||
// We're in an escape, ignore this result
|
||||
index = prevIndex
|
||||
continue
|
||||
}
|
||||
|
||||
index = matcher.end()
|
||||
|
||||
// Shouldn't throw a number format exception since we've already
|
||||
// matched the pattern in the regexp
|
||||
val number: Int
|
||||
var numberString: String? = matcher.group(1)
|
||||
if (numberString != null) {
|
||||
// Strip off trailing $
|
||||
numberString = numberString.substring(0, numberString.length - 1)
|
||||
number = Integer.parseInt(numberString)
|
||||
nextNumber = number + 1
|
||||
}
|
||||
else {
|
||||
number = nextNumber++
|
||||
}
|
||||
|
||||
if (number > 0 && number < args.size) {
|
||||
val argExpression = args[number]
|
||||
var value: Any? = argExpression.evaluate()
|
||||
|
||||
if (value == null) {
|
||||
value = args[number].asSourceString()
|
||||
}
|
||||
|
||||
for (i in start..matchStart - 1) {
|
||||
sb.append(formatString[i])
|
||||
}
|
||||
|
||||
sb.append("{")
|
||||
sb.append(value)
|
||||
sb.append('}')
|
||||
start = index
|
||||
}
|
||||
}
|
||||
else {
|
||||
var i = start
|
||||
val n = formatString.length
|
||||
while (i < n) {
|
||||
sb.append(formatString[i])
|
||||
i++
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun getAppResources(element: PsiElement): LocalResourceRepository? = ModuleUtilCore.findModuleForPsiElement(element)?.let {
|
||||
ResourceRepositoryManager.getOrCreateInstance(it)?.getAppResources(false)
|
||||
}
|
||||
}
|
||||
-111
@@ -1,111 +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.navigation
|
||||
|
||||
import org.jetbrains.android.util.AndroidResourceUtil
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.psi.PsiClass
|
||||
import org.jetbrains.android.util.AndroidUtils
|
||||
import com.android.SdkConstants
|
||||
import com.android.tools.idea.res.AndroidInternalRClassFinder
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
|
||||
fun getReferenceExpression(element: PsiElement?): KtSimpleNameExpression? {
|
||||
return PsiTreeUtil.getParentOfType<KtSimpleNameExpression>(element, KtSimpleNameExpression::class.java)
|
||||
}
|
||||
|
||||
// given 'R.a.b' returns info for all three parts of the expression 'a', 'b', 'R'
|
||||
fun getInfo(
|
||||
referenceExpression: KtSimpleNameExpression,
|
||||
facet: AndroidFacet
|
||||
): AndroidResourceUtil.MyReferredResourceFieldInfo? {
|
||||
val info = getReferredInfo(referenceExpression, facet)
|
||||
if (info != null) return info
|
||||
|
||||
val topMostQualified = referenceExpression.getParentQualified().getParentQualified() ?: return null
|
||||
val selectorCandidate = topMostQualified.selectorExpression as? KtSimpleNameExpression ?: return null
|
||||
return getReferredInfo(selectorCandidate, facet)
|
||||
}
|
||||
|
||||
private fun KtExpression?.getParentQualified(): KtDotQualifiedExpression? {
|
||||
return this?.parent as? KtDotQualifiedExpression
|
||||
}
|
||||
|
||||
// returns info if passed expression is 'b' in 'R.a.b'
|
||||
private fun getReferredInfo(
|
||||
lastPart: KtSimpleNameExpression,
|
||||
facet: AndroidFacet
|
||||
): AndroidResourceUtil.MyReferredResourceFieldInfo? {
|
||||
val resFieldName = lastPart.getReferencedName()
|
||||
if (resFieldName.isEmpty()) return null
|
||||
|
||||
val middlePart = getReceiverAsSimpleNameExpression(lastPart) ?: return null
|
||||
|
||||
val resClassName = middlePart.getReferencedName()
|
||||
if (resClassName.isEmpty()) return null
|
||||
|
||||
val firstPart = getReceiverAsSimpleNameExpression(middlePart) ?: return null
|
||||
|
||||
val resolvedClass = firstPart.mainReference.resolve() as? PsiClass ?: return null
|
||||
|
||||
//the following code is copied from
|
||||
// org.jetbrains.android.util.AndroidResourceUtil.getReferredResourceOrManifestField
|
||||
// (org.jetbrains.android.facet.AndroidFacet, com.intellij.psi.PsiReferenceExpression, java.lang.String, boolean)
|
||||
val classShortName = resolvedClass.name
|
||||
|
||||
val fromManifest = AndroidUtils.MANIFEST_CLASS_NAME == classShortName
|
||||
|
||||
if (!fromManifest && AndroidUtils.R_CLASS_NAME != classShortName) {
|
||||
return null
|
||||
}
|
||||
val qName = resolvedClass.qualifiedName
|
||||
|
||||
if (SdkConstants.CLASS_R == qName || AndroidInternalRClassFinder.INTERNAL_R_CLASS_QNAME == qName) {
|
||||
return AndroidResourceUtil.MyReferredResourceFieldInfo(resClassName, resFieldName, facet.module, true, false)
|
||||
}
|
||||
val containingFile = resolvedClass.containingFile ?: return null
|
||||
|
||||
val isFromCorrectFile =
|
||||
if (fromManifest) AndroidResourceUtil.isManifestJavaFile(facet, containingFile)
|
||||
else AndroidResourceUtil.isRJavaFile(facet, containingFile)
|
||||
|
||||
if (!isFromCorrectFile) {
|
||||
return null
|
||||
}
|
||||
|
||||
return AndroidResourceUtil.MyReferredResourceFieldInfo(resClassName, resFieldName, facet.module, false, fromManifest)
|
||||
}
|
||||
|
||||
private fun getReceiverAsSimpleNameExpression(exp: KtSimpleNameExpression): KtSimpleNameExpression? {
|
||||
val receiver = exp.getReceiverExpression()
|
||||
return when (receiver) {
|
||||
is KtSimpleNameExpression -> {
|
||||
receiver
|
||||
}
|
||||
is KtDotQualifiedExpression -> {
|
||||
receiver.selectorExpression as? KtSimpleNameExpression
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
|
||||
}
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.quickfix
|
||||
|
||||
import com.android.SdkConstants
|
||||
import com.android.tools.lint.checks.ApiDetector.REQUIRES_API_ANNOTATION
|
||||
import com.intellij.codeInsight.FileModificationService
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.android.inspections.lint.AndroidLintQuickFix
|
||||
import org.jetbrains.android.inspections.lint.AndroidQuickfixContexts
|
||||
import org.jetbrains.android.util.AndroidBundle
|
||||
import org.jetbrains.kotlin.android.hasBackingField
|
||||
import org.jetbrains.kotlin.idea.util.addAnnotation
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
|
||||
class AddTargetApiQuickFix(
|
||||
val api: Int,
|
||||
val useRequiresApi: Boolean,
|
||||
val useNewName: Boolean = false
|
||||
) : AndroidLintQuickFix {
|
||||
|
||||
private companion object {
|
||||
val FQNAME_TARGET_API = FqName(SdkConstants.FQCN_TARGET_API)
|
||||
}
|
||||
|
||||
override fun isApplicable(startElement: PsiElement, endElement: PsiElement, contextType: AndroidQuickfixContexts.ContextType): Boolean =
|
||||
getAnnotationContainer(startElement, useRequiresApi) != null
|
||||
|
||||
override fun getName(): String = getAnnotationValue(false).let {
|
||||
if (useRequiresApi) {
|
||||
// Not Available in Android plugin 2.0
|
||||
// AndroidBundle.message("android.lint.fix.add.requires.api", it)
|
||||
"Add @RequiresApi($it) Annotation"
|
||||
} else {
|
||||
AndroidBundle.message("android.lint.fix.add.target.api", it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun apply(startElement: PsiElement, endElement: PsiElement, context: AndroidQuickfixContexts.Context) {
|
||||
val annotationContainer = getAnnotationContainer(startElement, useRequiresApi) ?: return
|
||||
if (!FileModificationService.getInstance().preparePsiElementForWrite(annotationContainer)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (annotationContainer is KtModifierListOwner) {
|
||||
annotationContainer.addAnnotation(
|
||||
if (useRequiresApi)
|
||||
if (useNewName) FqName(REQUIRES_API_ANNOTATION.newName())
|
||||
else FqName(REQUIRES_API_ANNOTATION.oldName())
|
||||
else FQNAME_TARGET_API,
|
||||
getAnnotationValue(true),
|
||||
whiteSpaceText = if (annotationContainer.isNewLineNeededForAnnotation()) "\n" else " ")
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtElement.isNewLineNeededForAnnotation() = !(this is KtParameter || this is KtTypeParameter || this is KtPropertyAccessor)
|
||||
|
||||
private fun getAnnotationValue(fullyQualified: Boolean) = getVersionField(api, fullyQualified)
|
||||
|
||||
private fun getAnnotationContainer(element: PsiElement, useRequiresApi: Boolean): PsiElement? {
|
||||
return PsiTreeUtil.findFirstParent(element) {
|
||||
if (useRequiresApi)
|
||||
it.isRequiresApiAnnotationValidTarget()
|
||||
else
|
||||
it.isTargetApiAnnotationValidTarget()
|
||||
}
|
||||
}
|
||||
|
||||
private fun PsiElement.isRequiresApiAnnotationValidTarget(): Boolean {
|
||||
return this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral) ||
|
||||
(this is KtProperty && !isLocal && hasBackingField()) ||
|
||||
this is KtPropertyAccessor
|
||||
}
|
||||
|
||||
private fun PsiElement.isTargetApiAnnotationValidTarget(): Boolean {
|
||||
return this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral) ||
|
||||
this is KtPropertyAccessor
|
||||
}
|
||||
}
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.quickfix
|
||||
|
||||
import com.intellij.codeInsight.FileModificationService
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.android.inspections.lint.AndroidLintQuickFix
|
||||
import org.jetbrains.android.inspections.lint.AndroidQuickfixContexts
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement.KotlinIfSurrounder
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.inspections.findExistingEditor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
|
||||
class AddTargetVersionCheckQuickFix(val api: Int) : AndroidLintQuickFix {
|
||||
|
||||
override fun apply(startElement: PsiElement, endElement: PsiElement, context: AndroidQuickfixContexts.Context) {
|
||||
val targetExpression = getTargetExpression(startElement)
|
||||
val project = targetExpression?.project ?: return
|
||||
val editor = targetExpression.findExistingEditor() ?: return
|
||||
|
||||
val file = targetExpression.containingFile
|
||||
val documentManager = PsiDocumentManager.getInstance(project)
|
||||
val document = documentManager.getDocument(file) ?: return
|
||||
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(file)) {
|
||||
return
|
||||
}
|
||||
|
||||
val surrounder = getSurrounder(targetExpression, "\"VERSION.SDK_INT < ${getVersionField(api, false)}\"")
|
||||
val conditionRange = surrounder.surroundElements(project, editor, arrayOf(targetExpression)) ?: return
|
||||
val conditionText = "android.os.Build.VERSION.SDK_INT >= ${getVersionField(api, true)}"
|
||||
document.replaceString(conditionRange.startOffset, conditionRange.endOffset, conditionText)
|
||||
documentManager.commitDocument(document)
|
||||
|
||||
ShortenReferences.DEFAULT.process(documentManager.getPsiFile(document) as KtFile,
|
||||
conditionRange.startOffset,
|
||||
conditionRange.startOffset + conditionText.length)
|
||||
}
|
||||
|
||||
override fun isApplicable(startElement: PsiElement, endElement: PsiElement, contextType: AndroidQuickfixContexts.ContextType): Boolean =
|
||||
getTargetExpression(startElement) != null
|
||||
|
||||
override fun getName(): String = "Surround with if (VERSION.SDK_INT >= VERSION_CODES.${getVersionField(api, false)}) { ... }"
|
||||
|
||||
private fun getTargetExpression(element: PsiElement): KtElement? {
|
||||
var current = PsiTreeUtil.getParentOfType(element, KtExpression::class.java)
|
||||
while (current != null) {
|
||||
if (current.parent is KtBlockExpression ||
|
||||
current.parent is KtContainerNode ||
|
||||
current.parent is KtWhenEntry ||
|
||||
current.parent is KtFunction ||
|
||||
current.parent is KtPropertyAccessor ||
|
||||
current.parent is KtProperty ||
|
||||
current.parent is KtReturnExpression ||
|
||||
current.parent is KtDestructuringDeclaration) {
|
||||
break
|
||||
}
|
||||
current = PsiTreeUtil.getParentOfType(current, KtExpression::class.java, true)
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
private fun getSurrounder(element: KtElement, todoText: String?): KotlinIfSurrounder {
|
||||
val used = element.analyze(BodyResolveMode.PARTIAL_WITH_CFA)[BindingContext.USED_AS_EXPRESSION, element] ?: false
|
||||
return if (used) {
|
||||
object : KotlinIfSurrounder() {
|
||||
override fun getCodeTemplate(): String = "if (a) { \n} else {\nTODO(${todoText ?: ""})\n}"
|
||||
}
|
||||
} else {
|
||||
KotlinIfSurrounder()
|
||||
}
|
||||
}
|
||||
}
|
||||
-84
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.quickfix
|
||||
|
||||
import com.android.SdkConstants.SUPPORT_ANNOTATIONS_PREFIX
|
||||
import com.android.support.AndroidxName
|
||||
import com.android.tools.lint.checks.ApiDetector
|
||||
import com.android.tools.lint.checks.CommentDetector
|
||||
import com.android.tools.lint.checks.ParcelDetector
|
||||
import com.android.tools.lint.detector.api.Issue
|
||||
import com.intellij.psi.JavaPsiFacade
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.android.inspections.lint.AndroidLintQuickFix
|
||||
import org.jetbrains.android.inspections.lint.AndroidLintQuickFixProvider
|
||||
import java.util.regex.Pattern
|
||||
|
||||
|
||||
class KotlinAndroidQuickFixProvider : AndroidLintQuickFixProvider {
|
||||
override fun getQuickFixes(
|
||||
issue: Issue,
|
||||
startElement: PsiElement,
|
||||
endElement: PsiElement,
|
||||
message: String,
|
||||
data: Any?
|
||||
): Array<AndroidLintQuickFix> {
|
||||
val fixes: Array<AndroidLintQuickFix> = when (issue) {
|
||||
ApiDetector.UNSUPPORTED, ApiDetector.INLINED -> getApiQuickFixes(issue, startElement, message)
|
||||
ParcelDetector.ISSUE -> arrayOf(ParcelableQuickFix())
|
||||
else -> emptyArray()
|
||||
}
|
||||
|
||||
if (issue != CommentDetector.STOP_SHIP) {
|
||||
return fixes + SuppressLintQuickFix(issue.id)
|
||||
}
|
||||
|
||||
return fixes
|
||||
}
|
||||
|
||||
fun getApiQuickFixes(issue: Issue, element: PsiElement, message: String): Array<AndroidLintQuickFix> {
|
||||
val api = getRequiredVersion(message)
|
||||
if (api == -1) {
|
||||
return AndroidLintQuickFix.EMPTY_ARRAY
|
||||
}
|
||||
|
||||
val project = element.project
|
||||
if (JavaPsiFacade.getInstance(project).findClass(REQUIRES_API_ANNOTATION.newName(), GlobalSearchScope.allScope(project)) != null) {
|
||||
return arrayOf(AddTargetApiQuickFix(api, true, true), AddTargetApiQuickFix(api, false, true), AddTargetVersionCheckQuickFix(api))
|
||||
}
|
||||
if (JavaPsiFacade.getInstance(project).findClass(REQUIRES_API_ANNOTATION.oldName(), GlobalSearchScope.allScope(project)) != null) {
|
||||
return arrayOf(AddTargetApiQuickFix(api, true), AddTargetApiQuickFix(api, false), AddTargetVersionCheckQuickFix(api))
|
||||
}
|
||||
|
||||
return arrayOf(AddTargetApiQuickFix(api, false), AddTargetVersionCheckQuickFix(api))
|
||||
}
|
||||
|
||||
private fun getRequiredVersion(errorMessage: String): Int {
|
||||
val pattern = Pattern.compile("\\s(\\d+)\\s")
|
||||
val matcher = pattern.matcher(errorMessage)
|
||||
if (matcher.find()) {
|
||||
return Integer.parseInt(matcher.group(1))
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
companion object {
|
||||
val REQUIRES_API_ANNOTATION = AndroidxName.of(SUPPORT_ANNOTATIONS_PREFIX, "RequiresApi")
|
||||
}
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 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;
|
||||
|
||||
import com.android.SdkConstants;
|
||||
import com.android.tools.idea.rendering.RenderSecurityManager;
|
||||
import com.android.tools.idea.startup.AndroidCodeStyleSettingsModifier;
|
||||
import com.intellij.analysis.AnalysisScope;
|
||||
import com.intellij.codeInspection.GlobalInspectionTool;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper;
|
||||
import com.intellij.codeInspection.ex.InspectionManagerEx;
|
||||
import com.intellij.facet.FacetManager;
|
||||
import com.intellij.facet.ModifiableFacetModel;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.projectRoots.ProjectJdkTable;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.roots.ModuleRootModificationUtil;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSchemes;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.testFramework.InspectionTestUtil;
|
||||
import com.intellij.testFramework.ThreadTracker;
|
||||
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
|
||||
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
|
||||
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
|
||||
import com.intellij.testFramework.fixtures.JavaTestFixtureFactory;
|
||||
import com.intellij.testFramework.fixtures.TestFixtureBuilder;
|
||||
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl;
|
||||
import com.intellij.testFramework.fixtures.impl.GlobalInspectionContextForTests;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.android.facet.AndroidFacet;
|
||||
import org.jetbrains.android.facet.AndroidRootUtil;
|
||||
import org.jetbrains.android.formatter.AndroidXmlCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.picocontainer.MutablePicoContainer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Copied from AS 2.3 sources
|
||||
*/
|
||||
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"})
|
||||
public abstract class AndroidTestCase extends AndroidTestBase {
|
||||
protected Module myModule;
|
||||
protected List<Module> myAdditionalModules;
|
||||
|
||||
protected AndroidFacet myFacet;
|
||||
protected CodeStyleSettings mySettings;
|
||||
|
||||
private List<String> myAllowedRoots = new ArrayList<>();
|
||||
private boolean myUseCustomSettings;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
VfsRootAccess.allowRootAccess(KotlinTestUtils.getHomeDirectory());
|
||||
|
||||
TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());
|
||||
myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());
|
||||
JavaModuleFixtureBuilder moduleFixtureBuilder = projectBuilder.addModule(JavaModuleFixtureBuilder.class);
|
||||
File moduleRoot = new File(myFixture.getTempDirPath());
|
||||
|
||||
if (!moduleRoot.exists()) {
|
||||
assertTrue(moduleRoot.mkdirs());
|
||||
}
|
||||
initializeModuleFixtureBuilderWithSrcAndGen(moduleFixtureBuilder, moduleRoot.toString());
|
||||
|
||||
ArrayList<MyAdditionalModuleData> modules = new ArrayList<>();
|
||||
configureAdditionalModules(projectBuilder, modules);
|
||||
|
||||
myFixture.setUp();
|
||||
myFixture.setTestDataPath(getTestDataPath());
|
||||
myModule = moduleFixtureBuilder.getFixture().getModule();
|
||||
|
||||
// Must be done before addAndroidFacet, and must always be done, even if a test provides
|
||||
// its own custom manifest file. However, in that case, we will delete it shortly below.
|
||||
createManifest();
|
||||
|
||||
myFacet = addAndroidFacet(myModule);
|
||||
|
||||
LanguageLevel languageLevel = getLanguageLevel();
|
||||
if (languageLevel != null) {
|
||||
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(myModule.getProject());
|
||||
if (extension != null) {
|
||||
extension.setLanguageLevel(languageLevel);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: myFixture.copyDirectoryToProject(getResDir(), "res");
|
||||
|
||||
myAdditionalModules = new ArrayList<>();
|
||||
for (MyAdditionalModuleData data : modules) {
|
||||
Module additionalModule = data.myModuleFixtureBuilder.getFixture().getModule();
|
||||
myAdditionalModules.add(additionalModule);
|
||||
AndroidFacet facet = addAndroidFacet(additionalModule);
|
||||
facet.getProperties().PROJECT_TYPE = data.myProjectType;
|
||||
String rootPath = getAdditionalModulePath(data.myDirName);
|
||||
myFixture.copyDirectoryToProject(getResDir(), rootPath + "/res");
|
||||
myFixture.copyFileToProject(SdkConstants.FN_ANDROID_MANIFEST_XML, rootPath + '/' + SdkConstants.FN_ANDROID_MANIFEST_XML);
|
||||
if (data.myIsMainModuleDependency) {
|
||||
ModuleRootModificationUtil.addDependency(myModule, additionalModule);
|
||||
}
|
||||
}
|
||||
|
||||
if (providesCustomManifest()) {
|
||||
deleteManifest();
|
||||
}
|
||||
|
||||
if (RenderSecurityManager.RESTRICT_READS) {
|
||||
// Unit test class loader includes disk directories which security manager does not allow access to
|
||||
RenderSecurityManager.sEnabled = false;
|
||||
}
|
||||
|
||||
ArrayList<String> allowedRoots = new ArrayList<>();
|
||||
collectAllowedRoots(allowedRoots);
|
||||
// TODO: registerAllowedRoots(allowedRoots, myTestRootDisposable);
|
||||
mySettings = CodeStyleSettingsManager.getSettings(getProject()).clone();
|
||||
AndroidCodeStyleSettingsModifier.modify(mySettings);
|
||||
CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(mySettings);
|
||||
myUseCustomSettings = getAndroidCodeStyleSettings().USE_CUSTOM_SETTINGS;
|
||||
getAndroidCodeStyleSettings().USE_CUSTOM_SETTINGS = true;
|
||||
|
||||
// Layoutlib rendering thread will be shutdown when the app is closed so do not report it as a leak
|
||||
ThreadTracker.longRunningThreadCreated(ApplicationManager.getApplication(), "Layoutlib");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
try {
|
||||
Sdk androidSdk = ProjectJdkTable.getInstance().findJdk(ANDROID_SDK_NAME);
|
||||
if (androidSdk != null) {
|
||||
ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().removeJdk(androidSdk));
|
||||
}
|
||||
|
||||
CodeStyleSettingsManager.getInstance(getProject()).dropTemporarySettings();
|
||||
myModule = null;
|
||||
myAdditionalModules = null;
|
||||
myFixture.tearDown();
|
||||
myFixture = null;
|
||||
myFacet = null;
|
||||
getAndroidCodeStyleSettings().USE_CUSTOM_SETTINGS = myUseCustomSettings;
|
||||
if (RenderSecurityManager.RESTRICT_READS) {
|
||||
RenderSecurityManager.sEnabled = true;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
super.tearDown();
|
||||
VfsRootAccess.disallowRootAccess(KotlinTestUtils.getHomeDirectory());
|
||||
}
|
||||
}
|
||||
|
||||
private static void initializeModuleFixtureBuilderWithSrcAndGen(JavaModuleFixtureBuilder moduleFixtureBuilder, String moduleRoot) {
|
||||
moduleFixtureBuilder.addContentRoot(moduleRoot);
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(moduleRoot + "/src/").mkdir();
|
||||
moduleFixtureBuilder.addSourceRoot("src");
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(moduleRoot + "/gen/").mkdir();
|
||||
moduleFixtureBuilder.addSourceRoot("gen");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path that any additional modules registered by
|
||||
* {@link #configureAdditionalModules(TestFixtureBuilder, List)} or
|
||||
* {@link #addModuleWithAndroidFacet(TestFixtureBuilder, List, String, int, boolean)} are
|
||||
* installed into.
|
||||
*/
|
||||
protected static String getAdditionalModulePath(@NotNull String moduleName) {
|
||||
return "/additionalModules/" + moduleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this class provides its own {@code AndroidManifest.xml} for its tests. If
|
||||
* {@code true}, then {@link #setUp()} calls {@link #deleteManifest()} before finishing.
|
||||
*/
|
||||
protected boolean providesCustomManifest() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "res" directory for this SDK. Children classes can override this if they need to
|
||||
* provide a custom "res" location for tests.
|
||||
*/
|
||||
protected String getResDir() {
|
||||
return "res";
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the project level to set for the test project, or null to get the default language
|
||||
* level associated with the test project.
|
||||
*/
|
||||
@Nullable
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static AndroidXmlCodeStyleSettings getAndroidCodeStyleSettings() {
|
||||
return AndroidXmlCodeStyleSettings.getInstance(CodeStyleSchemes.getInstance().getDefaultScheme().getCodeStyleSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook point for child test classes to register directories that can be safely accessed by all
|
||||
* of its tests.
|
||||
*
|
||||
* @see {@link VfsRootAccess}
|
||||
*/
|
||||
protected void collectAllowedRoots(List<String> roots) throws IOException {
|
||||
}
|
||||
|
||||
private void registerAllowedRoots(List<String> roots, @NotNull Disposable disposable) {
|
||||
List<String> newRoots = new ArrayList<>(roots);
|
||||
newRoots.removeAll(myAllowedRoots);
|
||||
|
||||
String[] newRootsArray = ArrayUtil.toStringArray(newRoots);
|
||||
VfsRootAccess.allowRootAccess(newRootsArray);
|
||||
myAllowedRoots.addAll(newRoots);
|
||||
|
||||
Disposer.register(disposable, () -> {
|
||||
VfsRootAccess.disallowRootAccess(newRootsArray);
|
||||
myAllowedRoots.removeAll(newRoots);
|
||||
});
|
||||
}
|
||||
|
||||
public static AndroidFacet addAndroidFacet(Module module) {
|
||||
return addAndroidFacet(module, true);
|
||||
}
|
||||
|
||||
private static AndroidFacet addAndroidFacet(Module module, boolean attachSdk) {
|
||||
FacetManager facetManager = FacetManager.getInstance(module);
|
||||
AndroidFacet facet = facetManager.createFacet(AndroidFacet.getFacetType(), "Android", null);
|
||||
|
||||
if (attachSdk) {
|
||||
addLatestAndroidSdk(module);
|
||||
}
|
||||
ModifiableFacetModel facetModel = facetManager.createModifiableModel();
|
||||
facetModel.addFacet(facet);
|
||||
ApplicationManager.getApplication().runWriteAction(facetModel::commit);
|
||||
return facet;
|
||||
}
|
||||
|
||||
protected void configureAdditionalModules(
|
||||
@NotNull TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder, @NotNull List<MyAdditionalModuleData> modules) {
|
||||
}
|
||||
|
||||
protected final void addModuleWithAndroidFacet(
|
||||
@NotNull TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder,
|
||||
@NotNull List<MyAdditionalModuleData> modules,
|
||||
@NotNull String dirName,
|
||||
int projectType) {
|
||||
// By default, created module is declared as a main module's dependency
|
||||
addModuleWithAndroidFacet(projectBuilder, modules, dirName, projectType, true);
|
||||
}
|
||||
|
||||
protected final void addModuleWithAndroidFacet(
|
||||
@NotNull TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder,
|
||||
@NotNull List<MyAdditionalModuleData> modules,
|
||||
@NotNull String dirName,
|
||||
int projectType,
|
||||
boolean isMainModuleDependency) {
|
||||
JavaModuleFixtureBuilder moduleFixtureBuilder = projectBuilder.addModule(JavaModuleFixtureBuilder.class);
|
||||
String moduleDirPath = myFixture.getTempDirPath() + getAdditionalModulePath(dirName);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(moduleDirPath).mkdirs();
|
||||
initializeModuleFixtureBuilderWithSrcAndGen(moduleFixtureBuilder, moduleDirPath);
|
||||
modules.add(new MyAdditionalModuleData(moduleFixtureBuilder, dirName, projectType, isMainModuleDependency));
|
||||
}
|
||||
|
||||
protected void createManifest() throws IOException {
|
||||
myFixture.copyFileToProject(SdkConstants.FN_ANDROID_MANIFEST_XML, SdkConstants.FN_ANDROID_MANIFEST_XML);
|
||||
}
|
||||
|
||||
protected final void createProjectProperties() throws IOException {
|
||||
myFixture.copyFileToProject(SdkConstants.FN_PROJECT_PROPERTIES, SdkConstants.FN_PROJECT_PROPERTIES);
|
||||
}
|
||||
|
||||
protected final void deleteManifest() throws IOException {
|
||||
deleteManifest(myModule);
|
||||
}
|
||||
|
||||
protected final void deleteManifest(final Module module) throws IOException {
|
||||
AndroidFacet facet = AndroidFacet.getInstance(module);
|
||||
assertNotNull(facet);
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String manifestRelativePath = facet.getProperties().MANIFEST_FILE_RELATIVE_PATH;
|
||||
VirtualFile manifest = AndroidRootUtil.getFileByRelativeModulePath(module, manifestRelativePath, true);
|
||||
if (manifest != null) {
|
||||
try {
|
||||
manifest.delete(this);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Could not delete default manifest");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected final void doGlobalInspectionTest(
|
||||
@NotNull GlobalInspectionTool inspection, @NotNull String globalTestDir, @NotNull AnalysisScope scope) {
|
||||
doGlobalInspectionTest(new GlobalInspectionToolWrapper(inspection), globalTestDir, scope);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an inspection and a path to a directory that contains an "expected.xml" file, run the
|
||||
* inspection on the current test project and verify that its output matches that of the
|
||||
* expected file.
|
||||
*/
|
||||
protected final void doGlobalInspectionTest(
|
||||
@NotNull GlobalInspectionToolWrapper wrapper, @NotNull String globalTestDir, @NotNull AnalysisScope scope) {
|
||||
myFixture.enableInspections(wrapper.getTool());
|
||||
|
||||
scope.invalidate();
|
||||
|
||||
InspectionManagerEx inspectionManager = (InspectionManagerEx)InspectionManager.getInstance(getProject());
|
||||
GlobalInspectionContextForTests globalContext =
|
||||
CodeInsightTestFixtureImpl.createGlobalContextForTool(scope, getProject(), inspectionManager, wrapper);
|
||||
|
||||
InspectionTestUtil.runTool(wrapper, scope, globalContext);
|
||||
InspectionTestUtil.compareToolResults(globalContext, wrapper, false, getTestDataPath() + globalTestDir);
|
||||
}
|
||||
|
||||
protected static class MyAdditionalModuleData {
|
||||
final JavaModuleFixtureBuilder myModuleFixtureBuilder;
|
||||
final String myDirName;
|
||||
final int myProjectType;
|
||||
final boolean myIsMainModuleDependency;
|
||||
|
||||
private MyAdditionalModuleData(
|
||||
@NotNull JavaModuleFixtureBuilder moduleFixtureBuilder, @NotNull String dirName, int projectType, boolean isMainModuleDependency) {
|
||||
myModuleFixtureBuilder = moduleFixtureBuilder;
|
||||
myDirName = dirName;
|
||||
myProjectType = projectType;
|
||||
myIsMainModuleDependency = isMainModuleDependency;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected <T> T registerApplicationComponent(@NotNull Class<T> key, @NotNull T instance) throws Exception {
|
||||
MutablePicoContainer picoContainer = (MutablePicoContainer)ApplicationManager.getApplication().getPicoContainer();
|
||||
@SuppressWarnings("unchecked")
|
||||
T old = (T)picoContainer.getComponentInstance(key.getName());
|
||||
picoContainer.unregisterComponent(key.getName());
|
||||
picoContainer.registerComponentInstance(key.getName(), instance);
|
||||
return old;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected <T> T registerProjectComponent(@NotNull Class<T> key, @NotNull T instance) {
|
||||
MutablePicoContainer picoContainer = (MutablePicoContainer)getProject().getPicoContainer();
|
||||
@SuppressWarnings("unchecked")
|
||||
T old = (T)picoContainer.getComponentInstance(key.getName());
|
||||
picoContainer.unregisterComponent(key.getName());
|
||||
picoContainer.registerComponentInstance(key.getName(), instance);
|
||||
return old;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user