jsonServerMain.ts 10.4 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 14
import { DocumentColorRequest, ServerCapabilities as CPServerCapabilities } from 'vscode-languageserver-protocol/lib/protocol.colorProvider.proposed';

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');
J
Johannes Rieken 已提交
20 21
import { JSONDocument, JSONSchema, LanguageSettings, getLanguageService } from 'vscode-json-languageservice';
import { getLanguageModelCache } from './languageModelCache';
M
Martin Aeschlimann 已提交
22

23 24 25
import * as nls from 'vscode-nls';
nls.config(process.env['VSCODE_NLS_CONFIG']);

26 27 28 29
interface ISchemaAssociations {
	[pattern: string]: string[];
}

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

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

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

M
Martin Aeschlimann 已提交
41 42 43
console.log = connection.console.log.bind(connection.console);
console.error = connection.console.error.bind(connection.console);

44 45 46 47 48 49 50
// 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);

51 52 53
let clientSnippetSupport = false;
let clientDynamicRegisterSupport = false;

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

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

	return { capabilities };
79 80 81
});

let workspaceContext = {
82
	resolveRelativePath: (relativePath: string, resource: string) => {
83
		return URL.resolve(resource, relativePath);
84
	}
A
tslint  
Alex Dima 已提交
85
};
86

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

118
// create the JSON language service
119 120 121
let languageService = getLanguageService({
	schemaRequestService,
	workspaceContext,
122
	contributions: []
123 124
});

125
// The settings interface describes the server relevant settings part
126
interface Settings {
127
	json: {
A
tslint  
Alex Dima 已提交
128
		schemas: JSONSchemaSettings[];
129
		format: { enable: boolean; };
A
tslint  
Alex Dima 已提交
130
	};
M
Martin Aeschlimann 已提交
131
	http: {
A
tslint  
Alex Dima 已提交
132 133 134
		proxy: string;
		proxyStrictSSL: boolean;
	};
135 136
}

137
interface JSONSchemaSettings {
A
tslint  
Alex Dima 已提交
138 139
	fileMatch?: string[];
	url?: string;
140
	schema?: JSONSchema;
141 142
}

M
Martin Aeschlimann 已提交
143 144
let jsonConfigurationSettings: JSONSchemaSettings[] = void 0;
let schemaAssociations: ISchemaAssociations = void 0;
145
let formatterRegistration: Thenable<Disposable> = null;
146

147 148
// The settings have changed. Is send on server activation as well.
connection.onDidChangeConfiguration((change) => {
A
tslint  
Alex Dima 已提交
149
	var settings = <Settings>change.settings;
150
	configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
A
tslint  
Alex Dima 已提交
151

152
	jsonConfigurationSettings = settings.json && settings.json.schemas;
153
	updateConfiguration();
154 155 156 157 158 159 160 161 162 163 164 165 166

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

169
// The jsonValidation extension configuration has changed
170
connection.onNotification(SchemaAssociationNotification.type, associations => {
171 172 173 174 175
	schemaAssociations = associations;
	updateConfiguration();
});

function updateConfiguration() {
J
Johannes Rieken 已提交
176
	let languageSettings: LanguageSettings = {
177 178 179 180
		validate: true,
		allowComments: true,
		schemas: []
	};
181 182 183 184
	if (schemaAssociations) {
		for (var pattern in schemaAssociations) {
			let association = schemaAssociations[pattern];
			if (Array.isArray(association)) {
M
Martin Aeschlimann 已提交
185
				association.forEach(uri => {
186
					languageSettings.schemas.push({ uri, fileMatch: [pattern] });
A
tslint  
Alex Dima 已提交
187
				});
188 189 190 191
			}
		}
	}
	if (jsonConfigurationSettings) {
192
		jsonConfigurationSettings.forEach((schema, index) => {
193 194
			let uri = schema.url;
			if (!uri && schema.schema) {
195
				uri = schema.schema.id || `vscode://schemas/custom/${index}`;
196 197 198
			}
			if (uri) {
				languageSettings.schemas.push({ uri, fileMatch: schema.fileMatch, schema: schema.schema });
199 200 201
			}
		});
	}
202
	languageService.configure(languageSettings);
M
Martin Aeschlimann 已提交
203

204
	// Revalidate any open text documents
205
	documents.all().forEach(triggerValidation);
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219
// 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 已提交
220
let pendingValidationRequests: { [uri: string]: NodeJS.Timer; } = {};
221 222
const validationDelayMs = 200;

223
function cleanPendingValidation(textDocument: TextDocument): void {
224 225 226
	let request = pendingValidationRequests[textDocument.uri];
	if (request) {
		clearTimeout(request);
227
		delete pendingValidationRequests[textDocument.uri];
228
	}
229 230 231 232
}

function triggerValidation(textDocument: TextDocument): void {
	cleanPendingValidation(textDocument);
233 234 235 236 237
	pendingValidationRequests[textDocument.uri] = setTimeout(() => {
		delete pendingValidationRequests[textDocument.uri];
		validateTextDocument(textDocument);
	}, validationDelayMs);
}
238

239
function validateTextDocument(textDocument: TextDocument): void {
240 241 242 243 244 245
	if (textDocument.getText().length === 0) {
		// ignore empty documents
		connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
		return;
	}

246
	let jsonDocument = getJSONDocument(textDocument);
M
Martin Aeschlimann 已提交
247
	languageService.doValidation(textDocument, jsonDocument).then(diagnostics => {
248 249 250 251 252 253
		// Send the computed diagnostics to VSCode.
		connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
	});
}

connection.onDidChangeWatchedFiles((change) => {
254
	// Monitored files have changed in VSCode
255 256
	let hasChanges = false;
	change.changes.forEach(c => {
M
Martin Aeschlimann 已提交
257
		if (languageService.resetSchema(c.uri)) {
258 259 260 261 262 263 264 265
			hasChanges = true;
		}
	});
	if (hasChanges) {
		documents.all().forEach(validateTextDocument);
	}
});

266
let jsonDocuments = getLanguageModelCache<JSONDocument>(10, 60, document => languageService.parseJSONDocument(document));
267 268 269 270 271 272
documents.onDidClose(e => {
	jsonDocuments.onDocumentRemoved(e.document);
});
connection.onShutdown(() => {
	jsonDocuments.dispose();
});
273

274
function getJSONDocument(document: TextDocument): JSONDocument {
275
	return jsonDocuments.get(document);
276 277
}

M
Martin Aeschlimann 已提交
278
connection.onCompletion(textDocumentPosition => {
279
	let document = documents.get(textDocumentPosition.textDocument.uri);
280
	let jsonDocument = getJSONDocument(document);
M
Martin Aeschlimann 已提交
281
	return languageService.doComplete(document, textDocumentPosition.position, jsonDocument);
282 283
});

M
Martin Aeschlimann 已提交
284
connection.onCompletionResolve(completionItem => {
M
Martin Aeschlimann 已提交
285
	return languageService.doResolve(completionItem);
286 287
});

M
Martin Aeschlimann 已提交
288 289
connection.onHover(textDocumentPositionParams => {
	let document = documents.get(textDocumentPositionParams.textDocument.uri);
290
	let jsonDocument = getJSONDocument(document);
M
Martin Aeschlimann 已提交
291
	return languageService.doHover(document, textDocumentPositionParams.position, jsonDocument);
292 293
});

M
Martin Aeschlimann 已提交
294
connection.onDocumentSymbol(documentSymbolParams => {
295
	let document = documents.get(documentSymbolParams.textDocument.uri);
296
	let jsonDocument = getJSONDocument(document);
M
Martin Aeschlimann 已提交
297
	return languageService.findDocumentSymbols(document, jsonDocument);
298 299
});

M
Martin Aeschlimann 已提交
300
connection.onDocumentRangeFormatting(formatParams => {
301
	let document = documents.get(formatParams.textDocument.uri);
M
Martin Aeschlimann 已提交
302
	return languageService.format(document, formatParams.range, formatParams.options);
303 304
});

305 306
connection.onRequest(DocumentColorRequest.type, params => {
	let document = documents.get(params.textDocument.uri);
M
Martin Aeschlimann 已提交
307 308
	if (document) {
		let jsonDocument = getJSONDocument(document);
309
		return languageService.findDocumentColors(document, jsonDocument);
M
Martin Aeschlimann 已提交
310 311 312 313
	}
	return [];
});

314 315
// Listen on the connection
connection.listen();