-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathserver.ts
More file actions
executable file
·593 lines (498 loc) · 19.3 KB
/
server.ts
File metadata and controls
executable file
·593 lines (498 loc) · 19.3 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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*---------------------------------------------------------------------------------------------
* 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, NotificationType, RequestType,
DocumentFormattingRequest, Disposable, Range, IPCMessageReader, IPCMessageWriter, DiagnosticSeverity, Position,
Proposed, ProposedFeatures, CompletionList
} from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import path = require('path');
import fs = require('fs');
import URI from './languageservice/utils/uri';
import * as URL from 'url';
import Strings = require('./languageservice/utils/strings');
import { getLineOffsets, removeDuplicatesObj } from './languageservice/utils/arrUtils';
import { getLanguageService as getCustomLanguageService, LanguageSettings, CustomFormatterOptions } from './languageservice/yamlLanguageService';
import * as nls from 'vscode-nls';
import { FilePatternAssociation, CustomSchemaProvider } from './languageservice/services/jsonSchemaService';
import { parse as parseYAML } from './languageservice/parser/yamlParser';
import { JSONDocument } from './languageservice/parser/jsonParser';
import { JSONSchema } from './languageservice/jsonSchema';
nls.config(<any>process.env['VSCODE_NLS_CONFIG']);
interface ISchemaAssociations {
[pattern: string]: string[];
}
namespace SchemaAssociationNotification {
export const type: NotificationType<{}, {}> = new NotificationType('json/schemaAssociations');
}
namespace DynamicCustomSchemaRequestRegistration {
export const type: NotificationType<{}, {}> = new NotificationType('yaml/registerCustomSchemaRequest');
}
namespace VSCodeContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('vscode/content');
}
namespace CustomSchemaContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('custom/schema/content');
}
namespace CustomSchemaRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('custom/schema/request');
}
namespace ColorSymbolRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('json/colorSymbols');
}
// 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);
// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments = new TextDocuments();
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
let clientDynamicRegisterSupport = false;
let hasWorkspaceFolderCapability = false;
// After the server has started the client sends an initilize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilities.
let capabilities;
let workspaceFolders = [];
let workspaceRoot: URI;
connection.onInitialize((params: InitializeParams): InitializeResult => {
capabilities = params.capabilities;
workspaceFolders = params["workspaceFolders"];
workspaceRoot = URI.parse(params.rootPath);
function hasClientCapability(...keys: string[]) {
let c = params.capabilities;
for (let i = 0; c && i < keys.length; i++) {
c = c[keys[i]];
}
return !!c;
}
hasWorkspaceFolderCapability = capabilities.workspace && !!capabilities.workspace.workspaceFolders;
clientDynamicRegisterSupport = hasClientCapability('textDocument', 'formatting', 'dynamicRegistration');
return {
capabilities: {
textDocumentSync: documents.syncKind,
completionProvider: { resolveProvider: true },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false
}
};
});
let workspaceContext = {
resolveRelativePath: (relativePath: string, resource: string) => {
return URL.resolve(resource, relativePath);
}
};
let schemaRequestService = (uri: string): Thenable<string> => {
//For the case when we are multi root and specify a workspace location
if(hasWorkspaceFolderCapability){
for(let folder in workspaceFolders){
let currFolder = workspaceFolders[folder];
let currFolderUri = currFolder["uri"];
let currFolderName = currFolder["name"];
let isUriRegex = new RegExp('^(?:[a-z]+:)?//', 'i');
if(uri.indexOf(currFolderName) !== -1 && !uri.match(isUriRegex)){
let beforeFolderName = currFolderUri.split(currFolderName)[0];
let uriSplit = uri.split(currFolderName);
uriSplit.shift()
let afterFolderName = uriSplit.join(currFolderName);
uri = beforeFolderName + currFolderName + afterFolderName;
}
}
}
if (Strings.startsWith(uri, 'file://')) {
let fsPath = URI.parse(uri).fsPath;
return new Promise<string>((c, e) => {
fs.readFile(fsPath, 'UTF-8', (err, result) => {
err ? e('') : c(result.toString());
});
});
} else if (Strings.startsWith(uri, 'vscode://')) {
return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
return responseText;
}, error => {
return error.message;
});
} else {
let scheme = URI.parse(uri).scheme.toLowerCase();
if (scheme !== 'http' && scheme !== 'https') {
// custom scheme
return <Thenable<string>>connection.sendRequest(CustomSchemaContentRequest.type, uri);
}
}
if (uri.indexOf('//schema.management.azure.com/') !== -1) {
connection.telemetry.logEvent({
key: 'json.schema',
value: {
schemaURL: uri
}
});
}
let headers = { 'Accept-Encoding': 'gzip, deflate' };
return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
return response.responseText;
}, (error: XHRResponse) => {
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
});
};
export let KUBERNETES_SCHEMA_URL = "https://gist.githubusercontent.com/JPinkney/ccaf3909ef811e5657ca2e2e1fa05d76/raw/f85e51bfb67fdb99ab7653c2953b60087cc871ea/openshift_schema_all.json";
export let KEDGE_SCHEMA_URL = "https://raw.githubusercontent.com/kedgeproject/json-schema/master/master/kedge-json-schema.json";
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, []);
// The settings interface describes the server relevant settings part
interface Settings {
yaml: {
format: CustomFormatterOptions & {
enable: boolean;
};
schemas: JSONSchemaSettings[];
validate: boolean;
hover: boolean;
completion: boolean;
customTags: Array<String>;
};
http: {
proxy: string;
proxyStrictSSL: boolean;
};
}
interface JSONSchemaSettings {
fileMatch?: string[];
url?: string;
schema?: JSONSchema;
}
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"
} as CustomFormatterOptions;
let yamlShouldHover = true;
let yamlShouldCompletion = true;
let schemaStoreSettings = [];
let customTags = [];
connection.onDidChangeConfiguration((change) => {
var settings = <Settings>change.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.format) {
yamlFormatterSettings = {
singleQuote: settings.yaml.format.singleQuote || false,
proseWrap: settings.yaml.format.proseWrap || "preserve"
};
if (settings.yaml.format.bracketSpacing === false) {
yamlFormatterSettings.bracketSpacing = false;
}
}
}
schemaConfigurationSettings = [];
for(let url in yamlConfigurationSettings){
let globPattern = yamlConfigurationSettings[url];
let schemaObj = {
"fileMatch": Array.isArray(globPattern) ? globPattern : [globPattern],
"url": url
}
schemaConfigurationSettings.push(schemaObj);
}
setSchemaStoreSettingsIfNotSet();
updateConfiguration();
// dynamically enable & disable the formatter
if (clientDynamicRegisterSupport) {
let 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;
}
}
});
function setSchemaStoreSettingsIfNotSet(){
if(schemaStoreSettings.length === 0){
getSchemaStoreMatchingSchemas().then(schemaStore => {
schemaStoreSettings = schemaStore.schemas;
updateConfiguration();
});
}
}
function getSchemaStoreMatchingSchemas(){
return xhr({ url: "http://schemastore.org/api/json/catalog.json" }).then(response => {
let languageSettings= {
schemas: []
};
let schemas = JSON.parse(response.responseText);
for(let schemaIndex in schemas.schemas){
let schema = schemas.schemas[schemaIndex];
if(schema && schema.fileMatch){
for(let fileMatch in schema.fileMatch){
let currFileMatch = schema.fileMatch[fileMatch];
if(currFileMatch.indexOf('.yml') !== -1 || currFileMatch.indexOf('.yaml') !== -1){
languageSettings.schemas.push({ uri: schema.url, fileMatch: [currFileMatch] });
}
}
}
}
return languageSettings;
}, (error: XHRResponse) => {
throw error;
});
}
connection.onNotification(SchemaAssociationNotification.type, associations => {
schemaAssociations = associations;
specificValidatorPaths = [];
setSchemaStoreSettingsIfNotSet();
updateConfiguration();
});
connection.onNotification(DynamicCustomSchemaRequestRegistration.type, () => {
const schemaProvider = ((resource) => connection.sendRequest(CustomSchemaRequest.type, resource)) as CustomSchemaProvider;
customLanguageService.registerCustomSchemaProvider(schemaProvider);
});
function updateConfiguration() {
let languageSettings: LanguageSettings = {
validate: yamlShouldValidate,
hover: yamlShouldHover,
completion: yamlShouldCompletion,
schemas: [],
customTags: customTags
};
if (schemaAssociations) {
for (var pattern in schemaAssociations) {
let association = schemaAssociations[pattern];
if (Array.isArray(association)) {
association.forEach(uri => {
languageSettings = configureSchemas(uri, [pattern], null, languageSettings);
});
}
}
}
if (schemaConfigurationSettings) {
schemaConfigurationSettings.forEach(schema => {
let uri = schema.url;
if (!uri && schema.schema) {
uri = schema.schema.id;
}
if (!uri && schema.fileMatch) {
uri = 'vscode://schemas/custom/' + encodeURIComponent(schema.fileMatch.join('&'));
}
if (uri) {
if (uri[0] === '.' && workspaceRoot && !hasWorkspaceFolderCapability) {
// workspace relative path
uri = URI.file(path.normalize(path.join(workspaceRoot.fsPath, uri))).toString();
}
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);
}
function configureSchemas(uri, fileMatch, schema, languageSettings){
if(uri.toLowerCase().trim() === "kubernetes"){
uri = KUBERNETES_SCHEMA_URL;
}
if(uri.toLowerCase().trim() === "kedge"){
uri = KEDGE_SCHEMA_URL;
}
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 setKubernetesParserOption(jsonDocuments: JSONDocument[], option: boolean){
for(let jsonDoc in jsonDocuments){
jsonDocuments[jsonDoc].configureSettings({
isKubernetes: option
});
}
}
function isKubernetes(textDocument){
for(let path in specificValidatorPaths){
let globPath = specificValidatorPaths[path];
let fpa = new FilePatternAssociation(globPath);
if(fpa.matchesPattern(textDocument.uri)){
return true;
}
}
return false;
}
documents.onDidChangeContent((change) => {
triggerValidation(change.document);
});
documents.onDidClose(event => {
cleanPendingValidation(event.document);
connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
});
let pendingValidationRequests: { [uri: string]: NodeJS.Timer; } = {};
const validationDelayMs = 200;
function cleanPendingValidation(textDocument: TextDocument): void {
let 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;
}
let yamlDocument = parseYAML(textDocument.getText(), customTags);
isKubernetes(textDocument) ? setKubernetesParserOption(yamlDocument.documents, true) : setKubernetesParserOption(yamlDocument.documents, false);
customLanguageService.doValidation(textDocument, yamlDocument).then(function(diagnosticResults){
let diagnostics = [];
for(let 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){});
}
connection.onDidChangeWatchedFiles((change) => {
// Monitored files have changed in VSCode
let hasChanges = false;
change.changes.forEach(c => {
if (customLanguageService.resetSchema(c.uri)) {
hasChanges = true;
}
});
if (hasChanges) {
documents.all().forEach(validateTextDocument);
}
});
connection.onCompletion(textDocumentPosition => {
let textDocument = documents.get(textDocumentPosition.textDocument.uri);
let result: CompletionList = {
items: [],
isIncomplete: false
};
if(!textDocument){
return Promise.resolve(result);
}
let completionFix = completionHelper(textDocument, textDocumentPosition.position);
let newText = completionFix.newText;
let jsonDocument = parseYAML(newText);
isKubernetes(textDocument) ? setKubernetesParserOption(jsonDocument.documents, true) : setKubernetesParserOption(jsonDocument.documents, false);
return customLanguageService.doComplete(textDocument, textDocumentPosition.position, jsonDocument);
});
function is_EOL(c) {
return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);
}
function completionHelper(document: TextDocument, textDocumentPosition: Position){
//Get the string we are looking at via a substring
let linePos = textDocumentPosition.line;
let position = textDocumentPosition;
let lineOffset = getLineOffsets(document.getText());
let start = lineOffset[linePos]; //Start of where the autocompletion is happening
let end = 0; //End of where the autocompletion is happening
if(lineOffset[linePos+1]){
end = lineOffset[linePos+1];
}else{
end = document.getText().length;
}
while (end - 1 >= 0 && is_EOL(document.getText().charCodeAt(end - 1))) {
end--;
}
let textLine = document.getText().substring(start, end);
//Check if the string we are looking at is a node
if(textLine.indexOf(":") === -1){
//We need to add the ":" to load the nodes
let newText = "";
//This is for the empty line case
let trimmedText = textLine.trim();
if(trimmedText.length === 0 || (trimmedText.length === 1 && trimmedText[0] === '-')){
//Add a temp node that is in the document but we don't use at all.
newText = document.getText().substring(0, start + textLine.length) + (trimmedText[0] === '-' && !textLine.endsWith(" ") ? " " : "") + "holder:\r\n" + document.getText().substr(lineOffset[linePos + 1] || document.getText().length);
//For when missing semi colon case
}else{
//Add a semicolon to the end of the current line so we can validate the node
newText = document.getText().substring(0, start+textLine.length) + ":\r\n" + document.getText().substr(lineOffset[linePos+1] || document.getText().length);
}
return {
"newText": newText,
"newPosition": textDocumentPosition
}
}else{
//All the nodes are loaded
position.character = position.character - 1;
return {
"newText": document.getText(),
"newPosition": position
}
}
}
connection.onCompletionResolve(completionItem => {
return customLanguageService.doResolve(completionItem);
});
connection.onHover(textDocumentPositionParams => {
let document = documents.get(textDocumentPositionParams.textDocument.uri);
if(!document){
return Promise.resolve(void 0);
}
let jsonDocument = parseYAML(document.getText());
isKubernetes(document) ? setKubernetesParserOption(jsonDocument.documents, true) : setKubernetesParserOption(jsonDocument.documents, false);
return customLanguageService.doHover(document, textDocumentPositionParams.position, jsonDocument);
});
connection.onDocumentSymbol(documentSymbolParams => {
let document = documents.get(documentSymbolParams.textDocument.uri);
if(!document){
return;
}
let jsonDocument = parseYAML(document.getText());
return customLanguageService.findDocumentSymbols(document, jsonDocument);
});
connection.onDocumentFormatting(formatParams => {
let document = documents.get(formatParams.textDocument.uri);
if(!document){
return;
}
return customLanguageService.doFormat(document, yamlFormatterSettings);
});
connection.listen();