-
Notifications
You must be signed in to change notification settings - Fork 264
/
server.ts
executable file
·430 lines (370 loc) · 14.7 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
/*---------------------------------------------------------------------------------------------
* 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
} 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 { YAMLDocument, JSONSchema, LanguageSettings, getLanguageService } from 'vscode-yaml-languageservice';
import { getLineOffsets, removeDuplicatesObj } from './languageService/utils/arrUtils';
import { getLanguageService as getCustomLanguageService } from './languageService/yamlLanguageService';
import * as nls from 'vscode-nls';
import { FilePatternAssociation } from './languageService/services/jsonSchemaService';
import { parse as parseYAML } from './languageService/parser/yamlParser';
nls.config(process.env['VSCODE_NLS_CONFIG']);
interface ISchemaAssociations {
[pattern: string]: string[];
}
namespace SchemaAssociationNotification {
export const type: NotificationType<{}, {}> = new NotificationType('json/schemaAssociations');
}
namespace VSCodeContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('vscode/content');
}
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(new IPCMessageReader(process), new IPCMessageWriter(process));
} 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 clientSnippetSupport = false;
let clientDynamicRegisterSupport = 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 workspaceRoot: URI;
connection.onInitialize((params: InitializeParams): InitializeResult => {
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;
}
clientSnippetSupport = hasClientCapability('textDocument', 'completion', 'completionItem', 'snippetSupport');
clientDynamicRegisterSupport = hasClientCapability('workspace', 'symbol', '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> => {
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;
});
}
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());
});
};
// create the YAML language service
export let languageService = getLanguageService({
schemaRequestService,
workspaceContext,
contributions: []
});
export let KUBERNETES_SCHEMA_URL = "http://central.maven.org/maven2/io/fabric8/kubernetes-model/2.0.0/kubernetes-model-2.0.0-schema.json";
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, []);
// The settings interface describes the server relevant settings part
interface Settings {
yaml: {
format: { enable: boolean; };
schemas: JSONSchemaSettings[];
validate: boolean
};
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;
connection.onDidChangeConfiguration((change) => {
var settings = <Settings>change.settings;
configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
specificValidatorPaths = [];
yamlConfigurationSettings = settings.yaml && settings.yaml.schemas;
yamlShouldValidate = settings.yaml && settings.yaml.validate;
schemaConfigurationSettings = [];
for(let url in yamlConfigurationSettings){
let globPattern = yamlConfigurationSettings[url];
let schemaObj = {
"fileMatch": Array.isArray(globPattern) ? globPattern : [globPattern],
"url": url
}
schemaConfigurationSettings.push(schemaObj);
}
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;
}
}
});
connection.onNotification(SchemaAssociationNotification.type, associations => {
schemaAssociations = associations;
specificValidatorPaths = [];
updateConfiguration();
});
function updateConfiguration() {
let languageSettings: LanguageSettings = {
validate: yamlShouldValidate,
schemas: []
};
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) {
// workspace relative path
uri = URI.file(path.normalize(path.join(workspaceRoot.fsPath, uri))).toString();
}
languageSettings = configureSchemas(uri, schema.fileMatch, schema.schema, languageSettings);
}
});
}
languageService.configure(languageSettings);
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(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;
}
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.getText().length === 0) {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
return;
}
let yamlDocument = parseYAML(textDocument.getText());
let isKubernetesFile = isKubernetes(textDocument);
customLanguageService.doValidation(textDocument, yamlDocument, isKubernetesFile).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){});
}
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;
}
connection.onDidChangeWatchedFiles((change) => {
// Monitored files have changed in VSCode
let hasChanges = false;
change.changes.forEach(c => {
if (languageService.resetSchema(c.uri)) {
hasChanges = true;
}
});
if (hasChanges) {
documents.all().forEach(validateTextDocument);
}
});
connection.onCompletion(textDocumentPosition => {
let textDocument = documents.get(textDocumentPosition.textDocument.uri);
let isKubernetesFile = isKubernetes(textDocument);
let completionFix = completionHelper(textDocument, textDocumentPosition.position);
let newText = completionFix.newText;
let jsonDocument = parseYAML(newText);
return customLanguageService.doComplete(textDocument, textDocumentPosition.position, jsonDocument, isKubernetesFile);
});
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;
}
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.
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + "holder:\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + "holder:\r\n" + document.getText().substr(end+2);
}
//For when missing semi colon case
}else{
//Add a semicolon to the end of the current line so we can validate the node
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + ":\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + ":\r\n" + document.getText().substr(end+2);
}
}
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);
let jsonDocument = parseYAML(document.getText());
let isKubernetesFile = isKubernetes(textDocumentPositionParams.textDocument)
return customLanguageService.doHover(document, textDocumentPositionParams.position, jsonDocument, isKubernetesFile);
});
connection.onDocumentSymbol(documentSymbolParams => {
let document = documents.get(documentSymbolParams.textDocument.uri);
let jsonDocument = parseYAML(document.getText());
return customLanguageService.findDocumentSymbols(document, jsonDocument);
});
connection.onDocumentFormatting(formatParams => {
let document = documents.get(formatParams.textDocument.uri);
return languageService.format(document, formatParams.options);
});
connection.listen();