diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt index aba66c79738f..ced6bbd94197 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt @@ -4,20 +4,20 @@ package org.jetbrains.kotlin.idea.inspections import com.intellij.codeInsight.intention.FileModifier import com.intellij.codeInsight.intention.FileModifier.SafeFieldForPreview -import com.intellij.codeInspection.CleanupLocalInspectionTool -import com.intellij.codeInspection.LocalQuickFix -import com.intellij.codeInspection.ProblemDescriptor -import com.intellij.codeInspection.ProblemsHolder +import com.intellij.codeInspection.* +import com.intellij.codeInspection.ProblemHighlightType.* import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.annotations.Nls import org.jetbrains.kotlin.idea.base.psi.isOneLiner +import org.jetbrains.kotlin.idea.base.psi.textRangeIn import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection import org.jetbrains.kotlin.idea.codeinsight.utils.findExistingEditor import org.jetbrains.kotlin.idea.core.moveCaret +import org.jetbrains.kotlin.idea.inspections.Action.* import org.jetbrains.kotlin.idea.intentions.loopToCallChain.countUsages import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement import org.jetbrains.kotlin.lexer.KtTokens @@ -35,13 +35,16 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl if (initializer.isComplex()) return val action = property.action(expression) - if (action == Action.NOTHING) return - if (action == Action.MOVE && !property.isOneLiner()) return + if (action == NOTHING) return + if (action == MOVE && !property.isOneLiner()) return - holder.registerProblem( + val highlightType = if (action == INLINE) INFORMATION else GENERIC_ERROR_OR_WARNING + holder.registerProblemWithoutOfflineInformation( property, - TextRange.from(identifier.startOffsetInParent, identifier.textLength), action.description, + isOnTheFly, + highlightType, + TextRange.from(identifier.startOffsetInParent, identifier.textLength), action.createFix(subjectExpression.createSmartPointer()) ) }) @@ -78,8 +81,8 @@ private enum class Action { } private fun KtProperty.action(element: KtElement): Action = when (val elementUsages = countUsages(element)) { - countUsages() -> if (elementUsages == 1) Action.INLINE else Action.MOVE - else -> Action.NOTHING + countUsages() -> if (elementUsages == 1) INLINE else MOVE + else -> NOTHING } private fun KtWhenExpression.findDeclarationNear(): KtProperty? { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt index 36c987a6774f..9f2dfe815307 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test() = true fun foo(): Int { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt.after index 87dbe246186e..c27970604a8b 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt.after @@ -1,7 +1,8 @@ +// HIGHLIGHT: INFORMATION fun test() = true fun foo(): Int { - return when (test()) { + return when (test()) { true -> 42 else -> null } ?: 55 diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt index 7a6925f39ae4..d3450f121586 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test() = true fun foo() { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt.after index 90b2875fc133..9b6871f3cedb 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt.after @@ -1,7 +1,8 @@ +// HIGHLIGHT: INFORMATION fun test() = true fun foo() { - val b = !when (test()) { + val b = !when (test()) { true -> true else -> false } diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt index 11a85eee04af..f48dd9916484 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test() = 42 fun foo() { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt.after index ceb0742a8300..b4773bf1dc14 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLineWithOneLineInitializer.kt.after @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test() = 42 fun foo() { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt index 686db1722723..6be0c42d40dc 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test(){ /* aaa */ val foo = 1 /* bbb */ /* ccc */ diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt.after index 75653ccf6480..16ecaae8d29c 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withBlockComment.kt.after @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test(){ /* aaa */ when(1) { /* bbb */ /* ccc */ diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt index acdf7bee89ba..1cf022aeebfe 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun foo() { val a = 1 diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt.after index f1b0e7019f12..7ff444abf092 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment.kt.after @@ -1,6 +1,7 @@ +// HIGHLIGHT: INFORMATION fun foo() { // comment - when (1) { + when (1) { 1 -> { } else -> { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt index 7fbff3349b2e..b608e6272c61 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun foo(style: Int?) { val a = style // comment when (a) { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt.after index 4c0559f090e4..59b7ca5c8413 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withComment4.kt.after @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun foo(style: Int?) { when (style) { // comment 0 -> {} diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt index f5415f4632cc..76520d36e0c4 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test(){ /** aaa */ val foo = 1 /* bbb */ diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt.after index 6777713b4f8f..5077a4ae863a 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withDocComment.kt.after @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun test(){ // ccc /** aaa */ diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt index f7fe61f33cc7..af7b759982a8 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun foo() { val a = 1 diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt.after index 0a55924c655d..bf80d7304077 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt.after +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/withNewLine.kt.after @@ -1,3 +1,4 @@ +// HIGHLIGHT: INFORMATION fun foo() { when (1) { 1 -> {