From 058c8e76908029a3ff5bcc465870b62f13863b49 Mon Sep 17 00:00:00 2001 From: Girish Palya Date: Wed, 19 Jul 2023 02:22:16 +0200 Subject: [PATCH] API to find out if LSP server has replied Add a function to find out if LSP server has replied with completion matches. This is needed in case of using external completion engines that expect asynchronous callback. LSP client simply acts as a source of completion items through omnifunc (g:LspOmniFunc). Before calling the omnifunc the second time g:LspOmniCompletePending can be checked to see if LSP server is ready to provide completion items. M autoload/lsp/completion.vim M doc/lsp.txt --- autoload/lsp/completion.vim | 7 +++++++ doc/lsp.txt | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/autoload/lsp/completion.vim b/autoload/lsp/completion.vim index 9197165..70bb2ae 100644 --- a/autoload/lsp/completion.vim +++ b/autoload/lsp/completion.vim @@ -488,6 +488,13 @@ def g:LspOmniFunc(findstart: number, base: string): any endif enddef +# For plugins that implement async completion this function indicates if +# omnifunc is waiting for LSP response. +def g:LspOmniCompletePending(): bool + var lspserver: dict = buf.CurbufGetServerChecked('completion') + return !lspserver->empty() && lspserver.omniCompletePending +enddef + # Insert mode completion handler. Used when 24x7 completion is enabled # (default). def LspComplete() diff --git a/doc/lsp.txt b/doc/lsp.txt index 7d46ad1..0c60316 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -1257,6 +1257,18 @@ do the filtering. By default, case sensitive comparison is used to filter the returned items. You can modify the 'completionMatcher' option to use either case insensitive or fuzzy comparison. +In addition to the automatic completion and omni completion, it is possible to +use external completion engines which provide asynchronous completion +from various sources. LSP client can be used as a completion source by +repurposing `g:LspOmniFunc` function. It needs to be invoked twice as +described in |complete-functions|. After the first invocation a request is +sent to the LSP server to find completion candidates. Later the function is +called again to actually obtain the matches. If the LSP server has not replied +with the matches `g:LspOmniFunc` could wait up to 2 seconds. To avoid blocking +wait call `g:LspOmniCompletePending` function which returns `true` immediately +if LSP server is not ready. `g:LspOmniFunc` should be called the second time +only after this function returns `false`. + ============================================================================== 7. Diagnostics *lsp-diagnostics* -- 2.48.1