jsonServerMain.ts 12.2 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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 {
8
	createConnection, IConnection,
9
	TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType,
10
	DocumentRangeFormattingRequest, Disposable, ServerCapabilities
11 12
} from 'vscode-languageserver';

13
import { DocumentColorRequest, ServerCapabilities as CPServerCapabilities, ColorPresentationRequest } from 'vscode-languageserver-protocol/lib/protocol.colorProvider.proposed';
14

J
Johannes Rieken 已提交
15
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
16
import fs = require('fs');
M
Martin Aeschlimann 已提交
17
import URI from 'vscode-uri';
18
import * as URL from 'url';
19
import Strings = require('./utils/strings');
20
import { formatError, runSafe } from './utils/errors';
21
import { JSONDocument, JSONSchema, LanguageSettings, getLanguageService, DocumentLanguageSettings } from 'vscode-json-languageservice';
J
Johannes Rieken 已提交
22
import { getLanguageModelCache } from './languageModelCache';
M
Martin Aeschlimann 已提交
23

24 25 26 27
interface ISchemaAssociations {
	[pattern: string]: string[];
}

28
namespace SchemaAssociationNotification {
29
	export const type: NotificationType<ISchemaAssociations, any> = new NotificationType('json/schemaAssociations');
30 31 32
}

namespace VSCodeContentRequest {
33
	export const type: RequestType<string, string, any, any> = new RequestType('vscode/content');
34 35
}

36 37 38 39
namespace SchemaContentChangeNotification {
	export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
}

40
// Create a connection for the server
41
let connection: IConnection = createConnection();
42

43 44 45 46
process.on('unhandledRejection', e => {
	connection.console.error(formatError(`Unhandled exception`, e));
});

M
Martin Aeschlimann 已提交
47 48 49
console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);

50 51 52 53 54 55 56
// 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);

57 58 59
let clientSnippetSupport = false;
let clientDynamicRegisterSupport = false;

60
// After the server has started the client sends an initilize request. The server receives
A
Andre Weinand 已提交
61
// in the passed params the rootPath of the workspace plus the client capabilities.
M
Martin Aeschlimann 已提交
62
connection.onInitialize((params: InitializeParams): InitializeResult => {
63

64 65 66 67 68 69 70 71 72 73
	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');
74 75 76
	let capabilities: ServerCapabilities & CPServerCapabilities = {
		// Tell the client that the server works in FULL text document sync mode
		textDocumentSync: documents.syncKind,
77
		completionProvider: clientSnippetSupport ? { resolveProvider: true, triggerCharacters: ['"', ':'] } : void 0,
78 79 80 81
		hoverProvider: true,
		documentSymbolProvider: true,
		documentRangeFormattingProvider: false,
		colorProvider: true
A
tslint  
Alex Dima 已提交
82
	};
83 84

	return { capabilities };
85 86 87
});

let workspaceContext = {
88
	resolveRelativePath: (relativePath: string, resource: string) => {
89
		return URL.resolve(resource, relativePath);
90
	}
A
tslint  
Alex Dima 已提交
91
};
92

J
Johannes Rieken 已提交
93
let schemaRequestService = (uri: string): Thenable<string> => {
94 95 96
	if (Strings.startsWith(uri, 'file://')) {
		let fsPath = URI.parse(uri).fsPath;
		return new Promise<string>((c, e) => {
97
			fs.readFile(fsPath, 'UTF-8', (err, result) => {
98
				err ? e('') : c(result.toString());
99 100
			});
		});
101 102 103
	} else if (Strings.startsWith(uri, 'vscode://')) {
		return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
			return responseText;
104
		}, error => {
M
Martin Aeschlimann 已提交
105
			return Promise.reject(error.message);
106
		});
107
	}
108 109 110 111 112 113 114 115
	if (uri.indexOf('//schema.management.azure.com/') !== -1) {
		connection.telemetry.logEvent({
			key: 'json.schema',
			value: {
				schemaURL: uri
			}
		});
	}
116 117
	let headers = { 'Accept-Encoding': 'gzip, deflate' };
	return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
118 119
		return response.responseText;
	}, (error: XHRResponse) => {
120
		return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
121
	});
A
tslint  
Alex Dima 已提交
122
};
123

124
// create the JSON language service
125 126 127
let languageService = getLanguageService({
	schemaRequestService,
	workspaceContext,
128
	contributions: []
129 130
});

131
// The settings interface describes the server relevant settings part
132
interface Settings {
133
	json: {
A
tslint  
Alex Dima 已提交
134
		schemas: JSONSchemaSettings[];
135
		format: { enable: boolean; };
A
tslint  
Alex Dima 已提交
136
	};
M
Martin Aeschlimann 已提交
137
	http: {
A
tslint  
Alex Dima 已提交
138 139 140
		proxy: string;
		proxyStrictSSL: boolean;
	};
141 142
}

143
interface JSONSchemaSettings {
A
tslint  
Alex Dima 已提交
144 145
	fileMatch?: string[];
	url?: string;
146
	schema?: JSONSchema;
147 148
}

M
Martin Aeschlimann 已提交
149 150
let jsonConfigurationSettings: JSONSchemaSettings[] = void 0;
let schemaAssociations: ISchemaAssociations = void 0;
151
let formatterRegistration: Thenable<Disposable> = null;
152

153 154
// The settings have changed. Is send on server activation as well.
connection.onDidChangeConfiguration((change) => {
A
tslint  
Alex Dima 已提交
155
	var settings = <Settings>change.settings;
156
	configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
A
tslint  
Alex Dima 已提交
157

158
	jsonConfigurationSettings = settings.json && settings.json.schemas;
159
	updateConfiguration();
160 161 162 163 164 165

	// dynamically enable & disable the formatter
	if (clientDynamicRegisterSupport) {
		let enableFormatter = settings && settings.json && settings.json.format && settings.json.format.enable;
		if (enableFormatter) {
			if (!formatterRegistration) {
166
				formatterRegistration = connection.client.register(DocumentRangeFormattingRequest.type, { documentSelector: [{ language: 'json' }, { language: 'jsonc' }] });
167 168 169 170 171 172
			}
		} else if (formatterRegistration) {
			formatterRegistration.then(r => r.dispose());
			formatterRegistration = null;
		}
	}
173
});
174

175
// The jsonValidation extension configuration has changed
176
connection.onNotification(SchemaAssociationNotification.type, associations => {
177 178 179 180
	schemaAssociations = associations;
	updateConfiguration();
});

181 182 183 184 185
// A schema has changed
connection.onNotification(SchemaContentChangeNotification.type, uri => {
	languageService.resetSchema(uri);
});

186
function updateConfiguration() {
J
Johannes Rieken 已提交
187
	let languageSettings: LanguageSettings = {
188 189 190 191
		validate: true,
		allowComments: true,
		schemas: []
	};
192 193 194 195
	if (schemaAssociations) {
		for (var pattern in schemaAssociations) {
			let association = schemaAssociations[pattern];
			if (Array.isArray(association)) {
M
Martin Aeschlimann 已提交
196
				association.forEach(uri => {
197
					languageSettings.schemas.push({ uri, fileMatch: [pattern] });
A
tslint  
Alex Dima 已提交
198
				});
199 200 201 202
			}
		}
	}
	if (jsonConfigurationSettings) {
203
		jsonConfigurationSettings.forEach((schema, index) => {
204 205
			let uri = schema.url;
			if (!uri && schema.schema) {
206
				uri = schema.schema.id || `vscode://schemas/custom/${index}`;
207 208 209
			}
			if (uri) {
				languageSettings.schemas.push({ uri, fileMatch: schema.fileMatch, schema: schema.schema });
210 211 212
			}
		});
	}
213
	languageService.configure(languageSettings);
M
Martin Aeschlimann 已提交
214

215
	// Revalidate any open text documents
216
	documents.all().forEach(triggerValidation);
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent((change) => {
	triggerValidation(change.document);
});

// a document has closed: clear all diagnostics
documents.onDidClose(event => {
	cleanPendingValidation(event.document);
	connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
});

J
Johannes Rieken 已提交
231
let pendingValidationRequests: { [uri: string]: NodeJS.Timer; } = {};
232
const validationDelayMs = 500;
233

234
function cleanPendingValidation(textDocument: TextDocument): void {
235 236 237
	let request = pendingValidationRequests[textDocument.uri];
	if (request) {
		clearTimeout(request);
238
		delete pendingValidationRequests[textDocument.uri];
239
	}
240 241 242 243
}

function triggerValidation(textDocument: TextDocument): void {
	cleanPendingValidation(textDocument);
244 245 246 247 248
	pendingValidationRequests[textDocument.uri] = setTimeout(() => {
		delete pendingValidationRequests[textDocument.uri];
		validateTextDocument(textDocument);
	}, validationDelayMs);
}
249

250
function validateTextDocument(textDocument: TextDocument): void {
251 252 253 254 255
	if (textDocument.getText().length === 0) {
		// ignore empty documents
		connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
		return;
	}
256 257
	try {
		let jsonDocument = getJSONDocument(textDocument);
258

259 260 261 262 263 264 265 266
		let documentSettings: DocumentLanguageSettings = textDocument.languageId === 'jsonc' ? { comments: 'ignore', trailingCommas: 'ignore' } : { comments: 'error', trailingCommas: 'error' };
		languageService.doValidation(textDocument, jsonDocument, documentSettings).then(diagnostics => {
			// Send the computed diagnostics to VSCode.
			connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
		});
	} catch (e) {
		connection.console.error(formatError(`Error while validating ${textDocument.uri}`, e));
	}
267 268 269
}

connection.onDidChangeWatchedFiles((change) => {
270
	// Monitored files have changed in VSCode
271 272
	let hasChanges = false;
	change.changes.forEach(c => {
M
Martin Aeschlimann 已提交
273
		if (languageService.resetSchema(c.uri)) {
274 275 276 277 278 279 280 281
			hasChanges = true;
		}
	});
	if (hasChanges) {
		documents.all().forEach(validateTextDocument);
	}
});

282
let jsonDocuments = getLanguageModelCache<JSONDocument>(10, 60, document => languageService.parseJSONDocument(document));
283 284 285 286 287 288
documents.onDidClose(e => {
	jsonDocuments.onDocumentRemoved(e.document);
});
connection.onShutdown(() => {
	jsonDocuments.dispose();
});
289

290
function getJSONDocument(document: TextDocument): JSONDocument {
291
	return jsonDocuments.get(document);
292 293
}

M
Martin Aeschlimann 已提交
294
connection.onCompletion(textDocumentPosition => {
295 296 297 298 299
	return runSafe(() => {
		let document = documents.get(textDocumentPosition.textDocument.uri);
		let jsonDocument = getJSONDocument(document);
		return languageService.doComplete(document, textDocumentPosition.position, jsonDocument);
	}, null, `Error while computing completions for ${textDocumentPosition.textDocument.uri}`);
300 301
});

M
Martin Aeschlimann 已提交
302
connection.onCompletionResolve(completionItem => {
303 304 305
	return runSafe(() => {
		return languageService.doResolve(completionItem);
	}, null, `Error while resolving completion proposal`);
306 307
});

M
Martin Aeschlimann 已提交
308
connection.onHover(textDocumentPositionParams => {
309 310 311 312 313
	return runSafe(() => {
		let document = documents.get(textDocumentPositionParams.textDocument.uri);
		let jsonDocument = getJSONDocument(document);
		return languageService.doHover(document, textDocumentPositionParams.position, jsonDocument);
	}, null, `Error while computing hover for ${textDocumentPositionParams.textDocument.uri}`);
314 315
});

M
Martin Aeschlimann 已提交
316
connection.onDocumentSymbol(documentSymbolParams => {
317 318 319 320 321
	return runSafe(() => {
		let document = documents.get(documentSymbolParams.textDocument.uri);
		let jsonDocument = getJSONDocument(document);
		return languageService.findDocumentSymbols(document, jsonDocument);
	}, [], `Error while computing document symbols for ${documentSymbolParams.textDocument.uri}`);
322 323
});

M
Martin Aeschlimann 已提交
324
connection.onDocumentRangeFormatting(formatParams => {
325 326 327 328
	return runSafe(() => {
		let document = documents.get(formatParams.textDocument.uri);
		return languageService.format(document, formatParams.range, formatParams.options);
	}, [], `Error while formatting range for ${formatParams.textDocument.uri}`);
329 330
});

331
connection.onRequest(DocumentColorRequest.type, params => {
332 333 334 335 336 337 338 339
	return runSafe(() => {
		let document = documents.get(params.textDocument.uri);
		if (document) {
			let jsonDocument = getJSONDocument(document);
			return languageService.findDocumentColors(document, jsonDocument);
		}
		return [];
	}, [], `Error while computing document colors for ${params.textDocument.uri}`);
M
Martin Aeschlimann 已提交
340 341
});

342
connection.onRequest(ColorPresentationRequest.type, params => {
343 344 345 346 347 348 349 350
	return runSafe(() => {
		let document = documents.get(params.textDocument.uri);
		if (document) {
			let jsonDocument = getJSONDocument(document);
			return languageService.getColorPresentations(document, jsonDocument, params.color, params.range);
		}
		return [];
	}, [], `Error while computing color presentationsd for ${params.textDocument.uri}`);
351 352
});

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