mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 10:38:59 +01:00
[json] dynamically enable & disable formatter
This commit is contained in:
@@ -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<Disposable> = 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
|
||||
|
||||
Reference in New Issue
Block a user