From 5eb345bce8ddd6c5f895f17d82332c773de8ed75 Mon Sep 17 00:00:00 2001 From: krassowski Date: Tue, 24 Sep 2019 22:38:08 +0100 Subject: [PATCH] Add a temporary workaround for absolute paths using directories provided by the server --- src/adapters/jupyterlab/file_editor.ts | 11 +++++++-- src/adapters/jupyterlab/jl_adapter.ts | 22 ++++++++---------- src/adapters/jupyterlab/notebook.ts | 11 +++++++-- src/command_manager.ts | 1 + src/index.ts | 31 ++++++++++++++++++++++---- src/virtual/document.ts | 3 +++ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/adapters/jupyterlab/file_editor.ts b/src/adapters/jupyterlab/file_editor.ts index c46215507..b3af69d21 100644 --- a/src/adapters/jupyterlab/file_editor.ts +++ b/src/adapters/jupyterlab/file_editor.ts @@ -49,9 +49,16 @@ export class FileEditorAdapter extends JupyterLabWidgetAdapter { jumper: FileEditorJumper, app: JupyterFrontEnd, completion_manager: ICompletionManager, - rendermime_registry: IRenderMimeRegistry + rendermime_registry: IRenderMimeRegistry, + server_root: string ) { - super(app, editor_widget, rendermime_registry, 'completer:invoke-file'); + super( + app, + editor_widget, + rendermime_registry, + 'completer:invoke-file', + server_root + ); this.jumper = jumper; this.editor = editor_widget.content; diff --git a/src/adapters/jupyterlab/jl_adapter.ts b/src/adapters/jupyterlab/jl_adapter.ts index af7728cd3..3e1f4efaa 100644 --- a/src/adapters/jupyterlab/jl_adapter.ts +++ b/src/adapters/jupyterlab/jl_adapter.ts @@ -68,13 +68,10 @@ const mime_type_language_map: JSONObject = { */ export abstract class JupyterLabWidgetAdapter implements IJupyterLabComponentsManager { - app: JupyterFrontEnd; connections: Map; documents: Map; jumper: CodeJumper; protected adapters: Map; - protected rendermime_registry: IRenderMimeRegistry; - widget: IDocumentWidget; private readonly invoke_command: string; protected document_connected: Signal< JupyterLabWidgetAdapter, @@ -85,25 +82,22 @@ export abstract class JupyterLabWidgetAdapter private _tooltip: FreeTooltip; protected constructor( - app: JupyterFrontEnd, - widget: IDocumentWidget, - rendermime_registry: IRenderMimeRegistry, - invoke: string + protected app: JupyterFrontEnd, + protected widget: IDocumentWidget, + protected rendermime_registry: IRenderMimeRegistry, + invoke: string, + private server_root: string ) { - this.app = app; - this.rendermime_registry = rendermime_registry; this.invoke_command = invoke; this.connections = new Map(); this.documents = new Map(); this.document_connected = new Signal(this); this.adapters = new Map(); this.ignored_languages = new Set(); - this.widget = widget; } abstract virtual_editor: VirtualEditor; abstract get document_path(): string; - abstract get mime_type(): string; get language(): string { @@ -317,8 +311,10 @@ export abstract class JupyterLabWidgetAdapter serverUri: 'ws://jupyter-lsp/' + language, languageId: language, // paths handling needs testing on Windows and with other language servers - rootUri: 'file:///' + this.root_path, - documentUri: 'file:///' + virtual_document.uri, + rootUri: 'file://' + PathExt.join(this.server_root, this.root_path), + documentUri: + 'file://' + + PathExt.join(this.server_root, this.root_path, virtual_document.uri), documentText: () => { // NOTE: Update is async now and this is not really used, as an alternative method // which is compatible with async is used. diff --git a/src/adapters/jupyterlab/notebook.ts b/src/adapters/jupyterlab/notebook.ts index eb51a14bf..8df7df8cc 100644 --- a/src/adapters/jupyterlab/notebook.ts +++ b/src/adapters/jupyterlab/notebook.ts @@ -27,9 +27,16 @@ export class NotebookAdapter extends JupyterLabWidgetAdapter { jumper: NotebookJumper, app: JupyterFrontEnd, completion_manager: ICompletionManager, - rendermime_registry: IRenderMimeRegistry + rendermime_registry: IRenderMimeRegistry, + server_root: string ) { - super(app, editor_widget, rendermime_registry, 'completer:invoke-notebook'); + super( + app, + editor_widget, + rendermime_registry, + 'completer:invoke-notebook', + server_root + ); this.editor = editor_widget.content; this.completion_manager = completion_manager; this.jumper = jumper; diff --git a/src/command_manager.ts b/src/command_manager.ts index 02b5f6bc6..fb8a47bcc 100644 --- a/src/command_manager.ts +++ b/src/command_manager.ts @@ -137,6 +137,7 @@ export class NotebookCommandManager extends ContextMenuCommandManager { } } + export class FileEditorCommandManager extends ContextMenuCommandManager { protected tracker: IEditorTracker; selector = '.jp-FileEditor'; diff --git a/src/index.ts b/src/index.ts index a17b2eb0c..983eed3e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,7 @@ import { notebook_adapters, NotebookCommandManager } from './command_manager'; +import IPaths = JupyterFrontEnd.IPaths; const lsp_commands: Array = [].concat( ...lsp_features.map(feature => feature.commands) @@ -46,7 +47,8 @@ const plugin: JupyterFrontEndPlugin = { ICommandPalette, IDocumentManager, ICompletionManager, - IRenderMimeRegistry + IRenderMimeRegistry, + IPaths ], activate: ( app: JupyterFrontEnd, @@ -56,8 +58,27 @@ const plugin: JupyterFrontEndPlugin = { palette: ICommandPalette, documentManager: IDocumentManager, completion_manager: ICompletionManager, - rendermime_registry: IRenderMimeRegistry + rendermime_registry: IRenderMimeRegistry, + paths: IPaths ) => { + // temporary workaround for getting the absolute path + let server_root = paths.directories.serverRoot; + if (server_root.startsWith('~')) { + // try to guess the home location: + let user_settings = paths.directories.userSettings; + if (user_settings.startsWith('/home/')) { + server_root = server_root.replace( + '~', + user_settings.substring(0, user_settings.indexOf('/', 6)) + ); + console.log('Guessing the server root using user settings path', server_root); + } else { + console.warn( + 'Unable to solve the absolute path: some LSP servers may not work correctly' + ); + } + } + fileEditorTracker.widgetUpdated.connect((sender, widget) => { console.log(sender); console.log(widget); @@ -76,7 +97,8 @@ const plugin: JupyterFrontEndPlugin = { jumper, app, completion_manager, - rendermime_registry + rendermime_registry, + server_root ); file_editor_adapters.set(fileEditor.id, adapter); } @@ -97,7 +119,8 @@ const plugin: JupyterFrontEndPlugin = { jumper, app, completion_manager, - rendermime_registry + rendermime_registry, + server_root ); notebook_adapters.set(widget.id, adapter); }); diff --git a/src/virtual/document.ts b/src/virtual/document.ts index 11ecbccea..46cc525d3 100644 --- a/src/virtual/document.ts +++ b/src/virtual/document.ts @@ -529,6 +529,9 @@ export class VirtualDocument { } get uri(): string { + if (!this.parent) { + return this.path; + } return this.path + '.' + this.id_path + '.' + this.file_extension; }