LSP: Use async completion for omnifunc. (#11578)

This commit is contained in:
Ashkan Kiani 2019-12-20 22:49:29 -08:00 committed by GitHub
parent b2443361ca
commit ee7ac469c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 31 deletions

View File

@ -858,39 +858,30 @@ function lsp.omnifunc(findstart, base)
end end
end end
if findstart == 1 then -- Then, perform standard completion request
-- First, just return the current cursor column, we only really need that local _ = log.info() and log.info("base ", base)
return vim.fn.col('.')
else
-- Then, perform standard completion request
log.info("base ", base)
local pos = vim.api.nvim_win_get_cursor(0) local pos = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local line_to_cursor = line:sub(1, pos[2]) local line_to_cursor = line:sub(1, pos[2])
local _ = log.trace() and log.trace("omnifunc.line", pos, line) local _ = log.trace() and log.trace("omnifunc.line", pos, line)
-- Get the start postion of the current keyword -- Get the start postion of the current keyword
local textMatch = vim.fn.match(line_to_cursor, '\\k*$') local textMatch = vim.fn.match(line_to_cursor, '\\k*$')
local params = util.make_position_params() local params = util.make_position_params()
-- TODO handle timeout error differently? Like via an error? local items = {}
local client_responses = lsp.buf_request_sync(bufnr, 'textDocument/completion', params) or {} lsp.buf_request(bufnr, 'textDocument/completion', params, function(err, _, result)
local matches = {} if err or not result then return end
for _, response in pairs(client_responses) do local matches = util.text_document_completion_list_to_complete_items(result)
-- TODO how to handle errors? -- TODO(ashkan): is this the best way to do this?
if not response.error then vim.list_extend(items, matches)
local data = response.result vim.fn.complete(textMatch+1, items)
local completion_items = util.text_document_completion_list_to_complete_items(data or {}) end)
local _ = log.trace() and log.trace("omnifunc.completion_items", completion_items)
vim.list_extend(matches, completion_items)
end
end
-- Instead of returning matches call complete instead -- Return -2 to signal that we should continue completion so that we can
vim.fn.complete(textMatch+1, matches) -- async complete.
return {} return -2
end
end end
function lsp.client_is_stopped(client_id) function lsp.client_is_stopped(client_id)

View File

@ -62,9 +62,10 @@ M['textDocument/completion'] = function(_, _, result)
local row, col = unpack(api.nvim_win_get_cursor(0)) local row, col = unpack(api.nvim_win_get_cursor(0))
local line = assert(api.nvim_buf_get_lines(0, row-1, row, false)[1]) local line = assert(api.nvim_buf_get_lines(0, row-1, row, false)[1])
local line_to_cursor = line:sub(col+1) local line_to_cursor = line:sub(col+1)
local textMatch = vim.fn.match(line_to_cursor, '\\k*$')
local matches = util.text_document_completion_list_to_complete_items(result, line_to_cursor) local matches = util.text_document_completion_list_to_complete_items(result)
vim.fn.complete(col, matches) vim.fn.complete(textMatch+1, matches)
end end
M['textDocument/hover'] = function(_, method, result) M['textDocument/hover'] = function(_, method, result)