Skip to content

Commit e5e25f8

Browse files
author
Martin Larsson
authored
Nvim11 position encoding fix (#1513)
* Fix correct usages of: `make_position_params`, `make_given_range_params`, and `make_range_params`. They work the same way but has been given warnings if offset_encoding (or position_encoding) isn't passed. * Change deprecated `vim.tbl_islist` which will be removed in 0.12 to `vim.islist`. * Change deprecated `lsp.diagnostic.get_line_diagnostics` to `vim.diagnostic.get` * Fix incorrect row for code action preview, it displayed alright with default options but overlapped with git signs expansion. * Change to pass nil instead of main_buf to make_given_range_params to match rest of code * Fix missing space in parameter in call to `make_position_params` to pass stylua --check
1 parent 5fce153 commit e5e25f8

File tree

8 files changed

+49
-12
lines changed

8 files changed

+49
-12
lines changed

lua/lspsaga/callhierarchy.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ function ch:send_prepare_call()
470470
end
471471
self.list = slist.new()
472472

473-
local params = lsp.util.make_position_params()
473+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ client = client }))
474474
client.request(get_method(1), params, function(_, result, ctx)
475475
if api.nvim_get_current_buf() ~= ctx.bufnr then
476476
return

lua/lspsaga/codeaction/init.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,15 @@ function act:send_request(main_buf, options, callback)
169169
end
170170
local params
171171
local mode = api.nvim_get_mode().mode
172-
local client = vim.lsp.get_clients({ bufnr = main_buf })[1]
173-
local offset_encoding = client and client.offset_encoding or 'utf-16'
172+
local offset_encoding = util.get_offset_encoding({ bufnr = main_buf })
174173
if options.range then
175174
assert(type(options.range) == 'table', 'code_action range must be a table')
176175
local start = assert(options.range.start, 'range must have a `start` property')
177176
local end_ = assert(options.range['end'], 'range must have a `end` property')
178177
params = lsp.util.make_given_range_params(start, end_, nil, offset_encoding)
179178
elseif mode == 'v' or mode == 'V' then
180179
local range = range_from_selection(0, mode)
181-
params = lsp.util.make_given_range_params(range.start, range['end'])
180+
params = lsp.util.make_given_range_params(range.start, range['end'], nil, offset_encoding)
182181
else
183182
params = lsp.util.make_range_params(0, offset_encoding)
184183
end

lua/lspsaga/codeaction/lightbulb.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local api, lsp, fn = vim.api, vim.lsp, vim.fn
22
---@diagnostic disable-next-line: deprecated
33
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
44
local config = require('lspsaga').config
5+
local util = require('lspsaga.util')
56
local nvim_buf_set_extmark = api.nvim_buf_set_extmark
67
local inrender_row = -1
78
local inrender_buf = nil
@@ -106,9 +107,7 @@ end
106107

107108
local function render(bufnr)
108109
local row = api.nvim_win_get_cursor(0)[1] - 1
109-
local client = vim.lsp.get_clients({ bufnr = bufnr })[1]
110-
local offset_encoding = client and client.offset_encoding or 'utf-16'
111-
local params = lsp.util.make_range_params(0, offset_encoding)
110+
local params = lsp.util.make_range_params(0, util.get_offset_encoding({ bufnr = bufnr }))
112111
params.context = {
113112
diagnostics = diagnostic_vim_to_lsp(vim.diagnostic.get(bufnr, { lnum = row })),
114113
}

lua/lspsaga/definition.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function def:definition_request(method, handler_T, args)
209209

210210
local current_buf = api.nvim_get_current_buf()
211211

212-
local params = lsp.util.make_position_params()
212+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = current_buf }))
213213
if not self.opt_restore then
214214
self.opt_restore = win:minimal_restore()
215215
end

lua/lspsaga/finder/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ function fd:new(args)
511511
end
512512

513513
self.list = slist.new()
514-
local params = lsp.util.make_position_params()
514+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = curbuf }))
515515
params.context = {
516516
includeDeclaration = true,
517517
}

lua/lspsaga/hover.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ local function ignore_error(args, can_through)
222222
end
223223

224224
function hover:do_request(args)
225-
local params = lsp.util.make_position_params()
226225
local method = 'textDocument/hover'
227226
local clients = util.get_client_by_method(method)
228227
if #clients == 0 then
@@ -231,6 +230,7 @@ function hover:do_request(args)
231230
end
232231
local count = 0
233232

233+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ client = clients[1] }))
234234
_, self.cancel = lsp.buf_request(0, method, params, function(_, result, ctx, _)
235235
count = count + 1
236236

lua/lspsaga/rename/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ end
4747

4848
function rename:find_reference()
4949
local bufnr = api.nvim_get_current_buf()
50-
local params = lsp.util.make_position_params()
51-
params.context = { includeDeclaration = true }
5250
local clients = util.get_client_by_method('textDocument/references')
5351
if #clients == 0 then
5452
return
5553
end
5654

55+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ client = clients[1] }))
56+
params.context = { includeDeclaration = true }
5757
clients[1].request('textDocument/references', params, function(_, result)
5858
if not result then
5959
return

lua/lspsaga/util.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,45 @@ function M.sub_mac_c_header(fname)
216216
return fname:sub(pos + 1)
217217
end
218218

219+
--- Key value pairs used to filter the approach
220+
--- Use client directly
221+
--- @class lspsaga.util.get_offset_encoding.Filter
222+
--- @inlinedoc
223+
--- @field client? table
224+
---
225+
--- Try to use the first client that matches bufnr
226+
--- @field bufnr? integer
227+
---
228+
--- Try to use the given method to retrieve the client
229+
--- @field method? string
230+
---
231+
---@param filter table
232+
---@param fallback string
233+
---@return string 'utf-8'|'utf-16'|'utf-32'
234+
function M.get_offset_encoding(filter, fallback)
235+
vim.validate('filter', filter, 'table', true)
236+
filter = filter or {}
237+
fallback = fallback or 'utf-16'
238+
239+
if filter.client then
240+
if filter.client and filter.client.offset_encoding then
241+
return filter.client.offset_encoding
242+
end
243+
elseif filter.bufnr then
244+
local clients = lsp.get_clients({ bufnr = filter.bufnr })
245+
if #clients > 0 and clients[1].offset_encoding then
246+
return clients[1].offset_encoding
247+
end
248+
elseif filter.method then
249+
local clients = M.get_client_by_method(filter.method)[1].offset_encoding
250+
if #clients > 0 and clients[1].offset_encoding then
251+
return clients[1].offset_encoding
252+
end
253+
end
254+
255+
return fallback
256+
end
257+
219258
function M.valid_markdown_parser()
220259
local parsers = { 'parser/markdown.so', 'parser/markdown_inline.so' }
221260
for _, p in ipairs(parsers) do

0 commit comments

Comments
 (0)