aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-10-16 13:17:10 +0100
committerLewis Russell <lewis6991@gmail.com>2024-10-17 12:52:45 +0100
commitacbc6a7f91d15fe5f59df08227d196156fafedb4 (patch)
tree4e729aa999d9dea4dcba583cc17fd74c0f2edca8
parentff1d7d42995931d17395223cec6fb2031a870d15 (diff)
downloadrneovim-acbc6a7f91d15fe5f59df08227d196156fafedb4.tar.gz
rneovim-acbc6a7f91d15fe5f59df08227d196156fafedb4.tar.bz2
rneovim-acbc6a7f91d15fe5f59df08227d196156fafedb4.zip
fix(lsp.util): inconsistent handling of offset_encoding
-rw-r--r--runtime/lua/vim/lsp/_tagfunc.lua7
-rw-r--r--runtime/lua/vim/lsp/buf.lua2
-rw-r--r--runtime/lua/vim/lsp/util.lua7
-rw-r--r--test/functional/plugin/lsp_spec.lua2
4 files changed, 14 insertions, 4 deletions
diff --git a/runtime/lua/vim/lsp/_tagfunc.lua b/runtime/lua/vim/lsp/_tagfunc.lua
index 4ad50e4a58..70917de48d 100644
--- a/runtime/lua/vim/lsp/_tagfunc.lua
+++ b/runtime/lua/vim/lsp/_tagfunc.lua
@@ -27,15 +27,20 @@ local function query_definition(pattern)
return {}
end
local results = {}
+
+ --- @param range lsp.Range
+ --- @param uri string
+ --- @param offset_encoding string
local add = function(range, uri, offset_encoding)
table.insert(results, mk_tag_item(pattern, range, uri, offset_encoding))
end
+
for client_id, lsp_results in pairs(assert(results_by_client)) do
local client = lsp.get_client_by_id(client_id)
local offset_encoding = client and client.offset_encoding or 'utf-16'
local result = lsp_results.result or {}
if result.range then -- Location
- add(result.range, result.uri)
+ add(result.range, result.uri, offset_encoding)
else
result = result --[[@as (lsp.Location[]|lsp.LocationLink[])]]
for _, item in pairs(result) do
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index 301c1f0cb6..e3f4beeeb1 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -338,6 +338,8 @@ function M.rename(new_name, opts)
-- Compute early to account for cursor movements after going async
local cword = vim.fn.expand('<cword>')
+ --- @param range lsp.Range
+ --- @param offset_encoding string
local function get_text_at_range(range, offset_encoding)
return api.nvim_buf_get_text(
bufnr,
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 31a9fdb2fc..aed742f001 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -347,7 +347,7 @@ end
--- Position is a https://microsoft.github.io/language-server-protocol/specifications/specification-current/#position
---@param position lsp.Position
----@param offset_encoding? string utf-8|utf-16|utf-32
+---@param offset_encoding 'utf-8'|'utf-16'|'utf-32'
---@return integer
local function get_line_byte_from_position(bufnr, position, offset_encoding)
-- LSP's line and characters are 0-indexed
@@ -357,7 +357,7 @@ local function get_line_byte_from_position(bufnr, position, offset_encoding)
-- character
if col > 0 then
local line = get_line(bufnr, position.line) or ''
- return M._str_byteindex_enc(line, col, offset_encoding or 'utf-16')
+ return M._str_byteindex_enc(line, col, offset_encoding)
end
return col
end
@@ -512,6 +512,7 @@ function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
'apply_text_document_edit must be called with valid offset encoding',
vim.log.levels.WARN
)
+ return
end
-- For lists of text document edits,
@@ -703,6 +704,7 @@ function M.apply_workspace_edit(workspace_edit, offset_encoding)
'apply_workspace_edit must be called with valid offset encoding',
vim.log.levels.WARN
)
+ return
end
if workspace_edit.documentChanges then
for idx, change in ipairs(workspace_edit.documentChanges) do
@@ -988,6 +990,7 @@ function M.show_document(location, offset_encoding, opts)
end
if offset_encoding == nil then
vim.notify_once('show_document must be called with valid offset encoding', vim.log.levels.WARN)
+ return false
end
local bufnr = vim.uri_to_bufnr(uri)
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua
index 5309e1967c..6cd433b975 100644
--- a/test/functional/plugin/lsp_spec.lua
+++ b/test/functional/plugin/lsp_spec.lua
@@ -2586,7 +2586,7 @@ describe('LSP', function()
},
},
}
- eq(false, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit))
+ eq(false, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16'))
eq(false, vim.uv.fs_stat(tmpfile) ~= nil)
end)
end)