raw string literals: keep multiple tics at start pos sync with closing

IDEA-CR-31851
This commit is contained in:
Anna.Kozlova
2018-04-17 18:23:42 +02:00
parent 42d204757e
commit 13d2e4e4dd
4 changed files with 22 additions and 8 deletions
@@ -100,16 +100,23 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
@Nullable
@Override
public CharSequence getClosingQuote(HighlighterIterator iterator, int offset) {
if (isOpeningQuote(iterator, offset - 1) && iterator.getTokenType() == JavaTokenType.RAW_STRING_LITERAL) {
return " `";
if (iterator.getTokenType() == JavaTokenType.RAW_STRING_LITERAL) {
CharSequence text = iterator.getDocument().getImmutableCharSequence();
int leadingTicsSequence = PsiRawStringLiteralUtil.getLeadingTicsSequence(text.subSequence(iterator.getStart(), offset));
if (isOpeningQuote(iterator, offset - leadingTicsSequence)) {
int closingSequence = PsiRawStringLiteralUtil.getLeadingTicsSequence(text.subSequence(offset, iterator.getEnd()));
if (closingSequence + 1 == leadingTicsSequence) {
return "`";
}
}
}
return null;
}
@Override
public void insertString(Editor editor, int offset, CharSequence closingQuote) {
MultiCharQuoteHandler.super.insertString(editor, offset, closingQuote);
editor.getSelectionModel().setSelection(offset, offset + closingQuote.length() - 1);
public void insertClosingQuote(Editor editor, int offset, CharSequence closingQuote) {
editor.getDocument().insertString(offset, " " + closingQuote);
editor.getSelectionModel().setSelection(offset, offset + 1);
}
public static boolean isAppropriateElementTypeForLiteralStatic(final IElementType tokenType) {
@@ -30,20 +30,20 @@ public class PsiRawStringLiteralUtil {
/**
* Return number of leading tics in the <code>text</code>
*/
public static int getLeadingTicsSequence(String text) {
public static int getLeadingTicsSequence(CharSequence text) {
return getTicsSequence(text, text.length(), 0);
}
/**
* Return number of trailing tics in the <code>text</code>
*/
public static int getTrailingTicsSequence(String text) {
public static int getTrailingTicsSequence(CharSequence text) {
int length = text.length();
while (length > 0 && text.charAt(length - 1) == '`') length--;
return text.length() - length;
}
private static int getTicsSequence(String literalText, int length, int startIndex) {
private static int getTicsSequence(CharSequence literalText, int length, int startIndex) {
int quotesLength = startIndex;
while (quotesLength < length && literalText.charAt(quotesLength) == '`') quotesLength++;
return quotesLength - startIndex;
@@ -0,0 +1,5 @@
class A {
{
String s = ``<selection> </selection>``
}
}
@@ -94,6 +94,8 @@ public class LightAdvRawStringLiteralsTest extends LightCodeInsightFixtureTestCa
myFixture.configureByFile(getTestName(false ) + ".java");
myFixture.type('`');
myFixture.checkResultByFile(getTestName(false) + ".after.java");
myFixture.type('`');
myFixture.checkResultByFile(getTestName(false) + ".1.after.java");
}
private void doTestHighlighting() {