Convert RemoveCurlyBracesFromTemplateIntention to inspection

Relates to #KT-31717
This commit is contained in:
Dmitry Gridin
2019-06-03 11:42:43 +07:00
parent 7f4d2891e8
commit 7bc2fcd657
31 changed files with 129 additions and 159 deletions
+1 -6
View File
@@ -998,11 +998,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.InsertCurlyBracesToTemplateIntention</className>
<category>Kotlin</category>
@@ -1824,7 +1819,7 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateInspection"
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RemoveCurlyBracesFromTemplateInspection"
displayName="Redundant curly braces in string template"
groupPath="Kotlin"
groupName="Redundant constructs"
@@ -1,3 +0,0 @@
fun foo() {
val y = <spot>"$x"</spot>
}
@@ -1,3 +0,0 @@
fun foo() {
val y = <spot>"${x}"</spot>
}
@@ -1,5 +0,0 @@
<html>
<body>
This intention removes unnecessary curly braces around simple names in string templates.
</body>
</html>
@@ -20,10 +20,10 @@ import com.intellij.openapi.util.Key
import com.intellij.psi.PsiTreeChangeAdapter
import com.intellij.psi.PsiTreeChangeEvent
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.canDropBraces
import org.jetbrains.kotlin.idea.core.dropBraces
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
@@ -163,12 +163,8 @@ internal class ExpressionReplacementPerformer(
// simplify "${x}" to "$x"
val templateEntry = resultExpression?.parent as? KtBlockStringTemplateEntry
if (templateEntry != null) {
val intention = RemoveCurlyBracesFromTemplateIntention()
if (intention.isApplicableTo(templateEntry)) {
val newEntry = templateEntry.dropBraces()
return newEntry.expression
}
if (templateEntry?.canDropBraces() == true) {
return templateEntry.dropBraces().expression
}
return resultExpression ?: range.last as? KtExpression
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.idea.inspections
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.core.canDropBraces
import org.jetbrains.kotlin.idea.core.dropBraces
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
class RemoveCurlyBracesFromTemplateInspection :
AbstractApplicabilityBasedInspection<KtBlockStringTemplateEntry>(KtBlockStringTemplateEntry::class.java) {
override fun inspectionText(element: KtBlockStringTemplateEntry): String = "Redundant curly braces in string template"
override val defaultFixText: String = "Remove curly braces"
override fun isApplicable(element: KtBlockStringTemplateEntry): Boolean = element.canDropBraces()
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
(element as KtBlockStringTemplateEntry).dropBraces()
}
}
@@ -1,36 +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.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.core.canDropBraces
import org.jetbrains.kotlin.idea.core.dropBraces
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
@Suppress("DEPRECATION")
class RemoveCurlyBracesFromTemplateInspection :
IntentionBasedInspection<KtBlockStringTemplateEntry>(RemoveCurlyBracesFromTemplateIntention::class)
class RemoveCurlyBracesFromTemplateIntention :
SelfTargetingOffsetIndependentIntention<KtBlockStringTemplateEntry>(KtBlockStringTemplateEntry::class.java, "Remove curly braces") {
override fun isApplicableTo(element: KtBlockStringTemplateEntry) = element.canDropBraces()
override fun applyTo(element: KtBlockStringTemplateEntry, editor: Editor?) {
element.dropBraces()
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RemoveCurlyBracesFromTemplateInspection
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = "x"
val y = "$<caret>{x.length}"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = X()
val y = "$<caret>{x.bar()}"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = 3
val z = 4
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = 3
val y = "$<caret>{x}y"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = 3
val y = "$<caret>{x}_y"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo() {
val x = 3
val y = "$<caret>{x}コトリン"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$<caret>{x}"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$<caret>{x}.moretext"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$<caret>x.moretext"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$<caret>{x}\n"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$<caret>{x}() this is okay, x will not be thought of as a function call"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "$x() this is okay, x will not be thought of as a function call"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "text$<caret>{x} moretext"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = "text$x moretext"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = """text$<caret>{x} moretext"""
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo() {
val x = 4
val y = """text$x moretext"""
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo(`object`: Any) {
val bar = "$<caret>{`object`}"
}
@@ -1,4 +1,4 @@
// IS_APPLICABLE: true
fun foo(`object`: Any) {
val bar = "$`object`"
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention
@@ -6684,6 +6684,84 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveCurlyBracesFromTemplate extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveCurlyBracesFromTemplate() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("necessaryBrackets1.kt")
public void testNecessaryBrackets1() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets1.kt");
}
@TestMetadata("necessaryBrackets2.kt")
public void testNecessaryBrackets2() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets2.kt");
}
@TestMetadata("necessaryBrackets3.kt")
public void testNecessaryBrackets3() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets3.kt");
}
@TestMetadata("necessaryBrackets4.kt")
public void testNecessaryBrackets4() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets4.kt");
}
@TestMetadata("necessaryBrackets5.kt")
public void testNecessaryBrackets5() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets5.kt");
}
@TestMetadata("necessaryBrackets6.kt")
public void testNecessaryBrackets6() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/necessaryBrackets6.kt");
}
@TestMetadata("unnecessaryBrackets1.kt")
public void testUnnecessaryBrackets1() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets1.kt");
}
@TestMetadata("unnecessaryBrackets2.kt")
public void testUnnecessaryBrackets2() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets2.kt");
}
@TestMetadata("unnecessaryBrackets3.kt")
public void testUnnecessaryBrackets3() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets3.kt");
}
@TestMetadata("unnecessaryBrackets4.kt")
public void testUnnecessaryBrackets4() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets4.kt");
}
@TestMetadata("unnecessaryBrackets5.kt")
public void testUnnecessaryBrackets5() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets5.kt");
}
@TestMetadata("unnecessaryBrackets6.kt")
public void testUnnecessaryBrackets6() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets6.kt");
}
@TestMetadata("unnecessaryBrackets7.kt")
public void testUnnecessaryBrackets7() throws Exception {
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets7.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/removeEmptyParenthesesFromAnnotationEntry")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -13276,84 +13276,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeCurlyBracesFromTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveCurlyBracesFromTemplate extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveCurlyBracesFromTemplate() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeCurlyBracesFromTemplate"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("necessaryBrackets1.kt")
public void testNecessaryBrackets1() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets1.kt");
}
@TestMetadata("necessaryBrackets2.kt")
public void testNecessaryBrackets2() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets2.kt");
}
@TestMetadata("necessaryBrackets3.kt")
public void testNecessaryBrackets3() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets3.kt");
}
@TestMetadata("necessaryBrackets4.kt")
public void testNecessaryBrackets4() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets4.kt");
}
@TestMetadata("necessaryBrackets5.kt")
public void testNecessaryBrackets5() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets5.kt");
}
@TestMetadata("necessaryBrackets6.kt")
public void testNecessaryBrackets6() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/necessaryBrackets6.kt");
}
@TestMetadata("unnecessaryBrackets1.kt")
public void testUnnecessaryBrackets1() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets1.kt");
}
@TestMetadata("unnecessaryBrackets2.kt")
public void testUnnecessaryBrackets2() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets2.kt");
}
@TestMetadata("unnecessaryBrackets3.kt")
public void testUnnecessaryBrackets3() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets3.kt");
}
@TestMetadata("unnecessaryBrackets4.kt")
public void testUnnecessaryBrackets4() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets4.kt");
}
@TestMetadata("unnecessaryBrackets5.kt")
public void testUnnecessaryBrackets5() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets5.kt");
}
@TestMetadata("unnecessaryBrackets6.kt")
public void testUnnecessaryBrackets6() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets6.kt");
}
@TestMetadata("unnecessaryBrackets7.kt")
public void testUnnecessaryBrackets7() throws Exception {
runTest("idea/testData/intentions/removeCurlyBracesFromTemplate/unnecessaryBrackets7.kt");
}
}
@TestMetadata("idea/testData/intentions/removeEmptyClassBody")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)