Skip to content

Commit

Permalink
fix(lsp): fall back to default offset encoding if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Nov 29, 2024
1 parent 0362314 commit 4ac7a3c
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion ftplugin/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if not vim.g.loaded_rustaceanvim then
vim.lsp.commands['rust-analyzer.gotoLocation'] = function(command, ctx)
local client = vim.lsp.get_client_by_id(ctx.client_id)
if client then
compat.show_document(command.arguments[1], client.offset_encoding)
compat.show_document(command.arguments[1], client.offset_encoding or 'utf-8')
end
end

Expand Down
4 changes: 2 additions & 2 deletions lua/rustaceanvim/commands/code_action_group.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local M = {}
---@param ctx table
function M.apply_action(action, client, ctx)
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding or 'utf-8')
end
if action.command then
local command = type(action.command) == 'table' and action.command or action
Expand Down Expand Up @@ -383,7 +383,7 @@ M.code_action_group = function()
if #clients == 0 then
return
end
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding)
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding or 'utf-8')
---@diagnostic disable-next-line: inject-field
params.context = context

Expand Down
9 changes: 2 additions & 7 deletions lua/rustaceanvim/commands/expand_macro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ local ui = require('rustaceanvim.ui')

local M = {}

---@return lsp_position_params
---@param offset_encoding 'utf-8'|'utf-16'|'utf-32'
local function get_params(offset_encoding)
return vim.lsp.util.make_position_params(0, offset_encoding)
end

---@type integer | nil
local latest_buf_id = nil

Expand Down Expand Up @@ -78,7 +72,8 @@ function M.expand_macro()
if #clients == 0 then
return
end
ra.buf_request(0, 'rust-analyzer/expandMacro', get_params(clients[1].offset_encoding), handler)
local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8')
ra.buf_request(0, 'rust-analyzer/expandMacro', params, handler)
end

return M.expand_macro
2 changes: 1 addition & 1 deletion lua/rustaceanvim/commands/external_docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function M.open_external_docs()
ra.buf_request(
0,
'experimental/externalDocs',
vim.lsp.util.make_position_params(0, clients[1].offset_encoding),
vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8'),
function(_, url)
if url then
local config = require('rustaceanvim.config.internal')
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/commands/hover_range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function M.hover_range()
if #clients == 0 then
return
end
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding)
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding or 'utf-8')
---@diagnostic disable-next-line: inject-field
params.position = get_visual_selected_range()
params.range = nil
Expand Down
6 changes: 3 additions & 3 deletions lua/rustaceanvim/commands/join_lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local function modify_params(params)
end

local function handler(_, result, ctx)
vim.lsp.util.apply_text_edits(result, ctx.bufnr, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
vim.lsp.util.apply_text_edits(result, ctx.bufnr, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding or 'utf-8')
end

local ra = require('rustaceanvim.rust_analyzer')
Expand All @@ -24,7 +24,7 @@ function M.join_lines_visual()
if #clients == 0 then
return
end
local params = modify_params(vim.lsp.util.make_given_range_params(nil, nil, 0, clients[1].offset_encoding))
local params = modify_params(vim.lsp.util.make_given_range_params(nil, nil, 0, clients[1].offset_encoding or 'utf-8'))
ra.buf_request(0, 'experimental/joinLines', params, handler)
end

Expand All @@ -35,7 +35,7 @@ function M.join_lines()
if #clients == 0 then
return
end
local params = modify_params(vim.lsp.util.make_range_params(0, clients[1].offset_encoding))
local params = modify_params(vim.lsp.util.make_range_params(0, clients[1].offset_encoding or 'utf-8'))
ra.buf_request(0, 'experimental/joinLines', params, handler)
end

Expand Down
8 changes: 6 additions & 2 deletions lua/rustaceanvim/commands/move_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ local function handler(_, text_edits, ctx)
local cursor = extract_cursor_position(text_edits)
local overrides = require('rustaceanvim.overrides')
overrides.snippet_text_edits_to_text_edits(text_edits)
vim.lsp.util.apply_text_edits(text_edits, ctx.bufnr, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
vim.lsp.util.apply_text_edits(
text_edits,
ctx.bufnr,
vim.lsp.get_client_by_id(ctx.client_id).offset_encoding or 'utf-8'
)
vim.api.nvim_win_set_cursor(0, cursor)
end

Expand All @@ -53,7 +57,7 @@ function M.move_item(direction)
if #clients == 0 then
return
end
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding)
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding or 'utf-8')
---@diagnostic disable-next-line: inject-field
params.direction = direction
ra.buf_request(0, 'experimental/moveItem', params, handler)
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/commands/open_cargo_toml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function handler(_, result, ctx)

local client = vim.lsp.get_client_by_id(ctx.client_id)
if client then
compat.show_document(result, client.offset_encoding)
compat.show_document(result, client.offset_encoding or 'utf-8')
end
end

Expand Down
4 changes: 2 additions & 2 deletions lua/rustaceanvim/commands/parent_module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local function handler(_, result, ctx)

local client = vim.lsp.get_client_by_id(ctx.client_id)
if client then
compat.show_document(location, client.offset_encoding)
compat.show_document(location, client.offset_encoding or 'utf-8')
end
end

Expand All @@ -27,7 +27,7 @@ function M.parent_module()
if #clients == 0 then
return
end
local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding)
local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8')
ra.buf_request(0, 'experimental/parentModule', params, handler)
end

Expand Down
8 changes: 4 additions & 4 deletions lua/rustaceanvim/commands/ssr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local function handler(err, result, ctx)

local client = vim.lsp.get_client_by_id(ctx.client_id)
if client then
vim.lsp.util.apply_workspace_edit(result, client.offset_encoding)
vim.lsp.util.apply_workspace_edit(result, client.offset_encoding or 'utf-8')
end
end

Expand All @@ -30,8 +30,8 @@ local function ssr(query, make_range_params)
if #clients == 0 then
return
end
local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding)
local range = make_range_params(0, clients[1].offset_encoding).range
local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8')
local range = make_range_params(0, clients[1].offset_encoding or 'utf-8').range
if not query then
vim.ui.input({ prompt = 'Enter query: ' }, function(input)
query = input
Expand All @@ -51,7 +51,7 @@ end
---@param query string | nil
function M.ssr_visual(query)
ssr(query, function(winnr, offset_encoding)
return vim.lsp.util.make_given_range_params(nil, nil, winnr, offset_encoding)
return vim.lsp.util.make_given_range_params(nil, nil, winnr, offset_encoding or 'utf-8')
end)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/commands/syntax_tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function M.syntax_tree()
if #clients == 0 then
return
end
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding)
local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding or 'utf-8')
ra.buf_request(0, 'rust-analyzer/syntaxTree', params, handler)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/commands/view_ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function M.viewIR(level)
if #clients == 0 then
return
end
local position_params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding)
local position_params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8')
rl.buf_request(0, 'rust-analyzer/view' .. level, position_params, function(...)
return handler(level, ...)
end)
Expand Down
4 changes: 2 additions & 2 deletions lua/rustaceanvim/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ end
function compat.show_document(location, offset_encoding)
local show_document = vim.lsp.show_document
if not show_document then
return vim.lsp.util.jump_to_location(location, offset_encoding)
return vim.lsp.util.jump_to_location(location, offset_encoding or 'utf-8')
end
return show_document(location, offset_encoding, { focus = true })
return show_document(location, offset_encoding or 'utf-8', { focus = true })
end

--- @param client vim.lsp.Client
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/hover_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function M.hover_actions()
if #clients == 0 then
return
end
local params = lsp_util.make_position_params(0, clients[1].offset_encoding)
local params = lsp_util.make_position_params(0, clients[1].offset_encoding or 'utf-8')
ra.buf_request(0, 'textDocument/hover', params, M.handler)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function override_apply_text_edits()
vim.lsp.util.apply_text_edits = function(edits, bufnr, offset_encoding)
local overrides = require('rustaceanvim.overrides')
overrides.snippet_text_edits_to_text_edits(edits)
old_func(edits, bufnr, offset_encoding)
old_func(edits, bufnr, offset_encoding or 'utf-8')
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/runnables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function M.get_runnable_at_cursor_position(runnables)
return
end
---@type lsp.Position
local position = vim.lsp.util.make_position_params(0, clients[1].offset_encoding).position
local position = vim.lsp.util.make_position_params(0, clients[1].offset_encoding or 'utf-8').position
---@type integer|nil, integer|nil
local choice, fallback
for idx, runnable in ipairs(runnables) do
Expand Down

0 comments on commit 4ac7a3c

Please sign in to comment.