mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
short diff: merge neighbour highlighters with same text attributes - save memory
This commit is contained in:
+43
-4
@@ -16,6 +16,8 @@
|
||||
package com.intellij.openapi.editor.highlighter;
|
||||
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.HighlighterColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsScheme;
|
||||
import com.intellij.openapi.editor.event.DocumentEvent;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
@@ -36,12 +38,18 @@ public class FragmentedEditorHighlighter implements EditorHighlighter {
|
||||
private final TreeMap<Integer, Element> myPieces;
|
||||
private final Document myDocument;
|
||||
private final int myAdditionalOffset;
|
||||
private TextAttributes myUsualAttributes;
|
||||
private final boolean myMergeByTextAttributes;
|
||||
|
||||
public FragmentedEditorHighlighter(HighlighterIterator sourceIterator, List<TextRange> ranges) {
|
||||
this(sourceIterator, ranges, 0);
|
||||
this(sourceIterator, ranges, 0, false);
|
||||
}
|
||||
|
||||
public FragmentedEditorHighlighter(HighlighterIterator sourceIterator, List<TextRange> ranges, final int additionalOffset) {
|
||||
public FragmentedEditorHighlighter(HighlighterIterator sourceIterator,
|
||||
List<TextRange> ranges,
|
||||
final int additionalOffset,
|
||||
boolean mergeByTextAttributes) {
|
||||
myMergeByTextAttributes = mergeByTextAttributes;
|
||||
myDocument = sourceIterator.getDocument();
|
||||
myPieces = new TreeMap<Integer, Element>();
|
||||
myAdditionalOffset = additionalOffset;
|
||||
@@ -58,8 +66,23 @@ public class FragmentedEditorHighlighter implements EditorHighlighter {
|
||||
}
|
||||
while (range.getEndOffset() >= iterator.getEnd()) {
|
||||
int relativeStart = iterator.getStart() - range.getStartOffset();
|
||||
myPieces.put(offset + relativeStart, new Element(offset + relativeStart,
|
||||
offset + (iterator.getEnd() - range.getStartOffset()), iterator.getTokenType(), iterator.getTextAttributes()));
|
||||
boolean merged = false;
|
||||
if (myMergeByTextAttributes && ! myPieces.isEmpty()) {
|
||||
final Integer first = myPieces.descendingKeySet().first();
|
||||
final Element element = myPieces.get(first);
|
||||
if (element.getEnd() >= offset + relativeStart && myPieces.get(first).getAttributes().equals(iterator.getTextAttributes())) {
|
||||
// merge
|
||||
merged = true;
|
||||
myPieces.put(element.getStart(), new Element(element.getStart(),
|
||||
offset + (iterator.getEnd() - range.getStartOffset()), iterator.getTokenType(),
|
||||
iterator.getTextAttributes()));
|
||||
}
|
||||
}
|
||||
if (! merged) {
|
||||
myPieces.put(offset + relativeStart, new Element(offset + relativeStart,
|
||||
offset + (iterator.getEnd() - range.getStartOffset()), iterator.getTokenType(),
|
||||
iterator.getTextAttributes()));
|
||||
}
|
||||
iterator.advance();
|
||||
if (iterator.atEnd()) return;
|
||||
}
|
||||
@@ -152,6 +175,22 @@ public class FragmentedEditorHighlighter implements EditorHighlighter {
|
||||
return myDocument;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUsualAttributes(final TextAttributes ta) {
|
||||
if (myUsualAttributes == null) {
|
||||
final EditorColorsManager manager = EditorColorsManager.getInstance();
|
||||
final EditorColorsScheme[] schemes = manager.getAllSchemes();
|
||||
EditorColorsScheme defaultScheme = schemes[0];
|
||||
for (EditorColorsScheme scheme : schemes) {
|
||||
if (manager.isDefaultScheme(scheme)) {
|
||||
defaultScheme = scheme;
|
||||
break;
|
||||
}
|
||||
}
|
||||
myUsualAttributes = defaultScheme.getAttributes(HighlighterColors.TEXT);
|
||||
}
|
||||
return myUsualAttributes.equals(ta);
|
||||
}
|
||||
|
||||
private static class Element {
|
||||
private final int myStart;
|
||||
|
||||
+2
-2
@@ -316,7 +316,7 @@ public class PreparedFragmentedContent {
|
||||
highlighter.setText(oldDocument.getText());
|
||||
HighlighterIterator iterator = highlighter.createIterator(ranges.get(0).getBefore().getStartOffset());
|
||||
FragmentedEditorHighlighter beforeHighlighter =
|
||||
new FragmentedEditorHighlighter(iterator, getBeforeFragments(), 1);
|
||||
new FragmentedEditorHighlighter(iterator, getBeforeFragments(), 1, true);
|
||||
setBeforeHighlighter(beforeHighlighter);
|
||||
|
||||
final EditorHighlighter highlighter1 =
|
||||
@@ -325,7 +325,7 @@ public class PreparedFragmentedContent {
|
||||
highlighter1.setText(document.getText());
|
||||
HighlighterIterator iterator1 = highlighter1.createIterator(ranges.get(0).getAfter().getStartOffset());
|
||||
FragmentedEditorHighlighter afterHighlighter =
|
||||
new FragmentedEditorHighlighter(iterator1, getAfterFragments(), 1);
|
||||
new FragmentedEditorHighlighter(iterator1, getAfterFragments(), 1, true);
|
||||
setAfterHighlighter(afterHighlighter);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user