]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add support for "customNotificationHandlers"
authorAndreas Louv <andreas@louv.dk>
Sun, 5 Mar 2023 22:55:31 +0000 (23:55 +0100)
committerAndreas Louv <andreas@louv.dk>
Tue, 7 Mar 2023 16:25:52 +0000 (17:25 +0100)
README.md
autoload/lsp/handlers.vim
autoload/lsp/lsp.vim
autoload/lsp/lspserver.vim
doc/lsp.txt

index b7cc6c97efed8321ac8b9969af8681c958083384..c9113fd13636ee13b519f48fc5c7ef205db6bb45 100644 (file)
--- a/README.md
+++ b/README.md
@@ -49,6 +49,9 @@ To use the plugin features with a particular file type(s), you need to first reg
 
 To register a LSP server, add the following lines to your .vimrc file (use only the LSP servers that you need from the below list).  If you used [vim-plug](https://github.com/junegunn/vim-plug) to install the LSP plugin, the steps are described later in this section.
 ```
+   function! TypeScriptCustomNotificationHandler(lspserver, reply) abort
+     echom printf("TypeScript Version = %s", reply.params.version)
+   endfunction
    let lspServers = [
                \     #{
                \        filetype: ['c', 'cpp'],
@@ -59,6 +62,9 @@ To register a LSP server, add the following lines to your .vimrc file (use only
                \        filetype: ['javascript', 'typescript'],
                \        path: '/usr/local/bin/typescript-language-server',
                \        args: ['--stdio']
+               \        customNotificationHandlers: {
+               \          '$/typescriptVersion': function('TypeScriptCustomNotificationHandler')
+               \        }
                \      },
                \     #{
                \        filetype: 'sh',
@@ -115,6 +121,7 @@ filetype|One or more file types supported by the LSP server.  This can be a Stri
 path|complete path to the LSP server executable (without any arguments).
 args|a list of command-line arguments passed to the LSP server. Each argument is a separate List item.
 initializationOptions|User provided initialization options. May be of any type. For example the *intelephense* PHP language server accept several options here with the License Key among others. 
+customNotificationHandlers|A dictionary of notifications and functions that can be specified to add support for custom language server notifications.
 
 The LSP servers are added using the LspAddServer() function. This function accepts a list of LSP servers with the above information.
 
index ce761f21189f9982d820f90b5ecc11fdbc23c882..70d56c86b866f99e01ec4e8c51f617c6758ce499 100644 (file)
@@ -73,10 +73,6 @@ def ProcessUnsupportedNotifOnce(lspserver: dict<any>, reply: dict<any>)
   endif
 enddef
 
-# silently ignore an unsupported notification message
-def IgnoreNotif(lspserver: dict<any>, reply: dict<any>)
-enddef
-
 # process notification messages from the LSP server
 export def ProcessNotif(lspserver: dict<any>, reply: dict<any>): void
   var lsp_notif_handlers: dict<func> =
@@ -84,22 +80,28 @@ export def ProcessNotif(lspserver: dict<any>, reply: dict<any>): void
       'window/showMessage': ProcessShowMsgNotif,
       'window/logMessage': ProcessLogMsgNotif,
       'textDocument/publishDiagnostics': ProcessDiagNotif,
-      '$/progress': IgnoreNotif,
       '$/logTrace': ProcessLogTraceNotif,
-      '$/status/report': IgnoreNotif,
-      '$/status/show': IgnoreNotif,
       'telemetry/event': ProcessUnsupportedNotifOnce,
+    }
+
+  var lsp_ignored_notif_handlers: list<string> =
+    [
+      '$/progress',
+      '$/status/report',
+      '$/status/show',
       # Java language server sends the 'language/status' notification which is
       # not in the LSP specification
-      'language/status': IgnoreNotif,
+      'language/status',
       # Typescript language server sends the '$/typescriptVersion' notification
       # which is not in the LSP specification
-      '$/typescriptVersion': IgnoreNotif
-    }
+      '$/typescriptVersion'
+    ]
 
   if lsp_notif_handlers->has_key(reply.method)
     lsp_notif_handlers[reply.method](lspserver, reply)
-  else
+  elseif lspserver.customNotificationHandlers->has_key(reply.method)
+    lspserver.customNotificationHandlers[reply.method](lspserver, reply)
+  elseif lsp_ignored_notif_handlers->index(reply.method) == -1
     util.ErrMsg($'Error: Unsupported notification received from LSP server {reply->string()}')
   endif
 enddef
index 616e7bab267de54ae96374a0b13c8bb4169ba793..e42b12a12cd354be40e40c224ccd89296cab0be2 100644 (file)
@@ -429,6 +429,11 @@ export def AddServer(serverList: list<dict<any>>)
       initializationOptions = server.initializationOptions
     endif
 
+    var customNotificationHandlers: dict<func> = {}
+    if server->has_key('customNotificationHandlers')
+      customNotificationHandlers = server.customNotificationHandlers
+    endif
+
     if server.omnicompl->type() != v:t_bool
       util.ErrMsg($'Error: Setting of omnicompl {server.omnicompl} is not a Boolean')
       return
@@ -441,7 +446,8 @@ export def AddServer(serverList: list<dict<any>>)
     var lspserver: dict<any> = lserver.NewLspServer(server.path,
                                                    args,
                                                    server.syncInit,
-                                                   initializationOptions)
+                                                   initializationOptions,
+                                                   customNotificationHandlers)
 
     var ftypes = server.filetype
     if ftypes->type() == v:t_string
index e366b8ffa3d2ead4b6a4944b1acd7279c65f82f2..fd78974088a37fc5a0bb76a38a6ef45186a63b6f 100644 (file)
@@ -1589,12 +1589,13 @@ def TagFunc(lspserver: dict<any>, pat: string, flags: string, info: dict<any>):
   return symbol.TagFunc(lspserver, taglocations, pat)
 enddef
 
-export def NewLspServer(path: string, args: list<string>, isSync: bool, initializationOptions: any): dict<any>
+export def NewLspServer(path: string, args: list<string>, isSync: bool, initializationOptions: any, customNotificationHandlers: dict<func>): dict<any>
   var lspserver: dict<any> = {
     path: path,
     args: args,
     syncInit: isSync,
     initializationOptions: initializationOptions,
+    customNotificationHandlers: customNotificationHandlers,
     running: false,
     ready: false,
     job: v:none,
index 9449483225de4d8a2e3e2d7ec05188e19c8b06d7..f25181de71680a476a9409eeb778650fe9ebafad 100644 (file)
@@ -220,6 +220,12 @@ To add a language server, the following information is needed:
                        or useful for initialization. Those can be provided in
                        this dictionary and if present will be transmitted to
                        the lsp server.
+       customNotificationHandlers
+                       (Optional) some lsp servers (e.g.
+                       typescript-language-server) will send additional
+                       notifications which you might want to silence or handle.
+                       The provided notification handlers will be called with a
+                       reference to the 'lspserver' and the 'reply'.
        omnicompl       (Optional) a boolean value that enables (true)
                        or disables (false) omni-completion for this file
                        types. By default this is set to 'v:true'.