]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Merge pull request #361 from girishji/omnifunc
authorYegappan Lakshmanan <4298407+yegappan@users.noreply.github.com>
Wed, 19 Jul 2023 14:29:59 +0000 (07:29 -0700)
committerGitHub <noreply@github.com>
Wed, 19 Jul 2023 14:29:59 +0000 (07:29 -0700)
Omnifunc optimization and readiness indicator

autoload/lsp/completion.vim
doc/lsp.txt

index 91971651eef229d686f95be0f6628c93097a5c0e..37690a0ff4fc0a3055d76ca28dd710e26ceaba5f 100644 (file)
@@ -441,15 +441,10 @@ def g:LspOmniFunc(findstart: number, base: string): any
     lspserver.getCompletion(1, '')
 
     # locate the start of the word
-    var line = getline('.')
-    var start = charcol('.') - 1
-    var keyword: string = ''
-    while start > 0 && line[start - 1] =~ '\k'
-      keyword = line[start - 1] .. keyword
-      start -= 1
-    endwhile
+    var line = getline('.')->strpart(0, col('.') - 1)
+    var keyword = line->matchstr('\k\+$')
     lspserver.omniCompleteKeyword = keyword
-    return line->byteidx(start)
+    return line->len() - keyword->len()
   else
     # Wait for the list of matches from the LSP server
     var count: number = 0
@@ -488,6 +483,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<any> = buf.CurbufGetServerChecked('completion')
+  return !lspserver->empty() && lspserver.omniCompletePending
+enddef
+
 # Insert mode completion handler. Used when 24x7 completion is enabled
 # (default).
 def LspComplete()
index 7d46ad1b7805bf2fd14e590580969172387dd836..f7cb09f875d1c34da941150ede3eed6208735724 100644 (file)
@@ -1257,6 +1257,19 @@ 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.  LSP client can be used as a completion source
+by repurposing `g:LspOmniFunc` function.  The adapter which interfaces with
+the external completion engine should invoke this function twice as
+described in |complete-functions|.  After the first invocation a request is
+sent to the LSP server to find completion candidates.  Second invocation
+returns the matches from LSP server.  If the LSP server is not ready to reply
+`g:LspOmniFunc` waits for up to 2 seconds.  This wait blocks the caller from
+doing other work, which may be a concern for asynchronous completion engines.
+To avoid blocking wait call `g:LspOmniCompletePending` function which returns
+`true` immediately if LSP server is not ready.  `g:LspOmniFunc` can be
+called (the second time) only after this function returns `false`.
+
 ==============================================================================
 7. Diagnostics                                         *lsp-diagnostics*