Fix "Add braces to if" when semicolon is used instead of a new line
So #KT-23123 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
369dbd604b
commit
8082a5daf7
@@ -19,12 +19,11 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
|
||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, "Add braces") {
|
||||
@@ -53,8 +52,15 @@ class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.ja
|
||||
if (editor == null) throw IllegalArgumentException("This intention requires an editor")
|
||||
val expression = element.getTargetExpression(editor.caretModel.offset)!!
|
||||
|
||||
if (element.nextSibling?.text == ";") {
|
||||
element.nextSibling!!.delete()
|
||||
val psiFactory = KtPsiFactory(element)
|
||||
|
||||
val semicolon = element.getNextSiblingIgnoringWhitespaceAndComments()?.takeIf { it.node.elementType == KtTokens.SEMICOLON }
|
||||
if (semicolon != null) {
|
||||
val afterSemicolon = semicolon.getNextSiblingIgnoringWhitespace()
|
||||
if (semicolon.getLineNumber() == afterSemicolon?.getLineNumber())
|
||||
semicolon.replace(psiFactory.createNewLine())
|
||||
else
|
||||
semicolon.delete()
|
||||
}
|
||||
|
||||
val nextComment = when {
|
||||
@@ -68,7 +74,6 @@ class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.ja
|
||||
}
|
||||
nextComment?.delete()
|
||||
|
||||
val psiFactory = KtPsiFactory(element)
|
||||
val result = expression.replace(psiFactory.createSingleStatementBlock(expression))
|
||||
|
||||
if (element is KtDoWhileExpression) { // remove new line between '}' and while
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun <T> doSomething(a: T) {}
|
||||
|
||||
fun foo() {
|
||||
<caret>if (true) doSomething("test"); doSomething("")
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun <T> doSomething(a: T) {}
|
||||
|
||||
fun foo() {
|
||||
if (true) {
|
||||
doSomething("test")
|
||||
}
|
||||
doSomething("")
|
||||
}
|
||||
@@ -65,6 +65,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addBracesForIfWithSemicolonAndExpression.kt")
|
||||
public void testAddBracesForIfWithSemicolonAndExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/addBracesForIfWithSemicolonAndExpression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addBracesForWhile.kt")
|
||||
public void testAddBracesForWhile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/addBracesForWhile.kt");
|
||||
|
||||
Reference in New Issue
Block a user