66
77import {
88 createConnection , IConnection ,
9- TextDocuments , TextDocument , InitializeParams , InitializeResult , NotificationType , RequestType
9+ TextDocuments , TextDocument , InitializeParams , InitializeResult , NotificationType , RequestType ,
10+ DocumentRangeFormattingRequest , Disposable
1011} from 'vscode-languageserver' ;
1112
1213import { xhr , XHRResponse , configure as configureHttpRequests , getErrorStatusDescription } from 'request-light' ;
@@ -49,6 +50,9 @@ let documents: TextDocuments = new TextDocuments();
4950// for open, change and close text document events
5051documents . listen ( connection ) ;
5152
53+ let clientSnippetSupport = false ;
54+ let clientDynamicRegisterSupport = false ;
55+
5256const filesAssociationContribution = new FileAssociationContribution ( ) ;
5357
5458// After the server has started the client sends an initilize request. The server receives
@@ -59,15 +63,24 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
5963 if ( params . initializationOptions ) {
6064 filesAssociationContribution . setLanguageIds ( params . initializationOptions . languageIds ) ;
6165 }
62- let snippetSupport = params . capabilities && params . capabilities . textDocument && params . capabilities . textDocument . completion && params . capabilities . textDocument . completion . completionItem && params . capabilities . textDocument . completion . completionItem . snippetSupport ;
66+ function hasClientCapability ( ...keys : string [ ] ) {
67+ let c = params . capabilities ;
68+ for ( let i = 0 ; c && i < keys . length ; i ++ ) {
69+ c = c [ keys [ i ] ] ;
70+ }
71+ return ! ! c ;
72+ }
73+
74+ clientSnippetSupport = hasClientCapability ( 'textDocument' , 'completion' , 'completionItem' , 'snippetSupport' ) ;
75+ clientDynamicRegisterSupport = hasClientCapability ( 'workspace' , 'symbol' , 'dynamicRegistration' ) ;
6376 return {
6477 capabilities : {
6578 // Tell the client that the server works in FULL text document sync mode
6679 textDocumentSync : documents . syncKind ,
67- completionProvider : snippetSupport ? { resolveProvider : true , triggerCharacters : [ '"' , ':' ] } : null ,
80+ completionProvider : clientDynamicRegisterSupport ? { resolveProvider : true , triggerCharacters : [ '"' , ':' ] } : null ,
6881 hoverProvider : true ,
6982 documentSymbolProvider : true ,
70- documentRangeFormattingProvider : ! params . initializationOptions || params . initializationOptions [ 'format.enable' ]
83+ documentRangeFormattingProvider : false
7184 }
7285 } ;
7386} ) ;
@@ -123,6 +136,7 @@ let languageService = getLanguageService({
123136interface Settings {
124137 json : {
125138 schemas : JSONSchemaSettings [ ] ;
139+ format : { enable : boolean ; } ;
126140 } ;
127141 http : {
128142 proxy : string ;
@@ -138,6 +152,7 @@ interface JSONSchemaSettings {
138152
139153let jsonConfigurationSettings : JSONSchemaSettings [ ] = void 0 ;
140154let schemaAssociations : ISchemaAssociations = void 0 ;
155+ let formatterRegistration : Thenable < Disposable > = null ;
141156
142157// The settings have changed. Is send on server activation as well.
143158connection . onDidChangeConfiguration ( ( change ) => {
@@ -146,6 +161,21 @@ connection.onDidChangeConfiguration((change) => {
146161
147162 jsonConfigurationSettings = settings . json && settings . json . schemas ;
148163 updateConfiguration ( ) ;
164+
165+ // dynamically enable & disable the formatter
166+ if ( clientDynamicRegisterSupport ) {
167+ let enableFormatter = settings && settings . json && settings . json . format && settings . json . format . enable ;
168+ if ( enableFormatter ) {
169+ if ( ! formatterRegistration ) {
170+ console . log ( 'enable' ) ;
171+ formatterRegistration = connection . client . register ( DocumentRangeFormattingRequest . type , { documentSelector : [ { language : 'json' } ] } ) ;
172+ }
173+ } else if ( formatterRegistration ) {
174+ console . log ( 'enable' ) ;
175+ formatterRegistration . then ( r => r . dispose ( ) ) ;
176+ formatterRegistration = null ;
177+ }
178+ }
149179} ) ;
150180
151181// The jsonValidation extension configuration has changed
0 commit comments