UNPKG

@jupyter-lsp/jupyterlab-lsp

Version:

Language Server Protocol integration for JupyterLab

82 lines (80 loc) 3.17 kB
import { ILSPCodeExtractorsManager, ILSPFeatureManager, ILSPDocumentConnectionManager } from '@jupyterlab/lsp'; import { NotebookAdapter as UpstreamNotebookAdapter, INotebookTracker } from '@jupyterlab/notebook'; import { ILSPCodeOverridesManager } from '../overrides/tokens'; import { VirtualDocument } from '../virtual/document'; export class NotebookAdapter extends UpstreamNotebookAdapter { constructor(editorWidget, options) { super(editorWidget, options); this.editorWidget = editorWidget; this.options = options; this._blockUpdates = false; } /** * Generate the virtual document associated with the document. */ createVirtualDocument() { return new VirtualDocument({ language: this.language, foreignCodeExtractors: this.options.foreignCodeExtractorsManager, path: this.documentPath, fileExtension: this.languageFileExtension, // notebooks are continuous, each cell is dependent on the previous one standalone: false, // notebooks are not supported by LSP servers hasLspSupportedFile: false, overridesRegistry: this.options.codeOverridesManager.registry }); } async onForeignDocumentOpened(virtualDocument, context) { /* Opening a standalone foreign document can result in an infinite loop, as a new connection gets opened for these, and once that is ready `updateDocuments()` gets called. To avoid the problem, `updateDocuments()` gets suppressed for standalone documents. It does not affect non-standalone documents, because no new connection gets opened for these. https://github.com/jupyter-lsp/jupyterlab-lsp/issues/959 */ try { this._blockUpdates = true; await super.onForeignDocumentOpened(virtualDocument, context); } finally { this._blockUpdates = false; } } updateDocuments() { if (this._blockUpdates) { return Promise.resolve(); } return super.updateDocuments(); } } /** * Activate the language server for notebook. */ function activateNotebookLanguageServer(app, notebooks, connectionManager, featureManager, codeExtractorManager, codeOverridesManager) { notebooks.widgetAdded.connect(async (_, notebook) => { const adapter = new NotebookAdapter(notebook, { connectionManager, featureManager, foreignCodeExtractorsManager: codeExtractorManager, codeOverridesManager }); connectionManager.registerAdapter(notebook.context.path, adapter); }); } export const NOTEBOOK_ADAPTER_PLUGIN = { id: '@jupyter-lsp/notebook-adapter', description: 'Adds language server capability to the notebooks.', requires: [ INotebookTracker, ILSPDocumentConnectionManager, ILSPFeatureManager, ILSPCodeExtractorsManager, ILSPCodeOverridesManager ], activate: activateNotebookLanguageServer, autoStart: true }; //# sourceMappingURL=notebook.js.map