Skip to content

Commit

Permalink
Add a temporary workaround for absolute paths
Browse files Browse the repository at this point in the history
using directories provided by the server
  • Loading branch information
krassowski committed Sep 24, 2019
1 parent 32a8a5d commit 5eb345b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/adapters/jupyterlab/file_editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 9 additions & 13 deletions src/adapters/jupyterlab/jl_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ const mime_type_language_map: JSONObject = {
*/
export abstract class JupyterLabWidgetAdapter
implements IJupyterLabComponentsManager {
app: JupyterFrontEnd;
connections: Map<VirtualDocument.id_path, LSPConnection>;
documents: Map<VirtualDocument.id_path, VirtualDocument>;
jumper: CodeJumper;
protected adapters: Map<VirtualDocument.id_path, CodeMirrorAdapter>;
protected rendermime_registry: IRenderMimeRegistry;
widget: IDocumentWidget;
private readonly invoke_command: string;
protected document_connected: Signal<
JupyterLabWidgetAdapter,
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions src/adapters/jupyterlab/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/command_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class NotebookCommandManager extends ContextMenuCommandManager {
}
}


export class FileEditorCommandManager extends ContextMenuCommandManager {
protected tracker: IEditorTracker;
selector = '.jp-FileEditor';
Expand Down
31 changes: 27 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
notebook_adapters,
NotebookCommandManager
} from './command_manager';
import IPaths = JupyterFrontEnd.IPaths;

const lsp_commands: Array<IFeatureCommand> = [].concat(
...lsp_features.map(feature => feature.commands)
Expand All @@ -46,7 +47,8 @@ const plugin: JupyterFrontEndPlugin<void> = {
ICommandPalette,
IDocumentManager,
ICompletionManager,
IRenderMimeRegistry
IRenderMimeRegistry,
IPaths
],
activate: (
app: JupyterFrontEnd,
Expand All @@ -56,8 +58,27 @@ const plugin: JupyterFrontEndPlugin<void> = {
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);
Expand All @@ -76,7 +97,8 @@ const plugin: JupyterFrontEndPlugin<void> = {
jumper,
app,
completion_manager,
rendermime_registry
rendermime_registry,
server_root
);
file_editor_adapters.set(fileEditor.id, adapter);
}
Expand All @@ -97,7 +119,8 @@ const plugin: JupyterFrontEndPlugin<void> = {
jumper,
app,
completion_manager,
rendermime_registry
rendermime_registry,
server_root
);
notebook_adapters.set(widget.id, adapter);
});
Expand Down
3 changes: 3 additions & 0 deletions src/virtual/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 5eb345b

Please sign in to comment.