[java] Join Lines in text blocks: automatically remove \s on the joined line boundaries (IDEA-349062)

GitOrigin-RevId: 97f4885283f6da2bb06c46df6ea19ee6cd8caa7c
This commit is contained in:
Tagir Valeev
2024-05-24 12:31:26 +02:00
committed by intellij-monorepo-bot
parent 70a3328aa8
commit f2d54019ba
10 changed files with 71 additions and 2 deletions

View File

@@ -37,12 +37,25 @@ public final class TextBlockJoinLinesHandler implements JoinRawLinesHandlerDeleg
&& lineNumber == doc.getLineNumber(tokenRange.getStartOffset());
boolean atEmptyStartLine = atStartLine && TEXT_BLOCK_START.matcher(token.getText()).find();
boolean singleSlash = false;
if (text.charAt(start) == '\\') {
int numSpaces = 0;
char charAtStart = text.charAt(start);
if (charAtStart == '\\') {
int startOffset = Math.max(tokenRange.getStartOffset(), doc.getLineStartOffset(lineNumber));
String substring = doc.getText(TextRange.create(startOffset, start)) + "\\\n";
CharSequence parsed = CodeInsightUtilCore.parseStringCharacters(substring, null);
singleSlash = parsed != null && parsed.charAt(parsed.length() - 1) != '\n';
}
else if (charAtStart == 's' && text.charAt(start - 1) == '\\') {
int startOffset = Math.max(tokenRange.getStartOffset(), doc.getLineStartOffset(lineNumber));
String substring = doc.getText(TextRange.create(startOffset, start + 1));
CharSequence parsed = CodeInsightUtilCore.parseStringCharacters(substring, null);
if (parsed != null) {
while (text.charAt(start - numSpaces * 2) == 's' && text.charAt(start - numSpaces * 2 - 1) == '\\' &&
parsed.charAt(parsed.length() - 1 - numSpaces) == ' ') {
numSpaces++;
}
}
}
if (!singleSlash) {
start++;
}
@@ -61,7 +74,8 @@ public final class TextBlockJoinLinesHandler implements JoinRawLinesHandlerDeleg
doc.deleteString(start, end);
endOffset += start - end;
} else {
doc.replaceString(start, end, "\\n");
doc.replaceString(start - numSpaces * 2, end, " ".repeat(numSpaces) + "\\n");
start -= numSpaces;
endOffset += start - end + 2;
}
if (fromStartTillEnd) {

View File

@@ -0,0 +1,7 @@
class A {
void test() {
String s = STR."""
<caret>Hello\s
World""";
}
}

View File

@@ -0,0 +1,7 @@
class A {
void test() {
String s = STR."""
<caret>Hello\s\s
World""";
}
}

View File

@@ -0,0 +1,6 @@
class A {
void test() {
String s = STR."""
Hello <caret>\nWorld""";
}
}

View File

@@ -0,0 +1,7 @@
class A {
void test() {
String s = <selection>STR."""
Hello\s\s\s
World"""</selection>;
}
}

View File

@@ -0,0 +1,5 @@
class A {
void test() {
String s = <selection>STR."Hello \nWorld"<caret></selection>;
}
}

View File

@@ -0,0 +1,7 @@
class A {
void test() {
String s = STR."""
<caret>Hello\\s
World""";
}
}

View File

@@ -0,0 +1,6 @@
class A {
void test() {
String s = STR."""
Hello\\s<caret>\nWorld""";
}
}

View File

@@ -0,0 +1,6 @@
class A {
void test() {
String s = STR."""
Hello <caret>\nWorld""";
}
}

View File

@@ -311,6 +311,10 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
public void testJoinTextBlockBackTripleSlash() {doTest();}
public void testJoinTextBlockBackSlashEmptyLineAfter() {doTest();}
public void testJoinTextBlockBackSlashLastLine() {doTest();}
public void testJoinTextBlockSlashS() {doTest();}
public void testJoinTextBlockSlashSFake() {doTest();}
public void testJoinTextBlockSlashS2() {doTest();}
public void testJoinTextBlockSlashSComplete() {doTest();}
public void testJoinStringTemplateBackSlash() {doTest();}
public void testJoinStringTemplateBackSlash2() {doTest();}