Extract string resource intention action for android (KT-11715)
#KT-11715 Fixed
This commit is contained in:
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
|
||||
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
|
||||
import org.jetbrains.kotlin.android.*
|
||||
import org.jetbrains.kotlin.android.configure.AbstractConfigureProjectTest
|
||||
import org.jetbrains.kotlin.android.intentions.AbstractAndroidResourceIntentionTest
|
||||
import org.jetbrains.kotlin.annotation.AbstractAnnotationProcessorBoxTest
|
||||
import org.jetbrains.kotlin.annotation.processing.test.sourceRetention.AbstractBytecodeListingTestForSourceRetention
|
||||
import org.jetbrains.kotlin.annotation.processing.test.wrappers.AbstractAnnotationProcessingTest
|
||||
@@ -1153,6 +1154,10 @@ fun main(args: Array<String>) {
|
||||
testClass<AbstractConfigureProjectTest>() {
|
||||
model("configuration/android-gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestAndroidGradle")
|
||||
}
|
||||
|
||||
testClass<AbstractAndroidResourceIntentionTest> {
|
||||
model("android/resourceIntentions", extension = "test", singleClass = true)
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("plugins/plugins-tests/tests", "plugins/android-extensions/android-extensions-jps/testData") {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<orderEntry type="module" module-name="idea" />
|
||||
<orderEntry type="module" module-name="ide-common" />
|
||||
<orderEntry type="module" module-name="idea-analysis" />
|
||||
<orderEntry type="module" module-name="idea-test-framework" scope="TEST" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.intentions
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
|
||||
|
||||
val CREATE_XML_RESOURCE_PARAMETERS_NAME_KEY = Key<String>("CREATE_XML_RESOURCE_PARAMETERS_NAME_KEY")
|
||||
|
||||
class CreateXmlResourceParameters(val name: String,
|
||||
val value: String,
|
||||
val fileName: String,
|
||||
val directoryNames: List<String>)
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* 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.intentions
|
||||
|
||||
import com.android.resources.ResourceType
|
||||
import com.intellij.CommonBundle
|
||||
import com.intellij.codeInsight.template.*
|
||||
import com.intellij.codeInsight.template.impl.*
|
||||
import com.intellij.codeInsight.template.macro.VariableOfTypeMacro
|
||||
import com.intellij.openapi.application.Application
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.command.undo.UndoUtil
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.ui.Messages
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.android.actions.CreateXmlResourceDialog
|
||||
import org.jetbrains.android.facet.AndroidFacet
|
||||
import org.jetbrains.android.util.AndroidBundle
|
||||
import org.jetbrains.android.util.AndroidResourceUtil
|
||||
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
|
||||
class KotlinAndroidAddStringResource : SelfTargetingIntention<KtLiteralStringTemplateEntry>(KtLiteralStringTemplateEntry::class.java,
|
||||
"Extract string resource") {
|
||||
private val GET_STRING_METHOD = "getString"
|
||||
private val EXTRACT_RESOURCE_DIALOG_TITLE = "Extract Resource"
|
||||
private val PACKAGE_NOT_FOUND_ERROR = "package.not.found.error"
|
||||
|
||||
override fun isApplicableTo(element: KtLiteralStringTemplateEntry, caretOffset: Int): Boolean {
|
||||
if (AndroidFacet.getInstance(element.containingFile) == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
return element.parent.children.size == 1
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtLiteralStringTemplateEntry, editor: Editor?) {
|
||||
val facet = AndroidFacet.getInstance(element.containingFile)
|
||||
if (editor == null) {
|
||||
throw IllegalArgumentException("This intention requires an editor.")
|
||||
}
|
||||
|
||||
if (facet == null) {
|
||||
throw IllegalStateException("This intention requires android facet.")
|
||||
}
|
||||
|
||||
val file = element.containingFile
|
||||
val project = file.project
|
||||
|
||||
val manifestPackage = getManifestPackage(facet)
|
||||
if (manifestPackage == null) {
|
||||
Messages.showErrorDialog(project, AndroidBundle.message(PACKAGE_NOT_FOUND_ERROR), CommonBundle.getErrorTitle())
|
||||
return
|
||||
}
|
||||
|
||||
val parameters = getCreateXmlResourceParameters(facet.module, element) ?:
|
||||
return
|
||||
|
||||
if (!AndroidResourceUtil.createValueResource(facet.module, parameters.name, ResourceType.STRING,
|
||||
parameters.fileName, parameters.directoryNames, parameters.value)) {
|
||||
return
|
||||
}
|
||||
|
||||
createResourceReference(facet.module, editor, file, element, manifestPackage, parameters.name, ResourceType.STRING)
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
||||
UndoUtil.markPsiFileForUndo(file)
|
||||
}
|
||||
|
||||
private fun getCreateXmlResourceParameters(module: Module, element: KtLiteralStringTemplateEntry): CreateXmlResourceParameters? {
|
||||
|
||||
val stringValue = element.text
|
||||
|
||||
val showDialog = !ApplicationManager.getApplication().isUnitTestMode
|
||||
val resourceName = element.getUserData(CREATE_XML_RESOURCE_PARAMETERS_NAME_KEY)
|
||||
|
||||
val dialog = CreateXmlResourceDialog(module, ResourceType.STRING, resourceName, stringValue, true)
|
||||
dialog.title = EXTRACT_RESOURCE_DIALOG_TITLE
|
||||
if (showDialog && !dialog.showAndGet()) {
|
||||
return null
|
||||
}
|
||||
|
||||
return CreateXmlResourceParameters(dialog.resourceName,
|
||||
dialog.value,
|
||||
dialog.fileName,
|
||||
dialog.dirNames)
|
||||
}
|
||||
|
||||
private fun createResourceReference(module: Module, editor: Editor, file: PsiFile, element: PsiElement, aPackage: String,
|
||||
resName: String, resType: ResourceType) {
|
||||
val rFieldName = AndroidResourceUtil.getRJavaFieldName(resName)
|
||||
val fieldName = "$aPackage.R.$resType.$rFieldName"
|
||||
|
||||
val template: TemplateImpl
|
||||
if (!needContextReceiver(element)) {
|
||||
template = TemplateImpl("", "$GET_STRING_METHOD($fieldName)", "")
|
||||
}
|
||||
else {
|
||||
template = TemplateImpl("", "\$context\$.$GET_STRING_METHOD($fieldName)", "")
|
||||
val marker = MacroCallNode(VariableOfTypeMacro())
|
||||
marker.addParameter(ConstantNode("android.content.Context"))
|
||||
template.addVariable("context", marker, ConstantNode("context"), true)
|
||||
}
|
||||
|
||||
val containingLiteralExpression = element.parent
|
||||
editor.caretModel.moveToOffset(containingLiteralExpression.textOffset)
|
||||
editor.document.deleteString(containingLiteralExpression.textRange.startOffset, containingLiteralExpression.textRange.endOffset)
|
||||
val marker = editor.document.createRangeMarker(containingLiteralExpression.textOffset, containingLiteralExpression.textOffset)
|
||||
marker.isGreedyToLeft = true
|
||||
marker.isGreedyToRight = true
|
||||
|
||||
TemplateManager.getInstance(module.project).startTemplate(editor, template, false, null, object : TemplateEditingAdapter() {
|
||||
override fun waitingForInput(template: Template?) {
|
||||
JavaCodeStyleManager.getInstance(module.project).shortenClassReferences(file, marker.startOffset, marker.endOffset)
|
||||
}
|
||||
|
||||
override fun beforeTemplateFinished(state: TemplateState?, template: Template?) {
|
||||
JavaCodeStyleManager.getInstance(module.project).shortenClassReferences(file, marker.startOffset, marker.endOffset)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun needContextReceiver(element: PsiElement): Boolean {
|
||||
val classesWithGetSting = listOf("android.content.Context", "android.app.Fragment", "android.support.v4.app.Fragment")
|
||||
val viewClass = listOf("android.view.View")
|
||||
var parent = PsiTreeUtil.findFirstParent(element, true) { it is KtClassOrObject || it is KtFunction || it is KtLambdaExpression }
|
||||
|
||||
while (parent != null) {
|
||||
|
||||
if (parent.isSubclassOrSubclassExtension(classesWithGetSting)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (parent.isSubclassOrSubclassExtension(viewClass) ||
|
||||
(parent is KtClassOrObject && !parent.isInnerClass() && !parent.isObjectLiteral())) {
|
||||
return true
|
||||
}
|
||||
|
||||
parent = PsiTreeUtil.findFirstParent(parent, true) { it is KtClassOrObject || it is KtFunction || it is KtLambdaExpression }
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun getManifestPackage(facet: AndroidFacet) = facet.manifest?.`package`?.value
|
||||
|
||||
private fun PsiElement.isSubclassOrSubclassExtension(baseClasses: Collection<String>) =
|
||||
(this as? KtClassOrObject)?.isSubclassOfAny(baseClasses) ?:
|
||||
this.isSubclassExtensionOfAny(baseClasses)
|
||||
|
||||
private fun PsiElement.isSubclassExtensionOfAny(baseClasses: Collection<String>) =
|
||||
(this as? KtLambdaExpression)?.isSubclassExtensionOfAny(baseClasses) ?:
|
||||
(this as? KtFunction)?.isSubclassExtensionOfAny(baseClasses) ?:
|
||||
false
|
||||
|
||||
private fun KtClassOrObject.isObjectLiteral() = (this as? KtObjectDeclaration)?.isObjectLiteral() ?: false
|
||||
|
||||
private fun KtClassOrObject.isInnerClass() = (this as? KtClass)?.isInner() ?: false
|
||||
|
||||
private fun KtFunction.isSubclassExtensionOfAny(baseClasses: Collection<String>): Boolean {
|
||||
val descriptor = resolveToDescriptor() as FunctionDescriptor
|
||||
val extendedTypeDescriptor = descriptor.extensionReceiverParameter?.type?.constructor?.declarationDescriptor as? ClassDescriptor
|
||||
return extendedTypeDescriptor != null && baseClasses.any { extendedTypeDescriptor.isSubclassOf(it) }
|
||||
}
|
||||
|
||||
private fun KtLambdaExpression.isSubclassExtensionOfAny(baseClasses: Collection<String>): Boolean {
|
||||
val bindingContext = analyze(BodyResolveMode.PARTIAL)
|
||||
val type = bindingContext.getType(this)
|
||||
|
||||
if (type == null || !type.isExtensionFunctionType) {
|
||||
return false
|
||||
}
|
||||
|
||||
val extendedTypeDescriptor = type.arguments.first().type.constructor.declarationDescriptor
|
||||
if (extendedTypeDescriptor is ClassDescriptor) {
|
||||
return baseClasses.any { extendedTypeDescriptor.isSubclassOf(it) }
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private fun KtClassOrObject.isSubclassOfAny(baseClasses: Collection<String>): Boolean {
|
||||
val bindingContext = analyze(BodyResolveMode.PARTIAL)
|
||||
val declarationDescriptor = bindingContext.get(BindingContext.CLASS, this)
|
||||
return baseClasses.any { declarationDescriptor?.isSubclassOf(it) ?: false }
|
||||
}
|
||||
|
||||
private fun ClassDescriptor.isSubclassOf(className: String): Boolean {
|
||||
return fqNameSafe.asString() == className || defaultType.constructor.supertypes.any {
|
||||
(it.constructor.declarationDescriptor as? ClassDescriptor)?.isSubclassOf(className) ?: false
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.intentions
|
||||
|
||||
import com.android.SdkConstants
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.openapi.vfs.LocalFileSystem
|
||||
import com.intellij.testFramework.PlatformTestUtil
|
||||
import com.intellij.util.PathUtil
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.android.KotlinAndroidTestCase
|
||||
import org.jetbrains.kotlin.idea.jsonUtils.getString
|
||||
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import java.io.File
|
||||
|
||||
|
||||
abstract class AbstractAndroidResourceIntentionTest : KotlinAndroidTestCase() {
|
||||
|
||||
fun doTest(path: String) {
|
||||
val configFile = File(path)
|
||||
val testDataPath = configFile.parent
|
||||
|
||||
myFixture.testDataPath = testDataPath
|
||||
|
||||
val config = JsonParser().parse(FileUtil.loadFile(File(path), true)) as JsonObject
|
||||
|
||||
val intentionClass = config.getString("intentionClass")
|
||||
val isApplicableExpected = if (config.has("isApplicable")) config.get("isApplicable").asBoolean else true
|
||||
val rFile = if (config.has("rFile")) config.get("rFile").asString else null
|
||||
val resDirectory = if (config.has("resDirectory")) config.get("resDirectory").asString else null
|
||||
|
||||
if (rFile != null) {
|
||||
myFixture.copyFileToProject(rFile, "gen/" + PathUtil.getFileName(rFile))
|
||||
}
|
||||
else {
|
||||
if (File(testDataPath + "/R.java").isFile) {
|
||||
myFixture.copyFileToProject("R.java", "gen/R.java")
|
||||
}
|
||||
}
|
||||
|
||||
if (resDirectory != null) {
|
||||
myFixture.copyDirectoryToProject(resDirectory, "res")
|
||||
}
|
||||
else {
|
||||
if (File(testDataPath + "/res").isDirectory) {
|
||||
myFixture.copyDirectoryToProject("res", "res")
|
||||
}
|
||||
}
|
||||
|
||||
val sourceFile = myFixture.copyFileToProject("main.kt", "src/main.kt")
|
||||
myFixture.configureFromExistingVirtualFile(sourceFile)
|
||||
|
||||
DirectiveBasedActionUtils.checkForUnexpectedErrors(myFixture.file as KtFile)
|
||||
|
||||
val intentionAction = Class.forName(intentionClass).newInstance() as IntentionAction
|
||||
|
||||
TestCase.assertEquals(isApplicableExpected, intentionAction.isAvailable(myFixture.project, myFixture.editor, myFixture.file))
|
||||
if (!isApplicableExpected) {
|
||||
return
|
||||
}
|
||||
|
||||
val element = getTargetElement()
|
||||
element?.putUserData(CREATE_XML_RESOURCE_PARAMETERS_NAME_KEY, "resource_id")
|
||||
|
||||
myFixture.launchAction(intentionAction)
|
||||
|
||||
FileDocumentManager.getInstance().saveAllDocuments()
|
||||
DirectiveBasedActionUtils.checkForUnexpectedErrors(myFixture.file as KtFile)
|
||||
|
||||
myFixture.checkResultByFile("/expected/main.kt")
|
||||
assertResourcesEqual(testDataPath + "/expected/res")
|
||||
}
|
||||
|
||||
fun assertResourcesEqual(expectedPath: String) {
|
||||
PlatformTestUtil.assertDirectoriesEqual(LocalFileSystem.getInstance().findFileByPath(expectedPath), getResourceDirectory())
|
||||
}
|
||||
|
||||
fun getResourceDirectory() = LocalFileSystem.getInstance().findFileByPath(myFixture.tempDirPath + "/res")
|
||||
|
||||
fun getTargetElement() = myFixture.file.findElementAt(myFixture.caretOffset)?.parent
|
||||
|
||||
override fun createManifest() {
|
||||
myFixture.copyFileToProject("idea/testData/android/AndroidManifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML)
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.intentions;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/android/resourceIntentions")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class AndroidResourceIntentionTestGenerated extends AbstractAndroidResourceIntentionTest {
|
||||
public void testAllFilesPresentInResourceIntentions() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/android/resourceIntentions"), Pattern.compile("^(.+)\\.test$"));
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/activityExtension/activityExtension.test")
|
||||
public void testKotlinAndroidAddStringResource_activityExtension_ActivityExtension() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/activityExtension/activityExtension.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/activityMethod/activityMethod.test")
|
||||
public void testKotlinAndroidAddStringResource_activityMethod_ActivityMethod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/activityMethod/activityMethod.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/classInActivity/classInActivity.test")
|
||||
public void testKotlinAndroidAddStringResource_classInActivity_ClassInActivity() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/classInActivity/classInActivity.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/extensionLambda/extensionLambda.test")
|
||||
public void testKotlinAndroidAddStringResource_extensionLambda_ExtensionLambda() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/extensionLambda/extensionLambda.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/function/function.test")
|
||||
public void testKotlinAndroidAddStringResource_function_Function() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/function/function.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/innerClassInActivity/innerClassInActivity.test")
|
||||
public void testKotlinAndroidAddStringResource_innerClassInActivity_InnerClassInActivity() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/innerClassInActivity/innerClassInActivity.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/innerViewInActivity/innerViewInActivity.test")
|
||||
public void testKotlinAndroidAddStringResource_innerViewInActivity_InnerViewInActivity() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/innerViewInActivity/innerViewInActivity.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/objectInActivity/objectInActivity.test")
|
||||
public void testKotlinAndroidAddStringResource_objectInActivity_ObjectInActivity() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/objectInActivity/objectInActivity.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/objectInActivityMethod/objectInActivityMethod.test")
|
||||
public void testKotlinAndroidAddStringResource_objectInActivityMethod_ObjectInActivityMethod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/objectInActivityMethod/objectInActivityMethod.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/objectInFunction/objectInFunction.test")
|
||||
public void testKotlinAndroidAddStringResource_objectInFunction_ObjectInFunction() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/objectInFunction/objectInFunction.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/stringTemplate/stringTemplate.test")
|
||||
public void testKotlinAndroidAddStringResource_stringTemplate_StringTemplate() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/stringTemplate/stringTemplate.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/viewExtensionActivityMethod/viewExtensionActivityMethod.test")
|
||||
public void testKotlinAndroidAddStringResource_viewExtensionActivityMethod_ViewExtensionActivityMethod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/viewExtensionActivityMethod/viewExtensionActivityMethod.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlinAndroidAddStringResource/viewMethod/viewMethod.test")
|
||||
public void testKotlinAndroidAddStringResource_viewMethod_ViewMethod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/viewMethod/viewMethod.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<spot>title = getString(R.string.some_text)</spot>
|
||||
@@ -0,0 +1 @@
|
||||
<spot>title = "Some Text"</spot>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention extracts string literal to android resources.
|
||||
</body>
|
||||
</html>
|
||||
@@ -17,6 +17,12 @@
|
||||
level="ERROR"/>
|
||||
|
||||
<editorNotificationProvider implementation="org.jetbrains.kotlin.android.actions.KotlinNewActivityNotification"/>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource</className>
|
||||
<category>Kotlin Android</category>
|
||||
</intentionAction>
|
||||
|
||||
</extensions>
|
||||
|
||||
<extensions defaultExtensionNs="org.jetbrains.kotlin">
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.myapp"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.myapp
|
||||
|
||||
class R {
|
||||
object string {
|
||||
val app_name = 0x7f060021
|
||||
val resource_id = 0x7f060022
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
fun Activity.getSomeText() {
|
||||
getString(R.string.resource_id)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some text</string>
|
||||
</resources>
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
fun Activity.getSomeText() {
|
||||
"some <caret>text"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun foo() {
|
||||
val a = getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun foo() {
|
||||
val a = "some <caret>string"
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
class MyActivity: Activity() {
|
||||
class Helper {
|
||||
fun test(context: Context) {
|
||||
val b = context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
class MyActivity: Activity() {
|
||||
class Helper {
|
||||
fun test(context: Context) {
|
||||
val b = "some <caret>string"
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun foo(context: Context) {
|
||||
with (context) {
|
||||
with (2) {
|
||||
getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">zxc</string>
|
||||
</resources>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun foo(context: Context) {
|
||||
with (context) {
|
||||
with (2) {
|
||||
"z<caret>xc"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun getSomeText(context: Context) {
|
||||
context.getString(R.string.resource_id)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some text</string>
|
||||
</resources>
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun getSomeText(context: Context) {
|
||||
"some <caret>text"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
inner class Helper {
|
||||
fun a(): String = "q"
|
||||
|
||||
inner class HelperOfHelper {
|
||||
fun b(): String = getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
idea/testData/android/resourceIntentions/kotlinAndroidAddStringResource/innerClassInActivity/main.kt
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
inner class Helper {
|
||||
fun a(): String = "q"
|
||||
|
||||
inner class HelperOfHelper {
|
||||
fun b(): String = "some <caret>string"
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
|
||||
class MyActivity: Activity() {
|
||||
inner class SubView constructor(context: Context): View(context) {
|
||||
fun test() {
|
||||
val b = context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
|
||||
class MyActivity: Activity() {
|
||||
inner class SubView constructor(context: Context): View(context) {
|
||||
fun test() {
|
||||
val b = "some <caret>string"
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
class MyActivity: Activity() {
|
||||
object A {
|
||||
fun doSomething(context: Context) {
|
||||
context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
class MyActivity: Activity() {
|
||||
object A {
|
||||
fun doSomething(context: Context) {
|
||||
"some <caret>string"
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun foo() {
|
||||
object {
|
||||
val text = getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun foo() {
|
||||
object {
|
||||
val text = "some <caret>string"
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
fun foo(context: Context) {
|
||||
object {
|
||||
val text = context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
|
||||
fun foo(context: Context) {
|
||||
object {
|
||||
val text = "some <caret>string"
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val a = "some"
|
||||
val b = "text"
|
||||
val bar = "$a <caret>+ $b"
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource",
|
||||
"isApplicable": false
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.View
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun View.foo() {
|
||||
val a = context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.View
|
||||
|
||||
class MyActivity: Activity() {
|
||||
fun View.foo() {
|
||||
val a = "some <caret>string"
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
|
||||
class DemoView constructor(context: Context): View(context) {
|
||||
fun test() {
|
||||
val b = context.getString(R.string.resource_id)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="resource_id">some string</string>
|
||||
</resources>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.myapp
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
|
||||
class DemoView constructor(context: Context): View(context) {
|
||||
fun test() {
|
||||
val b = "some <caret>string"
|
||||
}
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"rFile": "../../R.kt",
|
||||
"resDirectory": "../../res",
|
||||
"intentionClass": "org.jetbrains.kotlin.android.intentions.KotlinAndroidAddStringResource"
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
<resources>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user