forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockLanguageClient.ts
More file actions
229 lines (217 loc) · 8.39 KB
/
mockLanguageClient.ts
File metadata and controls
229 lines (217 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import {
CancellationToken,
DiagnosticCollection,
Disposable,
Event,
OutputChannel,
TextDocumentContentChangeEvent
} from 'vscode';
import {
Code2ProtocolConverter,
CompletionItem,
DynamicFeature,
ErrorHandler,
GenericNotificationHandler,
GenericRequestHandler,
InitializeResult,
LanguageClient,
LanguageClientOptions,
MessageTransports,
NotificationHandler,
NotificationHandler0,
NotificationType,
NotificationType0,
Protocol2CodeConverter,
RequestHandler,
RequestHandler0,
RequestType,
RequestType0,
RPCMessageType,
ServerOptions,
StateChangeEvent,
StaticFeature,
TextDocumentItem,
Trace,
VersionedTextDocumentIdentifier
} from 'vscode-languageclient';
import { createDeferred, Deferred } from '../../client/common/utils/async';
import { noop } from '../core';
import { MockProtocolConverter } from './mockProtocolConverter';
// tslint:disable:no-any unified-signatures
export class MockLanguageClient extends LanguageClient {
private notificationPromise : Deferred<void> | undefined;
private contents : string;
private versionId: number | null;
private converter: MockProtocolConverter;
public constructor(name: string, serverOptions: ServerOptions, clientOptions: LanguageClientOptions, forceDebug?: boolean) {
(LanguageClient.prototype as any).checkVersion = noop;
super(name, serverOptions, clientOptions, forceDebug);
this.contents = '';
this.versionId = 0;
this.converter = new MockProtocolConverter();
}
public waitForNotification() : Promise<void> {
this.notificationPromise = createDeferred();
return this.notificationPromise.promise;
}
// Returns the current contents of the document being built by the completion provider calls
public getDocumentContents() : string {
return this.contents;
}
public getVersionId() : number | null {
return this.versionId;
}
public stop(): Thenable<void> {
throw new Error('Method not implemented.');
}
public registerProposedFeatures(): void {
throw new Error('Method not implemented.');
}
public get initializeResult(): InitializeResult | undefined {
throw new Error('Method not implemented.');
}
public sendRequest<R, E, RO>(type: RequestType0<R, E, RO>, token?: CancellationToken | undefined): Thenable<R>;
public sendRequest<P, R, E, RO>(type: RequestType<P, R, E, RO>, params: P, token?: CancellationToken | undefined): Thenable<R>;
public sendRequest<R>(method: string, token?: CancellationToken | undefined): Thenable<R>;
public sendRequest<R>(method: string, param: any, token?: CancellationToken | undefined): Thenable<R>;
public sendRequest(_method: any, _param?: any, _token?: any) : Thenable<any> {
switch (_method.method) {
case 'textDocument/completion':
// Just return one for each line of our contents
return Promise.resolve(this.getDocumentCompletions());
break;
default:
break;
}
return Promise.resolve();
}
public onRequest<R, E, RO>(type: RequestType0<R, E, RO>, handler: RequestHandler0<R, E>): void;
public onRequest<P, R, E, RO>(type: RequestType<P, R, E, RO>, handler: RequestHandler<P, R, E>): void;
public onRequest<R, E>(method: string, handler: GenericRequestHandler<R, E>): void;
public onRequest(_method: any, _handler: any) {
throw new Error('Method not implemented.');
}
public sendNotification<RO>(type: NotificationType0<RO>): void;
public sendNotification<P, RO>(type: NotificationType<P, RO>, params?: P | undefined): void;
public sendNotification(method: string): void;
public sendNotification(method: string, params: any): void;
public sendNotification(method: any, params?: any) {
switch (method.method) {
case 'textDocument/didOpen':
const item = params.textDocument as TextDocumentItem;
if (item) {
this.contents = item.text;
this.versionId = item.version;
}
break;
case 'textDocument/didChange':
const id = params.textDocument as VersionedTextDocumentIdentifier;
const changes = params.contentChanges as TextDocumentContentChangeEvent[];
if (id && changes) {
this.applyChanges(changes);
this.versionId = id.version;
}
break;
default:
if (this.notificationPromise) {
this.notificationPromise.reject(new Error(`Unknown notification ${method.method}`));
}
break;
}
if (this.notificationPromise && !this.notificationPromise.resolved) {
this.notificationPromise.resolve();
}
}
public onNotification<RO>(type: NotificationType0<RO>, handler: NotificationHandler0): void;
public onNotification<P, RO>(type: NotificationType<P, RO>, handler: NotificationHandler<P>): void;
public onNotification(method: string, handler: GenericNotificationHandler): void;
public onNotification(_method: any, _handler: any) {
throw new Error('Method not implemented.');
}
public get clientOptions(): LanguageClientOptions {
throw new Error('Method not implemented.');
}
public get protocol2CodeConverter(): Protocol2CodeConverter {
throw new Error('Method not implemented.');
}
public get code2ProtocolConverter(): Code2ProtocolConverter {
return this.converter;
}
public get onTelemetry(): Event<any> {
throw new Error('Method not implemented.');
}
public get onDidChangeState(): Event<StateChangeEvent> {
throw new Error('Method not implemented.');
}
public get outputChannel(): OutputChannel {
throw new Error('Method not implemented.');
}
public get diagnostics(): DiagnosticCollection | undefined {
throw new Error('Method not implemented.');
}
public createDefaultErrorHandler(): ErrorHandler {
throw new Error('Method not implemented.');
}
public get trace(): Trace {
throw new Error('Method not implemented.');
}
public info(_message: string, _data?: any): void {
throw new Error('Method not implemented.');
}
public warn(_message: string, _data?: any): void {
throw new Error('Method not implemented.');
}
public error(_message: string, _data?: any): void {
throw new Error('Method not implemented.');
}
public needsStart(): boolean {
throw new Error('Method not implemented.');
}
public needsStop(): boolean {
throw new Error('Method not implemented.');
}
public onReady(): Promise<void> {
throw new Error('Method not implemented.');
}
public start(): Disposable {
throw new Error('Method not implemented.');
}
public registerFeatures(_features: (StaticFeature | DynamicFeature<any>)[]): void {
throw new Error('Method not implemented.');
}
public registerFeature(_feature: StaticFeature | DynamicFeature<any>): void {
throw new Error('Method not implemented.');
}
public logFailedRequest(_type: RPCMessageType, _error: any): void {
throw new Error('Method not implemented.');
}
protected handleConnectionClosed(): void {
throw new Error('Method not implemented.');
}
protected createMessageTransports(_encoding: string): Thenable<MessageTransports> {
throw new Error('Method not implemented.');
}
protected registerBuiltinFeatures(): void {
noop();
}
private applyChanges(changes: TextDocumentContentChangeEvent[]) {
changes.forEach(c => {
const before = this.contents.substr(0, c.rangeOffset);
const after = this.contents.substr(c.rangeOffset + c.rangeLength);
this.contents = `${before}${c.text}${after}`;
});
}
private getDocumentCompletions() : CompletionItem[] {
const lines = this.contents.splitLines();
return lines.map(l => {
return {
label: l,
insertText: l,
sortText: l
};
});
}
}