StringLiteralCopyPasteProcessor: keep indent for multiline paste into text block (IDEA-217406)

GitOrigin-RevId: b1a5d55f8749fea0002723ae2a6b2370fb8ed4ab
This commit is contained in:
Artemiy Sartakov
2019-09-11 19:02:17 +07:00
committed by intellij-monorepo-bot
parent 2e80954276
commit caac9ef3d8
6 changed files with 24 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.editorActions;
import com.google.common.base.Strings;
import com.intellij.application.options.CodeStyle;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
@@ -124,7 +125,9 @@ public class StringLiteralCopyPasteProcessor implements CopyPastePreProcessor {
else if (isTextBlock(token)) {
final String before = document.getText(new TextRange(selectionStart - 1, selectionStart));
final String after = document.getText(new TextRange(selectionEnd, selectionEnd + 1));
return escapeTextBlock(text, "\"".equals(before), "\"".equals(after));
int caretOffset = editor.getCaretModel().getOffset();
int offset = caretOffset - document.getLineStartOffset(document.getLineNumber(caretOffset));
return escapeTextBlock(text, offset, "\"".equals(before), "\"".equals(after));
}
return text;
}
@@ -242,16 +245,17 @@ public class StringLiteralCopyPasteProcessor implements CopyPastePreProcessor {
}
@NotNull
protected String escapeTextBlock(@NotNull String text, boolean escapeStartQuote, boolean escapeEndQuote) {
protected String escapeTextBlock(@NotNull String text, int offset, boolean escapeStartQuote, boolean escapeEndQuote) {
StringBuilder buffer = new StringBuilder(text.length());
final String[] lines = LineTokenizer.tokenize(text.toCharArray(), false, false);
String indent = Strings.repeat(" ", offset);
for (int i = 0; i < lines.length; i++) {
String content = PsiLiteralUtil.escapeBackSlashesInTextBlock(lines[i]);
content = PsiLiteralUtil.escapeTextBlockCharacters(content, i == 0 && escapeStartQuote,
i == lines.length - 1 && escapeEndQuote, true);
buffer.append(content);
if (i < lines.length - 1) {
buffer.append('\n');
buffer.append('\n').append(indent);
}
}
return buffer.toString();

View File

@@ -1,5 +1,5 @@
class C {
String empty = """
""\"
target""\"<caret>""";
target""\"<caret>""";
}

View File

@@ -1,6 +1,6 @@
class C {
String x = """
"\"
target\""
target\""
""";
}

View File

@@ -0,0 +1,8 @@
class C {
String empty = """
<html>
<body>
</body>
</html>
""";
}

View File

@@ -0,0 +1,5 @@
class C {
String empty = """
<caret>
""";
}

View File

@@ -32,6 +32,8 @@ class JavaTextBlocksHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
fun testPasteNewline() = doTestPaste("\n\n")
fun testPasteMultilineText() = doTestPaste("<html>\n <body>\n </body>\n</html>")
fun testBadEscape() = doTestPaste("\\");
private fun doTestPaste(textToPaste: String) {