lsp: use utf-8 when utf-16 not requested

This commit is contained in:
Michael Lingelbach 2021-03-28 03:11:12 -07:00
parent 2ed5a77602
commit 0cadab1412
2 changed files with 21 additions and 9 deletions

View File

@ -850,20 +850,23 @@ do
end end
util.buf_versions[bufnr] = changedtick util.buf_versions[bufnr] = changedtick
-- Lazy initialize these because clients may not even need them.
local incremental_changes = once(function(client) local incremental_changes = function(client)
local lines = nvim_buf_get_lines(bufnr, 0, -1, true) local lines = nvim_buf_get_lines(bufnr, 0, -1, true)
local startline = math.min(firstline + 1, math.min(#client._cached_buffers[bufnr], #lines)) local startline = math.min(firstline + 1, math.min(#client._cached_buffers[bufnr], #lines))
local endline = math.min(-(#lines - new_lastline), -1) local endline = math.min(-(#lines - new_lastline), -1)
local incremental_change = vim.lsp.util.compute_diff(client._cached_buffers[bufnr], lines, startline, endline) local incremental_change = vim.lsp.util.compute_diff(
client._cached_buffers[bufnr], lines, startline, endline, client.offset_encoding or "utf-16")
client._cached_buffers[bufnr] = lines client._cached_buffers[bufnr] = lines
return incremental_change return incremental_change
end) end
local full_changes = once(function() local full_changes = once(function()
return { return {
text = buf_get_full_text(bufnr); text = buf_get_full_text(bufnr);
}; };
end) end)
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
for_each_buffer_client(bufnr, function(client) for_each_buffer_client(bufnr, function(client)
local allow_incremental_sync = if_nil(client.config.flags.allow_incremental_sync, true) local allow_incremental_sync = if_nil(client.config.flags.allow_incremental_sync, true)

View File

@ -357,8 +357,11 @@ end
--- Returns the range table for the difference between old and new lines --- Returns the range table for the difference between old and new lines
--@param old_lines table list of lines --@param old_lines table list of lines
--@param new_lines table list of lines --@param new_lines table list of lines
--@param start_line_idx int line to begin search for first difference
--@param end_line_idx int line to begin search for last difference
--@param offset_encoding string encoding requested by language server
--@returns table start_line_idx and start_col_idx of range --@returns table start_line_idx and start_col_idx of range
function M.compute_diff(old_lines, new_lines, start_line_idx, end_line_idx) function M.compute_diff(old_lines, new_lines, start_line_idx, end_line_idx, offset_encoding)
local start_line, start_char = first_difference(old_lines, new_lines, start_line_idx) local start_line, start_char = first_difference(old_lines, new_lines, start_line_idx)
local end_line, end_char = last_difference(vim.list_slice(old_lines, start_line, #old_lines), local end_line, end_char = last_difference(vim.list_slice(old_lines, start_line, #old_lines),
vim.list_slice(new_lines, start_line, #new_lines), start_char, end_line_idx) vim.list_slice(new_lines, start_line, #new_lines), start_char, end_line_idx)
@ -373,13 +376,19 @@ function M.compute_diff(old_lines, new_lines, start_line_idx, end_line_idx)
adj_end_char = #old_lines[#old_lines + end_line + 1] + end_char + 1 adj_end_char = #old_lines[#old_lines + end_line + 1] + end_char + 1
end end
local _, utf16_start_char = vim.str_utfindex(old_lines[start_line], start_char - 1) local _
local _, utf16_end_char = vim.str_utfindex(old_lines[#old_lines + end_line + 1], adj_end_char) if offset_encoding == "utf-16" then
_, start_char = vim.str_utfindex(old_lines[start_line], start_char - 1)
_, end_char = vim.str_utfindex(old_lines[#old_lines + end_line + 1], adj_end_char)
else
start_char = start_char - 1
end_char = adj_end_char
end
local result = { local result = {
range = { range = {
start = { line = start_line - 1, character = utf16_start_char}, start = { line = start_line - 1, character = start_char},
["end"] = { line = adj_end_line, character = utf16_end_char} ["end"] = { line = adj_end_line, character = end_char}
}, },
text = text, text = text,
rangeLength = length + 1, rangeLength = length + 1,