autoload/handlers.vim | 1 + autoload/lsp.vim | 15 +++++++++++++++ autoload/lspserver.vim | 3 +++ test/unit_tests.vim | 9 +++++++-- diff --git a/autoload/handlers.vim b/autoload/handlers.vim index 67b78651a1aca05680688f92640cb8d5a85fc8a9..c17de720915ffa4a1133ab4841ce947f987b24c8 100644 --- a/autoload/handlers.vim +++ b/autoload/handlers.vim @@ -99,6 +99,7 @@ endif # send a "initialized" notification to server lspserver.sendInitializedNotif() + lspserver.ready = true # if the outline window is opened, then request the symbols for the current # buffer diff --git a/autoload/lsp.vim b/autoload/lsp.vim index c3a2e86bd0c65120b3fa38065494e74fc64d4ffa..a8308b0e9dfa12ee64e70b57d6c9e04d87f10691 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -488,6 +488,21 @@ endif endfor enddef +# The LSP server is considered ready when the server capabilities are +# received ('initialize' LSP reply message) +def lsp#serverReady(): bool + var ftype = &filetype + if ftype == '' || @% == '' + return false + endif + + var lspserver: dict = s:lspGetServer(ftype) + if lspserver->empty() + return false + endif + return lspserver.ready +enddef + # set the LSP server trace level for the current buffer # Params: SetTraceParams def lsp#setTraceServer(traceVal: string) diff --git a/autoload/lspserver.vim b/autoload/lspserver.vim index 59cb5bcc9fac391a2b5b75cd2a06562d1245f491..d3b2bd529b4b4f895cb4df6539a3c079900a9d1f 100644 --- a/autoload/lspserver.vim +++ b/autoload/lspserver.vim @@ -65,6 +65,7 @@ def s:exit_cb(lspserver: dict, job: job, status: number): void util.WarnMsg("LSP server exited with status " .. status) lspserver.job = v:none lspserver.running = false + lspserver.ready = false lspserver.requests = {} enddef @@ -202,6 +203,7 @@ lspserver.job->job_stop() lspserver.job = v:none lspserver.running = false + lspserver.ready = false lspserver.requests = {} return 0 enddef @@ -843,6 +845,7 @@ var lspserver: dict = { path: path, args: args, running: false, + ready: false, job: v:none, data: '', nextID: 1, diff --git a/test/unit_tests.vim b/test/unit_tests.vim index 15d23244f3448b67c5e3d2d8fdaf7005086da7e0..a8076f705335a4b0b4ea898ab99f9f4d9840e1ef 100644 --- a/test/unit_tests.vim +++ b/test/unit_tests.vim @@ -10,7 +10,7 @@ set rtp+=../ source ../plugin/lsp.vim var lspServers = [{ filetype: ['c', 'cpp'], - path: '/usr/bin/clangd-12', + path: '/usr/bin/clangd', args: ['--background-index', '--clang-tidy'] }] lsp#addServer(lspServers) @@ -396,7 +396,12 @@ delete('results.txt') # Edit a dummy C file to start the LSP server :edit Xtest.c - :sleep 2 + # Wait for the LSP server to become ready (max 10 seconds) + var maxcount = 100 + while maxcount > 0 && !lsp#serverReady() + :sleep 100m + maxcount -= 1 + endwhile :%bw! # Get the list of test functions in this file and call them