Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement console log window messages, update lsp-ws-connection #606

Merged
merged 7 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/jupyterlab-lsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@krassowski/theme-material": "~2.1.0",
"@krassowski/theme-vscode": "~2.1.0",
"lodash.mergewith": "^4.6.1",
"lsp-ws-connection": "~0.5.1"
"lsp-ws-connection": "~0.6.0"
},
"devDependencies": {
"@babel/preset-env": "^7.4.3",
Expand All @@ -78,6 +78,7 @@
"@jupyterlab/docmanager": "^3.0.0",
"@jupyterlab/docregistry": "^3.0.0",
"@jupyterlab/fileeditor": "^3.0.0",
"@jupyterlab/logconsole": "^3.0.0",
"@jupyterlab/notebook": "^3.0.0",
"@jupyterlab/rendermime": "^3.0.0",
"@jupyterlab/services": "^6.0.0",
Expand All @@ -102,7 +103,8 @@
"react": "^17.0.1",
"rimraf": "^3.0.2",
"ts-jest": "^26.4.3",
"typescript": "~4.1.3"
"typescript": "~4.1.3",
"vscode-languageserver-protocol": "^3.16.0"
},
"peerDependencies": {
"@jupyterlab/application": "^3.0.0",
Expand Down
39 changes: 39 additions & 0 deletions packages/jupyterlab-lsp/src/adapters/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
import { ILogPayload } from '@jupyterlab/logconsole';
import { nullTranslator, TranslationBundle } from '@jupyterlab/translation';
import { JSONObject } from '@lumino/coreutils';
import { Signal } from '@lumino/signaling';
Expand Down Expand Up @@ -341,6 +342,44 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
'have been initialized'
);
});

// Note: the logger extension behaves badly with non-default names
// as it changes the source to the active file afterwards anyways
const loggerSourceName = virtual_document.uri;
const logger = this.extension.user_console.getLogger(loggerSourceName);

data.connection.notifications.window.logMessage.connect(
(connection, message) => {
this.console.log(
data.connection.serverIdentifier,
virtual_document.uri,
message
);
logger.log({
type: 'text',
data: connection.serverIdentifier + ': ' + message.message
} as ILogPayload);
}
);

data.connection.notifications.window.showMessage.connect(
(connection, message) => {
this.console.log(
data.connection.serverIdentifier,
virtual_document.uri,
message
);
logger.log({
type: 'text',
data: connection.serverIdentifier + ': ' + message.message
} as ILogPayload);
this.extension.app.commands
.execute('logconsole:open', {
source: loggerSourceName
})
.catch(console.log);
}
);
}

/**
Expand Down
51 changes: 50 additions & 1 deletion packages/jupyterlab-lsp/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,63 @@
// ISC licence is, quote, "functionally equivalent to the simplified BSD and MIT licenses,
// but without language deemed unnecessary following the Berne Convention." (Wikipedia).
// Introduced modifications are BSD licenced, copyright JupyterLab development team.
import { Signal } from '@lumino/signaling';
import {
IDocumentInfo,
ILspOptions,
IPosition,
LspWsConnection
} from 'lsp-ws-connection';
import * as lsProtocol from 'vscode-languageserver-protocol';
import type * as rpc from 'vscode-jsonrpc';
import type * as lsProtocol from 'vscode-languageserver-protocol';

import { until_ready } from './utils';

interface ILSPOptions extends ILspOptions {
serverIdentifier?: string;
}

interface ConnectionSignal<T> extends Signal<LSPConnection, T> {
// empty
}

interface INamespace {
[index: string]: ConnectionSignal<any> | ((params: any) => void);
}

interface ILSPNotifications {
$: {
logTrace: ConnectionSignal<rpc.LogTraceParams>;
setTrace: (params: rpc.SetTraceParams) => void;
};
window: {
showMessage: ConnectionSignal<lsProtocol.ShowMessageParams>;
logMessage: ConnectionSignal<lsProtocol.LogMessageParams>;
};
[index: string]: INamespace;
}

export class LSPConnection extends LspWsConnection {
protected documentsToOpen: IDocumentInfo[];
public serverIdentifier: string;
public notifications: ILSPNotifications;

constructor(options: ILSPOptions) {
super(options);
this.serverIdentifier = options?.serverIdentifier;
this.documentsToOpen = [];
this.notifications = {
$: {
logTrace: new Signal(this),
setTrace: params => {
this.connection.sendNotification('$/setTrace', params);
}
},
window: {
showMessage: new Signal(this),
logMessage: new Signal(this)
}
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @bollwyvl . I know this is a different pattern from the existing one, but trying to experiment on doing it better. I also know the dream would be to generate typings from a JSON file; the handlers for signals could be also generated from JSON, but not sure if those would work for all handlers + some signals might be performance-critical and maybe using direct callbacks rather than lumino Signal could be better.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welp, even lower in the stack is the lumino Message bus. Nobody talks about it much, but it is extremely robust, and what makes all the core widget stuff very smooth.

But performance-critical is as performance-critical-is-measured... we'd need some no-fooling benchmarks before stepping too far off something that at least quacks likes other parts of the stack.

}

sendOpenWhenReady(documentInfo: IDocumentInfo) {
Expand All @@ -39,6 +74,20 @@ export class LSPConnection extends LspWsConnection {
while (this.documentsToOpen.length) {
this.sendOpen(this.documentsToOpen.pop());
}
for (const namespaceName in this.notifications) {
const namespace = this.notifications[namespaceName];
for (const memberName in namespace) {
const endpoint = namespace[memberName];
if (endpoint instanceof Signal) {
this.connection.onNotification(
`${namespaceName}/${memberName}`,
params => {
endpoint.emit(params);
}
);
}
}
}
}

public sendSelectiveChange(
Expand Down
3 changes: 3 additions & 0 deletions packages/jupyterlab-lsp/src/editor_integration/testutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TextModelFactory
} from '@jupyterlab/docregistry';
import { FileEditor, FileEditorFactory } from '@jupyterlab/fileeditor';
import { ILoggerRegistry } from '@jupyterlab/logconsole';
import * as nbformat from '@jupyterlab/nbformat';
import {
Notebook,
Expand Down Expand Up @@ -107,6 +108,7 @@ export class MockExtension implements ILSPExtension {
foreign_code_extractors: IForeignCodeExtractorsRegistry;
code_overrides: ICodeOverridesRegistry;
console: ILSPLogConsole;
user_console: ILoggerRegistry;
translator: ITranslator;

constructor() {
Expand All @@ -128,6 +130,7 @@ export class MockExtension implements ILSPExtension {
this.foreign_code_extractors = {};
this.code_overrides = {};
this.console = new BrowserConsole();
this.user_console = null;
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/jupyterlab-lsp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** The Public API, as exposed in the `main` field of package.json */

/** General public tokens, including lumino Tokens and namespaces */
import { ILoggerRegistry } from '@jupyterlab/logconsole';

export * from './tokens';

/** Component- and feature-specific APIs */
Expand Down Expand Up @@ -124,6 +126,7 @@ export interface ILSPExtension {
code_overrides: ICodeOverridesRegistry;
console: ILSPLogConsole;
translator: ITranslator;
user_console: ILoggerRegistry | null;
}

export class LSPExtension implements ILSPExtension {
Expand All @@ -143,6 +146,7 @@ export class LSPExtension implements ILSPExtension {
private code_overrides_manager: ILSPCodeOverridesManager,
public console: ILSPLogConsole,
public translator: ITranslator,
public user_console: ILoggerRegistry,
status_bar: IStatusBar | null
) {
const trans = (translator || nullTranslator).load('jupyterlab-lsp');
Expand Down Expand Up @@ -251,7 +255,7 @@ const plugin: JupyterFrontEndPlugin<ILSPFeatureManager> = {
ILSPLogConsole,
ITranslator
],
optional: [IStatusBar],
optional: [ILoggerRegistry, IStatusBar],
activate: (app, ...args) => {
let extension = new LSPExtension(
app,
Expand All @@ -266,6 +270,7 @@ const plugin: JupyterFrontEndPlugin<ILSPFeatureManager> = {
ILSPCodeOverridesManager,
ILSPLogConsole,
ITranslator,
ILoggerRegistry | null,
IStatusBar | null
])
);
Expand Down
11 changes: 5 additions & 6 deletions packages/lsp-ws-connection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lsp-ws-connection",
"version": "0.5.1",
"version": "0.6.0",
"description": "Utility for adapting editors to language server protocol",
"main": "dist/index.js",
"module": "dist/index.js",
Expand All @@ -27,11 +27,10 @@
"homepage": "https://github.com/krassowski/jupyterlab-lsp/tree/master/packages/lsp-ws-connection",
"repository": "github:krassowski/jupyterlab-lsp",
"dependencies": {
"vscode-jsonrpc": "^4.1.0-next",
"vscode-languageclient": "^5.2.1",
"vscode-languageserver-protocol": "^3.14.1",
"vscode-languageserver-types": "^3.14.0",
"vscode-ws-jsonrpc": "0.1.1"
"vscode-jsonrpc": "^6.0.0",
"vscode-languageserver-protocol": "^3.16.0",
"vscode-languageserver-types": "^3.16.0",
"vscode-ws-jsonrpc": "0.2.0"
},
"devDependencies": {
"@types/chai": "^4.1.7",
Expand Down
Loading