Search in sources :

Example 1 with QuoteHandler

use of com.intellij.codeInsight.editorActions.QuoteHandler in project intellij-community by JetBrains.

the class EnterInStringLiteralHandler method isInStringLiteral.

private static boolean isInStringLiteral(@NotNull Editor editor, @NotNull DataContext dataContext, int offset) {
    Language language = EnterHandler.getLanguage(dataContext);
    if (offset > 0 && language != null) {
        QuoteHandler quoteHandler = TypedHandler.getLanguageQuoteHandler(language);
        if (quoteHandler == null) {
            FileType fileType = language.getAssociatedFileType();
            quoteHandler = fileType != null ? TypedHandler.getQuoteHandlerForType(fileType) : null;
        }
        if (quoteHandler != null) {
            EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
            HighlighterIterator iterator = highlighter.createIterator(offset - 1);
            return StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()) || quoteHandler.isInsideLiteral(iterator);
        }
    }
    return false;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) JavaLikeQuoteHandler(com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler) QuoteHandler(com.intellij.codeInsight.editorActions.QuoteHandler) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 2 with QuoteHandler

use of com.intellij.codeInsight.editorActions.QuoteHandler in project intellij-community by JetBrains.

the class PyTripleQuoteBackspaceDelegate method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    isTripleQuote = false;
    if (c == '"' || c == '\'' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        final QuoteHandler quoteHandler = TypedHandler.getQuoteHandler(file, editor);
        if (quoteHandler == null || !(quoteHandler instanceof BaseQuoteHandler))
            return;
        final int offset = editor.getCaretModel().getCurrentCaret().getOffset();
        String text = editor.getDocument().getText();
        boolean mayBeTripleQuote = offset >= 3 && offset + 2 < text.length();
        if (mayBeTripleQuote) {
            HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
            boolean hasTripleQuoteAfter = offset + 2 < text.length() && text.charAt(offset) == c && text.charAt(offset + 1) == c && text.charAt(offset + 2) == c;
            isTripleQuote = quoteHandler.isOpeningQuote(iterator, offset - 1) && hasTripleQuoteAfter;
        }
    }
}
Also used : BaseQuoteHandler(com.jetbrains.python.editor.BaseQuoteHandler) QuoteHandler(com.intellij.codeInsight.editorActions.QuoteHandler) BaseQuoteHandler(com.jetbrains.python.editor.BaseQuoteHandler) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

QuoteHandler (com.intellij.codeInsight.editorActions.QuoteHandler)2 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)2 JavaLikeQuoteHandler (com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler)1 Language (com.intellij.lang.Language)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 FileType (com.intellij.openapi.fileTypes.FileType)1 BaseQuoteHandler (com.jetbrains.python.editor.BaseQuoteHandler)1