From 6defe872da2f2869cdda05f6906587a777b5ed19 Mon Sep 17 00:00:00 2001 From: krassowski Date: Sun, 14 Feb 2021 01:25:32 +0000 Subject: [PATCH 1/3] Prevent crash of the diagnostics panel when foreign documents are empty --- packages/jupyterlab-lsp/src/components/utils.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/jupyterlab-lsp/src/components/utils.tsx b/packages/jupyterlab-lsp/src/components/utils.tsx index a8460f554..0127cd65b 100644 --- a/packages/jupyterlab-lsp/src/components/utils.tsx +++ b/packages/jupyterlab-lsp/src/components/utils.tsx @@ -65,7 +65,11 @@ export function DocumentLocator(props: { let target: HTMLElement = null; if (adapter.has_multiple_editors) { let first_line = document.virtual_lines.get(0); - target = adapter.get_editor_wrapper(first_line.editor); + if (first_line) { + target = adapter.get_editor_wrapper(first_line.editor); + } else { + console.warn('Could not get first line of ', document); + } } let breadcrumbs = get_breadcrumbs(document, adapter); return ( From d84ecb5c123384de08e4e90782a12866169efeb9 Mon Sep 17 00:00:00 2001 From: krassowski Date: Sun, 14 Feb 2021 02:02:11 +0000 Subject: [PATCH 2/3] Fix diagnostics not being cleared upon foreign document closure --- atest/04_Interface/DiagnosticsPanel.robot | 18 ++++++++++++++++++ .../src/features/diagnostics/diagnostics.ts | 9 +++++++++ .../jupyterlab-lsp/src/virtual/document.ts | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/atest/04_Interface/DiagnosticsPanel.robot b/atest/04_Interface/DiagnosticsPanel.robot index 5217d272e..eed984caf 100644 --- a/atest/04_Interface/DiagnosticsPanel.robot +++ b/atest/04_Interface/DiagnosticsPanel.robot @@ -9,6 +9,8 @@ Test Teardown Clean Up ${EXPECTED_COUNT} 1 ${DIAGNOSTIC} W291 trailing whitespace (pycodestyle) ${DIAGNOSTIC MESSAGE} trailing whitespace +${DIAGNOSTIC MESSAGE R} Closing curly-braces should always be on their own line +${R CELL} %%R\n{} ${MENU COLUMNS} xpath://div[contains(@class, 'lm-Menu-itemLabel')][contains(text(), "columns")] ${LAB MENU} css:.lm-Menu @@ -78,6 +80,22 @@ Diagnostic Message Can Be Copied Close Diagnostics Panel Wait Until Element Contains css:.lsp-statusbar-item Successfully copied timeout=10s +Diagnostics Panel Works After Removing Foreign Document + Enter Cell Editor 2 + Lab Command Insert Cell Below + Enter Cell Editor 3 + Press Keys None ${R CELL} + Wait Until Keyword Succeeds 10 x 1s Element Should Contain ${DIAGNOSTICS PANEL} ${DIAGNOSTIC MESSAGE} + Wait Until Keyword Succeeds 10 x 1s Element Should Contain ${DIAGNOSTICS PANEL} ${DIAGNOSTIC MESSAGE R} + Lab Command Delete Cells + # regain focus by entering cell + Enter Cell Editor 2 + # trigger 7 document updates to trigger the garbage collector that removes unused documents + # (search for VirtualDocument.remainining_lifetime for more) + Press Keys None 1234567 + Wait Until Keyword Succeeds 10 x 1s Element Should Contain ${DIAGNOSTICS PANEL} ${DIAGNOSTIC MESSAGE} + Wait Until Keyword Succeeds 10 x 1s Element Should Not Contain ${DIAGNOSTICS PANEL} ${DIAGNOSTIC MESSAGE R} + *** Keywords *** Expand Menu Entry [Arguments] ${label} diff --git a/packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts b/packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts index cdcc1d06e..98c735de9 100644 --- a/packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts +++ b/packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts @@ -279,9 +279,18 @@ export class DiagnosticsCM extends CodeMirrorIntegration { this.adapter.adapterConnected.connect(() => this.switchDiagnosticsPanelSource() ); + this.virtual_document.foreign_document_closed.connect( + (document, context) => { + this.clearDocumentDiagnostics(context.foreign_document); + } + ); super.register(); } + clearDocumentDiagnostics(document: VirtualDocument) { + this.diagnostics_db.set(document, []); + } + private unique_editor_ids: DefaultMap; private marked_diagnostics: Map = new Map(); /** diff --git a/packages/jupyterlab-lsp/src/virtual/document.ts b/packages/jupyterlab-lsp/src/virtual/document.ts index 6d56cfd17..e0c9a3426 100644 --- a/packages/jupyterlab-lsp/src/virtual/document.ts +++ b/packages/jupyterlab-lsp/src/virtual/document.ts @@ -252,7 +252,7 @@ export class VirtualDocument { this.unused_standalone_documents = new DefaultMap( () => new Array() ); - this._remaining_lifetime = 10; + this._remaining_lifetime = 6; this.foreign_document_closed = new Signal(this); this.foreign_document_opened = new Signal(this); this.changed = new Signal(this); From 5fb736d26fb85ac55bb74c7cd04c2214221f5d18 Mon Sep 17 00:00:00 2001 From: krassowski Date: Sun, 14 Feb 2021 02:11:59 +0000 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c398c9480..cd1332a05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,10 @@ - bug fixes: - completer documentation will now consistently show up after filtering the completion items ([#520]) - completions containing HTML-like syntax will be displayed properly (an upstream issue) ([#520]) + - diagnostics panel will no longer break when foreign documents (e.g. `%%R` cell magics) are removed ([#522]) [#520]: https://github.com/krassowski/jupyterlab-lsp/pull/520 +[#522]: https://github.com/krassowski/jupyterlab-lsp/pull/522 ### `@krassowski/jupyterlab-lsp 3.3.1` (2020-02-07)