@@ -44,27 +44,27 @@ const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
4444/**
4545 * Client capabilities manager, to define what is and is not able to do.
4646 */
47- const clientCapabilities = new Capabilities ( ) ;
47+ const capabilities = new Capabilities ( ) ;
4848
4949/**
5050 * Provider of the semantic highlighting capability of the language server.
5151 */
52- let semanticTokensProvider : SemanticTokensProvider ;
52+ let provider : SemanticTokensProvider ;
5353
5454/**
5555 * Defines procedures to be executed on the initialization process
5656 * of the connection with the client
5757 */
5858connection . onInitialize ( ( params : InitializeParams ) => {
59- const capabilities = params . capabilities ;
60- clientCapabilities . initialize ( capabilities ) ;
61- semanticTokensProvider = new SemanticTokensProvider ( params . capabilities . textDocument ! . semanticTokens ! ) ;
59+ const caps = params . capabilities ;
60+ capabilities . initialize ( caps ) ;
61+ provider = new SemanticTokensProvider ( params . capabilities . textDocument ! . semanticTokens ! ) ;
6262 const result : InitializeResult = {
6363 capabilities : {
6464 textDocumentSync : TextDocumentSyncKind . Incremental
6565 }
6666 } ;
67- if ( clientCapabilities . workspace ) {
67+ if ( capabilities . workspace ) {
6868 result . capabilities . workspace = {
6969 workspaceFolders : {
7070 supported : true
@@ -82,24 +82,24 @@ connection.onInitialize((params: InitializeParams) => {
8282 * Configuration, Workspace Folder and Document Semantic Tokens
8383 */
8484connection . onInitialized ( ( ) => {
85- if ( clientCapabilities . configuration ) {
85+ if ( capabilities . configuration ) {
8686 connection . client . register ( DidChangeConfigurationNotification . type , void 0 ) ;
8787 }
88- if ( clientCapabilities . workspace ) {
88+ if ( capabilities . workspace ) {
8989 connection . workspace . onDidChangeWorkspaceFolders ( _event => {
9090 connection . console . log ( "Workspace folder change event received" ) ;
9191 } ) ;
9292 }
93- if ( clientCapabilities . tokens ) {
94- const registrationOptions : SemanticTokensRegistrationOptions = {
93+ if ( capabilities . tokens ) {
94+ const options : SemanticTokensRegistrationOptions = {
9595 documentSelector : null ,
96- legend : semanticTokensProvider . legend ,
96+ legend : provider . legend ,
9797 range : false ,
9898 full : {
9999 delta : true
100100 }
101101 } ;
102- connection . client . register ( SemanticTokensRegistrationType . type , registrationOptions ) ;
102+ connection . client . register ( SemanticTokensRegistrationType . type , options ) ;
103103 }
104104} ) ;
105105
@@ -111,29 +111,29 @@ const defaultSettings: DefaultSettings = { limit: 1000 };
111111/**
112112 * The global settings, used when the `workspace/configuration` request is not supported by the client.
113113 */
114- let globalSettings : DefaultSettings = defaultSettings ;
114+ let settings : DefaultSettings = defaultSettings ;
115115
116116/**
117117 * Cache for the settings of all open documents
118118 */
119- const documentSettings : Map < string , Thenable < DefaultSettings > > = new Map ( ) ;
119+ const cache : Map < string , Thenable < DefaultSettings > > = new Map ( ) ;
120120
121121/**
122122 * Retrieves the settings for a document
123123 * @param resource - String for the scheme of the document for which to retrieve its settings
124124 * @returns - A Promise for the settings of the document requested
125125 */
126126function getDocumentSettings ( resource : string ) : Thenable < DefaultSettings > {
127- if ( ! clientCapabilities . configuration ) {
128- return Promise . resolve ( globalSettings ) ;
127+ if ( ! capabilities . configuration ) {
128+ return Promise . resolve ( settings ) ;
129129 }
130- let result = documentSettings . get ( resource ) ;
130+ let result = cache . get ( resource ) ;
131131 if ( ! result ) {
132132 result = connection . workspace . getConfiguration ( {
133133 scopeUri : resource ,
134134 section : "languageServerExample"
135- } ) ;
136- documentSettings . set ( resource , result ) ;
135+ } ) . then ( config => ( config && typeof config === "object" ) ? config : defaultSettings ) ;
136+ cache . set ( resource , result ) ;
137137 }
138138 return result ;
139139}
@@ -145,13 +145,15 @@ function getDocumentSettings(resource: string): Thenable<DefaultSettings> {
145145 * @param textDocument - Document for which to perform the validation procedure
146146 * @returns {Promise<void> }
147147 */
148- async function validateTextDocument ( textDocument : TextDocument ) : Promise < void > {
149- const settings = await getDocumentSettings ( textDocument . uri ) ;
150- const text = textDocument . getText ( ) ;
148+ async function validateTextDocument ( document : TextDocument ) : Promise < void > {
149+ const config = await getDocumentSettings ( document . uri ) ;
150+ const text = document . getText ( ) ;
151151 const diagnostics : Diagnostic [ ] = [ ] ;
152152 const errors = getParserErrors ( text ) ;
153+ const effective = config || defaultSettings ;
154+ const limit = effective . limit ;
153155 errors . forEach ( ( error , index ) => {
154- if ( settings ?. limit !== null && index >= settings . limit ) {
156+ if ( limit !== null && index >= limit ) {
155157 return ;
156158 }
157159 const diagnostic : Diagnostic = {
@@ -165,20 +167,19 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
165167 } ;
166168 diagnostics . push ( diagnostic ) ;
167169 } ) ;
168- connection . sendDiagnostics ( { uri : textDocument . uri , diagnostics } ) ;
170+ connection . sendDiagnostics ( { uri : document . uri , diagnostics } ) ;
169171}
170172
171173/**
172174 * Resets all cached document settings and revalidates all open text
173175 * documents with there is a change in the configuration of the client.
174176 */
175177connection . onDidChangeConfiguration ( change => {
176- if ( clientCapabilities . configuration ) {
177- documentSettings . clear ( ) ;
178+ if ( capabilities . configuration ) {
179+ cache . clear ( ) ;
178180 } else {
179- globalSettings = < DefaultSettings > (
180- ( change . settings . languageServerExample || defaultSettings )
181- ) ;
181+ const config = change . settings . languageServerExample ;
182+ settings = ( config && typeof config === "object" ) ? config : defaultSettings ;
182183 }
183184 documents . all ( ) . forEach ( validateTextDocument ) ;
184185} ) ;
@@ -187,7 +188,7 @@ connection.onDidChangeConfiguration(change => {
187188 * Clears the settings cache for a closed document, once it is closed
188189 */
189190documents . onDidClose ( e => {
190- documentSettings . delete ( e . document . uri ) ;
191+ cache . delete ( e . document . uri ) ;
191192} ) ;
192193
193194/**
@@ -214,7 +215,7 @@ connection.languages.semanticTokens.on(params => {
214215 if ( ! document ) {
215216 return { data : [ ] } ;
216217 }
217- return semanticTokensProvider . provideSemanticTokens ( document ) ;
218+ return provider . provideSemanticTokens ( document ) ;
218219} ) ;
219220
220221/**
@@ -226,7 +227,7 @@ connection.languages.semanticTokens.onDelta(params => {
226227 if ( ! document ) {
227228 return { data : [ ] } ;
228229 }
229- return semanticTokensProvider . provideDeltas ( document ) ;
230+ return provider . provideDeltas ( document ) ;
230231} ) ;
231232
232233/**
0 commit comments