Java: fix text block extend selection behaviour (IDEA-227005, IDEA-226986)

GitOrigin-RevId: 9d9f4201db2e0b2dfedc8485701ea6f050e0df41
This commit is contained in:
Bas Leijdekkers
2023-10-31 23:25:21 +00:00
committed by intellij-monorepo-bot
parent 775979aed7
commit fb26d47cda
18 changed files with 78 additions and 24 deletions
@@ -7,7 +7,6 @@ import com.intellij.lexer.StringLiteralLexer;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.BasicLiteralUtil;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.BasicJavaAstTreeUtil;
@@ -43,20 +42,12 @@ public class LiteralSelectioner extends AbstractBasicBackBasicSelectioner {
if (textBlock) {
int contentStart = StringUtil.indexOf(editorText, '\n', range.getStartOffset());
if (contentStart == -1) return result;
contentStart += 1;
String[] lines = BasicLiteralUtil.getTextBlockLines(node.getText());
if (lines == null) return result;
int indent = BasicLiteralUtil.getTextBlockIndent(lines);
if (indent == -1) return result;
for (int i = 0; i < indent; i++) {
if (editorText.charAt(contentStart + i) == '\n') return result;
}
int start = contentStart + indent;
int end = range.getEndOffset() - 4;
int start = contentStart + 1;
int end = range.getEndOffset();
end -= StringUtil.endsWith(editorText, start, end, "\"\"\"") ? 4 : 1;
for (; end >= start; end--) {
char c = editorText.charAt(end);
if (c == '\n') break;
if (!Character.isWhitespace(c)) {
if (c == '\n' || !Character.isWhitespace(c)) {
end += 1;
break;
}
@@ -64,7 +55,10 @@ public class LiteralSelectioner extends AbstractBasicBackBasicSelectioner {
if (start < end) result.add(new TextRange(start, end));
}
else {
result.add(new TextRange(range.getStartOffset() + 1, range.getEndOffset() - 1));
int endOffset = editorText.charAt(range.getEndOffset() - 1) == '"'
? range.getEndOffset() - 1
: range.getEndOffset();
result.add(new TextRange(range.getStartOffset() + 1, endOffset));
}
return result;
@@ -198,6 +198,12 @@ public abstract class AbstractBasicJavaSelectWordTest extends SelectWordTestBase
public void testTextBlockEscapedLineEndings() { doTest("java"); }
public void testTextBlockLeadingWhitespace() { doTest("java"); }
public void testUnclosedTextBlock() { doTest("java"); }
public void testUnclosedLiteral() { doTest("java"); }
public void testLineComments() { doTest("java"); }
public void testLineCommentsAtStart() { doTest("java"); }
@@ -1,7 +1,7 @@
class Test {
String textBlock = <selection>"""
String textBlock = """
<selection>
fo<caret>o
"""</selection>;
</selection> """;
}
@@ -0,0 +1,7 @@
class Test {
String textBlock = <selection>"""
foo
"""</selection>;
}
@@ -1,7 +1,7 @@
class Test {
String textBlock = """
<selection>Info: \
<selection> Info: \
textInSameLin<caret>eButEscapedInSourCode \
textInSameLineButEscapedInSourCode2 \</selection>
""";
textInSameLineButEscapedInSourCode2 \
</selection> """;
}
@@ -0,0 +1,5 @@
class Test {{
String s1 = """
<selection> <caret> test
</selection> """;
}}
@@ -0,0 +1,5 @@
class Test {{
String s1 = <selection>"""
<caret> test
"""</selection>;
}}
@@ -0,0 +1,5 @@
class Test {{
String s1 = """
<caret> test
""";
}}
@@ -1,6 +1,6 @@
class Test {
String textBlock = """
<selection>fo<caret>o
bar</selection>
""";
<selection> fo<caret>o
bar
</selection> """;
}
@@ -1,6 +1,6 @@
class Test {
String textBlock = """
<selection>fo<caret>o
<selection> fo<caret>o
bar
baz</selection>""";
}
@@ -0,0 +1,3 @@
class Test {{
String s = "one <selection><caret>two</selection> three
}}
@@ -0,0 +1,3 @@
class Test {{
String s = "<selection>one <caret>two three</selection>
}}
@@ -0,0 +1,3 @@
class Test {{
String s = <selection>"one <caret>two three</selection>
}}
@@ -0,0 +1,3 @@
class Test {{
String s = "one <caret>two three
}}
@@ -0,0 +1,5 @@
class {{
String s = """
one
<selection><caret>two</selection>
three
@@ -0,0 +1,5 @@
class {{
String s = """
<selection> one
<caret>two
three</selection>
@@ -0,0 +1,5 @@
class {{
String s = <selection>"""
one
<caret>two
three</selection>
@@ -0,0 +1,5 @@
class {{
String s = """
one
<caret>two
three