mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
raw string literal: insert whitespace to avoid literal till end of file
IDEA-190406
This commit is contained in:
@@ -15,21 +15,23 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.editorActions;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.tree.ElementType;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.tree.TokenSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements JavaLikeQuoteHandler {
|
||||
public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements JavaLikeQuoteHandler, MultiCharQuoteHandler {
|
||||
private final TokenSet concatenatableStrings;
|
||||
|
||||
public JavaQuoteHandler() {
|
||||
super(JavaTokenType.STRING_LITERAL, JavaTokenType.CHARACTER_LITERAL);
|
||||
super(JavaTokenType.STRING_LITERAL, JavaTokenType.CHARACTER_LITERAL, JavaTokenType.RAW_STRING_LITERAL);
|
||||
concatenatableStrings = TokenSet.create(JavaTokenType.STRING_LITERAL);
|
||||
}
|
||||
|
||||
@@ -95,6 +97,21 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
|
||||
return element.getParent() instanceof PsiLiteralExpression && element.getParent().getParent() instanceof PsiReferenceExpression;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public CharSequence getClosingQuote(HighlighterIterator iterator, int offset) {
|
||||
if (isOpeningQuote(iterator, offset - 1) && iterator.getTokenType() == JavaTokenType.RAW_STRING_LITERAL) {
|
||||
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 static boolean isAppropriateElementTypeForLiteralStatic(final IElementType tokenType) {
|
||||
return ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)
|
||||
|| tokenType == JavaTokenType.SEMICOLON
|
||||
@@ -103,6 +120,7 @@ public class JavaQuoteHandler extends SimpleTokenSetQuoteHandler implements Java
|
||||
|| tokenType == JavaTokenType.RBRACKET
|
||||
|| tokenType == JavaTokenType.RBRACE
|
||||
|| tokenType == JavaTokenType.STRING_LITERAL
|
||||
|| tokenType == JavaTokenType.CHARACTER_LITERAL;
|
||||
|| tokenType == JavaTokenType.CHARACTER_LITERAL
|
||||
|| tokenType == JavaTokenType.RAW_STRING_LITERAL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
{
|
||||
String s = `<selection> </selection>`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
{
|
||||
String s = <caret>
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,12 @@ public class LightAdvRawStringLiteralsTest extends LightCodeInsightFixtureTestCa
|
||||
assertEquals("abc", ((PsiLiteralExpressionImpl)rawStringLiteral).getRawString());
|
||||
}
|
||||
|
||||
public void testTypingOpeningTic() {
|
||||
myFixture.configureByFile(getTestName(false ) + ".java");
|
||||
myFixture.type('`');
|
||||
myFixture.checkResultByFile(getTestName(false) + ".after.java");
|
||||
}
|
||||
|
||||
private void doTestHighlighting() {
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
myFixture.checkHighlighting();
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.editorActions;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -28,4 +29,13 @@ public interface MultiCharQuoteHandler extends QuoteHandler {
|
||||
*/
|
||||
@Nullable
|
||||
CharSequence getClosingQuote(HighlighterIterator iterator, int offset);
|
||||
|
||||
/**
|
||||
* Should insert <code>closingQuote</code> in the document.
|
||||
*
|
||||
* May select the inserted quote
|
||||
*/
|
||||
default void insertString(Editor editor, int offset, CharSequence closingQuote) {
|
||||
editor.getDocument().insertString(offset, closingQuote);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -496,7 +496,7 @@ public class TypedHandler extends TypedActionHandlerBase {
|
||||
if (closingQuote != null && hasNonClosedLiterals(editor, quoteHandler, offset - 1)) {
|
||||
if (offset == document.getTextLength() ||
|
||||
!Character.isUnicodeIdentifierPart(document.getCharsSequence().charAt(offset))) { //any better heuristic or an API?
|
||||
document.insertString(offset, closingQuote);
|
||||
((MultiCharQuoteHandler)quoteHandler).insertString(editor, offset, closingQuote);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user