Skip to content

Commit 1f171ab

Browse files
fix: Invoke colorizer when editor is cloned or splitted #827 (#870)
For new models the colorizer will be invoked after tokenization. For reused models (e.g. when splitting an editor) this must be done explicitly. --------- Co-authored-by: Sebastian Thomschke <sebthom@users.noreply.github.com>
1 parent 0bbd2a8 commit 1f171ab

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

org.eclipse.tm4e.ui.tests/src/main/java/org/eclipse/tm4e/ui/tests/TMinGenericEditorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.eclipse.tm4e.ui.tests.support.TestUtils;
2020
import org.eclipse.ui.IEditorDescriptor;
2121
import org.eclipse.ui.IEditorPart;
22+
import org.eclipse.ui.IWorkbenchPage;
2223
import org.eclipse.ui.ide.IDE;
2324
import org.junit.jupiter.api.AfterEach;
2425
import org.junit.jupiter.api.BeforeEach;
@@ -28,6 +29,7 @@ class TMinGenericEditorTest {
2829

2930
private IEditorDescriptor genericEditorDescr;
3031
private IEditorPart editor;
32+
private IEditorPart clonedEditor;
3133

3234
@BeforeEach
3335
public void setup() throws Exception {
@@ -40,6 +42,9 @@ public void tearDown() throws Exception {
4042
TestUtils.closeEditor(editor);
4143
editor = null;
4244

45+
TestUtils.closeEditor(clonedEditor);
46+
clonedEditor = null;
47+
4348
TestUtils.assertNoTM4EThreadsRunning();
4449
}
4550

@@ -72,6 +77,19 @@ void testTMHighlightInGenericEditorEdit() throws Exception {
7277
() -> text.getStyleRanges().length > initialNumberOfRanges + 3);
7378
}
7479

80+
@Test
81+
void testTMHighlightInClonedGenericEditor() throws Exception {
82+
final var f = TestUtils.createTempFile(".ts");
83+
try (var fileOutputStream = new FileOutputStream(f)) {
84+
fileOutputStream.write("let a = '';".getBytes());
85+
}
86+
editor = IDE.openEditor(UI.getActivePage(), f.toURI(), genericEditorDescr.getId(), true);
87+
clonedEditor = UI.getActivePage().openEditor(editor.getEditorInput(), genericEditorDescr.getId(), true, IWorkbenchPage.MATCH_NONE);
88+
89+
final var text = (StyledText) clonedEditor.getAdapter(Control.class);
90+
TestUtils.waitForAndAssertCondition(3_000, () -> text.getStyleRanges().length > 1);
91+
}
92+
7593
@Test
7694
void testReconcilierStartsAndDisposeThread() throws Exception {
7795
testTMHighlightInGenericEditor();

org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,23 @@ public void inputDocumentChanged(final @Nullable IDocument oldDoc, final @Nullab
217217
viewer.getTextWidget().getBackground().getRGB());
218218
}
219219

220-
TMPresentationReconciler.this.colorizer = new Colorizer(viewer, theme, listeners);
220+
final var colorizer = TMPresentationReconciler.this.colorizer = new Colorizer(viewer, theme, listeners);
221+
final var isModelReused = TMModelManager.INSTANCE.isConnected(newDoc);
221222

222223
// connect a TextMate model to the new document
223224
final var docModel = TMModelManager.INSTANCE.connect(newDoc);
224225
docModel.setGrammar(newDocGrammar);
225226
docModel.addModelTokensChangedListener(modelsTokensChangedListener);
227+
228+
// For new models the colorizer will be invoked after tokenization. For reused
229+
// models (e.g. when splitting an editor) this must be done explicitly.
230+
if (isModelReused) {
231+
try {
232+
colorizer.colorize(new Region(0, newDoc.getLength()), docModel);
233+
} catch (final BadLocationException ex) {
234+
TMUIPlugin.logError(ex);
235+
}
236+
}
226237
}
227238

228239
@Override

0 commit comments

Comments
 (0)