3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { CancellationToken , Connection , InitializeParams , InitializeResult , NotebookDocuments , ResponseError , TextDocuments } from 'vscode-languageserver' ;
6
+ import { CancellationToken , CompletionRegistrationOptions , CompletionRequest , Connection , Disposable , DocumentHighlightRegistrationOptions , DocumentHighlightRequest , InitializeParams , InitializeResult , NotebookDocuments , ResponseError , TextDocuments } from 'vscode-languageserver' ;
7
7
import { TextDocument } from 'vscode-languageserver-textdocument' ;
8
8
import * as lsp from 'vscode-languageserver-types' ;
9
9
import * as md from 'vscode-markdown-languageservice' ;
10
10
import { URI } from 'vscode-uri' ;
11
11
import { getLsConfiguration , LsConfiguration } from './config' ;
12
- import { ConfigurationManager } from './configuration' ;
12
+ import { ConfigurationManager , Settings } from './configuration' ;
13
13
import { registerValidateSupport } from './languageFeatures/diagnostics' ;
14
14
import { LogFunctionLogger } from './logging' ;
15
15
import * as protocol from './protocol' ;
@@ -77,7 +77,7 @@ export async function startServer(connection: Connection, serverConfig: {
77
77
} ) ;
78
78
79
79
registerCompletionsSupport ( connection , documents , mdLs , configurationManager ) ;
80
- registerDocumentHightlightSupport ( connection , documents , mdLs , configurationManager ) ;
80
+ registerDocumentHighlightSupport ( connection , documents , mdLs , configurationManager ) ;
81
81
registerValidateSupport ( connection , workspace , documents , mdLs , configurationManager , serverConfig . logger ) ;
82
82
83
83
return {
@@ -89,10 +89,8 @@ export async function startServer(connection: Connection, serverConfig: {
89
89
workspaceDiagnostics : false ,
90
90
} ,
91
91
codeActionProvider : { resolveProvider : true } ,
92
- completionProvider : { triggerCharacters : [ '.' , '/' , '#' ] } ,
93
92
definitionProvider : true ,
94
93
documentLinkProvider : { resolveProvider : true } ,
95
- documentHighlightProvider : true ,
96
94
documentSymbolProvider : true ,
97
95
foldingRangeProvider : true ,
98
96
referencesProvider : true ,
@@ -257,50 +255,57 @@ export async function startServer(connection: Connection, serverConfig: {
257
255
connection . listen ( ) ;
258
256
}
259
257
258
+ function registerDynamicClientFeature (
259
+ config : ConfigurationManager ,
260
+ isEnabled : ( settings : Settings | undefined ) => boolean ,
261
+ register : ( ) => Promise < Disposable > ,
262
+ ) {
263
+ let registration : Promise < IDisposable > | undefined ;
264
+ function update ( ) {
265
+ const settings = config . getSettings ( ) ;
266
+ if ( isEnabled ( settings ) ) {
267
+ if ( ! registration ) {
268
+ registration = register ( ) ;
269
+ }
270
+ } else {
271
+ registration ?. then ( x => x . dispose ( ) ) ;
272
+ registration = undefined ;
273
+ }
274
+ }
275
+
276
+ update ( ) ;
277
+ return config . onDidChangeConfiguration ( ( ) => update ( ) ) ;
278
+ }
260
279
261
280
function registerCompletionsSupport (
262
281
connection : Connection ,
263
282
documents : TextDocuments < md . ITextDocument > ,
264
283
ls : md . IMdLanguageService ,
265
284
config : ConfigurationManager ,
266
285
) : IDisposable {
267
- // let registration: Promise<IDisposable> | undefined;
268
- function update ( ) {
269
- // TODO: client still makes the request in this case. Figure our how to properly unregister.
270
- return ;
271
- // const settings = config.getSettings();
272
- // if (settings?.markdown.suggest.paths.enabled) {
273
- // if (!registration) {
274
- // registration = connection.client.register(CompletionRequest.type);
275
- // }
276
- // } else {
277
- // registration?.then(x => x.dispose());
278
- // registration = undefined;
279
- // }
280
- }
281
-
282
286
connection . onCompletion ( async ( params , token ) : Promise < lsp . CompletionItem [ ] > => {
283
- try {
284
- const settings = config . getSettings ( ) ;
285
- if ( ! settings ?. markdown . suggest . paths . enabled ) {
286
- return [ ] ;
287
- }
287
+ const settings = config . getSettings ( ) ;
288
+ if ( ! settings ?. markdown . suggest . paths . enabled ) {
289
+ return [ ] ;
290
+ }
288
291
289
- const document = documents . get ( params . textDocument . uri ) ;
290
- if ( document ) {
291
- return await ls . getCompletionItems ( document , params . position , params . context ! , token ) ;
292
- }
293
- } catch ( e ) {
294
- console . error ( e . stack ) ;
292
+ const document = documents . get ( params . textDocument . uri ) ;
293
+ if ( document ) {
294
+ return ls . getCompletionItems ( document , params . position , params . context ! , token ) ;
295
295
}
296
296
return [ ] ;
297
297
} ) ;
298
298
299
- update ( ) ;
300
- return config . onDidChangeConfiguration ( ( ) => update ( ) ) ;
299
+ return registerDynamicClientFeature ( config , ( settings ) => ! ! settings ?. markdown . suggest . paths . enabled , ( ) => {
300
+ const registrationOptions : CompletionRegistrationOptions = {
301
+ documentSelector : null ,
302
+ triggerCharacters : [ '.' , '/' , '#' ] ,
303
+ } ;
304
+ return connection . client . register ( CompletionRequest . type , registrationOptions ) ;
305
+ } ) ;
301
306
}
302
307
303
- function registerDocumentHightlightSupport (
308
+ function registerDocumentHighlightSupport (
304
309
connection : Connection ,
305
310
documents : TextDocuments < md . ITextDocument > ,
306
311
mdLs : md . IMdLanguageService ,
@@ -319,4 +324,11 @@ function registerDocumentHightlightSupport(
319
324
320
325
return mdLs ! . getDocumentHighlights ( document , params . position , token ) ;
321
326
} ) ;
327
+
328
+ return registerDynamicClientFeature ( configurationManager , ( settings ) => ! ! settings ?. markdown . occurrencesHighlight . enabled , ( ) => {
329
+ const registrationOptions : DocumentHighlightRegistrationOptions = {
330
+ documentSelector : null ,
331
+ } ;
332
+ return connection . client . register ( DocumentHighlightRequest . type , registrationOptions ) ;
333
+ } ) ;
322
334
}
0 commit comments