From cfb5af3e40d83774f994f5ec0de5f2bcb9c0b038 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 24 May 2016 12:35:28 +0200 Subject: [PATCH] LexerEditorHighlighter: do nothing in setText if the text is identical to the previous one --- .../ex/util/LayeredLexerEditorHighlighter.java | 17 ++++++++--------- .../editor/ex/util/LexerEditorHighlighter.java | 13 ++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LayeredLexerEditorHighlighter.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LayeredLexerEditorHighlighter.java index df600d3963ef..8af290897cd1 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LayeredLexerEditorHighlighter.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LayeredLexerEditorHighlighter.java @@ -47,7 +47,6 @@ import java.util.*; public class LayeredLexerEditorHighlighter extends LexerEditorHighlighter { private final Map myTokensToLayer = new HashMap<>(); private final Map myLayerBuffers = new HashMap<>(); - private CharSequence myText; public LayeredLexerEditorHighlighter(@NotNull SyntaxHighlighter highlighter, @NotNull EditorColorsScheme scheme) { super(highlighter, scheme); @@ -120,7 +119,6 @@ public class LayeredLexerEditorHighlighter extends LexerEditorHighlighter { // do NOT synchronize before updateLayers due to deadlock with PsiLock updateLayers(); - myText = text; super.setText(text); } @@ -158,12 +156,11 @@ public class LayeredLexerEditorHighlighter extends LexerEditorHighlighter { @Override public void documentChanged(DocumentEvent e) { // do NOT synchronize before updateLayers due to deadlock with PsiLock - final boolean b = updateLayers(); + boolean changed = updateLayers(); synchronized (this) { - myText = e.getDocument().getCharsSequence(); - if (b) { - setText(myText); + if (changed) { + setText(e.getDocument().getImmutableCharSequence()); } else { super.documentChanged(e); @@ -175,11 +172,13 @@ public class LayeredLexerEditorHighlighter extends LexerEditorHighlighter { @Override public HighlighterIterator createIterator(int startOffset) { // do NOT synchronize before updateLayers due to deadlock with PsiLock - final boolean b = updateLayers(); + final boolean changed = updateLayers(); synchronized (this) { - if (b) { - setText(myText); + if (changed) { + CharSequence text = myText; + myText = null; + setText(text); } return new LayeredHighlighterIteratorImpl(startOffset); } diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LexerEditorHighlighter.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LexerEditorHighlighter.java index a31eacbdfc62..baaf4c21183a 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LexerEditorHighlighter.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LexerEditorHighlighter.java @@ -37,6 +37,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.psi.tree.IElementType; import com.intellij.util.ArrayUtil; +import com.intellij.util.text.ImmutableCharSequence; import com.intellij.util.text.ImmutableText; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; @@ -54,6 +55,7 @@ public class LexerEditorHighlighter implements EditorHighlighter, PrioritizedDoc private final SyntaxHighlighter myHighlighter; private EditorColorsScheme myScheme; private final int myInitialState; + protected CharSequence myText; public LexerEditorHighlighter(@NotNull SyntaxHighlighter highlighter, @NotNull EditorColorsScheme scheme) { myScheme = scheme; @@ -112,7 +114,7 @@ public class LexerEditorHighlighter implements EditorHighlighter, PrioritizedDoc if(document instanceof DocumentEx && ((DocumentEx)document).isInBulkUpdate()) { ((DocumentEx)document).setInBulkUpdate(false); // bulk mode failed } - doSetText(document.getCharsSequence()); + doSetText(document.getImmutableCharSequence()); } final int latestValidOffset = mySegments.getLastValidOffset(); @@ -147,18 +149,20 @@ public class LexerEditorHighlighter implements EditorHighlighter, PrioritizedDoc public synchronized void documentChanged(DocumentEvent e) { try { final Document document = e.getDocument(); + CharSequence text = document.getImmutableCharSequence(); if (document instanceof DocumentEx && ((DocumentEx)document).isInBulkUpdate()) { + myText = null; mySegments.removeAll(); return; } if(mySegments.getSegmentCount() == 0) { - setText(document.getCharsSequence()); + setText(text); return; } - CharSequence text = document.getCharsSequence(); + myText = text; int oldStartOffset = e.getOffset(); final int segmentIndex = mySegments.findSegmentIndex(oldStartOffset) - 2; @@ -312,6 +316,9 @@ public class LexerEditorHighlighter implements EditorHighlighter, PrioritizedDoc } private void doSetText(final CharSequence text) { + if (Comparing.equal(myText, text)) return; + myText = ImmutableCharSequence.asImmutable(text); + final TokenProcessor processor = createTokenProcessor(0); final int textLength = text.length(); myLexer.start(text, 0, textLength, myInitialState);