Skip to content

Commit 2a3f054

Browse files
committed
Add middleware to intercept diagnostics from the server
Clients may want to inspect diagnostics received from the server before letting VS Code handle and display them. It is not possible to use onNotification because the existing handler will be replaced. Thus, we need to introduce a new middleware for the language client to route the server's textDocument/publishDiagnostics notification to. This will allow the client can do what it needs to do before then forwarding it off to VS Code. Signed-off-by: Remy Suen <[email protected]>
1 parent 6501c8f commit 2a3f054

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

client/src/client.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ export interface _Middleware {
415415
willSaveWaitUntil?: NextSignature<TextDocumentWillSaveEvent, Thenable<VTextEdit[]>>;
416416
didSave?: NextSignature<TextDocument, void>;
417417
didClose?: NextSignature<TextDocument, void>;
418+
publishDiagnostics?: NextSignature<PublishDiagnosticsParams, void>;
418419

419420
provideCompletionItem?: (this: void, document: TextDocument, position: VPosition, context: VCompletionContext, token: CancellationToken, next: ProvideCompletionItemsSignature) => ProviderResult<VCompletionItem[] | VCompletionList>;
420421
resolveCompletionItem?: (this: void, item: VCompletionItem, token: CancellationToken, next: ResolveCompletionItemSignature) => ProviderResult<VCompletionItem>;
@@ -2740,6 +2741,18 @@ export abstract class BaseLanguageClient {
27402741
}
27412742

27422743
private handleDiagnostics(params: PublishDiagnosticsParams) {
2744+
if (!this._diagnostics) {
2745+
return;
2746+
}
2747+
let middleware = this.clientOptions.middleware!.publishDiagnostics;
2748+
if (middleware) {
2749+
middleware(params, (params) => this.setDiagnostics(params));
2750+
} else {
2751+
this.setDiagnostics(params);
2752+
}
2753+
}
2754+
2755+
private setDiagnostics(params: PublishDiagnosticsParams) {
27432756
if (!this._diagnostics) {
27442757
return;
27452758
}

client/src/test/integration.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as assert from 'assert';
88
import * as lsclient from '../main';
99
import * as path from 'path';
10+
import { PublishDiagnosticsParams } from '../main';
1011

1112
suite('Client integration', () => {
1213

@@ -17,7 +18,19 @@ suite('Client integration', () => {
1718
debug: { module: serverModule, transport: lsclient.TransportKind.ipc, options: { execArgv: ['--nolazy', '--inspect=6014'] } }
1819
};
1920
let documentSelector: lsclient.DocumentSelector = ['css'];
20-
let clientOptions: lsclient.LanguageClientOptions = { documentSelector, synchronize: {}, initializationOptions: {} };
21+
let clientOptions: lsclient.LanguageClientOptions = {
22+
documentSelector, synchronize: {}, initializationOptions: {},
23+
middleware: {
24+
publishDiagnostics: (params: PublishDiagnosticsParams, next) => {
25+
assert.equal(params.uri, "uri:/test.ts");
26+
assert.ok(Array.isArray(params.diagnostics));
27+
assert.equal(params.diagnostics.length, 0);
28+
next(params);
29+
disposable.dispose();
30+
done();
31+
}
32+
}
33+
};
2134
let client = new lsclient.LanguageClient('css', 'Test Language Server', serverOptions, clientOptions);
2235
let disposable = client.start();
2336

@@ -36,8 +49,6 @@ suite('Client integration', () => {
3649
}
3750
};
3851
assert.deepEqual(client.initializeResult, expected);
39-
disposable.dispose();
40-
done();
4152
} catch (e) {
4253
disposable.dispose();
4354
done(e);

client/src/test/servers/testInitializeResult.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ connection.onInitialize((params: InitializeParams): any => {
3333
return { capabilities, customResults: { "hello": "world" } };
3434
});
3535

36+
connection.onInitialized(() => {
37+
connection.sendDiagnostics({ uri: "uri:/test.ts", diagnostics: [] });
38+
});
3639

3740
// Listen on the connection
3841
connection.listen();

0 commit comments

Comments
 (0)