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

Add middleware to intercept textDocument/publishDiagnostics #322

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {
workspace as Workspace, window as Window, languages as Languages, commands as Commands,
TextDocumentChangeEvent, TextDocument, Disposable, OutputChannel,
FileSystemWatcher as VFileSystemWatcher, DiagnosticCollection, Uri, ProviderResult,
FileSystemWatcher as VFileSystemWatcher, DiagnosticCollection, Diagnostic as VDiagnostic, Uri, ProviderResult,
CancellationToken, Position as VPosition, Location as VLocation, Range as VRange,
CompletionItem as VCompletionItem, CompletionList as VCompletionList, SignatureHelp as VSignatureHelp, Definition as VDefinition, DocumentHighlight as VDocumentHighlight,
SymbolInformation as VSymbolInformation, CodeActionContext as VCodeActionContext, Command as VCommand, CodeLens as VCodeLens,
Expand Down Expand Up @@ -318,6 +318,10 @@ export enum RevealOutputChannelOn {
Never = 4
}

export interface HandleDiagnosticsSignature {
(uri: Uri, diagnostics: VDiagnostic[]): void;
}

export interface ProvideCompletionItemsSignature {
(document: TextDocument, position: VPosition, context: VCompletionContext, token: CancellationToken): ProviderResult<VCompletionItem[] | VCompletionList>;
}
Expand Down Expand Up @@ -416,6 +420,7 @@ export interface _Middleware {
didSave?: NextSignature<TextDocument, void>;
didClose?: NextSignature<TextDocument, void>;

handleDiagnostics?: (this: void, uri: Uri, diagnostics: VDiagnostic[], next: HandleDiagnosticsSignature) => void;
provideCompletionItem?: (this: void, document: TextDocument, position: VPosition, context: VCompletionContext, token: CancellationToken, next: ProvideCompletionItemsSignature) => ProviderResult<VCompletionItem[] | VCompletionList>;
resolveCompletionItem?: (this: void, item: VCompletionItem, token: CancellationToken, next: ResolveCompletionItemSignature) => ProviderResult<VCompletionItem>;
provideHover?: (this: void, document: TextDocument, position: VPosition, token: CancellationToken, next: ProvideHoverSignature) => ProviderResult<VHover>;
Expand Down Expand Up @@ -2745,6 +2750,18 @@ export abstract class BaseLanguageClient {
}
let uri = this._p2c.asUri(params.uri);
let diagnostics = this._p2c.asDiagnostics(params.diagnostics);
let middleware = this.clientOptions.middleware!.handleDiagnostics;
if (middleware) {
middleware(uri, diagnostics, (uri, diagnostics) => this.setDiagnostics(uri, diagnostics));
} else {
this.setDiagnostics(uri, diagnostics);
}
}

private setDiagnostics(uri: Uri, diagnostics: VDiagnostic[] | undefined) {
if (!this._diagnostics) {
return;
}
this._diagnostics.set(uri, diagnostics);
}

Expand Down
16 changes: 13 additions & 3 deletions client/src/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ suite('Client integration', () => {
debug: { module: serverModule, transport: lsclient.TransportKind.ipc, options: { execArgv: ['--nolazy', '--inspect=6014'] } }
};
let documentSelector: lsclient.DocumentSelector = ['css'];
let clientOptions: lsclient.LanguageClientOptions = { documentSelector, synchronize: {}, initializationOptions: {} };
let clientOptions: lsclient.LanguageClientOptions = {
documentSelector, synchronize: {}, initializationOptions: {},
middleware: {
handleDiagnostics: (uri, diagnostics, next) => {
assert.equal(uri, "uri:/test.ts");
assert.ok(Array.isArray(diagnostics));
assert.equal(diagnostics.length, 0);
next(uri, diagnostics);
disposable.dispose();
done();
}
}
};
let client = new lsclient.LanguageClient('css', 'Test Language Server', serverOptions, clientOptions);
let disposable = client.start();

Expand All @@ -36,8 +48,6 @@ suite('Client integration', () => {
}
};
assert.deepEqual(client.initializeResult, expected);
disposable.dispose();
done();
} catch (e) {
disposable.dispose();
done(e);
Expand Down
3 changes: 3 additions & 0 deletions client/src/test/servers/testInitializeResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ connection.onInitialize((params: InitializeParams): any => {
return { capabilities, customResults: { "hello": "world" } };
});

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

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