diff --git a/runtime/doc/deprecated.txt b/runtime/doc/deprecated.txt index d9d165036c..959ac3dedc 100644 --- a/runtime/doc/deprecated.txt +++ b/runtime/doc/deprecated.txt @@ -38,6 +38,10 @@ LUA • vim.F.npcall() Renamed to |vim.npcall()| • vim.F.nil_wrap() Use |vim.npcall()| instead +LSP + +• vim.lsp.util.character_offset() Use to |vim.str_utfindex()| instead + ------------------------------------------------------------------------------ DEPRECATED IN 0.12 *deprecated-0.12* diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index 973f7a033e..c0a2afb94d 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -2941,20 +2941,6 @@ buf_highlight_references({bufnr}, {references}, {position_encoding}) See also: ~ • https://microsoft.github.io/language-server-protocol/specification/#textDocumentContentChangeEvent - *vim.lsp.util.character_offset()* -character_offset({buf}, {row}, {col}, {position_encoding}) - Returns the UTF-32 and UTF-16 offsets for a position in a certain buffer. - - Parameters: ~ - • {buf} (`integer`) buffer number (0 for current) - • {row} (`integer`) 0-indexed line - • {col} (`integer`) 0-indexed byte offset in line - • {position_encoding} (`'utf-8'|'utf-16'|'utf-32'`) - - Return: ~ - (`integer`) `position_encoding` index of the character in line {row} - column {col} in buffer {buf} - *vim.lsp.util.convert_input_to_markdown_lines()* convert_input_to_markdown_lines({input}, {contents}) Converts any of `MarkedString` | `MarkedString[]` | `MarkupContent` into a diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index d846ce1d01..7a37d5ebfa 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -1856,32 +1856,11 @@ function M.make_given_range_params(start_pos, end_pos, bufnr, position_encoding) validate('position_encoding', position_encoding, 'string') bufnr = vim._resolve_bufnr(bufnr) - --- @type [integer, integer] - local A = { unpack(start_pos or api.nvim_buf_get_mark(bufnr, '<')) } - --- @type [integer, integer] - local B = { unpack(end_pos or api.nvim_buf_get_mark(bufnr, '>')) } - -- convert to 0-index - A[1] = A[1] - 1 - B[1] = B[1] - 1 - -- account for position_encoding. - if A[2] > 0 then - A[2] = M.character_offset(bufnr, A[1], A[2], position_encoding) - end - if B[2] > 0 then - B[2] = M.character_offset(bufnr, B[1], B[2], position_encoding) - end - -- we need to offset the end character position otherwise we loose the last - -- character of the selection, as LSP end position is exclusive - -- see https://microsoft.github.io/language-server-protocol/specification#range - if vim.o.selection ~= 'exclusive' then - B[2] = B[2] + 1 - end + local start_row, start_col = unpack(start_pos or api.nvim_buf_get_mark(bufnr, '<')) + local end_row, end_col = unpack(end_pos or api.nvim_buf_get_mark(bufnr, '>')) return { textDocument = M.make_text_document_params(bufnr), - range = { - start = { line = A[1], character = A[2] }, - ['end'] = { line = B[1], character = B[2] }, - }, + range = vim.range.mark(bufnr, start_row, start_col, end_row, end_col):to_lsp(position_encoding), } end @@ -1933,12 +1912,14 @@ end --- Returns the UTF-32 and UTF-16 offsets for a position in a certain buffer. --- +---@deprecated ---@param buf integer buffer number (0 for current) ---@param row integer 0-indexed line ---@param col integer 0-indexed byte offset in line ---@param position_encoding 'utf-8'|'utf-16'|'utf-32' ---@return integer `position_encoding` index of the character in line {row} column {col} in buffer {buf} function M.character_offset(buf, row, col, position_encoding) + vim.deprecate('vim.lsp.util.character_offset', 'vim.str_utfindex', '0.14') vim.validate('position_encoding', position_encoding, 'string') local line = get_line(buf, row) diff --git a/test/functional/plugin/lsp/buf_spec.lua b/test/functional/plugin/lsp/buf_spec.lua index f5fc498ef6..d2fd84f4e3 100644 --- a/test/functional/plugin/lsp/buf_spec.lua +++ b/test/functional/plugin/lsp/buf_spec.lua @@ -1456,7 +1456,7 @@ describe('vim.lsp.buf', function() eq('textDocument/rangeFormatting', result[3].method) local expected_range = { start = { line = 0, character = 0 }, - ['end'] = { line = 1, character = 4 }, + ['end'] = { line = 2, character = 0 }, } eq(expected_range, result[3].params.range) end)