KT-18006 Don't cut off trailing new lines. Make behavior consistent with Java literal copy/paste
This commit is contained in:
@@ -135,14 +135,21 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor {
|
||||
|
||||
return if (beginTp.isSingleQuoted()) {
|
||||
val res = StringBuilder()
|
||||
val lineBreak = "\\n\"+\n \""
|
||||
var endsInLineBreak = false
|
||||
TemplateTokenSequence(text).forEach {
|
||||
when (it) {
|
||||
is LiteralChunk -> StringUtil.escapeStringCharacters(it.text.length, it.text, "\$\"", res)
|
||||
is EntryChunk -> res.append(it.text)
|
||||
is NewLineChunk -> res.append("\\n\"+\n \"")
|
||||
is NewLineChunk -> res.append(lineBreak)
|
||||
}
|
||||
endsInLineBreak = it is NewLineChunk
|
||||
}
|
||||
return if (endsInLineBreak){
|
||||
res.removeSuffix(lineBreak).toString() + "\\n"
|
||||
} else{
|
||||
res.toString()
|
||||
}
|
||||
res.toString()
|
||||
}
|
||||
else {
|
||||
val tripleQuoteRe = Regex("[\"]{3,}")
|
||||
@@ -211,7 +218,7 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence<
|
||||
}
|
||||
|
||||
private suspend fun SequenceBuilder<TemplateChunk>.yieldLiteral(chunk: String) {
|
||||
val splitLines = LineTokenizer.tokenize(chunk, false, true)
|
||||
val splitLines = LineTokenizer.tokenize(chunk, false, false)
|
||||
for (i in 0..splitLines.size - 1) {
|
||||
if (i != 0) {
|
||||
yield(NewLineChunk)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
val a = "a\n" +
|
||||
"b\n" +
|
||||
"c\n" +
|
||||
"\n" +
|
||||
"\n" +
|
||||
"\n"
|
||||
@@ -0,0 +1 @@
|
||||
val a = "<caret>"
|
||||
@@ -0,0 +1,10 @@
|
||||
<selection>a
|
||||
b
|
||||
c
|
||||
|
||||
|
||||
|
||||
</selection>d
|
||||
e
|
||||
f
|
||||
g
|
||||
+6
@@ -66,6 +66,12 @@ public class LiteralTextToKotlinCopyPasteTestGenerated extends AbstractLiteralTe
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("TrailingLines.txt")
|
||||
public void testTrailingLines() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextLiteral/TrailingLines.txt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("WithBackslashes.txt")
|
||||
public void testWithBackslashes() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextLiteral/WithBackslashes.txt");
|
||||
|
||||
+2
-1
@@ -38,7 +38,8 @@ abstract class AbstractLiteralTextToKotlinCopyPasteTest : AbstractCopyPasteTest(
|
||||
myFixture.configureByFiles(testName + ".txt")
|
||||
val fileText = myFixture.editor.document.text
|
||||
|
||||
myFixture.editor.selectionModel.setSelection(0, fileText.length)
|
||||
if (!myFixture.editor.selectionModel.hasSelection())
|
||||
myFixture.editor.selectionModel.setSelection(0, fileText.length)
|
||||
myFixture.performEditorAction(IdeActions.ACTION_COPY)
|
||||
|
||||
configureTargetFile(testName + ".kt")
|
||||
|
||||
@@ -44,7 +44,7 @@ class TemplateTokenSequenceTest:UsefulTestCase() {
|
||||
|
||||
//last empty line is skipped
|
||||
fun `test trailing new lines`(){
|
||||
doTest("literal\n\n", "LITERAL_CHUNK(literal)NEW_LINE()")
|
||||
doTest("literal\n\n", "LITERAL_CHUNK(literal)NEW_LINE()NEW_LINE()")
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user