Preserve comments in "Remove variable" quick fix #KT-23753 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-04-14 12:29:51 +03:00
committed by Mikhail Glukhikh
parent 61edfaced5
commit 633679ac16
4 changed files with 22 additions and 3 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
@@ -63,9 +64,10 @@ open class RemovePsiElementSimpleFix(element: PsiElement, private val text: Stri
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val initializer = expression.initializer
if (initializer != null && initializer !is KtConstantExpression) {
expression.replace(initializer)
}
else {
val commentSaver = CommentSaver(expression)
val replaced = expression.replace(initializer)
commentSaver.restore(replaced)
} else {
expression.delete()
}
}
@@ -0,0 +1,6 @@
// "Remove variable 'a'" "true"
var cnt = 5
fun getCnt() = cnt++
fun f() {
var <caret>a = getCnt() // comment
}
@@ -0,0 +1,6 @@
// "Remove variable 'a'" "true"
var cnt = 5
fun getCnt() = cnt++
fun f() {
getCnt() // comment
}
@@ -11668,6 +11668,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/variables/unusedVariableWithInitializer.kt");
}
@TestMetadata("unusedVariableWithInitializerAndComment.kt")
public void testUnusedVariableWithInitializerAndComment() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithInitializerAndComment.kt");
}
@TestMetadata("unusedVariableWithNullInitializer.kt")
public void testUnusedVariableWithNullInitializer() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithNullInitializer.kt");