Search in sources :

Example 81 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project Perl5-IDEA by Camelcade.

the class PerlBackspaceHandler method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    CaretModel caretModel = editor.getCaretModel();
    int currentOffset = caretModel.getOffset() - 1;
    if (currentOffset < 0) {
        return;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(currentOffset);
    IElementType tokenToDelete = iterator.atEnd() ? null : iterator.getTokenType();
    if (QUOTE_OPEN_ANY.contains(tokenToDelete)) {
        PerlEditorUtil.moveToNextMeaningfulToken(iterator);
        if (iterator.atEnd()) {
            return;
        }
        IElementType nextTokenType = iterator.getTokenType();
        if (QUOTE_CLOSE_PAIRED.contains(nextTokenType)) {
            int startOffsetToDelete = currentOffset + 1;
            if (currentOffset > 0 && QUOTE_MIDDLE.contains(tokenToDelete)) {
                HighlighterIterator preQuoteIterator = PerlEditorUtil.moveToPreviousMeaningfulToken(highlighter.createIterator(currentOffset - 1));
                if (!preQuoteIterator.atEnd() && QUOTE_OPEN_ANY.contains(preQuoteIterator.getTokenType())) {
                    startOffsetToDelete = preQuoteIterator.getEnd();
                    caretModel.moveToOffset(preQuoteIterator.getEnd());
                }
            }
            editor.getDocument().deleteString(startOffsetToDelete, iterator.getEnd());
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 82 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-code-outline by sitano.

the class CodeOutlineImageEx method renderToImg.

/**
 * Renders the given characters to the code outline image starting at the
 * given position.
 *
 * @param chars a character array
 * @param offset the offset into the given array to start rendering
 * @param len the number of characters to render
 * @param pos the position at which to start rendering
 */
@Override
protected void renderToImg(CharSequence chars, int offset, int len, LogicalPosition pos) {
    if (img == null)
        return;
    final EditorEx ex = (EditorEx) editor;
    final EditorHighlighter hl = ex.getHighlighter();
    final HighlighterIterator hi = hl.createIterator(offset);
    int line = pos.line, col = pos.column;
    // Render characters
    for (int i = offset; i < len; i++) {
        final char ch = chars.charAt(i);
        while (!hi.atEnd() && i > hi.getEnd()) {
            hi.advance();
        }
        if (ch == '\n') {
            line++;
            if (getScaledLine(line, scale) >= visibleImgHeight)
                break;
            col = 0;
        } else {
            if (col >= visibleImgWidth)
                continue;
            // Whitespaces are skipped inside drawChar
            drawChar(ch, col, line, getCharColor(editor, hi));
            col++;
        }
    }
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 83 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-elixir by KronicDeth.

the class TypedHandler method charTyped.

/*
     * Instance Methods
     */
/**
 * Called after the specified character typed by the user has been inserted in the editor.
 *
 * @param charTyped the character that was typed
 * @param project the project in which the {@code file} exists
 * @param editor the editor that has the {@code file} open
 * @param file the file into which the {@code charTyped} was typed
 */
@Override
public Result charTyped(char charTyped, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    Result result = Result.CONTINUE;
    if (file instanceof ElixirFile) {
        if (charTyped == ' ') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "(do|fn)<space><caret>"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 2);
                IElementType tokenType = iterator.getTokenType();
                if (tokenType == ElixirTypes.DO || tokenType == ElixirTypes.FN) {
                    editor.getDocument().insertString(caret, " end");
                    result = Result.STOP;
                }
            }
        } else if (charTyped == '<') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "~<sigil_name><"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 1);
                IElementType tokenType = iterator.getTokenType();
                if (tokenType == LINE_PROMOTER) {
                    editor.getDocument().insertString(caret, ">");
                    result = Result.STOP;
                }
            }
            if (result == Result.CONTINUE && caret > 1) {
                // "<<"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 2);
                if (iterator.getTokenType() == ElixirTypes.OPENING_BIT) {
                    editor.getDocument().insertString(caret, ">>");
                    result = Result.STOP;
                }
            }
        } else if (charTyped == '/' || charTyped == '|') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "~<sigil_name>(/|\|)"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 1);
                IElementType tokenType = iterator.getTokenType();
                if (tokenType == LINE_PROMOTER) {
                    editor.getDocument().insertString(caret, String.valueOf(charTyped));
                    result = Result.STOP;
                }
            }
        } else if (charTyped == '{') {
            // #{
            int caret = editor.getCaretModel().getOffset();
            if (caret > 1) {
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 1);
                IElementType tokenType = iterator.getTokenType();
                if (tokenType == ElixirTypes.INTERPOLATION_START) {
                    editor.getDocument().insertString(caret, "}");
                    result = Result.STOP;
                }
            }
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) ElixirFile(org.elixir_lang.psi.ElixirFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Aggregations

HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)83 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)33 IElementType (com.intellij.psi.tree.IElementType)33 Document (com.intellij.openapi.editor.Document)12 FileType (com.intellij.openapi.fileTypes.FileType)12 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)9 TextRange (com.intellij.openapi.util.TextRange)8 Editor (com.intellij.openapi.editor.Editor)7 PsiElement (com.intellij.psi.PsiElement)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 PsiFile (com.intellij.psi.PsiFile)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)4 Project (com.intellij.openapi.project.Project)4 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 Trinity (com.intellij.openapi.util.Trinity)3 ArrayList (java.util.ArrayList)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2