From 1e10310f4cc70cf95a68457c2be9e7459b5bbba6 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 21 Oct 2023 09:47:24 +0200 Subject: refactor(lsp): move completion logic into _completion module To reduce cross-chatter between modules and for https://github.com/neovim/neovim/issues/25272 Also preparing for https://github.com/neovim/neovim/issues/25714 --- runtime/lua/vim/lsp/_completion.lua | 210 ++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 runtime/lua/vim/lsp/_completion.lua (limited to 'runtime/lua/vim/lsp/_completion.lua') diff --git a/runtime/lua/vim/lsp/_completion.lua b/runtime/lua/vim/lsp/_completion.lua new file mode 100644 index 0000000000..efd1aaacf7 --- /dev/null +++ b/runtime/lua/vim/lsp/_completion.lua @@ -0,0 +1,210 @@ +local M = {} +local api = vim.api +local lsp = vim.lsp +local protocol = lsp.protocol +local ms = protocol.Methods + +---@param input string unparsed snippet +---@return string parsed snippet +local function parse_snippet(input) + local ok, parsed = pcall(function() + return require('vim.lsp._snippet_grammar').parse(input) + end) + return ok and tostring(parsed) or input +end + +--- Returns text that should be inserted when selecting completion item. The +--- precedence is as follows: textEdit.newText > insertText > label +--- +--- See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion +--- +---@param item lsp.CompletionItem +---@return string +local function get_completion_word(item) + if item.textEdit ~= nil and item.textEdit.newText ~= nil and item.textEdit.newText ~= '' then + if item.insertTextFormat == protocol.InsertTextFormat.PlainText then + return item.textEdit.newText + else + return parse_snippet(item.textEdit.newText) + end + elseif item.insertText ~= nil and item.insertText ~= '' then + if item.insertTextFormat == protocol.InsertTextFormat.PlainText then + return item.insertText + else + return parse_snippet(item.insertText) + end + end + return item.label +end + +---@param result lsp.CompletionList|lsp.CompletionItem[] +---@return lsp.CompletionItem[] +local function get_items(result) + if result.items then + return result.items + end + return result +end + +--- Turns the result of a `textDocument/completion` request into vim-compatible +--- |complete-items|. +--- +---@param result lsp.CompletionList|lsp.CompletionItem[] Result of `textDocument/completion` +---@param prefix string prefix to filter the completion items +---@return table[] +---@see complete-items +function M._lsp_to_complete_items(result, prefix) + local items = get_items(result) + if vim.tbl_isempty(items) then + return {} + end + + local function matches_prefix(item) + return vim.startswith(get_completion_word(item), prefix) + end + + items = vim.tbl_filter(matches_prefix, items) --[[@as lsp.CompletionItem[]|]] + table.sort(items, function(a, b) + return (a.sortText or a.label) < (b.sortText or b.label) + end) + + local matches = {} + for _, item in ipairs(items) do + local info = '' + local documentation = item.documentation + if documentation then + if type(documentation) == 'string' and documentation ~= '' then + info = documentation + elseif type(documentation) == 'table' and type(documentation.value) == 'string' then + info = documentation.value + else + vim.notify( + ('invalid documentation value %s'):format(vim.inspect(documentation)), + vim.log.levels.WARN + ) + end + end + local word = get_completion_word(item) + table.insert(matches, { + word = word, + abbr = item.label, + kind = protocol.CompletionItemKind[item.kind] or 'Unknown', + menu = item.detail or '', + info = #info > 0 and info or nil, + icase = 1, + dup = 1, + empty = 1, + user_data = { + nvim = { + lsp = { + completion_item = item, + }, + }, + }, + }) + end + return matches +end + +---@param items lsp.CompletionItem[] +local function adjust_start_col(lnum, line, items, encoding) + local min_start_char = nil + for _, item in pairs(items) do + if item.textEdit and item.textEdit.range.start.line == lnum - 1 then + if min_start_char and min_start_char ~= item.textEdit.range.start.character then + return nil + end + min_start_char = item.textEdit.range.start.character + end + end + if min_start_char then + return vim.lsp.util._str_byteindex_enc(line, min_start_char, encoding) + else + return nil + end +end + +---@param findstart integer 0 or 1, decides behavior +---@param base integer findstart=0, text to match against +---@return integer|table Decided by {findstart}: +--- - findstart=0: column where the completion starts, or -2 or -3 +--- - findstart=1: list of matches (actually just calls |complete()|) +function M.omnifunc(findstart, base) + local bufnr = api.nvim_get_current_buf() + local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_completion }) + local remaining = #clients + if remaining == 0 then + return findstart == 1 and -1 or {} + end + + local log = require('vim.lsp.log') + -- Then, perform standard completion request + if log.info() then + log.info('base ', base) + end + + local win = api.nvim_get_current_win() + local pos = api.nvim_win_get_cursor(win) + local line = api.nvim_get_current_line() + local line_to_cursor = line:sub(1, pos[2]) + log.trace('omnifunc.line', pos, line) + + local word_boundary = vim.fn.match(line_to_cursor, '\\k*$') + 1 --[[@as integer]] + local items = {} + local startbyte = nil + + local function on_done() + local mode = api.nvim_get_mode()['mode'] + if mode == 'i' or mode == 'ic' then + vim.fn.complete(startbyte or word_boundary, items) + end + end + + local util = vim.lsp.util + for _, client in ipairs(clients) do + local params = util.make_position_params(win, client.offset_encoding) + client.request(ms.textDocument_completion, params, function(err, result) + if err then + log.warn(err.message) + end + if result and vim.fn.mode() == 'i' then + -- Completion response items may be relative to a position different than `textMatch`. + -- Concrete example, with sumneko/lua-language-server: + -- + -- require('plenary.asy| + -- ▲ ▲ ▲ + -- │ │ └── cursor_pos: 20 + -- │ └────── textMatch: 17 + -- └────────────── textEdit.range.start.character: 9 + -- .newText = 'plenary.async' + -- ^^^ + -- prefix (We'd remove everything not starting with `asy`, + -- so we'd eliminate the `plenary.async` result + -- + -- `adjust_start_col` is used to prefer the language server boundary. + -- + local encoding = client.offset_encoding + local candidates = get_items(result) + local curstartbyte = adjust_start_col(pos[1], line, candidates, encoding) + if startbyte == nil then + startbyte = curstartbyte + elseif curstartbyte ~= nil and curstartbyte ~= startbyte then + startbyte = word_boundary + end + local prefix = startbyte and line:sub(startbyte + 1) or line_to_cursor:sub(word_boundary) + local matches = M._lsp_to_complete_items(result, prefix) + vim.list_extend(items, matches) + end + remaining = remaining - 1 + if remaining == 0 then + vim.schedule(on_done) + end + end, bufnr) + end + + -- Return -2 to signal that we should continue completion so that we can + -- async complete. + return -2 +end + +return M -- cgit From 5e5f5174e3faa862a9bc353aa7da41487911140b Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 21 Oct 2023 13:44:53 +0200 Subject: fix(lsp): fix off-by-one error for omnifunc word boundary Fixes https://github.com/neovim/neovim/issues/25177 I initially wanted to split this into a refactor commit to make it more testable, but it appears that already accidentally fixed the issue by normalizing lnum/col to 0-indexing --- runtime/lua/vim/lsp/_completion.lua | 104 ++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 40 deletions(-) (limited to 'runtime/lua/vim/lsp/_completion.lua') diff --git a/runtime/lua/vim/lsp/_completion.lua b/runtime/lua/vim/lsp/_completion.lua index efd1aaacf7..f0e3af7f03 100644 --- a/runtime/lua/vim/lsp/_completion.lua +++ b/runtime/lua/vim/lsp/_completion.lua @@ -106,11 +106,12 @@ function M._lsp_to_complete_items(result, prefix) return matches end +---@param lnum integer 0-indexed ---@param items lsp.CompletionItem[] local function adjust_start_col(lnum, line, items, encoding) local min_start_char = nil for _, item in pairs(items) do - if item.textEdit and item.textEdit.range.start.line == lnum - 1 then + if item.textEdit and item.textEdit.range.start.line == lnum then if min_start_char and min_start_char ~= item.textEdit.range.start.character then return nil end @@ -124,12 +125,57 @@ local function adjust_start_col(lnum, line, items, encoding) end end +---@private +---@param line string line content +---@param lnum integer 0-indexed line number +---@param client_start_boundary integer 0-indexed word boundary +---@param server_start_boundary? integer 0-indexed word boundary, based on textEdit.range.start.character +---@param result lsp.CompletionList|lsp.CompletionItem[] +---@param encoding string +---@return table[] matches +---@return integer? server_start_boundary +function M._convert_results( + line, + lnum, + client_start_boundary, + server_start_boundary, + result, + encoding +) + -- Completion response items may be relative to a position different than `client_start_boundary`. + -- Concrete example, with lua-language-server: + -- + -- require('plenary.asy| + -- ▲ ▲ ▲ + -- │ │ └── cursor_pos: 20 + -- │ └────── client_start_boundary: 17 + -- └────────────── textEdit.range.start.character: 9 + -- .newText = 'plenary.async' + -- ^^^ + -- prefix (We'd remove everything not starting with `asy`, + -- so we'd eliminate the `plenary.async` result + -- + -- `adjust_start_col` is used to prefer the language server boundary. + -- + local candidates = get_items(result) + local curstartbyte = adjust_start_col(lnum, line, candidates, encoding) + if server_start_boundary == nil then + server_start_boundary = curstartbyte + elseif curstartbyte ~= nil and curstartbyte ~= server_start_boundary then + server_start_boundary = client_start_boundary + end + local prefix = line:sub((server_start_boundary or client_start_boundary) + 1) + local matches = M._lsp_to_complete_items(result, prefix) + return matches, server_start_boundary +end + ---@param findstart integer 0 or 1, decides behavior ---@param base integer findstart=0, text to match against ---@return integer|table Decided by {findstart}: --- - findstart=0: column where the completion starts, or -2 or -3 --- - findstart=1: list of matches (actually just calls |complete()|) function M.omnifunc(findstart, base) + assert(base) -- silence luals local bufnr = api.nvim_get_current_buf() local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_completion }) local remaining = #clients @@ -137,26 +183,20 @@ function M.omnifunc(findstart, base) return findstart == 1 and -1 or {} end - local log = require('vim.lsp.log') - -- Then, perform standard completion request - if log.info() then - log.info('base ', base) - end - local win = api.nvim_get_current_win() - local pos = api.nvim_win_get_cursor(win) + local cursor = api.nvim_win_get_cursor(win) + local lnum = cursor[1] - 1 + local cursor_col = cursor[2] local line = api.nvim_get_current_line() - local line_to_cursor = line:sub(1, pos[2]) - log.trace('omnifunc.line', pos, line) - - local word_boundary = vim.fn.match(line_to_cursor, '\\k*$') + 1 --[[@as integer]] + local line_to_cursor = line:sub(1, cursor_col) + local client_start_boundary = vim.fn.match(line_to_cursor, '\\k*$') --[[@as integer]] + local server_start_boundary = nil local items = {} - local startbyte = nil local function on_done() local mode = api.nvim_get_mode()['mode'] if mode == 'i' or mode == 'ic' then - vim.fn.complete(startbyte or word_boundary, items) + vim.fn.complete((server_start_boundary or client_start_boundary) + 1, items) end end @@ -165,34 +205,18 @@ function M.omnifunc(findstart, base) local params = util.make_position_params(win, client.offset_encoding) client.request(ms.textDocument_completion, params, function(err, result) if err then - log.warn(err.message) + require('vim.lsp.log').warn(err.message) end if result and vim.fn.mode() == 'i' then - -- Completion response items may be relative to a position different than `textMatch`. - -- Concrete example, with sumneko/lua-language-server: - -- - -- require('plenary.asy| - -- ▲ ▲ ▲ - -- │ │ └── cursor_pos: 20 - -- │ └────── textMatch: 17 - -- └────────────── textEdit.range.start.character: 9 - -- .newText = 'plenary.async' - -- ^^^ - -- prefix (We'd remove everything not starting with `asy`, - -- so we'd eliminate the `plenary.async` result - -- - -- `adjust_start_col` is used to prefer the language server boundary. - -- - local encoding = client.offset_encoding - local candidates = get_items(result) - local curstartbyte = adjust_start_col(pos[1], line, candidates, encoding) - if startbyte == nil then - startbyte = curstartbyte - elseif curstartbyte ~= nil and curstartbyte ~= startbyte then - startbyte = word_boundary - end - local prefix = startbyte and line:sub(startbyte + 1) or line_to_cursor:sub(word_boundary) - local matches = M._lsp_to_complete_items(result, prefix) + local matches + matches, server_start_boundary = M._convert_results( + line, + lnum, + client_start_boundary, + server_start_boundary, + result, + client.offset_encoding + ) vim.list_extend(items, matches) end remaining = remaining - 1 -- cgit From ba6761eafe615a7f904c585dba3b7d6e98f665e1 Mon Sep 17 00:00:00 2001 From: Lajos Koszti Date: Thu, 26 Oct 2023 22:40:36 +0200 Subject: fix(lsp): fix omnicomplete in middle of the line (#25787) Fixes a regression from 5e5f5174e3faa862a9bc353aa7da41487911140b Until that commit we had a logic like this: `local prefix = startbyte and line:sub(startbyte + 1) or line_to_cursor:sub(word_boundary)` The commit changed the logic and no longer cut off the line at the cursor, resulting in a prefix that included trailing characters --- runtime/lua/vim/lsp/_completion.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/lsp/_completion.lua') diff --git a/runtime/lua/vim/lsp/_completion.lua b/runtime/lua/vim/lsp/_completion.lua index f0e3af7f03..7a607d6c13 100644 --- a/runtime/lua/vim/lsp/_completion.lua +++ b/runtime/lua/vim/lsp/_completion.lua @@ -137,6 +137,7 @@ end function M._convert_results( line, lnum, + cursor_col, client_start_boundary, server_start_boundary, result, @@ -164,7 +165,7 @@ function M._convert_results( elseif curstartbyte ~= nil and curstartbyte ~= server_start_boundary then server_start_boundary = client_start_boundary end - local prefix = line:sub((server_start_boundary or client_start_boundary) + 1) + local prefix = line:sub((server_start_boundary or client_start_boundary) + 1, cursor_col) local matches = M._lsp_to_complete_items(result, prefix) return matches, server_start_boundary end @@ -212,6 +213,7 @@ function M.omnifunc(findstart, base) matches, server_start_boundary = M._convert_results( line, lnum, + cursor_col, client_start_boundary, server_start_boundary, result, -- cgit