IPP: keep escaping when joining string literals (IDEA-239079)

in "Join concatenated string literals" intention

GitOrigin-RevId: 67c08f332be1d78a3019cb45824c7405b104552c
This commit is contained in:
Bas Leijdekkers
2020-05-07 19:36:52 +00:00
committed by intellij-monorepo-bot
parent 14fcbcacac
commit 790698eefe
7 changed files with 40 additions and 41 deletions
@@ -69,32 +69,36 @@ public class JoinConcatenatedStringLiteralsIntention extends Intention {
}
private static void merge(PsiLiteralExpression left, PsiLiteralExpression right, StringBuilder newExpression) {
String leftText = Objects.requireNonNull(left.getValue()).toString();
String rightText = Objects.requireNonNull(right.getValue()).toString();
final String leftText = getLiteralExpressionText(left);
final String rightText = getLiteralExpressionText(right);
if (left.isTextBlock()) {
String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(left));
newExpression.append("\"\"\"").append('\n').append(indent);
newExpression.append(leftText.replaceAll("\n", "\n" + indent));
if (right.isTextBlock()) {
newExpression.append(rightText.replaceAll("\n", "\n" + indent));
}
else {
newExpression.append(StringUtil.escapeStringCharacters(rightText));
}
newExpression.append("\"\"\"");
newExpression.append("\"\"\"\n").append(leftText)
.append(right.isTextBlock() ? rightText : PsiLiteralUtil.escapeTextBlockCharacters(rightText)).append("\"\"\"");
}
else if (right.isTextBlock()) {
String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(right));
newExpression.append("\"\"\"").append('\n').append(indent);
newExpression.append(StringUtil.escapeStringCharacters(leftText));
newExpression.append(rightText.replaceAll("\n", "\n" + indent));
newExpression.append("\"\"\"");
newExpression.append("\"\"\"\n").append(PsiLiteralUtil.escapeTextBlockCharacters(leftText)).append(rightText).append("\"\"\"");
}
else {
newExpression.append('"');
newExpression.append(StringUtil.escapeStringCharacters(leftText));
newExpression.append(StringUtil.escapeStringCharacters(rightText));
newExpression.append('"');
newExpression.append('"').append(leftText).append(rightText).append('"');
}
}
private static String getLiteralExpressionText(PsiLiteralExpression expression) {
final PsiType type = expression.getType();
if (PsiType.CHAR.equals(type)) {
final String result = StringUtil.unquoteString(expression.getText());
if (result.equals("\"")) return "\\\"";
if (result.equals("\\'")) return "'";
return result;
}
else if (type instanceof PsiPrimitiveType) {
return Objects.requireNonNull(expression.getValue()).toString();
}
else if (expression.isTextBlock()) {
return PsiLiteralUtil.getTextBlockText(expression);
}
else {
return PsiLiteralUtil.getStringLiteralContent(expression);
}
}
}
@@ -0,0 +1,3 @@
class KeepEscapes {{
System.out.println("\u1234" +<caret> "\u5678");
}}
@@ -0,0 +1,3 @@
class KeepEscapes {{
System.out.println("\u1234\u5678");
}}
@@ -4,6 +4,7 @@ class TextBlocks {
System.out.println("""
first
second
third \\forth\""<caret>"");
third\
\\forth\""<caret>"");
}
}
@@ -6,6 +6,6 @@ class TextBlocks {
second
third
forth
<caret> """);
<caret>""");
}
}
@@ -2,8 +2,9 @@ class TextBlocks {
static {
//c1
System.out.println("""
first second
first \s\
second
third_no space
<caret> """);
<caret>""");
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2020 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.siyeh.ipp.concatenation;
import com.intellij.testFramework.LightProjectDescriptor;
@@ -31,11 +17,12 @@ public class JoinConcatenatedStringLiteralsIntentionTest extends IPPTestCase {
public void testTextBlocks() { doTest(); }
public void testTextBlocksTailingLineBreak() { doTest(); }
public void testTextBlocksAndStringLiteral() { doTest(); }
public void testKeepEscapes() { doTest(); }
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_13;
return JAVA_14;
}
@Override