-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathserver.ts
executable file
·577 lines (486 loc) · 19.1 KB
/
server.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Copyright (c) Adam Voss. All rights reserved.
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {
createConnection, IConnection, TextDocuments, TextDocument, InitializeParams, InitializeResult,
Disposable, ProposedFeatures, CompletionList, ClientCapabilities, WorkspaceFolder, DocumentFormattingRequest
} from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests } from 'request-light';
import * as URL from 'url';
import { removeDuplicatesObj } from './languageservice/utils/arrUtils';
import { getLanguageService as getCustomLanguageService, LanguageSettings, CustomFormatterOptions, WorkspaceContextService } from './languageservice/yamlLanguageService';
import * as nls from 'vscode-nls';
import { CustomSchemaProvider, FilePatternAssociation, SchemaDeletions, SchemaAdditions, MODIFICATION_ACTIONS } from './languageservice/services/yamlSchemaService';
import { JSONSchema } from './languageservice/jsonSchema';
import { SchemaAssociationNotification, DynamicCustomSchemaRequestRegistration, CustomSchemaRequest, SchemaModificationNotification } from './requestTypes';
import { schemaRequestHandler } from './languageservice/services/schemaRequestHandler';
import { isRelativePath, relativeToAbsolutePath } from './languageservice/utils/paths';
import { URI } from 'vscode-uri';
import { KUBERNETES_SCHEMA_URL, JSON_SCHEMASTORE_URL } from './languageservice/utils/schemaUrls';
// tslint:disable-next-line: no-any
nls.config(process.env['VSCODE_NLS_CONFIG'] as any);
/**************************
* Generic helper functions
**************************/
const workspaceContext: WorkspaceContextService = {
resolveRelativePath: (relativePath: string, resource: string) =>
URL.resolve(resource, relativePath)
};
/********************
* Helper interfaces
********************/
interface ISchemaAssociations {
[pattern: string]: string[];
}
// Client settings interface to grab settings relevant for the language server
interface Settings {
yaml: {
format: CustomFormatterOptions;
schemas: JSONSchemaSettings[];
validate: boolean;
hover: boolean;
completion: boolean;
customTags: Array<String>;
schemaStore: {
enable: boolean
}
};
http: {
proxy: string;
proxyStrictSSL: boolean;
};
}
interface JSONSchemaSettings {
fileMatch?: string[];
url?: string;
schema?: JSONSchema;
}
/****************
* Variables
****************/
// Language server configuration
let yamlConfigurationSettings: JSONSchemaSettings[] = void 0;
let schemaAssociations: ISchemaAssociations = void 0;
let formatterRegistration: Thenable<Disposable> = null;
let specificValidatorPaths = [];
let schemaConfigurationSettings = [];
let yamlShouldValidate = true;
let yamlFormatterSettings = {
singleQuote: false,
bracketSpacing: true,
proseWrap: 'preserve',
printWidth: 80,
enable: true
} as CustomFormatterOptions;
let yamlShouldHover = true;
let yamlShouldCompletion = true;
let schemaStoreSettings = [];
let customTags = [];
let schemaStoreEnabled = true;
// File validation helpers
const pendingValidationRequests: { [uri: string]: NodeJS.Timer; } = { };
const validationDelayMs = 200;
// Create a simple text document manager. The text document manager
// supports full document sync only
const documents: TextDocuments = new TextDocuments();
// Language client configuration
let capabilities: ClientCapabilities;
let workspaceRoot: URI = null;
let workspaceFolders: WorkspaceFolder[] = [];
let clientDynamicRegisterSupport = false;
let hierarchicalDocumentSymbolSupport = false;
/****************************
* Reusable helper functions
****************************/
const checkSchemaURI = (uri: string): string => {
if (uri.trim().toLowerCase() === 'kubernetes') {
return KUBERNETES_SCHEMA_URL;
} else if (isRelativePath(uri)) {
return relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
} else {
return uri;
}
};
/**
* This function helps set the schema store if it hasn't already been set
* AND the schema store setting is enabled. If the schema store setting
* is not enabled we need to clear the schemas.
*/
function setSchemaStoreSettingsIfNotSet() {
const schemaStoreIsSet = (schemaStoreSettings.length !== 0);
if (schemaStoreEnabled && !schemaStoreIsSet) {
getSchemaStoreMatchingSchemas().then(schemaStore => {
schemaStoreSettings = schemaStore.schemas;
updateConfiguration();
}).catch((error: XHRResponse) => { });
} else if (!schemaStoreEnabled) {
schemaStoreSettings = [];
updateConfiguration();
}
}
/**
* When the schema store is enabled, download and store YAML schema associations
*/
function getSchemaStoreMatchingSchemas() {
return xhr({ url: JSON_SCHEMASTORE_URL }).then(response => {
const languageSettings = {
schemas: []
};
// Parse the schema store catalog as JSON
const schemas = JSON.parse(response.responseText);
for (const schemaIndex in schemas.schemas) {
const schema = schemas.schemas[schemaIndex];
if (schema && schema.fileMatch) {
for (const fileMatch in schema.fileMatch) {
const currFileMatch = schema.fileMatch[fileMatch];
// If the schema is for files with a YAML extension, save the schema association
if (currFileMatch.indexOf('.yml') !== -1 || currFileMatch.indexOf('.yaml') !== -1) {
languageSettings.schemas.push({ uri: schema.url, fileMatch: [currFileMatch] });
}
}
}
}
return languageSettings;
});
}
/**
* Called when server settings or schema associations are changed
* Re-creates schema associations and revalidates any open YAML files
*/
function updateConfiguration() {
let languageSettings: LanguageSettings = {
validate: yamlShouldValidate,
hover: yamlShouldHover,
completion: yamlShouldCompletion,
schemas: [],
customTags: customTags,
format: yamlFormatterSettings.enable
};
if (schemaAssociations) {
for (const pattern in schemaAssociations) {
const association = schemaAssociations[pattern];
if (Array.isArray(association)) {
association.forEach(uri => {
languageSettings = configureSchemas(uri, [pattern], null, languageSettings);
});
}
}
}
if (schemaConfigurationSettings) {
schemaConfigurationSettings.forEach(schema => {
let uri = schema.uri;
if (!uri && schema.schema) {
uri = schema.schema.id;
}
if (!uri && schema.fileMatch) {
uri = 'vscode://schemas/custom/' + encodeURIComponent(schema.fileMatch.join('&'));
}
if (uri) {
if (isRelativePath(uri)) {
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
}
languageSettings = configureSchemas(uri, schema.fileMatch, schema.schema, languageSettings);
}
});
}
if (schemaStoreSettings) {
languageSettings.schemas = languageSettings.schemas.concat(schemaStoreSettings);
}
customLanguageService.configure(languageSettings);
// Revalidate any open text documents
documents.all().forEach(triggerValidation);
}
/**
* Stores schema associations in server settings, handling kubernetes
* @param uri string path to schema (whether local or online)
* @param fileMatch file pattern to apply the schema to
* @param schema schema id
* @param languageSettings current server settings
*/
function configureSchemas(uri: string, fileMatch: string[], schema: any, languageSettings: LanguageSettings) {
uri = checkSchemaURI(uri);
if (schema === null) {
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
} else {
languageSettings.schemas.push({ uri, fileMatch: fileMatch, schema: schema });
}
if (fileMatch.constructor === Array && uri === KUBERNETES_SCHEMA_URL) {
fileMatch.forEach(url => {
specificValidatorPaths.push(url);
});
} else if (uri === KUBERNETES_SCHEMA_URL) {
specificValidatorPaths.push(fileMatch);
}
return languageSettings;
}
function isKubernetes(textDocument: TextDocument) {
for (const path in specificValidatorPaths) {
const globPath = specificValidatorPaths[path];
const fpa = new FilePatternAssociation(globPath);
if (fpa.matchesPattern(textDocument.uri)) {
return true;
}
}
return false;
}
function cleanPendingValidation(textDocument: TextDocument): void {
const request = pendingValidationRequests[textDocument.uri];
if (request) {
clearTimeout(request);
delete pendingValidationRequests[textDocument.uri];
}
}
function triggerValidation(textDocument: TextDocument): void {
cleanPendingValidation(textDocument);
pendingValidationRequests[textDocument.uri] = setTimeout(() => {
delete pendingValidationRequests[textDocument.uri];
validateTextDocument(textDocument);
}, validationDelayMs);
}
function validateTextDocument(textDocument: TextDocument): void {
if (!textDocument) {
return;
}
if (textDocument.getText().length === 0) {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
return;
}
customLanguageService.doValidation(textDocument, isKubernetes(textDocument))
.then(function (diagnosticResults) {
const diagnostics = [];
for (const diagnosticItem in diagnosticResults) {
diagnosticResults[diagnosticItem].severity = 1; //Convert all warnings to errors
diagnostics.push(diagnosticResults[diagnosticItem]);
}
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: removeDuplicatesObj(diagnostics) });
}, function (error) { });
}
/*************
* Main setup
*************/
// Create a connection for the server.
let connection: IConnection = null;
if (process.argv.indexOf('--stdio') === -1) {
connection = createConnection(ProposedFeatures.all);
} else {
connection = createConnection();
}
console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
const schemaRequestService = schemaRequestHandler.bind(this, connection);
export const customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, []);
/***********************
* Connection listeners
**********************/
/**
* Run when the client connects to the server after it is activated.
* The server receives the root path(s) of the workspace and the client capabilities.
*/
connection.onInitialize((params: InitializeParams): InitializeResult => {
capabilities = params.capabilities;
// Only try to parse the workspace root if its not null. Otherwise initialize will fail
if (params.rootUri) {
workspaceRoot = URI.parse(params.rootUri);
}
workspaceFolders = params.workspaceFolders || [];
hierarchicalDocumentSymbolSupport = !!(
capabilities.textDocument &&
capabilities.textDocument.documentSymbol &&
capabilities.textDocument.documentSymbol.hierarchicalDocumentSymbolSupport
);
clientDynamicRegisterSupport = !!(
capabilities.textDocument &&
capabilities.textDocument.rangeFormatting &&
capabilities.textDocument.rangeFormatting.dynamicRegistration
);
return {
capabilities: {
textDocumentSync: documents.syncKind,
completionProvider: { resolveProvider: true },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false,
documentRangeFormattingProvider: false
}
};
});
/**
* Received a notification from the client with schema associations from other extensions
* Update the associations in the server
*/
connection.onNotification(SchemaAssociationNotification.type, associations => {
schemaAssociations = associations;
specificValidatorPaths = [];
setSchemaStoreSettingsIfNotSet();
updateConfiguration();
});
/**
* Received a notification from the client that it can accept custom schema requests
* Register the custom schema provider and use it for requests of unknown scheme
*/
connection.onNotification(DynamicCustomSchemaRequestRegistration.type, () => {
const schemaProvider = (resource => connection.sendRequest(CustomSchemaRequest.type, resource)) as CustomSchemaProvider;
customLanguageService.registerCustomSchemaProvider(schemaProvider);
});
/**
* Run when the editor configuration is changed
* The client syncs the 'yaml', 'http.proxy', 'http.proxyStrictSSL' settings sections
* Update relevant settings with fallback to defaults if needed
*/
connection.onDidChangeConfiguration(change => {
const settings = change.settings as Settings;
configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
specificValidatorPaths = [];
if (settings.yaml) {
yamlConfigurationSettings = settings.yaml.schemas;
yamlShouldValidate = settings.yaml.validate;
yamlShouldHover = settings.yaml.hover;
yamlShouldCompletion = settings.yaml.completion;
customTags = settings.yaml.customTags ? settings.yaml.customTags : [];
if (settings.yaml.schemaStore) {
schemaStoreEnabled = settings.yaml.schemaStore.enable;
}
if (settings.yaml.format) {
yamlFormatterSettings = {
proseWrap: settings.yaml.format.proseWrap || 'preserve',
printWidth: settings.yaml.format.printWidth || 80
};
if (settings.yaml.format.singleQuote !== undefined) {
yamlFormatterSettings.singleQuote = settings.yaml.format.singleQuote;
}
if (settings.yaml.format.bracketSpacing !== undefined) {
yamlFormatterSettings.bracketSpacing = settings.yaml.format.bracketSpacing;
}
if (settings.yaml.format.enable !== undefined) {
yamlFormatterSettings.enable = settings.yaml.format.enable;
}
}
}
schemaConfigurationSettings = [];
for (const uri in yamlConfigurationSettings) {
const globPattern = yamlConfigurationSettings[uri];
const schemaObj = {
'fileMatch': Array.isArray(globPattern) ? globPattern : [globPattern],
'uri': checkSchemaURI(uri)
};
schemaConfigurationSettings.push(schemaObj);
}
setSchemaStoreSettingsIfNotSet();
updateConfiguration();
// dynamically enable & disable the formatter
if (clientDynamicRegisterSupport) {
const enableFormatter = settings && settings.yaml && settings.yaml.format && settings.yaml.format.enable;
if (enableFormatter) {
if (!formatterRegistration) {
formatterRegistration = connection.client.register(DocumentFormattingRequest.type, {
documentSelector: [
{ language: 'yaml' }
]
});
}
} else if (formatterRegistration) {
formatterRegistration.then(r => r.dispose());
formatterRegistration = null;
}
}
});
documents.onDidChangeContent(change => {
triggerValidation(change.document);
});
documents.onDidClose(event => {
cleanPendingValidation(event.document);
connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
});
/**
* Called when a monitored file is changed in an editor
* Revalidates the entire document
*/
connection.onDidChangeWatchedFiles(change => {
let hasChanges = false;
change.changes.forEach(c => {
if (customLanguageService.resetSchema(c.uri)) {
hasChanges = true;
}
});
if (hasChanges) {
documents.all().forEach(validateTextDocument);
}
});
/**
* Called when auto-complete is triggered in an editor
* Returns a list of valid completion items
*/
connection.onCompletion(textDocumentPosition => {
const textDocument = documents.get(textDocumentPosition.textDocument.uri);
const result: CompletionList = {
items: [],
isIncomplete: false
};
if (!textDocument) {
return Promise.resolve(result);
}
return customLanguageService.doComplete(textDocument, textDocumentPosition.position, isKubernetes(textDocument));
});
/**
* Like onCompletion, but called only for currently selected completion item
* Provides additional information about the item, not just the keyword
*/
connection.onCompletionResolve(completionItem => customLanguageService.doResolve(completionItem));
/**
* Called when the user hovers with their mouse over a keyword
* Returns an informational tooltip
*/
connection.onHover(textDocumentPositionParams => {
const document = documents.get(textDocumentPositionParams.textDocument.uri);
if (!document) {
return Promise.resolve(void 0);
}
return customLanguageService.doHover(document, textDocumentPositionParams.position);
});
/**
* Called when the code outline in an editor needs to be populated
* Returns a list of symbols that is then shown in the code outline
*/
connection.onDocumentSymbol(documentSymbolParams => {
const document = documents.get(documentSymbolParams.textDocument.uri);
if (!document) {
return;
}
if (hierarchicalDocumentSymbolSupport) {
return customLanguageService.findDocumentSymbols2(document);
} else {
return customLanguageService.findDocumentSymbols(document);
}
});
/**
* Called when the formatter is invoked
* Returns the formatted document content using prettier
*/
connection.onDocumentFormatting(formatParams => {
const document = documents.get(formatParams.textDocument.uri);
if (!document) {
return;
}
const customFormatterSettings = {
tabWidth: formatParams.options.tabSize,
...yamlFormatterSettings
};
return customLanguageService.doFormat(document, customFormatterSettings);
});
connection.onRequest(SchemaModificationNotification.type, (modifications: SchemaAdditions | SchemaDeletions) => {
if (modifications.action === MODIFICATION_ACTIONS.add) {
customLanguageService.modifySchemaContent(modifications);
} else if (modifications.action === MODIFICATION_ACTIONS.delete) {
customLanguageService.deleteSchemaContent(modifications);
}
return Promise.resolve();
});
// Start listening for any messages from the client
connection.listen();