@@ -113,8 +113,18 @@ export class LanguageClient extends BaseService implements LanguageService {
113113 console . log ( params ) ;
114114 } ) ;
115115
116- this . connection . onRequest ( 'client/registerCapability' , ( params ) => {
117- console . log ( params ) ;
116+ this . connection . onRequest ( 'client/registerCapability' , ( params : lsp . RegistrationParams ) => {
117+ params . registrations . forEach ( registration => {
118+ this . registerCapability ( registration ) ;
119+ } ) ;
120+ return null ;
121+ } ) ;
122+
123+ this . connection . onRequest ( 'client/unregisterCapability' , ( params : lsp . UnregistrationParams ) => {
124+ params . unregisterations . forEach ( unregistration => {
125+ this . unregisterCapability ( unregistration ) ;
126+ } ) ;
127+ return null ;
118128 } ) ;
119129
120130 this . connection . onRequest ( 'workspace/applyEdit' , async ( params : lsp . ApplyWorkspaceEditParams ) => {
@@ -547,4 +557,156 @@ export class LanguageClient extends BaseService implements LanguageService {
547557 return this . connection . sendRequest ( name , args ) ;
548558 }
549559
560+ private registerCapability ( registration : lsp . Registration ) {
561+ if ( ! this . serviceCapabilities ) {
562+ this . serviceCapabilities = { } ;
563+ }
564+
565+ switch ( registration . method ) {
566+ case 'textDocument/diagnostic' :
567+ if ( this . clientCapabilities . textDocument ?. diagnostic ?. dynamicRegistration ) {
568+ this . serviceCapabilities . diagnosticProvider = registration . registerOptions ;
569+ }
570+ break ;
571+ case 'textDocument/hover' :
572+ if ( this . clientCapabilities . textDocument ?. hover ?. dynamicRegistration ) {
573+ this . serviceCapabilities . hoverProvider = registration . registerOptions || true ;
574+ }
575+ break ;
576+ case 'textDocument/formatting' :
577+ case 'textDocument/rangeFormatting' :
578+ if ( this . clientCapabilities . textDocument ?. formatting ?. dynamicRegistration ) {
579+ if ( registration . method === 'textDocument/formatting' ) {
580+ this . serviceCapabilities . documentFormattingProvider = registration . registerOptions || true ;
581+ } else {
582+ this . serviceCapabilities . documentRangeFormattingProvider = registration . registerOptions || true ;
583+ }
584+ }
585+ break ;
586+ case 'textDocument/completion' :
587+ if ( this . clientCapabilities . textDocument ?. completion ?. dynamicRegistration ) {
588+ this . serviceCapabilities . completionProvider = registration . registerOptions ;
589+ }
590+ break ;
591+ case 'textDocument/signatureHelp' :
592+ if ( this . clientCapabilities . textDocument ?. signatureHelp ?. dynamicRegistration ) {
593+ this . serviceCapabilities . signatureHelpProvider = registration . registerOptions ;
594+ }
595+ break ;
596+ case 'textDocument/documentHighlight' :
597+ if ( this . clientCapabilities . textDocument ?. documentHighlight ?. dynamicRegistration ) {
598+ this . serviceCapabilities . documentHighlightProvider = registration . registerOptions || true ;
599+ }
600+ break ;
601+ case 'textDocument/semanticTokens/full' :
602+ case 'textDocument/semanticTokens/range' :
603+ if ( this . clientCapabilities . textDocument ?. semanticTokens ?. dynamicRegistration ) {
604+ this . serviceCapabilities . semanticTokensProvider = registration . registerOptions ;
605+ }
606+ break ;
607+ case 'textDocument/codeAction' :
608+ if ( this . clientCapabilities . textDocument ?. codeAction ?. dynamicRegistration ) {
609+ this . serviceCapabilities . codeActionProvider = registration . registerOptions || true ;
610+ }
611+ break ;
612+ case 'textDocument/inlineCompletion' :
613+ if ( this . clientCapabilities . textDocument ?. inlineCompletion ?. dynamicRegistration ) {
614+ this . serviceCapabilities . inlineCompletionProvider = registration . registerOptions || true ;
615+ }
616+ break ;
617+ case 'workspace/executeCommand' :
618+ if ( this . clientCapabilities . workspace ?. executeCommand ?. dynamicRegistration ) {
619+ this . serviceCapabilities . executeCommandProvider = registration . registerOptions ;
620+ }
621+ break ;
622+ default :
623+ console . warn ( `Unhandled dynamic capability registration: ${ registration . method } ` ) ;
624+ }
625+
626+ this . notifyCapabilitiesChanged ( ) ;
627+ }
628+
629+ private unregisterCapability ( unregistration : lsp . Unregistration ) {
630+ if ( ! this . serviceCapabilities ) {
631+ return ;
632+ }
633+
634+ switch ( unregistration . method ) {
635+ case 'textDocument/diagnostic' :
636+ if ( this . clientCapabilities . textDocument ?. diagnostic ?. dynamicRegistration ) {
637+ delete this . serviceCapabilities . diagnosticProvider ;
638+ }
639+ break ;
640+ case 'textDocument/hover' :
641+ if ( this . clientCapabilities . textDocument ?. hover ?. dynamicRegistration ) {
642+ delete this . serviceCapabilities . hoverProvider ;
643+ }
644+ break ;
645+ case 'textDocument/formatting' :
646+ if ( this . clientCapabilities . textDocument ?. formatting ?. dynamicRegistration ) {
647+ delete this . serviceCapabilities . documentFormattingProvider ;
648+ }
649+ break ;
650+ case 'textDocument/rangeFormatting' :
651+ if ( this . clientCapabilities . textDocument ?. formatting ?. dynamicRegistration ) {
652+ delete this . serviceCapabilities . documentRangeFormattingProvider ;
653+ }
654+ break ;
655+ case 'textDocument/completion' :
656+ if ( this . clientCapabilities . textDocument ?. completion ?. dynamicRegistration ) {
657+ delete this . serviceCapabilities . completionProvider ;
658+ }
659+ break ;
660+ case 'textDocument/signatureHelp' :
661+ if ( this . clientCapabilities . textDocument ?. signatureHelp ?. dynamicRegistration ) {
662+ delete this . serviceCapabilities . signatureHelpProvider ;
663+ }
664+ break ;
665+ case 'textDocument/documentHighlight' :
666+ if ( this . clientCapabilities . textDocument ?. documentHighlight ?. dynamicRegistration ) {
667+ delete this . serviceCapabilities . documentHighlightProvider ;
668+ }
669+ break ;
670+ case 'textDocument/semanticTokens/full' :
671+ case 'textDocument/semanticTokens/range' :
672+ if ( this . clientCapabilities . textDocument ?. semanticTokens ?. dynamicRegistration ) {
673+ delete this . serviceCapabilities . semanticTokensProvider ;
674+ }
675+ break ;
676+ case 'textDocument/codeAction' :
677+ if ( this . clientCapabilities . textDocument ?. codeAction ?. dynamicRegistration ) {
678+ delete this . serviceCapabilities . codeActionProvider ;
679+ }
680+ break ;
681+ case 'textDocument/inlineCompletion' :
682+ if ( this . clientCapabilities . textDocument ?. inlineCompletion ?. dynamicRegistration ) {
683+ delete this . serviceCapabilities . inlineCompletionProvider ;
684+ }
685+ break ;
686+ case 'workspace/executeCommand' :
687+ if ( this . clientCapabilities . workspace ?. executeCommand ?. dynamicRegistration ) {
688+ delete this . serviceCapabilities . executeCommandProvider ;
689+ }
690+ break ;
691+ default :
692+ console . warn ( `Unhandled dynamic capability unregistration: ${ unregistration . method } ` ) ;
693+ }
694+
695+ this . notifyCapabilitiesChanged ( ) ;
696+ }
697+
698+ private notifyCapabilitiesChanged ( ) {
699+ const serviceName = this . serviceName ;
700+ Object . keys ( this . documents ) . forEach ( ( documentUri ) => {
701+ const postMessage = {
702+ "type" : MessageType . capabilitiesChange ,
703+ "value" : {
704+ [ serviceName ] : this . serviceCapabilities
705+ } ,
706+ documentUri : documentUri
707+ } ;
708+ this . ctx . postMessage ( postMessage ) ;
709+ } ) ;
710+ }
711+
550712}
0 commit comments