@@ -269,6 +269,14 @@ export class MainThreadTextEditor {
269269 }
270270 } ) ) ;
271271
272+ const isValidCodeEditor = ( ) => {
273+ // Due to event timings, it is possible that there is a model change event not yet delivered to us.
274+ // > e.g. a model change event is emitted to a listener which then decides to update editor options
275+ // > In this case the editor configuration change event reaches us first.
276+ // So simply check that the model is still attached to this code editor
277+ return ( this . _codeEditor && this . _codeEditor . getModel ( ) === this . _model ) ;
278+ } ;
279+
272280 const updateProperties = ( selectionChangeSource : string | null ) => {
273281 // Some editor events get delivered faster than model content changes. This is
274282 // problematic, as this leads to editor properties reaching the extension host
@@ -287,18 +295,30 @@ export class MainThreadTextEditor {
287295
288296 this . _codeEditorListeners . add ( this . _codeEditor . onDidChangeCursorSelection ( ( e ) => {
289297 // selection
298+ if ( ! isValidCodeEditor ( ) ) {
299+ return ;
300+ }
290301 updateProperties ( e . source ) ;
291302 } ) ) ;
292- this . _codeEditorListeners . add ( this . _codeEditor . onDidChangeConfiguration ( ( ) => {
303+ this . _codeEditorListeners . add ( this . _codeEditor . onDidChangeConfiguration ( ( e ) => {
293304 // options
305+ if ( ! isValidCodeEditor ( ) ) {
306+ return ;
307+ }
294308 updateProperties ( null ) ;
295309 } ) ) ;
296310 this . _codeEditorListeners . add ( this . _codeEditor . onDidLayoutChange ( ( ) => {
297311 // visibleRanges
312+ if ( ! isValidCodeEditor ( ) ) {
313+ return ;
314+ }
298315 updateProperties ( null ) ;
299316 } ) ) ;
300317 this . _codeEditorListeners . add ( this . _codeEditor . onDidScrollChange ( ( ) => {
301318 // visibleRanges
319+ if ( ! isValidCodeEditor ( ) ) {
320+ return ;
321+ }
302322 updateProperties ( null ) ;
303323 } ) ) ;
304324 this . _updatePropertiesNow ( null ) ;
0 commit comments