Search in sources :

Example 61 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class CodeBlockUtil method calcBlockStartOffset.

private static int calcBlockStartOffset(Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset() - 1;
    if (offset < 0)
        return -1;
    Document document = editor.getDocument();
    final FileType fileType = file.getFileType();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    int depth = 0;
    Language braceType;
    boolean isAfterRBrace = false;
    if (isRStructuralBrace(fileType, iterator, document.getCharsSequence())) {
        isAfterRBrace = true;
        depth = -1;
        braceType = getBraceType(iterator);
    } else {
        braceType = null;
    }
    boolean moved = false;
    while (true) {
        if (iterator.atEnd())
            return -1;
        if (isLStructuralBrace(fileType, iterator, document.getCharsSequence()) && (braceType == getBraceType(iterator) || braceType == null)) {
            if (braceType == null) {
                braceType = getBraceType(iterator);
            }
            if (moved) {
                if (depth == 0)
                    break;
                depth--;
            }
        } else if (isRStructuralBrace(fileType, iterator, document.getCharsSequence()) && (braceType == getBraceType(iterator) || braceType == null)) {
            if (braceType == null) {
                braceType = getBraceType(iterator);
            }
            depth++;
        }
        moved = true;
        iterator.retreat();
    }
    return isAfterRBrace ? iterator.getStart() : iterator.getEnd();
}
Also used : Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 62 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class GStringBackspaceHandlerDelegate method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    if (c != '{')
        return;
    if (!(file instanceof GroovyFile))
        return;
    final int offset = editor.getCaretModel().getOffset();
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    if (offset < 1)
        return;
    HighlighterIterator iterator = highlighter.createIterator(offset);
    if (iterator.getTokenType() != GroovyTokenTypes.mRCURLY)
        return;
    iterator.retreat();
    if (iterator.getStart() < 1 || iterator.getTokenType() != GroovyTokenTypes.mLCURLY)
        return;
    editor.getDocument().deleteString(offset, offset + 1);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 63 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class SyntaxHighlighterOverEditorHighlighter method resetPosition.

public void resetPosition(int startOffset) {
    if (lexer instanceof LexerEditorHighlighterLexer) {
        ((LexerEditorHighlighterLexer) lexer).resetPosition(startOffset);
        HighlighterIterator iterator = ((LexerEditorHighlighterLexer) lexer).getHighlighterIterator();
        if (iterator instanceof LayeredHighlighterIterator) {
            layeredHighlighterIterator = (LayeredHighlighterIterator) iterator;
        } else {
            layeredHighlighterIterator = null;
        }
    } else {
        CharSequence text = lexer.getBufferSequence();
        lexer.start(text, startOffset, text.length());
    }
}
Also used : LayeredHighlighterIterator(com.intellij.openapi.editor.ex.util.LayeredHighlighterIterator) LexerEditorHighlighterLexer(com.intellij.psi.impl.search.LexerEditorHighlighterLexer) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) LayeredHighlighterIterator(com.intellij.openapi.editor.ex.util.LayeredHighlighterIterator)

Example 64 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class EditorPainter method paintBorderEffect.

private void paintBorderEffect(Graphics2D g, ClipDetector clipDetector, EditorHighlighter highlighter, int clipStartOffset, int clipEndOffset) {
    HighlighterIterator it = highlighter.createIterator(clipStartOffset);
    while (!it.atEnd() && it.getStart() < clipEndOffset) {
        TextAttributes attributes = it.getTextAttributes();
        if (isBorder(attributes)) {
            paintBorderEffect(g, clipDetector, it.getStart(), it.getEnd(), attributes);
        }
        it.advance();
    }
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 65 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class LineLayout method createRuns.

private static List<BidiRun> createRuns(EditorView view, char[] text, int startOffsetInEditor) {
    int textLength = text.length;
    if (view.getEditor().myDisableRtl || !Bidi.requiresBidi(text, 0, textLength)) {
        return Collections.singletonList(new BidiRun(textLength));
    }
    List<BidiRun> runs = new ArrayList<>();
    int flags = view.getBidiFlags();
    if (startOffsetInEditor >= 0) {
        // running bidi algorithm separately for text fragments corresponding to different lexer tokens
        int relLastOffset = 0;
        IElementType lastToken = null;
        HighlighterIterator iterator = view.getEditor().getHighlighter().createIterator(startOffsetInEditor);
        while (!iterator.atEnd() && (iterator.getStart() - startOffsetInEditor) < textLength) {
            int iteratorRelStart = alignToCodePointBoundary(text, iterator.getStart() - startOffsetInEditor);
            int iteratorRelEnd = alignToCodePointBoundary(text, iterator.getEnd() - startOffsetInEditor);
            IElementType currentToken = iterator.getTokenType();
            int relStartOffset = Math.max(0, iteratorRelStart);
            String lcPrefix = getLineCommentPrefix(currentToken);
            // for line comments we process prefix and following text separately
            if (!StringUtil.isEmpty(lcPrefix) && lcPrefix.length() <= (iteratorRelEnd - iteratorRelStart) && CharArrayUtil.regionMatches(text, relStartOffset, relStartOffset + lcPrefix.length(), lcPrefix) && !isInsideSurrogatePair(text, relStartOffset + lcPrefix.length())) {
                addRuns(runs, text, relLastOffset, relStartOffset, flags);
                int textStartOffset = Math.min(textLength, Math.min(iteratorRelEnd, CharArrayUtil.shiftForward(text, relStartOffset + lcPrefix.length(), " \t")));
                relLastOffset = Math.min(iteratorRelEnd, textLength);
                lastToken = null;
                addRuns(runs, text, relStartOffset, textStartOffset, flags);
                addRuns(runs, text, textStartOffset, relLastOffset, flags);
            } else if (distinctTokens(lastToken, currentToken)) {
                addRuns(runs, text, relLastOffset, relStartOffset, flags);
                lastToken = currentToken;
                relLastOffset = relStartOffset;
            }
            iterator.advance();
        }
        addRuns(runs, text, relLastOffset, textLength, flags);
    } else {
        addRuns(runs, text, 0, textLength, flags);
    }
    for (BidiRun run : runs) {
        assert !isInsideSurrogatePair(text, run.startOffset);
        assert !isInsideSurrogatePair(text, run.endOffset);
    }
    return runs;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

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