Skip to content

Commit

Permalink
Invoke colorizer when editor is cloned or splitted eclipse-tm4e#827
Browse files Browse the repository at this point in the history
For new models the colorizer will be invoked after tokenization. For
reused models (e.g. when splitting an editor) this must be done
explicitly.
  • Loading branch information
oleosterhagen committed Mar 2, 2025
1 parent 0bbd2a8 commit 27e6c7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.tm4e.ui.tests.support.TestUtils;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.IDE;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,6 +29,7 @@ class TMinGenericEditorTest {

private IEditorDescriptor genericEditorDescr;
private IEditorPart editor;
private IEditorPart clonedEditor;

@BeforeEach
public void setup() throws Exception {
Expand All @@ -40,6 +42,9 @@ public void tearDown() throws Exception {
TestUtils.closeEditor(editor);
editor = null;

TestUtils.closeEditor(clonedEditor);
clonedEditor = null;

TestUtils.assertNoTM4EThreadsRunning();
}

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

@Test
void testTMHighlightInClonedGenericEditor() throws Exception {
final var f = TestUtils.createTempFile(".ts");
try (var fileOutputStream = new FileOutputStream(f)) {
fileOutputStream.write("let a = '';".getBytes());
}
editor = IDE.openEditor(UI.getActivePage(), f.toURI(), genericEditorDescr.getId(), true);
clonedEditor = UI.getActivePage().openEditor(editor.getEditorInput(), genericEditorDescr.getId(), true, IWorkbenchPage.MATCH_NONE);

final var text = (StyledText) clonedEditor.getAdapter(Control.class);
TestUtils.waitForAndAssertCondition(3_000, () -> text.getStyleRanges().length > 1);
}

@Test
void testReconcilierStartsAndDisposeThread() throws Exception {
testTMHighlightInGenericEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,22 @@ public void inputDocumentChanged(final @Nullable IDocument oldDoc, final @Nullab
}

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

// connect a TextMate model to the new document
final var docModel = TMModelManager.INSTANCE.connect(newDoc);
docModel.setGrammar(newDocGrammar);
docModel.addModelTokensChangedListener(modelsTokensChangedListener);

// For new models the colorizer will be invoked after tokenization. For reused
// models (e.g. when splitting an editor) this must be done explicitly.
if (colorizer != null && isModelReused) {
try {
colorizer.colorize(new Region(0, newDoc.getLength()), docModel);
} catch (final BadLocationException ex) {
TMUIPlugin.logError(ex);
}
}
}

@Override
Expand Down

0 comments on commit 27e6c7f

Please sign in to comment.