diff --git a/extensions/json/client/src/jsonMain.ts b/extensions/json/client/src/jsonMain.ts index 3705a050212..04b0d918bff 100644 --- a/extensions/json/client/src/jsonMain.ts +++ b/extensions/json/client/src/jsonMain.ts @@ -57,12 +57,11 @@ export function activate(context: ExtensionContext) { documentSelector: ['json'], synchronize: { // Synchronize the setting section 'json' to the server - configurationSection: ['json.schemas', 'http.proxy', 'http.proxyStrictSSL'], + configurationSection: ['json', 'http.proxy', 'http.proxyStrictSSL'], fileEvents: workspace.createFileSystemWatcher('**/*.json') }, initializationOptions: { - languageIds, - ['format.enable']: workspace.getConfiguration('json').get('format.enable') + languageIds } }; diff --git a/extensions/json/server/src/jsonServerMain.ts b/extensions/json/server/src/jsonServerMain.ts index daafb4e19fb..3719674ec66 100644 --- a/extensions/json/server/src/jsonServerMain.ts +++ b/extensions/json/server/src/jsonServerMain.ts @@ -6,7 +6,8 @@ import { createConnection, IConnection, - TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType + TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType, + DocumentRangeFormattingRequest, Disposable } from 'vscode-languageserver'; import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light'; @@ -49,6 +50,9 @@ let documents: TextDocuments = new TextDocuments(); // for open, change and close text document events documents.listen(connection); +let clientSnippetSupport = false; +let clientDynamicRegisterSupport = false; + const filesAssociationContribution = new FileAssociationContribution(); // After the server has started the client sends an initilize request. The server receives @@ -59,15 +63,24 @@ connection.onInitialize((params: InitializeParams): InitializeResult => { if (params.initializationOptions) { filesAssociationContribution.setLanguageIds(params.initializationOptions.languageIds); } - let snippetSupport = params.capabilities && params.capabilities.textDocument && params.capabilities.textDocument.completion && params.capabilities.textDocument.completion.completionItem && params.capabilities.textDocument.completion.completionItem.snippetSupport; + 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: { // Tell the client that the server works in FULL text document sync mode textDocumentSync: documents.syncKind, - completionProvider: snippetSupport ? { resolveProvider: true, triggerCharacters: ['"', ':'] } : null, + completionProvider: clientDynamicRegisterSupport ? { resolveProvider: true, triggerCharacters: ['"', ':'] } : null, hoverProvider: true, documentSymbolProvider: true, - documentRangeFormattingProvider: !params.initializationOptions || params.initializationOptions['format.enable'] + documentRangeFormattingProvider: false } }; }); @@ -123,6 +136,7 @@ let languageService = getLanguageService({ interface Settings { json: { schemas: JSONSchemaSettings[]; + format: { enable: boolean; }; }; http: { proxy: string; @@ -138,6 +152,7 @@ interface JSONSchemaSettings { let jsonConfigurationSettings: JSONSchemaSettings[] = void 0; let schemaAssociations: ISchemaAssociations = void 0; +let formatterRegistration: Thenable = null; // The settings have changed. Is send on server activation as well. connection.onDidChangeConfiguration((change) => { @@ -146,6 +161,21 @@ connection.onDidChangeConfiguration((change) => { jsonConfigurationSettings = settings.json && settings.json.schemas; updateConfiguration(); + + // dynamically enable & disable the formatter + if (clientDynamicRegisterSupport) { + let enableFormatter = settings && settings.json && settings.json.format && settings.json.format.enable; + if (enableFormatter) { + if (!formatterRegistration) { + console.log('enable'); + formatterRegistration = connection.client.register(DocumentRangeFormattingRequest.type, { documentSelector: [{ language: 'json' }] }); + } + } else if (formatterRegistration) { + console.log('enable'); + formatterRegistration.then(r => r.dispose()); + formatterRegistration = null; + } + } }); // The jsonValidation extension configuration has changed