From 09d52e2e456dfa7be6eb4dfe80737e52c588395d Mon Sep 17 00:00:00 2001 From: Nicholas Bollweg Date: Mon, 27 Jul 2020 15:55:23 -0400 Subject: [PATCH 1/3] #299: stop using throw --- docs/Installation.ipynb | 11 +++++++---- .../src/adapters/codemirror/features/hover.ts | 2 +- packages/jupyterlab-lsp/src/command_manager.ts | 9 +++++++-- packages/jupyterlab-lsp/src/manager.ts | 3 ++- .../jupyterlab-lsp/src/virtual/editors/notebook.ts | 7 ++++--- .../src/server-capability-registration.ts | 5 +++-- packages/lsp-ws-connection/src/ws-connection.ts | 12 ++++++++---- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/docs/Installation.ipynb b/docs/Installation.ipynb index 97c1e0768..ef53063b8 100644 --- a/docs/Installation.ipynb +++ b/docs/Installation.ipynb @@ -8,16 +8,19 @@ }, "outputs": [], "source": [ - "from markdown import markdown\n", "import sys\n", - "sys.path.insert(0, '..')\n", + "\n", + "from markdown import markdown\n", + "\n", "from versions import (\n", " JUPYTER_LSP_VERSION,\n", " JUPYTERLAB_LSP_VERSION,\n", " JUPYTERLAB_NEXT_MAJOR_VERSION,\n", " JUPYTERLAB_VERSION,\n", - " REQUIRED_JUPYTERLAB\n", - ")" + " REQUIRED_JUPYTERLAB,\n", + ")\n", + "\n", + "sys.path.insert(0, \"..\")" ] }, { diff --git a/packages/jupyterlab-lsp/src/adapters/codemirror/features/hover.ts b/packages/jupyterlab-lsp/src/adapters/codemirror/features/hover.ts index 536056797..05e6324fe 100644 --- a/packages/jupyterlab-lsp/src/adapters/codemirror/features/hover.ts +++ b/packages/jupyterlab-lsp/src/adapters/codemirror/features/hover.ts @@ -201,7 +201,7 @@ export class Hover extends CodeMirrorLSPFeature { e.message === "Cannot read property 'string' of undefined" ) ) { - throw e; + console.warn(e); } } }; diff --git a/packages/jupyterlab-lsp/src/command_manager.ts b/packages/jupyterlab-lsp/src/command_manager.ts index 69e7d11e1..07e41ef03 100644 --- a/packages/jupyterlab-lsp/src/command_manager.ts +++ b/packages/jupyterlab-lsp/src/command_manager.ts @@ -201,7 +201,7 @@ export class NotebookCommandManager extends ContextCommandManager { return notebook_adapters.get(notebook.id); } - context_from_active_document(): ICommandContext { + context_from_active_document(): ICommandContext | null { if (!this.is_widget_current) { return null; } @@ -213,7 +213,7 @@ export class NotebookCommandManager extends ContextCommandManager { let virtual_editor = this.current_adapter?.virtual_editor; - if (!virtual_editor) { + if (virtual_editor == null) { return null; } @@ -222,6 +222,11 @@ export class NotebookCommandManager extends ContextCommandManager { cm_cursor ); + if (root_position == null) { + console.warn('Could not retrieve current context', virtual_editor); + return null; + } + return this.current_adapter?.get_context(root_position); } } diff --git a/packages/jupyterlab-lsp/src/manager.ts b/packages/jupyterlab-lsp/src/manager.ts index 68028af48..ade479059 100644 --- a/packages/jupyterlab-lsp/src/manager.ts +++ b/packages/jupyterlab-lsp/src/manager.ts @@ -53,7 +53,8 @@ export class LanguageServerManager implements ILanguageServerManager { ); if (!response.ok) { - throw new Error(response.statusText); + console.error('Could not fetch sessions', response); + return; } let sessions: SCHEMA.Sessions; diff --git a/packages/jupyterlab-lsp/src/virtual/editors/notebook.ts b/packages/jupyterlab-lsp/src/virtual/editors/notebook.ts index 00578410c..fdcb00f83 100644 --- a/packages/jupyterlab-lsp/src/virtual/editors/notebook.ts +++ b/packages/jupyterlab-lsp/src/virtual/editors/notebook.ts @@ -118,11 +118,12 @@ export class VirtualEditorForNotebook extends VirtualEditor { transform_from_notebook_to_root( cell: Cell, position: IEditorPosition - ): IRootPosition { + ): IRootPosition | null { // TODO: if cell is not known, refresh let shift = this.cell_to_corresponding_source_line.get(cell); - if (shift === undefined) { - throw Error('Cell not found in cell_line_map'); + if (shift == null) { + console.warn('Cell not found in cell_line_map'); + return null; } return { ...(position as CodeMirror.Position), diff --git a/packages/lsp-ws-connection/src/server-capability-registration.ts b/packages/lsp-ws-connection/src/server-capability-registration.ts index 12d158634..765612cc3 100644 --- a/packages/lsp-ws-connection/src/server-capability-registration.ts +++ b/packages/lsp-ws-connection/src/server-capability-registration.ts @@ -11,7 +11,7 @@ interface IFlexibleServerCapabilities extends ServerCapabilities { function registerServerCapability( serverCapabilities: ServerCapabilities, registration: Registration -): ServerCapabilities { +): ServerCapabilities | null { const serverCapabilitiesCopy = JSON.parse( JSON.stringify(serverCapabilities) ) as IFlexibleServerCapabilities; @@ -27,7 +27,8 @@ function registerServerCapability( ); } } else { - throw new Error('Could not register server capability.'); + console.warn('Could not register server capability.', registration); + return null; } return serverCapabilitiesCopy; diff --git a/packages/lsp-ws-connection/src/ws-connection.ts b/packages/lsp-ws-connection/src/ws-connection.ts index 24acea4eb..cd3ff9b99 100644 --- a/packages/lsp-ws-connection/src/ws-connection.ts +++ b/packages/lsp-ws-connection/src/ws-connection.ts @@ -79,10 +79,14 @@ export class LspWsConnection extends events.EventEmitter (params: protocol.RegistrationParams) => { params.registrations.forEach( (capabilityRegistration: protocol.Registration) => { - this.serverCapabilities = registerServerCapability( - this.serverCapabilities, - capabilityRegistration - ); + try { + this.serverCapabilities = registerServerCapability( + this.serverCapabilities, + capabilityRegistration + ); + } catch (err) { + console.error(err); + } } ); From 3208570a9217c9feba6b3bc9df8f1cc094a128f6 Mon Sep 17 00:00:00 2001 From: Nicholas Bollweg Date: Mon, 27 Jul 2020 16:01:04 -0400 Subject: [PATCH 2/3] roll back lint on notebook --- docs/Installation.ipynb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/Installation.ipynb b/docs/Installation.ipynb index ef53063b8..97c1e0768 100644 --- a/docs/Installation.ipynb +++ b/docs/Installation.ipynb @@ -8,19 +8,16 @@ }, "outputs": [], "source": [ - "import sys\n", - "\n", "from markdown import markdown\n", - "\n", + "import sys\n", + "sys.path.insert(0, '..')\n", "from versions import (\n", " JUPYTER_LSP_VERSION,\n", " JUPYTERLAB_LSP_VERSION,\n", " JUPYTERLAB_NEXT_MAJOR_VERSION,\n", " JUPYTERLAB_VERSION,\n", - " REQUIRED_JUPYTERLAB,\n", - ")\n", - "\n", - "sys.path.insert(0, \"..\")" + " REQUIRED_JUPYTERLAB\n", + ")" ] }, { From 2b8f8b036075e7bacfb12865dcd49c0297214ab5 Mon Sep 17 00:00:00 2001 From: Nicholas Bollweg Date: Mon, 27 Jul 2020 20:59:20 -0400 Subject: [PATCH 3/3] update changelog, linting --- CHANGELOG.md | 9 +++++++++ docs/Installation.ipynb | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30ccc4ee6..1a1ccb4ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ ## CHANGELOG +### `@krassowski/jupyterlab-lsp 1.1.1` (unreleased) + +- bug fixes + + - emits console warnings instead of throwing errors in hover handlers and connections ([#299][], [#300][]) + +[#299]: https://github.com/krassowski/jupyterlab-lsp/pull/299 +[#300]: https://github.com/krassowski/jupyterlab-lsp/pull/300 + ### `@krassowski/jupyterlab-lsp 1.1.0` (2020-07-20) - features diff --git a/docs/Installation.ipynb b/docs/Installation.ipynb index 97c1e0768..52b59f7e4 100644 --- a/docs/Installation.ipynb +++ b/docs/Installation.ipynb @@ -8,15 +8,27 @@ }, "outputs": [], "source": [ - "from markdown import markdown\n", "import sys\n", - "sys.path.insert(0, '..')\n", + "\n", + "from markdown import markdown\n", + "\n", + "sys.path.insert(0, \"..\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "hide_input": true + }, + "outputs": [], + "source": [ "from versions import (\n", " JUPYTER_LSP_VERSION,\n", " JUPYTERLAB_LSP_VERSION,\n", " JUPYTERLAB_NEXT_MAJOR_VERSION,\n", " JUPYTERLAB_VERSION,\n", - " REQUIRED_JUPYTERLAB\n", + " REQUIRED_JUPYTERLAB,\n", ")" ] }, @@ -285,7 +297,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.7.6" } }, "nbformat": 4,