Search in sources :

Example 51 with HighlighterIterator

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

the class EditorActionUtil method isLexemeBoundary.

/**
   * Finds out whether there's a boundary between two lexemes of different type at given offset.
   */
public static boolean isLexemeBoundary(@NotNull Editor editor, int offset) {
    if (!(editor instanceof EditorEx) || offset <= 0 || offset >= editor.getDocument().getTextLength() || DocumentUtil.isInsideSurrogatePair(editor.getDocument(), offset)) {
        return false;
    }
    if (CharArrayUtil.isEmptyOrSpaces(editor.getDocument().getImmutableCharSequence(), offset - 1, offset + 1))
        return false;
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator it = highlighter.createIterator(offset);
    if (it.getStart() != offset) {
        return false;
    }
    IElementType rightToken = it.getTokenType();
    it.retreat();
    IElementType leftToken = it.getTokenType();
    return !Comparing.equal(leftToken, rightToken);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 52 with HighlighterIterator

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

the class UnifiedEditorHighlighter method init.

private void init(@NotNull HighlighterIterator it1, @NotNull HighlighterIterator it2, @NotNull List<HighlightRange> ranges, int textLength) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    int offset = 0;
    for (HighlightRange range : ranges) {
        TextRange base = range.getBase();
        TextRange changed = range.getChanged();
        if (base.isEmpty())
            continue;
        if (base.getStartOffset() > offset) {
            addElement(new Element(offset, base.getStartOffset(), null, TextAttributes.ERASE_MARKER));
            offset = base.getStartOffset();
        }
        HighlighterIterator it = range.getSide().select(it1, it2);
        while (!it.atEnd() && changed.getStartOffset() >= it.getEnd()) {
            it.advance();
        }
        if (it.atEnd()) {
            LOG.error("Unexpected end of highlighter");
            break;
        }
        if (changed.getEndOffset() <= it.getStart()) {
            continue;
        }
        while (true) {
            int relativeStart = Math.max(it.getStart() - changed.getStartOffset(), 0);
            int relativeEnd = Math.min(it.getEnd() - changed.getStartOffset(), changed.getLength() + 1);
            addElement(new Element(offset + relativeStart, offset + relativeEnd, it.getTokenType(), it.getTextAttributes()));
            if (changed.getEndOffset() <= it.getEnd()) {
                offset += changed.getLength();
                break;
            }
            it.advance();
            if (it.atEnd()) {
                LOG.error("Unexpected end of highlighter");
                break;
            }
        }
    }
    if (offset < textLength) {
        addElement(new Element(offset, textLength, null, TextAttributes.ERASE_MARKER));
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 53 with HighlighterIterator

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

the class HippieWordCompletionHandler method processWords.

private static void processWords(Editor editor, int startOffset, TokenProcessor processor) {
    CharSequence chars = editor.getDocument().getCharsSequence();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(startOffset);
    while (!iterator.atEnd()) {
        int start = iterator.getStart();
        int end = iterator.getEnd();
        while (start < end) {
            int wordStart = start;
            while (wordStart < end && !isWordPart(chars.charAt(wordStart))) wordStart++;
            int wordEnd = wordStart;
            while (wordEnd < end && isWordPart(chars.charAt(wordEnd))) wordEnd++;
            if (wordEnd > wordStart && containsLettersOrDigits(chars, wordStart, wordEnd) && !processor.processToken(wordStart, wordEnd)) {
                return;
            }
            start = wordEnd + 1;
        }
        iterator.advance();
    }
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 54 with HighlighterIterator

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

the class TextPainter method drawText.

private void drawText(Graphics2D g, Rectangle2D clip) {
    float lineHeight = getLineHeight(g);
    HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
    if (hIterator.atEnd()) {
        myOffset = mySegmentEnd;
        return;
    }
    LineIterator lIterator = myDocument.createLineIterator();
    lIterator.start(myOffset);
    if (lIterator.atEnd()) {
        myOffset = mySegmentEnd;
        return;
    }
    TextAttributes attributes = hIterator.getTextAttributes();
    Color currentColor = attributes.getForegroundColor();
    Color backColor = attributes.getBackgroundColor();
    Color underscoredColor = attributes.getEffectColor();
    Font currentFont = getFont(attributes.getFontType());
    setForegroundColor(g, currentColor);
    setFont(g, currentFont);
    g.translate(clip.getX(), 0);
    Point2D position = new Point2D.Double(0, clip.getY());
    double lineY = position.getY();
    if (myPerformActualDrawing) {
        getMethodSeparator(lIterator.getLineNumber());
    }
    char[] text = myDocument.getCharsSequence().toString().toCharArray();
    while (!hIterator.atEnd() && !lIterator.atEnd()) {
        int hEnd = hIterator.getEnd();
        int lEnd = lIterator.getEnd();
        int lStart = lIterator.getStart();
        if (hEnd >= lEnd) {
            if (!drawString(g, text, lEnd - lIterator.getSeparatorLength(), myOffset == lStart, position, clip, backColor, underscoredColor)) {
                drawLineNumber(g, 0, lineY);
                break;
            }
            drawLineNumber(g, 0, lineY);
            lIterator.advance();
            myLineNumber++;
            position.setLocation(0, position.getY() + lineHeight);
            lineY = position.getY();
            myOffset = lEnd;
            if (myPerformActualDrawing) {
                LineMarkerInfo marker = getMethodSeparator(lIterator.getLineNumber());
                if (marker != null) {
                    Color save = g.getColor();
                    setForegroundColor(g, marker.separatorColor);
                    UIUtil.drawLine(g, 0, (int) lineY, (int) clip.getWidth(), (int) lineY);
                    setForegroundColor(g, save);
                }
            }
            if (position.getY() > clip.getY() + clip.getHeight() - lineHeight) {
                break;
            }
        } else {
            if (hEnd > lEnd - lIterator.getSeparatorLength()) {
                if (!drawString(g, text, lEnd - lIterator.getSeparatorLength(), myOffset == lStart, position, clip, backColor, underscoredColor)) {
                    drawLineNumber(g, 0, lineY);
                    break;
                }
            } else {
                if (!drawString(g, text, hEnd, myOffset == lStart, position, clip, backColor, underscoredColor)) {
                    drawLineNumber(g, 0, lineY);
                    break;
                }
            }
            hIterator.advance();
            attributes = hIterator.getTextAttributes();
            Color color = attributes.getForegroundColor();
            if (color == null) {
                color = Color.black;
            }
            if (color != currentColor) {
                setForegroundColor(g, color);
                currentColor = color;
            }
            backColor = attributes.getBackgroundColor();
            underscoredColor = attributes.getEffectColor();
            Font font = getFont(attributes.getFontType());
            if (font != currentFont) {
                setFont(g, font);
                currentFont = font;
            }
            myOffset = hEnd;
        }
    }
    g.translate(-clip.getX(), 0);
}
Also used : LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) Point2D(java.awt.geom.Point2D) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) LineIterator(com.intellij.openapi.editor.ex.LineIterator) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 55 with HighlighterIterator

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

the class SimpleEditorPreview method startBlinkingHighlights.

private List<HighlightData> startBlinkingHighlights(final EditorEx editor, final String attrKey, final SyntaxHighlighter highlighter, final boolean show, final Alarm alarm, final int count, final ColorSettingsPage page) {
    if (show && count <= 0)
        return Collections.emptyList();
    removeDecorations(editor);
    boolean found = false;
    List<HighlightData> highlights = new ArrayList<>();
    List<HighlightData> matchingHighlights = new ArrayList<>();
    for (HighlightData highlightData : myHighlightData) {
        boolean highlight = show && highlightData.getHighlightType().equals(attrKey);
        highlightData.addToCollection(highlights, highlight);
        if (highlight) {
            matchingHighlights.add(highlightData);
            found = true;
        }
    }
    if (!found && highlighter != null) {
        HighlighterIterator iterator = editor.getHighlighter().createIterator(0);
        do {
            IElementType tokenType = iterator.getTokenType();
            TextAttributesKey[] tokenHighlights = highlighter.getTokenHighlights(tokenType);
            for (final TextAttributesKey tokenHighlight : tokenHighlights) {
                String type = tokenHighlight.getExternalName();
                if (show && type != null && type.equals(attrKey)) {
                    HighlightData highlightData = new HighlightData(iterator.getStart(), iterator.getEnd(), BLINKING_HIGHLIGHTS_ATTRIBUTES);
                    highlights.add(highlightData);
                    matchingHighlights.add(highlightData);
                }
            }
            iterator.advance();
        } while (!iterator.atEnd());
    }
    final Map<TextAttributesKey, String> displayText = ColorSettingsUtil.keyToDisplayTextMap(page);
    // sort highlights to avoid overlappings
    Collections.sort(highlights, Comparator.comparingInt(HighlightData::getStartOffset));
    for (int i = highlights.size() - 1; i >= 0; i--) {
        HighlightData highlightData = highlights.get(i);
        int startOffset = highlightData.getStartOffset();
        HighlightData prevHighlightData = i == 0 ? null : highlights.get(i - 1);
        if (prevHighlightData != null && startOffset <= prevHighlightData.getEndOffset() && highlightData.getHighlightType().equals(prevHighlightData.getHighlightType())) {
            prevHighlightData.setEndOffset(highlightData.getEndOffset());
        } else {
            highlightData.addHighlToView(editor, myOptions.getSelectedScheme(), displayText);
        }
    }
    alarm.cancelAllRequests();
    alarm.addComponentRequest(() -> startBlinkingHighlights(editor, attrKey, highlighter, !show, alarm, count - 1, page), 400);
    return matchingHighlights;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlightData(com.intellij.application.options.colors.highlighting.HighlightData) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) 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