RemoveCurlyBracesFromTemplateInspection: add heuristic to change severity
Change severity to INFORMATION if a variable hasn't whitespace around & add an option to return the old behavior #KT-31717 Fixed
This commit is contained in:
+20
-1
@@ -5,16 +5,23 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
|
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.core.canDropBraces
|
import org.jetbrains.kotlin.idea.core.canDropBraces
|
||||||
import org.jetbrains.kotlin.idea.core.dropBraces
|
import org.jetbrains.kotlin.idea.core.dropBraces
|
||||||
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
|
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
|
||||||
|
|
||||||
class RemoveCurlyBracesFromTemplateInspection :
|
class RemoveCurlyBracesFromTemplateInspection(@JvmField var reportWithoutWhitespace: Boolean = false) :
|
||||||
AbstractApplicabilityBasedInspection<KtBlockStringTemplateEntry>(KtBlockStringTemplateEntry::class.java) {
|
AbstractApplicabilityBasedInspection<KtBlockStringTemplateEntry>(KtBlockStringTemplateEntry::class.java) {
|
||||||
override fun inspectionText(element: KtBlockStringTemplateEntry): String = "Redundant curly braces in string template"
|
override fun inspectionText(element: KtBlockStringTemplateEntry): String = "Redundant curly braces in string template"
|
||||||
|
|
||||||
|
override fun inspectionHighlightType(element: KtBlockStringTemplateEntry) =
|
||||||
|
if (reportWithoutWhitespace || element.hasWhitespaceAround()) ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
||||||
|
else ProblemHighlightType.INFORMATION
|
||||||
|
|
||||||
override val defaultFixText: String = "Remove curly braces"
|
override val defaultFixText: String = "Remove curly braces"
|
||||||
|
|
||||||
override fun isApplicable(element: KtBlockStringTemplateEntry): Boolean = element.canDropBraces()
|
override fun isApplicable(element: KtBlockStringTemplateEntry): Boolean = element.canDropBraces()
|
||||||
@@ -22,4 +29,16 @@ class RemoveCurlyBracesFromTemplateInspection :
|
|||||||
override fun applyTo(element: KtBlockStringTemplateEntry, project: Project, editor: Editor?) {
|
override fun applyTo(element: KtBlockStringTemplateEntry, project: Project, editor: Editor?) {
|
||||||
element.dropBraces()
|
element.dropBraces()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun createOptionsPanel() = MultipleCheckboxOptionsPanel(this).apply {
|
||||||
|
addCheckbox("Report also for a variables without a whitespace around", "reportWithoutWhitespace")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtBlockStringTemplateEntry.hasWhitespaceAround(): Boolean =
|
||||||
|
prevSibling?.isWhitespaceOrQuote(true) == true && nextSibling?.isWhitespaceOrQuote(false) == true
|
||||||
|
|
||||||
|
private fun PsiElement.isWhitespaceOrQuote(prev: Boolean): Boolean {
|
||||||
|
val char = if (prev) text.lastOrNull() else text.firstOrNull()
|
||||||
|
return char != null && (char.isWhitespace() || char == '"')
|
||||||
|
}
|
||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
val x = 4
|
val x = 4
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
fun foo(`object`: Any) {
|
fun foo(`object`: Any) {
|
||||||
val bar = "$<caret>{`object`}"
|
val bar = "$<caret>{`object`}"
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
fun foo(`object`: Any) {
|
fun foo(`object`: Any) {
|
||||||
val bar = "$`object`"
|
val bar = "$`object`"
|
||||||
|
|||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val x = 4
|
||||||
|
val y = "text $<caret>{x} moretext"
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// HIGHLIGHT: GENERIC_ERROR_OR_WARNING
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val x = 4
|
||||||
|
val y = "text $x moretext"
|
||||||
|
}
|
||||||
+5
@@ -7884,6 +7884,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
public void testUnnecessaryBrackets7() throws Exception {
|
public void testUnnecessaryBrackets7() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets7.kt");
|
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets7.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("unnecessaryBrackets8.kt")
|
||||||
|
public void testUnnecessaryBrackets8() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/removeCurlyBracesFromTemplate/unnecessaryBrackets8.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/inspectionsLocal/removeEmptyParenthesesFromAnnotationEntry")
|
@TestMetadata("idea/testData/inspectionsLocal/removeEmptyParenthesesFromAnnotationEntry")
|
||||||
|
|||||||
Reference in New Issue
Block a user