Skip to content

Commit b1f6311

Browse files
committed
Implement console log window messages, update lsp-ws-connection
1 parent 76f59d5 commit b1f6311

File tree

9 files changed

+498
-204
lines changed

9 files changed

+498
-204
lines changed

packages/jupyterlab-lsp/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@krassowski/theme-material": "~2.1.0",
6363
"@krassowski/theme-vscode": "~2.1.0",
6464
"lodash.mergewith": "^4.6.1",
65-
"lsp-ws-connection": "~0.5.1"
65+
"lsp-ws-connection": "~0.6.0"
6666
},
6767
"devDependencies": {
6868
"@babel/preset-env": "^7.4.3",
@@ -78,6 +78,7 @@
7878
"@jupyterlab/docmanager": "^3.0.0",
7979
"@jupyterlab/docregistry": "^3.0.0",
8080
"@jupyterlab/fileeditor": "^3.0.0",
81+
"@jupyterlab/logconsole": "^3.0.0",
8182
"@jupyterlab/notebook": "^3.0.0",
8283
"@jupyterlab/rendermime": "^3.0.0",
8384
"@jupyterlab/services": "^6.0.0",
@@ -102,7 +103,8 @@
102103
"react": "^17.0.1",
103104
"rimraf": "^3.0.2",
104105
"ts-jest": "^26.4.3",
105-
"typescript": "~4.1.3"
106+
"typescript": "~4.1.3",
107+
"vscode-languageserver-protocol": "^3.16.0"
106108
},
107109
"peerDependencies": {
108110
"@jupyterlab/application": "^3.0.0",

packages/jupyterlab-lsp/src/adapters/adapter.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { JupyterFrontEnd } from '@jupyterlab/application';
22
import { CodeEditor } from '@jupyterlab/codeeditor';
33
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
4+
import { ILogPayload } from '@jupyterlab/logconsole';
45
import { nullTranslator, TranslationBundle } from '@jupyterlab/translation';
56
import { JSONObject } from '@lumino/coreutils';
67
import { Signal } from '@lumino/signaling';
@@ -341,6 +342,44 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
341342
'have been initialized'
342343
);
343344
});
345+
346+
// Note: the logger extension behaves badly with non-default names
347+
// as it changes the source to the active file afterwards anyways
348+
const loggerSourceName = virtual_document.uri;
349+
const logger = this.extension.user_console.getLogger(loggerSourceName);
350+
351+
data.connection.notifications.window.logMessage.connect(
352+
(connection, message) => {
353+
this.console.log(
354+
data.connection.serverIdentifier,
355+
virtual_document.uri,
356+
message
357+
);
358+
logger.log({
359+
type: 'text',
360+
data: message.message
361+
} as ILogPayload);
362+
}
363+
);
364+
365+
data.connection.notifications.window.showMessage.connect(
366+
(connection, message) => {
367+
this.console.log(
368+
data.connection.serverIdentifier,
369+
virtual_document.uri,
370+
message
371+
);
372+
logger.log({
373+
type: 'text',
374+
data: message.message
375+
} as ILogPayload);
376+
this.extension.app.commands
377+
.execute('logconsole:open', {
378+
source: loggerSourceName
379+
})
380+
.catch(console.log);
381+
}
382+
);
344383
}
345384

346385
/**

packages/jupyterlab-lsp/src/connection.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,63 @@
22
// ISC licence is, quote, "functionally equivalent to the simplified BSD and MIT licenses,
33
// but without language deemed unnecessary following the Berne Convention." (Wikipedia).
44
// Introduced modifications are BSD licenced, copyright JupyterLab development team.
5+
import { Signal } from '@lumino/signaling';
56
import {
67
IDocumentInfo,
78
ILspOptions,
89
IPosition,
910
LspWsConnection
1011
} from 'lsp-ws-connection';
11-
import * as lsProtocol from 'vscode-languageserver-protocol';
12+
import type * as rpc from 'vscode-jsonrpc';
13+
import type * as lsProtocol from 'vscode-languageserver-protocol';
1214

1315
import { until_ready } from './utils';
1416

1517
interface ILSPOptions extends ILspOptions {
1618
serverIdentifier?: string;
1719
}
1820

21+
interface ConnectionSignal<T> extends Signal<LSPConnection, T> {
22+
// empty
23+
}
24+
25+
interface INamespace {
26+
[index: string]: ConnectionSignal<any> | ((params: any) => void);
27+
}
28+
29+
interface ILSPNotifications {
30+
$: {
31+
logTrace: ConnectionSignal<rpc.LogTraceParams>;
32+
setTrace: (params: rpc.SetTraceParams) => void;
33+
};
34+
window: {
35+
showMessage: ConnectionSignal<lsProtocol.ShowMessageParams>;
36+
logMessage: ConnectionSignal<lsProtocol.LogMessageParams>;
37+
};
38+
[index: string]: INamespace;
39+
}
40+
1941
export class LSPConnection extends LspWsConnection {
2042
protected documentsToOpen: IDocumentInfo[];
2143
public serverIdentifier: string;
44+
public notifications: ILSPNotifications;
2245

2346
constructor(options: ILSPOptions) {
2447
super(options);
2548
this.serverIdentifier = options?.serverIdentifier;
2649
this.documentsToOpen = [];
50+
this.notifications = {
51+
$: {
52+
logTrace: new Signal(this),
53+
setTrace: params => {
54+
this.connection.sendNotification('$/setTrace', params);
55+
}
56+
},
57+
window: {
58+
showMessage: new Signal(this),
59+
logMessage: new Signal(this)
60+
}
61+
};
2762
}
2863

2964
sendOpenWhenReady(documentInfo: IDocumentInfo) {
@@ -39,6 +74,20 @@ export class LSPConnection extends LspWsConnection {
3974
while (this.documentsToOpen.length) {
4075
this.sendOpen(this.documentsToOpen.pop());
4176
}
77+
for (const namespaceName in this.notifications) {
78+
const namespace = this.notifications[namespaceName];
79+
for (const memberName in namespace) {
80+
const endpoint = namespace[memberName];
81+
if (endpoint instanceof Signal) {
82+
this.connection.onNotification(
83+
`${namespaceName}/${memberName}`,
84+
params => {
85+
endpoint.emit(params);
86+
}
87+
);
88+
}
89+
}
90+
}
4291
}
4392

4493
public sendSelectiveChange(

packages/jupyterlab-lsp/src/editor_integration/testutils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TextModelFactory
1313
} from '@jupyterlab/docregistry';
1414
import { FileEditor, FileEditorFactory } from '@jupyterlab/fileeditor';
15+
import { ILoggerRegistry } from '@jupyterlab/logconsole';
1516
import * as nbformat from '@jupyterlab/nbformat';
1617
import {
1718
Notebook,
@@ -107,6 +108,7 @@ export class MockExtension implements ILSPExtension {
107108
foreign_code_extractors: IForeignCodeExtractorsRegistry;
108109
code_overrides: ICodeOverridesRegistry;
109110
console: ILSPLogConsole;
111+
user_console: ILoggerRegistry;
110112
translator: ITranslator;
111113

112114
constructor() {
@@ -128,6 +130,7 @@ export class MockExtension implements ILSPExtension {
128130
this.foreign_code_extractors = {};
129131
this.code_overrides = {};
130132
this.console = new BrowserConsole();
133+
this.user_console = null;
131134
}
132135
}
133136

packages/jupyterlab-lsp/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/** The Public API, as exposed in the `main` field of package.json */
22

33
/** General public tokens, including lumino Tokens and namespaces */
4+
import { ILoggerRegistry } from '@jupyterlab/logconsole';
5+
46
export * from './tokens';
57

68
/** Component- and feature-specific APIs */
@@ -124,6 +126,7 @@ export interface ILSPExtension {
124126
code_overrides: ICodeOverridesRegistry;
125127
console: ILSPLogConsole;
126128
translator: ITranslator;
129+
user_console: ILoggerRegistry | null;
127130
}
128131

129132
export class LSPExtension implements ILSPExtension {
@@ -143,6 +146,7 @@ export class LSPExtension implements ILSPExtension {
143146
private code_overrides_manager: ILSPCodeOverridesManager,
144147
public console: ILSPLogConsole,
145148
public translator: ITranslator,
149+
public user_console: ILoggerRegistry,
146150
status_bar: IStatusBar | null
147151
) {
148152
const trans = (translator || nullTranslator).load('jupyterlab-lsp');
@@ -251,7 +255,7 @@ const plugin: JupyterFrontEndPlugin<ILSPFeatureManager> = {
251255
ILSPLogConsole,
252256
ITranslator
253257
],
254-
optional: [IStatusBar],
258+
optional: [ILoggerRegistry, IStatusBar],
255259
activate: (app, ...args) => {
256260
let extension = new LSPExtension(
257261
app,
@@ -266,6 +270,7 @@ const plugin: JupyterFrontEndPlugin<ILSPFeatureManager> = {
266270
ILSPCodeOverridesManager,
267271
ILSPLogConsole,
268272
ITranslator,
273+
ILoggerRegistry | null,
269274
IStatusBar | null
270275
])
271276
);

packages/lsp-ws-connection/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lsp-ws-connection",
3-
"version": "0.5.1",
3+
"version": "0.6.0",
44
"description": "Utility for adapting editors to language server protocol",
55
"main": "dist/index.js",
66
"module": "dist/index.js",
@@ -27,11 +27,10 @@
2727
"homepage": "https://github.com/krassowski/jupyterlab-lsp/tree/master/packages/lsp-ws-connection",
2828
"repository": "github:krassowski/jupyterlab-lsp",
2929
"dependencies": {
30-
"vscode-jsonrpc": "^4.1.0-next",
31-
"vscode-languageclient": "^5.2.1",
32-
"vscode-languageserver-protocol": "^3.14.1",
33-
"vscode-languageserver-types": "^3.14.0",
34-
"vscode-ws-jsonrpc": "0.1.1"
30+
"vscode-jsonrpc": "^6.0.0",
31+
"vscode-languageserver-protocol": "^3.16.0",
32+
"vscode-languageserver-types": "^3.16.0",
33+
"vscode-ws-jsonrpc": "0.2.0"
3534
},
3635
"devDependencies": {
3736
"@types/chai": "^4.1.7",

0 commit comments

Comments
 (0)