Skip to content

Commit

Permalink
Nvim11 position encoding fix (#1513)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LarssonMartin1998 authored Jan 14, 2025
1 parent 5fce153 commit e5e25f8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lua/lspsaga/callhierarchy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function ch:send_prepare_call()
end
self.list = slist.new()

local params = lsp.util.make_position_params()
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ client = client }))
client.request(get_method(1), params, function(_, result, ctx)
if api.nvim_get_current_buf() ~= ctx.bufnr then
return
Expand Down
5 changes: 2 additions & 3 deletions lua/lspsaga/codeaction/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,15 @@ function act:send_request(main_buf, options, callback)
end
local params
local mode = api.nvim_get_mode().mode
local client = vim.lsp.get_clients({ bufnr = main_buf })[1]
local offset_encoding = client and client.offset_encoding or 'utf-16'
local offset_encoding = util.get_offset_encoding({ bufnr = main_buf })
if options.range then
assert(type(options.range) == 'table', 'code_action range must be a table')
local start = assert(options.range.start, 'range must have a `start` property')
local end_ = assert(options.range['end'], 'range must have a `end` property')
params = lsp.util.make_given_range_params(start, end_, nil, offset_encoding)
elseif mode == 'v' or mode == 'V' then
local range = range_from_selection(0, mode)
params = lsp.util.make_given_range_params(range.start, range['end'])
params = lsp.util.make_given_range_params(range.start, range['end'], nil, offset_encoding)
else
params = lsp.util.make_range_params(0, offset_encoding)
end
Expand Down
5 changes: 2 additions & 3 deletions lua/lspsaga/codeaction/lightbulb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local api, lsp, fn = vim.api, vim.lsp, vim.fn
---@diagnostic disable-next-line: deprecated
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
local config = require('lspsaga').config
local util = require('lspsaga.util')
local nvim_buf_set_extmark = api.nvim_buf_set_extmark
local inrender_row = -1
local inrender_buf = nil
Expand Down Expand Up @@ -106,9 +107,7 @@ end

local function render(bufnr)
local row = api.nvim_win_get_cursor(0)[1] - 1
local client = vim.lsp.get_clients({ bufnr = bufnr })[1]
local offset_encoding = client and client.offset_encoding or 'utf-16'
local params = lsp.util.make_range_params(0, offset_encoding)
local params = lsp.util.make_range_params(0, util.get_offset_encoding({ bufnr = bufnr }))
params.context = {
diagnostics = diagnostic_vim_to_lsp(vim.diagnostic.get(bufnr, { lnum = row })),
}
Expand Down
2 changes: 1 addition & 1 deletion lua/lspsaga/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function def:definition_request(method, handler_T, args)

local current_buf = api.nvim_get_current_buf()

local params = lsp.util.make_position_params()
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = current_buf }))
if not self.opt_restore then
self.opt_restore = win:minimal_restore()
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lspsaga/finder/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ function fd:new(args)
end

self.list = slist.new()
local params = lsp.util.make_position_params()
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = curbuf }))
params.context = {
includeDeclaration = true,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/lspsaga/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ local function ignore_error(args, can_through)
end

function hover:do_request(args)
local params = lsp.util.make_position_params()
local method = 'textDocument/hover'
local clients = util.get_client_by_method(method)
if #clients == 0 then
Expand All @@ -231,6 +230,7 @@ function hover:do_request(args)
end
local count = 0

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

Expand Down
4 changes: 2 additions & 2 deletions lua/lspsaga/rename/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ end

function rename:find_reference()
local bufnr = api.nvim_get_current_buf()
local params = lsp.util.make_position_params()
params.context = { includeDeclaration = true }
local clients = util.get_client_by_method('textDocument/references')
if #clients == 0 then
return
end

local params = lsp.util.make_position_params(0, util.get_offset_encoding({ client = clients[1] }))
params.context = { includeDeclaration = true }
clients[1].request('textDocument/references', params, function(_, result)
if not result then
return
Expand Down
39 changes: 39 additions & 0 deletions lua/lspsaga/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,45 @@ function M.sub_mac_c_header(fname)
return fname:sub(pos + 1)
end

--- Key value pairs used to filter the approach
--- Use client directly
--- @class lspsaga.util.get_offset_encoding.Filter
--- @inlinedoc
--- @field client? table
---
--- Try to use the first client that matches bufnr
--- @field bufnr? integer
---
--- Try to use the given method to retrieve the client
--- @field method? string
---
---@param filter table
---@param fallback string
---@return string 'utf-8'|'utf-16'|'utf-32'
function M.get_offset_encoding(filter, fallback)
vim.validate('filter', filter, 'table', true)
filter = filter or {}
fallback = fallback or 'utf-16'

if filter.client then
if filter.client and filter.client.offset_encoding then
return filter.client.offset_encoding
end
elseif filter.bufnr then
local clients = lsp.get_clients({ bufnr = filter.bufnr })
if #clients > 0 and clients[1].offset_encoding then
return clients[1].offset_encoding
end
elseif filter.method then
local clients = M.get_client_by_method(filter.method)[1].offset_encoding
if #clients > 0 and clients[1].offset_encoding then
return clients[1].offset_encoding
end
end

return fallback
end

function M.valid_markdown_parser()
local parsers = { 'parser/markdown.so', 'parser/markdown_inline.so' }
for _, p in ipairs(parsers) do
Expand Down

0 comments on commit e5e25f8

Please sign in to comment.