KT-11680 Code completion of label for existing return with value inserts redundant whitespace
#KT-11680 Fixed #KT-9993 Fixed
This commit is contained in:
+13
-3
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.completion.InsertHandler
|
|||||||
import com.intellij.codeInsight.completion.InsertionContext
|
import com.intellij.codeInsight.completion.InsertionContext
|
||||||
import com.intellij.codeInsight.lookup.Lookup
|
import com.intellij.codeInsight.lookup.Lookup
|
||||||
import com.intellij.codeInsight.lookup.LookupElement
|
import com.intellij.codeInsight.lookup.LookupElement
|
||||||
|
import com.intellij.openapi.editor.Document
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import org.jetbrains.kotlin.idea.completion.KEEP_OLD_ARGUMENT_LIST_ON_TAB_KEY
|
import org.jetbrains.kotlin.idea.completion.KEEP_OLD_ARGUMENT_LIST_ON_TAB_KEY
|
||||||
import org.jetbrains.kotlin.idea.completion.smart.SmartCompletion
|
import org.jetbrains.kotlin.idea.completion.smart.SmartCompletion
|
||||||
@@ -55,8 +56,11 @@ class WithTailInsertHandler(val tailText: String,
|
|||||||
//TODO: analyze parenthesis balance to decide whether to replace or not
|
//TODO: analyze parenthesis balance to decide whether to replace or not
|
||||||
var insert = true
|
var insert = true
|
||||||
if (overwriteText) {
|
if (overwriteText) {
|
||||||
var offset = document.charsSequence.skipSpacesAndLineBreaks(tailOffset)
|
var offset = tailOffset
|
||||||
if (document.isTextAt(offset, tailText)) {
|
if (tailText != " ") {
|
||||||
|
offset = document.charsSequence.skipSpacesAndLineBreaks(offset)
|
||||||
|
}
|
||||||
|
if (shouldOverwriteChar(document, offset)) {
|
||||||
insert = false
|
insert = false
|
||||||
offset += tailText.length
|
offset += tailText.length
|
||||||
tailOffset = offset
|
tailOffset = offset
|
||||||
@@ -85,6 +89,12 @@ class WithTailInsertHandler(val tailText: String,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun shouldOverwriteChar(document: Document, offset: Int): Boolean {
|
||||||
|
if (!document.isTextAt(offset, tailText)) return false
|
||||||
|
if (tailText == " " && document.charsSequence.isCharAt(offset + 1, '}')) return false // do not overwrite last space before '}'
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val COMMA = WithTailInsertHandler(",", spaceBefore = false, spaceAfter = true /*TODO: use code style option*/)
|
val COMMA = WithTailInsertHandler(",", spaceBefore = false, spaceAfter = true /*TODO: use code style option*/)
|
||||||
val RPARENTH = WithTailInsertHandler(")", spaceBefore = false, spaceAfter = false)
|
val RPARENTH = WithTailInsertHandler(")", spaceBefore = false, spaceAfter = false)
|
||||||
@@ -92,6 +102,6 @@ class WithTailInsertHandler(val tailText: String,
|
|||||||
val RBRACE = WithTailInsertHandler("}", spaceBefore = true, spaceAfter = false)
|
val RBRACE = WithTailInsertHandler("}", spaceBefore = true, spaceAfter = false)
|
||||||
val ELSE = WithTailInsertHandler("else", spaceBefore = true, spaceAfter = true)
|
val ELSE = WithTailInsertHandler("else", spaceBefore = true, spaceAfter = true)
|
||||||
val EQ = WithTailInsertHandler("=", spaceBefore = true, spaceAfter = true) /*TODO: use code style options*/
|
val EQ = WithTailInsertHandler("=", spaceBefore = true, spaceAfter = true) /*TODO: use code style options*/
|
||||||
val SPACE = WithTailInsertHandler(" ", spaceBefore = false, spaceAfter = false, overwriteText = false)
|
val SPACE = WithTailInsertHandler(" ", spaceBefore = false, spaceAfter = false, overwriteText = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
val v: Boolean = run {
|
||||||
|
return<caret> true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ELEMENT: "return@run"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
val v: Boolean = run {
|
||||||
|
return@run <caret>true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ELEMENT: "return@run"
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
cl<caret>interface MyClass
|
||||||
|
|
||||||
|
// ELEMENT: "class"
|
||||||
|
// CHAR: '\t'
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
class <caret>MyClass
|
||||||
|
|
||||||
|
// ELEMENT: "class"
|
||||||
|
// CHAR: '\t'
|
||||||
+12
@@ -31,6 +31,12 @@ import java.util.regex.Pattern;
|
|||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletionHandlerTest {
|
public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletionHandlerTest {
|
||||||
|
@TestMetadata("AddLabelToReturn.kt")
|
||||||
|
public void testAddLabelToReturn() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/AddLabelToReturn.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInBasic() throws Exception {
|
public void testAllFilesPresentInBasic() throws Exception {
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/handlers/basic"), Pattern.compile("^(.+)\\.kt$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/handlers/basic"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
@@ -53,6 +59,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ClassKeywordBeforeName.kt")
|
||||||
|
public void testClassKeywordBeforeName() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ClassKeywordBeforeName.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ClassWithClassObject.kt")
|
@TestMetadata("ClassWithClassObject.kt")
|
||||||
public void testClassWithClassObject() throws Exception {
|
public void testClassWithClassObject() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ClassWithClassObject.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ClassWithClassObject.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user