Skip to content

Commit 40be17c

Browse files
authored
Merge pull request #486 from jtpio/backport-474
Backport #474 to 2.x
2 parents a86b20b + 60c9525 commit 40be17c

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

atest/04_Interface/Statusbar.robot

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ Statusbar Popup Opens
1919
Element Should Contain ${POPOVER} initialized
2020
[Teardown] Clean Up After Working With File Python.ipynb
2121

22+
Status Changes Between Notebooks
23+
Setup Notebook Python Python.ipynb
24+
Wait Until Fully Initialized
25+
Lab Command New Notebook
26+
Wait For Dialog
27+
# Kernel selection dialog shows up, accept Python as default kernel
28+
Accept Default Dialog Option
29+
Element Should Contain ${STATUSBAR} Waiting...
30+
Wait Until Fully Initialized
31+
Switch To Tab Python.ipynb
32+
Wait Until Fully Initialized
33+
[Teardown] Clean Up After Working With File Python.ipynb
34+
2235
Status Changes Correctly Between Editors
2336
Prepare File for Editing Python status example.py
2437
Wait Until Fully Initialized

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
413413

414414
// TODO only send the difference, using connection.sendSelectiveChange()
415415
let connection = this.connection_manager.connections.get(
416-
virtual_document.id_path
416+
virtual_document.uri
417417
);
418418
let adapter = this.adapters.get(virtual_document.id_path);
419419

@@ -596,7 +596,7 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
596596
);
597597
return {
598598
document,
599-
connection: this.connection_manager.connections.get(document.id_path),
599+
connection: this.connection_manager.connections.get(document.uri),
600600
virtual_position,
601601
root_position,
602602
features: this.get_features(document),

packages/jupyterlab-lsp/src/components/statusbar.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class LSPPopup extends VDomRenderer<LSPStatus.Model> {
128128
// TODO: add a config buttons next to the language header
129129
let list = documents.map((document, i) => {
130130
let connection = this.model.connection_manager.connections.get(
131-
document.id_path
131+
document.uri
132132
);
133133

134134
let status = '';
@@ -457,8 +457,8 @@ export namespace LSPStatus {
457457
// detected documents with LSP servers available
458458
let documents_with_servers = new Set<VirtualDocument>();
459459

460-
detected_documents.forEach((document, id_path) => {
461-
let connection = this._connection_manager.connections.get(id_path);
460+
detected_documents.forEach((document, uri) => {
461+
let connection = this._connection_manager.connections.get(uri);
462462
if (!connection) {
463463
absent_documents.add(document);
464464
return;
@@ -572,7 +572,7 @@ export namespace LSPStatus {
572572

573573
change_adapter(adapter: WidgetAdapter<IDocumentWidget> | null) {
574574
if (this._adapter != null) {
575-
this._adapter.status_message.changed.connect(this._onChange);
575+
this._adapter.status_message.changed.disconnect(this._onChange);
576576
}
577577

578578
if (adapter != null) {
@@ -586,6 +586,9 @@ export namespace LSPStatus {
586586
return this._connection_manager;
587587
}
588588

589+
/**
590+
* Note: it is ever only set once, as connection_manager is a singleton.
591+
*/
589592
set connection_manager(connection_manager) {
590593
if (this._connection_manager != null) {
591594
this._connection_manager.connected.disconnect(this._onChange);

packages/jupyterlab-lsp/src/connection_manager.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export interface ISocketConnectionOptions {
3232
}
3333

3434
/**
35-
* Each Widget with a document (whether file or a notebook) has its own DocumentConnectionManager
36-
* (see JupyterLabWidgetAdapter), keeping the virtual document spaces separate if a file is opened twice.
35+
* Each Widget with a document (whether file or a notebook) has the same DocumentConnectionManager
36+
* (see JupyterLabWidgetAdapter). Using id_path instead of uri led to documents being overwritten
37+
* as two identical id_paths could be created for two different notebooks.
3738
*/
3839
export class DocumentConnectionManager {
39-
connections: Map<VirtualDocument.id_path, LSPConnection>;
40-
documents: Map<VirtualDocument.id_path, VirtualDocument>;
40+
connections: Map<VirtualDocument.uri, LSPConnection>;
41+
documents: Map<VirtualDocument.uri, VirtualDocument>;
4142
initialized: Signal<DocumentConnectionManager, IDocumentConnectionData>;
4243
connected: Signal<DocumentConnectionManager, IDocumentConnectionData>;
4344
/**
@@ -53,7 +54,7 @@ export class DocumentConnectionManager {
5354
closed: Signal<DocumentConnectionManager, IDocumentConnectionData>;
5455
documents_changed: Signal<
5556
DocumentConnectionManager,
56-
Map<VirtualDocument.id_path, VirtualDocument>
57+
Map<VirtualDocument.uri, VirtualDocument>
5758
>;
5859
language_server_manager: ILanguageServerManager;
5960
initial_configurations: TLanguageServerConfigurations;
@@ -83,7 +84,7 @@ export class DocumentConnectionManager {
8384
this
8485
);
8586

86-
this.documents.set(virtual_document.id_path, virtual_document);
87+
this.documents.set(virtual_document.uri, virtual_document);
8788
this.documents_changed.emit(this.documents);
8889
}
8990

@@ -98,7 +99,7 @@ export class DocumentConnectionManager {
9899
this
99100
);
100101

101-
this.documents.delete(virtual_document.id_path);
102+
this.documents.delete(virtual_document.uri);
102103
for (const foreign of virtual_document.foreign_documents.values()) {
103104
this.disconnect_document_signals(foreign, false);
104105
}
@@ -111,7 +112,7 @@ export class DocumentConnectionManager {
111112
on_foreign_document_opened(_host: VirtualDocument, context: IForeignContext) {
112113
console.log(
113114
'LSP: ConnectionManager received foreign document: ',
114-
context.foreign_document.id_path
115+
context.foreign_document.uri
115116
);
116117
}
117118

@@ -149,7 +150,7 @@ export class DocumentConnectionManager {
149150

150151
// if connecting for the first time, all documents subsequent documents will
151152
// be re-opened and synced
152-
this.connections.set(virtual_document.id_path, connection);
153+
this.connections.set(virtual_document.uri, connection);
153154

154155
return connection;
155156
}
@@ -188,12 +189,12 @@ export class DocumentConnectionManager {
188189
if (error.message.indexOf('code = 1005') !== -1) {
189190
console.warn(`LSP: Connection failed for ${connection}`);
190191
this.forEachDocumentOfConnection(connection, virtual_document => {
191-
console.warn('LSP: disconnecting ' + virtual_document.id_path);
192+
console.warn('LSP: disconnecting ' + virtual_document.uri);
192193
this.closed.emit({ connection, virtual_document });
193194
this.ignored_languages.add(virtual_document.language);
194195

195196
console.warn(
196-
`Cancelling further attempts to connect ${virtual_document.id_path} and other documents for this language (no support from the server)`
197+
`Cancelling further attempts to connect ${virtual_document.uri} and other documents for this language (no support from the server)`
197198
);
198199
});
199200
} else if (error.message.indexOf('code = 1006') !== -1) {
@@ -230,13 +231,13 @@ export class DocumentConnectionManager {
230231
callback: (virtual_document: VirtualDocument) => void
231232
) {
232233
for (const [
233-
virtual_document_id_path,
234+
virtual_document_uri,
234235
a_connection
235236
] of this.connections.entries()) {
236237
if (connection !== a_connection) {
237238
continue;
238239
}
239-
callback(this.documents.get(virtual_document_id_path));
240+
callback(this.documents.get(virtual_document_uri));
240241
}
241242
}
242243

@@ -287,20 +288,20 @@ export class DocumentConnectionManager {
287288
try {
288289
await until_ready(() => connection.isReady, 200, 200);
289290
} catch {
290-
console.warn(`LSP: Connect timed out for ${virtual_document.id_path}`);
291+
console.warn(`LSP: Connect timed out for ${virtual_document.uri}`);
291292
return;
292293
}
293294
}
294295

295-
console.log('LSP:', document_path, virtual_document.id_path, 'connected.');
296+
console.log('LSP:', document_path, virtual_document.uri, 'connected.');
296297

297298
this.connected.emit({ connection, virtual_document });
298299

299300
return connection;
300301
}
301302

302303
public unregister_document(virtual_document: VirtualDocument) {
303-
this.connections.delete(virtual_document.id_path);
304+
this.connections.delete(virtual_document.uri);
304305
this.documents_changed.emit(this.documents);
305306
}
306307
}

packages/jupyterlab-lsp/src/features/completion/completion_handler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class LSPConnector
4040
implements CompletionHandler.ICompletionItemsConnector {
4141
isDisposed = false;
4242
private _editor: CodeEditor.IEditor;
43-
private _connections: Map<VirtualDocument.id_path, LSPConnection>;
43+
private _connections: Map<VirtualDocument.uri, LSPConnection>;
4444
private _context_connector: ContextConnector;
4545
private _kernel_connector: KernelConnector;
4646
private _kernel_and_context_connector: CompletionConnector;
@@ -220,7 +220,7 @@ export class LSPConnector
220220
document: VirtualDocument,
221221
position_in_token: number
222222
): Promise<CompletionHandler.ICompletionItemsReply> {
223-
let connection = this._connections.get(document.id_path);
223+
let connection = this._connections.get(document.uri);
224224

225225
console.log('[LSP][Completer] Fetching and Transforming');
226226
console.log('[LSP][Completer] Token:', token, start, end);

packages/jupyterlab-lsp/src/virtual/document.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ export class VirtualDocument {
734734
return this.parent.id_path + '-' + this.virtual_id;
735735
}
736736

737-
get uri(): string {
737+
get uri(): VirtualDocument.uri {
738738
const encodedPath = encodeURI(this.path);
739739
if (!this.parent) {
740740
return encodedPath;
@@ -816,6 +816,10 @@ export namespace VirtualDocument {
816816
* for documents which should be interpreted as one when stretched across cells.
817817
*/
818818
export type virtual_id = string;
819+
/**
820+
* Identifier composed of the file path and id_path.
821+
*/
822+
export type uri = string;
819823
}
820824

821825
export function collect_documents(

0 commit comments

Comments
 (0)