@@ -165,20 +165,8 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
165165 this . widget . context . saveState . disconnect ( this . on_save_state , this ) ;
166166 this . connection_manager . closed . disconnect ( this . on_connection_closed , this ) ;
167167 this . widget . disposed . disconnect ( this . dispose , this ) ;
168- this . widget . context . model . contentChanged . disconnect (
169- this . onContentChanged ,
170- this
171- ) ;
172168
173- for ( let adapter of this . adapters . values ( ) ) {
174- adapter . dispose ( ) ;
175- }
176- this . adapters . clear ( ) ;
177-
178- this . connection_manager . disconnect_document_signals (
179- this . virtual_editor . virtual_document
180- ) ;
181- this . virtual_editor . dispose ( ) ;
169+ this . disconnect ( ) ;
182170
183171 // just to be sure
184172 this . virtual_editor = null ;
@@ -222,6 +210,31 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
222210
223211 abstract get language_file_extension ( ) : string ;
224212
213+ disconnect ( ) {
214+ this . connection_manager . unregister_document (
215+ this . virtual_editor . virtual_document
216+ ) ;
217+ this . widget . context . model . contentChanged . disconnect (
218+ this . onContentChanged ,
219+ this
220+ ) ;
221+
222+ // pretend that all editors were removed to trigger the disconnection of even handlers
223+ // they will be connected again on new connection
224+ for ( let editor of this . editors ) {
225+ this . editorRemoved . emit ( {
226+ editor : editor
227+ } ) ;
228+ }
229+
230+ for ( let adapter of this . adapters . values ( ) ) {
231+ adapter . dispose ( ) ;
232+ }
233+ this . adapters . clear ( ) ;
234+
235+ this . virtual_editor . dispose ( ) ;
236+ }
237+
225238 // equivalent to triggering didClose and didOpen, as per syncing specification,
226239 // but also reloads the connection; used during file rename (or when it was moved)
227240 protected reload_connection ( ) {
@@ -231,15 +244,18 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
231244 }
232245
233246 // disconnect all existing connections (and dispose adapters)
234- this . connection_manager . unregister_document (
235- this . virtual_editor . virtual_document
236- ) ;
247+ this . disconnect ( ) ;
237248
238249 // recreate virtual document using current path and language
239- let virtual_document = this . create_virtual_document ( ) ;
240- this . virtual_editor . virtual_document = virtual_document ;
250+ // as virtual editor assumes it gets the virtual document at init,
251+ // just dispose virtual editor (which disposes virtual document too)
252+ // and re-initialize both virtual editor and document
253+ this . init_virtual ( ) ;
254+
241255 // reconnect
242- this . connect_document ( virtual_document , true ) . catch ( console . warn ) ;
256+ this . connect_document ( this . virtual_editor . virtual_document , true ) . catch (
257+ console . warn
258+ ) ;
243259 }
244260
245261 protected on_save_state ( context : any , state : DocumentRegistry . SaveState ) {
@@ -361,6 +377,7 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
361377 return ;
362378 }
363379 this . virtual_editor = virtual_editor ;
380+ this . connect_contentChanged_signal ( ) ;
364381 }
365382
366383 /**
@@ -507,7 +524,7 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
507524 * (range) updates this can be still implemented by comparison of before/after states of the
508525 * virtual documents, which is even more resilient and -obviously - editor-independent.
509526 */
510- connect_contentChanged_signal ( ) {
527+ private connect_contentChanged_signal ( ) {
511528 this . widget . context . model . contentChanged . connect (
512529 this . onContentChanged ,
513530 this
0 commit comments