]> Sergey Matveev's repositories - vim-lsp.git/blobdiff - autoload/lsp/completion.vim
Make buffer completion responsive for large files
[vim-lsp.git] / autoload / lsp / completion.vim
index 66ea9c5d6d23ec9b74289171a576965915153aeb..9b2d598f512caea8a1d4cc853fabb7ea98ea8a32 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 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 opt.lspOptions.bufferCompletionTimeout > 0 && linenr % 200 == 0 && 
+       start->reltime()->reltimefloat() * 1000 > opt.lspOptions.bufferCompletionTimeout
+      break
+    endif
+    linenr += 1
+  endfor
 enddef
 
 # process the 'textDocument/completion' reply from the LSP server