]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Merge pull request #347 from girishji/buffer-completion-timeout
authorYegappan Lakshmanan <4298407+yegappan@users.noreply.github.com>
Sun, 25 Jun 2023 19:18:28 +0000 (12:18 -0700)
committerGitHub <noreply@github.com>
Sun, 25 Jun 2023 19:18:28 +0000 (12:18 -0700)
Make buffer completion responsive for large files

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

index 66ea9c5d6d23ec9b74289171a576965915153aeb..7ea24166c39b083fa43a87e0abe39cd6ffa84007 100644 (file)
@@ -114,22 +114,30 @@ enddef
 
 # add completion from current buf
 def CompletionFromBuffer(items: list<dict<any>>)
-    var words = {}
-    for line in getline(1, '$')
-        for word in line->split('\W\+')
-            if !words->has_key(word) && word->len() > 1
-                words[word] = 1
-                items->add({
-                    label: word,
-                    data: {
-                        entryNames: [word],
-                    },
-                    kind: 26,
-                    documentation: "",
-                })
-            endif
-        endfor
+  var words = {}
+  var start = reltime()
+  var timeout = opt.lspOptions.bufferCompletionTimeout
+  var linenr = 1
+  for line in getline(1, '$')
+    for word in line->split('\W\+')
+      if !words->has_key(word) && word->len() > 1
+       words[word] = 1
+       items->add({
+         label: word,
+         data: {
+           entryNames: [word],
+         },
+         kind: 26,
+         documentation: "",
+       })
+      endif
     endfor
+    # Check every 200 lines if timeout is exceeded
+    if timeout > 0 && linenr % 200 == 0 && start->reltime()->reltimefloat() * 1000 > timeout
+      break
+    endif
+    linenr += 1
+  endfor
 enddef
 
 # process the 'textDocument/completion' reply from the LSP server
index 8ccf7e478331da564d84774d7b6b788663569aca..be55f058a40a4723dc51ee30d5fb4e8718377db8 100644 (file)
@@ -77,6 +77,8 @@ export var lspOptions: dict<any> = {
   useQuickfixForLocations: false,
   # add to autocomplition list current buffer words
   useBufferCompletion: false,
+  # Limit the time autocompletion searches for words in current buffer (in milliseconds)
+  bufferCompletionTimeout: 100,
   # Enable support for custom completion kinds
   customCompletionKinds: false,
   # A dictionary with all completion kinds that you want to customize
index 910dcd1fc3690d1f7ac4100808bca1ded490b85e..e288e2033479678addd0460100e214ccf60a07d9 100644 (file)
@@ -589,10 +589,16 @@ useQuickfixForLocations   |Boolean| option.  Show |:LspShowReferences| in a
                                                *lsp-opt-useBufferCompletion*
 useBufferCompletion     |Boolean| option. If enabled, the words from the
                        current buffer are added to the auto completion list.
-                       This may degrade Vim performance as the current buffer
-                       contents are processed every time the completion menu
-                       is displayed.
                        By default this is set to false.
+                                               *lsp-opt-bufferCompletionTimeout*
+bufferCompletionTimeout |Number| option. Specifies how long (in milliseconds) 
+                       to wait while processing current buffer for 
+                       autocompletion words.  If set too high Vim performance
+                       may degrade as the current buffer contents are
+                       processed every time the completion menu is displayed.
+                       If set to 0 the entire buffer is processed without
+                       regard to timeout.
+                       By default this is set to 100 ms.
 
 For example, to disable the automatic placement of signs for the LSP
 diagnostic messages, you can add the following line to your .vimrc file: >