Skip to content

Commit 699e5e4

Browse files
committed
make docs, signatures more explicit about doc open lifecycle
1 parent 4e354ac commit 699e5e4

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
- ignores malformed diagnostic ranges, enabling markdown support ([#165][])
1212
- passes tests on Python 3.8 on Windows ([#165][])
1313

14+
- bug fixes
15+
16+
- reports files are open only after installing all handlers to avoid missing messages ([#201][])
17+
18+
[#201]: https://github.com/krassowski/jupyterlab-lsp/issues/201
19+
1420
### `lsp-ws-connection 0.4.0` (unreleased)
1521

1622
- breaking changes

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export class FileEditorAdapter extends JupyterLabWidgetAdapter {
7070
this.connect_contentChanged_signal();
7171

7272
console.log('LSP: file ready for connection:', this.path);
73+
74+
// connect the document, but do not open it as the adapter will handle this
75+
// after registering all features
7376
this.connect_document(this.virtual_editor.virtual_document).catch(
7477
console.warn
7578
);

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export abstract class JupyterLabWidgetAdapter
241241
// recreate virtual document using current path and language
242242
this.virtual_editor.create_virtual_document();
243243
// reconnect
244-
this.connect_document(this.virtual_editor.virtual_document).catch(
244+
this.connect_document(this.virtual_editor.virtual_document, true).catch(
245245
console.warn
246246
);
247247
}
@@ -287,36 +287,62 @@ export abstract class JupyterLabWidgetAdapter
287287
});
288288
}
289289

290-
protected async connect_document(virtual_document: VirtualDocument) {
290+
/**
291+
* Opens a connection for the document. The connection may or may
292+
* not be initialized, yet, and depending on when this is called, the client
293+
* may not be fully connected. In
294+
*
295+
* @param virtual_document a VirtualDocument
296+
* @param send_open whether to open the document immediately
297+
*/
298+
protected async connect_document(
299+
virtual_document: VirtualDocument,
300+
send_open = false
301+
): Promise<void> {
291302
virtual_document.changed.connect(this.document_changed, this);
292303

293304
virtual_document.foreign_document_opened.connect(
294305
this.on_foreign_document_opened,
295306
this
296307
);
297-
return await this.connect(virtual_document).catch(console.warn);
308+
309+
const connection_context = await this.connect(virtual_document).catch(
310+
console.warn
311+
);
312+
313+
if (!send_open) {
314+
return;
315+
}
316+
317+
if (connection_context && connection_context.connection) {
318+
connection_context.connection.sendOpenWhenReady(
319+
virtual_document.document_info
320+
);
321+
} else {
322+
console.warn(`Connection for ${virtual_document.path} was not opened`);
323+
}
298324
}
299325

326+
/**
327+
* Handler for opening a document contained in a larger document. The assumption
328+
* is that the editor already exists for this this, and as such the document
329+
* should be queued for immediate opening.
330+
*
331+
* @param host the VirtualDocument that contains the VirtualDocument in another language
332+
* @param context information about the foreign VirtualDocument
333+
*/
300334
protected async on_foreign_document_opened(
301335
host: VirtualDocument,
302336
context: IForeignContext
303337
) {
304338
const { foreign_document } = context;
305339

306-
const connection_context = await this.connect_document(foreign_document);
340+
await this.connect_document(foreign_document, true);
307341

308342
foreign_document.foreign_document_closed.connect(
309343
this.on_foreign_document_closed,
310344
this
311345
);
312-
313-
if (connection_context) {
314-
connection_context.connection.sendOpenWhenReady(
315-
foreign_document.document_info
316-
);
317-
} else {
318-
console.warn(`Connection for ${foreign_document.path} was not be opened`);
319-
}
320346
}
321347

322348
on_foreign_document_closed(host: VirtualDocument, context: IForeignContext) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export class NotebookAdapter extends JupyterLabWidgetAdapter {
158158
);
159159
this.connect_contentChanged_signal();
160160

161+
// connect the document, but do not open it as the adapter will handle this
162+
// after registering all features
161163
this.connect_document(this.virtual_editor.virtual_document).catch(
162164
console.warn
163165
);

0 commit comments

Comments
 (0)