Search in sources :

Example 1 with StringLiteralLexer

use of com.intellij.lexer.StringLiteralLexer in project intellij-community by JetBrains.

the class EnterInStringLiteralHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull Ref<Integer> caretOffsetRef, @NotNull final Ref<Integer> caretAdvanceRef, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
    int caretOffset = caretOffsetRef.get().intValue();
    int caretAdvance = caretAdvanceRef.get().intValue();
    if (!isInStringLiteral(editor, dataContext, caretOffset))
        return Result.Continue;
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
    PsiElement psiAtOffset = file.findElementAt(caretOffset);
    if (psiAtOffset != null && psiAtOffset.getTextOffset() < caretOffset) {
        Document document = editor.getDocument();
        CharSequence text = document.getText();
        ASTNode token = psiAtOffset.getNode();
        JavaLikeQuoteHandler quoteHandler = getJavaLikeQuoteHandler(editor, psiAtOffset);
        if (quoteHandler != null && quoteHandler.getConcatenatableStringTokenTypes() != null && quoteHandler.getConcatenatableStringTokenTypes().contains(token.getElementType())) {
            TextRange range = token.getTextRange();
            final char literalStart = token.getText().charAt(0);
            final StringLiteralLexer lexer = new StringLiteralLexer(literalStart, token.getElementType());
            lexer.start(text, range.getStartOffset(), range.getEndOffset());
            while (lexer.getTokenType() != null) {
                if (lexer.getTokenStart() < caretOffset && caretOffset < lexer.getTokenEnd()) {
                    if (StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(lexer.getTokenType())) {
                        caretOffset = lexer.getTokenEnd();
                    }
                    break;
                }
                lexer.advance();
            }
            if (quoteHandler.needParenthesesAroundConcatenation(psiAtOffset)) {
                document.insertString(psiAtOffset.getTextRange().getEndOffset(), ")");
                document.insertString(psiAtOffset.getTextRange().getStartOffset(), "(");
                caretOffset++;
                caretAdvance++;
            }
            final String insertedFragment = literalStart + " " + quoteHandler.getStringConcatenationOperatorRepresentation();
            document.insertString(caretOffset, insertedFragment + " " + literalStart);
            caretOffset += insertedFragment.length();
            caretAdvance = 1;
            CommonCodeStyleSettings langSettings = CodeStyleSettingsManager.getSettings(file.getProject()).getCommonSettings(file.getLanguage());
            if (langSettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE) {
                caretOffset -= 1;
                caretAdvance = 3;
            }
            caretOffsetRef.set(caretOffset);
            caretAdvanceRef.set(caretAdvance);
            return Result.DefaultForceIndent;
        }
    }
    return Result.Continue;
}
Also used : JavaLikeQuoteHandler(com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler) StringLiteralLexer(com.intellij.lexer.StringLiteralLexer) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 2 with StringLiteralLexer

use of com.intellij.lexer.StringLiteralLexer in project intellij-community by JetBrains.

the class CustomFileHighlighter method getHighlightingLexer.

@Override
@NotNull
public Lexer getHighlightingLexer() {
    Lexer customFileTypeLexer = new CustomFileTypeLexer(myTable, true);
    if (myTable.isHasStringEscapes()) {
        customFileTypeLexer = new LayeredLexer(customFileTypeLexer);
        ((LayeredLexer) customFileTypeLexer).registerSelfStoppingLayer(new StringLiteralLexer('\"', CustomHighlighterTokenType.STRING, true, "x"), new IElementType[] { CustomHighlighterTokenType.STRING }, IElementType.EMPTY_ARRAY);
        ((LayeredLexer) customFileTypeLexer).registerSelfStoppingLayer(new StringLiteralLexer('\'', CustomHighlighterTokenType.STRING, true, "x"), new IElementType[] { CustomHighlighterTokenType.SINGLE_QUOTED_STRING }, IElementType.EMPTY_ARRAY);
    }
    return customFileTypeLexer;
}
Also used : Lexer(com.intellij.lexer.Lexer) StringLiteralLexer(com.intellij.lexer.StringLiteralLexer) LayeredLexer(com.intellij.lexer.LayeredLexer) StringLiteralLexer(com.intellij.lexer.StringLiteralLexer) LayeredLexer(com.intellij.lexer.LayeredLexer) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with StringLiteralLexer

use of com.intellij.lexer.StringLiteralLexer in project intellij-community by JetBrains.

the class LiteralSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    TextRange range = e.getTextRange();
    SelectWordUtil.addWordHonoringEscapeSequences(editorText, range, cursorOffset, new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), result);
    result.add(new TextRange(range.getStartOffset() + 1, range.getEndOffset() - 1));
    return result;
}
Also used : StringLiteralLexer(com.intellij.lexer.StringLiteralLexer) TextRange(com.intellij.openapi.util.TextRange)

Example 4 with StringLiteralLexer

use of com.intellij.lexer.StringLiteralLexer in project intellij-community by JetBrains.

the class JsonStringLiteralSelectionHandler method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    final IElementType type = e.getNode().getElementType();
    final StringLiteralLexer lexer = new StringLiteralLexer(type == SINGLE_QUOTED_STRING ? '\'' : '"', type, false, "/", false, false);
    final List<TextRange> result = new ArrayList<>();
    SelectWordUtil.addWordHonoringEscapeSequences(editorText, e.getTextRange(), cursorOffset, lexer, result);
    final PsiElement parent = e.getParent();
    result.add(ElementManipulators.getValueTextRange(parent).shiftRight(parent.getTextOffset()));
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) StringLiteralLexer(com.intellij.lexer.StringLiteralLexer) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

StringLiteralLexer (com.intellij.lexer.StringLiteralLexer)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiElement (com.intellij.psi.PsiElement)2 JavaLikeQuoteHandler (com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler)1 ASTNode (com.intellij.lang.ASTNode)1 LayeredLexer (com.intellij.lexer.LayeredLexer)1 Lexer (com.intellij.lexer.Lexer)1 Document (com.intellij.openapi.editor.Document)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 IElementType (com.intellij.psi.tree.IElementType)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1