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);
}
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));
}
}
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();
}
}
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);
}
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;
}
Aggregations