Skip to content

Commit 832c7b5

Browse files
committed
hoist pathChanged concern to plugin level
1 parent 36aa419 commit 832c7b5

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

packages/jupyterlab-lsp/src/adapters/codemirror/feature.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export abstract class CodeMirrorLSPFeature implements ILSPFeature {
148148
for (let [event_name, handler] of this.wrapper_handlers) {
149149
this.wrapper.addEventListener(event_name, handler);
150150
}
151+
151152
this.is_registered = true;
152153
}
153154

packages/jupyterlab-lsp/src/adapters/codemirror/features/diagnostics_listing.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ export class DiagnosticsListing extends VDomRenderer<DiagnosticsListing.Model> {
270270

271271
let by_document = Array.from(diagnostics_db).map(
272272
([virtual_document, diagnostics]) => {
273+
if (virtual_document.isDisposed) {
274+
return [];
275+
}
273276
return diagnostics.map((diagnostic_data, i) => {
274277
let cell_number: number = null;
275278
if (editor.has_cells) {

packages/jupyterlab-lsp/src/adapters/jupyterlab/jl_adapter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ export abstract class JupyterLabWidgetAdapter
133133
this.connection_manager = connection_manager;
134134

135135
// set up signal connections
136-
this.widget.context.pathChanged.connect(this.reload_connection, this);
137136
this.widget.context.saveState.connect(this.on_save_state, this);
138137
this.connection_manager.closed.connect(this.on_connection_closed, this);
139138
this.document_connected.connect(this.connect_completion, this);
@@ -162,7 +161,6 @@ export abstract class JupyterLabWidgetAdapter
162161
this.disconnect_adapter(this.virtual_editor?.virtual_document);
163162
}
164163

165-
this.widget.context.pathChanged.disconnect(this.reload_connection, this);
166164
this.widget.context.saveState.disconnect(this.on_save_state, this);
167165
this.connection_manager.closed.disconnect(this.on_connection_closed, this);
168166
this.document_connected.disconnect(this.connect_completion, this);
@@ -181,7 +179,7 @@ export abstract class JupyterLabWidgetAdapter
181179
);
182180
this.virtual_editor.dispose();
183181

184-
this.current_completion_connector.dispose();
182+
this.current_completion_connector?.dispose();
185183

186184
// just to be sure
187185
this.virtual_editor = null;
@@ -239,6 +237,7 @@ export abstract class JupyterLabWidgetAdapter
239237
this.connection_manager.unregister_document(
240238
this.virtual_editor.virtual_document
241239
);
240+
242241
// recreate virtual document using current path and language
243242
this.virtual_editor.create_virtual_document();
244243
// reconnect

packages/jupyterlab-lsp/src/command_manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class NotebookCommandManager extends ContextCommandManager {
222222
cm_cursor
223223
);
224224

225-
return this.current_adapter.get_context(root_position);
225+
return this.current_adapter?.get_context(root_position);
226226
}
227227
}
228228

@@ -243,7 +243,7 @@ export class FileEditorCommandManager extends ContextCommandManager {
243243
let editor = this.tracker.currentWidget.content.editor;
244244
let ce_cursor = editor.getCursorPosition();
245245
let root_position = PositionConverter.ce_to_cm(ce_cursor) as IRootPosition;
246-
return this.current_adapter.get_context(root_position);
246+
return this.current_adapter?.get_context(root_position);
247247
}
248248
}
249249

packages/jupyterlab-lsp/src/index.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ import {
3333
import IPaths = JupyterFrontEnd.IPaths;
3434
import { IStatusBar } from '@jupyterlab/statusbar';
3535
import { LSPStatus } from './adapters/jupyterlab/components/statusbar';
36-
import { IDocumentWidget } from '@jupyterlab/docregistry/lib/registry';
36+
import {
37+
IDocumentWidget,
38+
DocumentRegistry
39+
} from '@jupyterlab/docregistry/lib/registry';
3740
import { DocumentConnectionManager } from './connection_manager';
3841

3942
const lsp_commands: Array<IFeatureCommand> = [].concat(
@@ -116,7 +119,9 @@ const plugin: JupyterFrontEndPlugin<void> = {
116119
// connection.close();
117120
});
118121

119-
fileEditorTracker.widgetAdded.connect((sender, widget) => {
122+
const connect_file_editor = (
123+
widget: IDocumentWidget<FileEditor, DocumentRegistry.IModel>
124+
) => {
120125
let fileEditor = widget.content;
121126

122127
if (fileEditor.editor instanceof CodeMirrorEditor) {
@@ -131,17 +136,30 @@ const plugin: JupyterFrontEndPlugin<void> = {
131136
);
132137
file_editor_adapters.set(fileEditor.id, adapter);
133138

134-
file_editor_adapters.set(widget.id, adapter);
135139
const disconnect = () => {
136-
file_editor_adapters.delete(widget.id);
140+
file_editor_adapters.delete(fileEditor.id);
137141
widget.disposed.disconnect(disconnect);
142+
widget.context.pathChanged.disconnect(reconnect);
138143
adapter.dispose();
139144
if (status_bar_item.model.adapter === adapter) {
140145
status_bar_item.model.adapter = null;
141146
}
142147
};
148+
149+
const reconnect = () => {
150+
disconnect();
151+
connect_file_editor(widget);
152+
};
153+
143154
widget.disposed.connect(disconnect);
155+
widget.context.pathChanged.connect(reconnect);
156+
157+
status_bar_item.model.adapter = adapter;
144158
}
159+
};
160+
161+
fileEditorTracker.widgetAdded.connect((sender, widget) => {
162+
connect_file_editor(widget);
145163
});
146164

147165
let command_manager = new FileEditorCommandManager(
@@ -152,7 +170,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
152170
);
153171
command_manager.add(lsp_commands);
154172

155-
notebookTracker.widgetAdded.connect(async (sender, widget) => {
173+
const connect_notebook = (widget: NotebookPanel) => {
156174
// NOTE: assuming that the default cells content factory produces CodeMirror editors(!)
157175
let jumper = new NotebookJumper(widget, documentManager);
158176
let adapter = new NotebookAdapter(
@@ -164,15 +182,30 @@ const plugin: JupyterFrontEndPlugin<void> = {
164182
connection_manager
165183
);
166184
notebook_adapters.set(widget.id, adapter);
185+
167186
const disconnect = () => {
168187
notebook_adapters.delete(widget.id);
169188
widget.disposed.disconnect(disconnect);
189+
widget.context.pathChanged.disconnect(reconnect);
170190
adapter.dispose();
171191
if (status_bar_item.model.adapter === adapter) {
172192
status_bar_item.model.adapter = null;
173193
}
174194
};
195+
196+
const reconnect = () => {
197+
disconnect();
198+
connect_notebook(widget);
199+
};
200+
201+
widget.context.pathChanged.connect(reconnect);
175202
widget.disposed.connect(disconnect);
203+
204+
status_bar_item.model.adapter = adapter;
205+
};
206+
207+
notebookTracker.widgetAdded.connect(async (sender, widget) => {
208+
connect_notebook(widget);
176209
});
177210

178211
// position context menu entries after 10th but before 11th default entry

0 commit comments

Comments
 (0)