4444import org .eclipse .jface .text .rules .IToken ;
4545import org .eclipse .jface .text .source .SourceViewer ;
4646import org .eclipse .swt .custom .StyleRange ;
47+ import org .eclipse .swt .custom .StyledText ;
4748import org .eclipse .swt .widgets .Control ;
4849import org .eclipse .tm4e .core .TMException ;
4950import org .eclipse .tm4e .core .grammar .IGrammar ;
@@ -110,7 +111,7 @@ public class TMPresentationReconciler implements IPresentationReconciler {
110111 final var colorizer = TMPresentationReconciler .this .colorizer ;
111112 if (colorizer != null ) {
112113 final Control control = colorizer .getTextViewer ().getTextWidget ();
113- if (control != null ) {
114+ if (control != null && ! control . isDisposed () ) {
114115 control .getDisplay ().asyncExec (() -> colorizer .colorize (event ));
115116 }
116117 }
@@ -159,6 +160,8 @@ public TMPresentationReconciler() {
159160 }
160161
161162 private final class TextViewerListener implements ITextInputListener , ITextListener {
163+ private static final Region EMPTY_REGION = new Region (0 , 0 );
164+
162165 @ Override
163166 public void inputDocumentAboutToBeChanged (final @ Nullable IDocument oldDoc , final @ Nullable IDocument newDoc ) {
164167 if (oldDoc == null )
@@ -248,12 +251,15 @@ public void textChanged(final TextEvent event) {
248251 if (diff == 0 || event .getOffset () <= 0 )
249252 return ;
250253
251- final StyleRange range = viewer .getTextWidget ().getStyleRangeAtOffset (event .getOffset () - 1 );
254+ final StyledText widget = viewer .getTextWidget ();
255+ if (widget .isDisposed ())
256+ return ;
257+ final StyleRange range = widget .getStyleRangeAtOffset (event .getOffset () - 1 );
252258 if (range == null )
253259 return ;
254260
255261 range .length = Math .max (0 , range .length + diff );
256- viewer . getTextWidget () .setStyleRange (range );
262+ widget .setStyleRange (range );
257263 return ;
258264 }
259265
@@ -263,6 +269,9 @@ public void textChanged(final TextEvent event) {
263269 return ;
264270
265271 final IRegion region = computeRegionToRedraw (event , doc );
272+ if (region .getLength () == 0 )
273+ return ;
274+
266275 final var colorizer = TMPresentationReconciler .this .colorizer ;
267276 if (colorizer != null ) {
268277 // case where there is grammar & theme -> update text presentation with the grammar tokens
@@ -292,11 +301,17 @@ public void textChanged(final TextEvent event) {
292301 }
293302
294303 IRegion computeRegionToRedraw (final TextEvent event , final IDocument doc ) {
295- final IRegion region = event .getOffset () == 0 && event .getLength () == 0 && event .getText () == null
296- ? new Region (0 , doc .getLength ()) // redraw state change, damage the whole document
297- : getRegionOfTextEvent (event );
304+ // Fast path: check for redraw state change first
305+ if (event .getOffset () == 0 && event .getLength () == 0 && event .getText () == null ) {
306+ // redraw state change, damage the whole document
307+ final int docLength = doc .getLength ();
308+ return docLength > 0 ? new Region (0 , docLength ) : EMPTY_REGION ;
309+ }
310+
311+ // Normal text event processing
312+ final IRegion region = getRegionOfTextEvent (event );
298313 return region == null || region .getLength () == 0
299- ? new Region ( 0 , 0 )
314+ ? EMPTY_REGION
300315 : region ;
301316 }
302317
@@ -354,8 +369,9 @@ public void setGrammar(final @Nullable IGrammar newGrammar) {
354369 if (Objects .equals (newGrammar , this .grammar ))
355370 return ;
356371
357- if (newGrammar == null )
372+ if (newGrammar == null ) {
358373 colorizer = null ;
374+ }
359375
360376 this .grammar = newGrammar ;
361377
0 commit comments