diff --git a/flake.lock b/flake.lock index dcf09972..51603447 100644 --- a/flake.lock +++ b/flake.lock @@ -281,11 +281,11 @@ "nixpkgs-stable": "nixpkgs-stable_2" }, "locked": { - "lastModified": 1731363552, - "narHash": "sha256-vFta1uHnD29VUY4HJOO/D6p6rxyObnf+InnSMT4jlMU=", + "lastModified": 1732021966, + "narHash": "sha256-mnTbjpdqF0luOkou8ZFi2asa1N3AA2CchR/RqCNmsGE=", "owner": "cachix", "repo": "git-hooks.nix", - "rev": "cd1af27aa85026ac759d5d3fccf650abe7e1bbf0", + "rev": "3308484d1a443fc5bc92012435d79e80458fe43c", "type": "github" }, "original": { @@ -310,11 +310,11 @@ ] }, "locked": { - "lastModified": 1731363552, - "narHash": "sha256-vFta1uHnD29VUY4HJOO/D6p6rxyObnf+InnSMT4jlMU=", + "lastModified": 1732021966, + "narHash": "sha256-mnTbjpdqF0luOkou8ZFi2asa1N3AA2CchR/RqCNmsGE=", "owner": "cachix", "repo": "git-hooks.nix", - "rev": "cd1af27aa85026ac759d5d3fccf650abe7e1bbf0", + "rev": "3308484d1a443fc5bc92012435d79e80458fe43c", "type": "github" }, "original": { @@ -506,11 +506,11 @@ "nixpkgs": "nixpkgs_4" }, "locked": { - "lastModified": 1731648623, - "narHash": "sha256-kL0qF6ETmWSeN0Nzjg7GtSbe/7AcNmuX2U6Mb7xLIOA=", + "lastModified": 1732253278, + "narHash": "sha256-/ONdyXzZ0dK+GlV6lKq9+efY0BsXxUJ2cQp5LbL0MVA=", "owner": "nvim-neorocks", "repo": "neorocks", - "rev": "b82f112cb43020ab23a2816dfc7a3453aafd754f", + "rev": "76c80c100d549c0674cf2d6721cf27cc44412e65", "type": "github" }, "original": { @@ -529,11 +529,11 @@ "nixpkgs": "nixpkgs_3" }, "locked": { - "lastModified": 1731629313, - "narHash": "sha256-rCTdc6oHM4Ostt6IR66QiDrPQNjCsxC2r00GfU0bADU=", + "lastModified": 1732234033, + "narHash": "sha256-94yZ7eJiLvW+UknI5RZBCV6OMHtoSv1oWyOwKjTmS88=", "owner": "nix-community", "repo": "neovim-nightly-overlay", - "rev": "81a983160c1acae623482e082a94a46c931d0261", + "rev": "10e11c32a4f4f7c9d64f45413642ded11fc538b6", "type": "github" }, "original": { @@ -545,11 +545,11 @@ "neovim-src": { "flake": false, "locked": { - "lastModified": 1731607016, - "narHash": "sha256-EjIQ7ok02IfDHDMKdKELuvFRHlZhzhymDGGx3jg6nvQ=", + "lastModified": 1732229552, + "narHash": "sha256-7tA7IeOjx1wgDQnY7RxIhIuwcFeSZu4Yc3WtLh+22TE=", "owner": "neovim", "repo": "neovim", - "rev": "05d354e2165c2c331a33949d49095eef3503a32f", + "rev": "ff75f345ab5fa57c6560db021e8eb099aff90472", "type": "github" }, "original": { @@ -704,11 +704,11 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1731531548, - "narHash": "sha256-sz8/v17enkYmfpgeeuyzniGJU0QQBfmAjlemAUYhfy8=", + "lastModified": 1731890469, + "narHash": "sha256-D1FNZ70NmQEwNxpSSdTXCSklBH1z2isPR84J6DQrJGs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "24f0d4acd634792badd6470134c387a3b039dace", + "rev": "5083ec887760adfe12af64830a66807423a859a7", "type": "github" }, "original": { @@ -720,11 +720,11 @@ }, "nixpkgs_4": { "locked": { - "lastModified": 1731531548, - "narHash": "sha256-sz8/v17enkYmfpgeeuyzniGJU0QQBfmAjlemAUYhfy8=", + "lastModified": 1731890469, + "narHash": "sha256-D1FNZ70NmQEwNxpSSdTXCSklBH1z2isPR84J6DQrJGs=", "owner": "nixos", "repo": "nixpkgs", - "rev": "24f0d4acd634792badd6470134c387a3b039dace", + "rev": "5083ec887760adfe12af64830a66807423a859a7", "type": "github" }, "original": { @@ -736,11 +736,11 @@ }, "nixpkgs_5": { "locked": { - "lastModified": 1731763621, - "narHash": "sha256-ddcX4lQL0X05AYkrkV2LMFgGdRvgap7Ho8kgon3iWZk=", + "lastModified": 1731890469, + "narHash": "sha256-D1FNZ70NmQEwNxpSSdTXCSklBH1z2isPR84J6DQrJGs=", "owner": "nixos", "repo": "nixpkgs", - "rev": "c69a9bffbecde46b4b939465422ddc59493d3e4d", + "rev": "5083ec887760adfe12af64830a66807423a859a7", "type": "github" }, "original": { @@ -790,11 +790,11 @@ "nixpkgs-stable": "nixpkgs-stable_3" }, "locked": { - "lastModified": 1731363552, - "narHash": "sha256-vFta1uHnD29VUY4HJOO/D6p6rxyObnf+InnSMT4jlMU=", + "lastModified": 1732021966, + "narHash": "sha256-mnTbjpdqF0luOkou8ZFi2asa1N3AA2CchR/RqCNmsGE=", "owner": "cachix", "repo": "pre-commit-hooks.nix", - "rev": "cd1af27aa85026ac759d5d3fccf650abe7e1bbf0", + "rev": "3308484d1a443fc5bc92012435d79e80458fe43c", "type": "github" }, "original": { diff --git a/lua/rustaceanvim/commands/code_action_group.lua b/lua/rustaceanvim/commands/code_action_group.lua index 4a063a90..555f3f53 100644 --- a/lua/rustaceanvim/commands/code_action_group.lua +++ b/lua/rustaceanvim/commands/code_action_group.lua @@ -1,5 +1,6 @@ local ui = require('rustaceanvim.ui') local config = require('rustaceanvim.config.internal') +local compat = require('rustaceanvim.compat') local M = {} ---@class rustaceanvim.RACodeAction @@ -47,7 +48,7 @@ function M.on_user_choice(action_tuple, ctx) return end if not action.edit and type(code_action_provider) == 'table' and code_action_provider.resolveProvider then - client.request('codeAction/resolve', action, function(err, resolved_action) + compat.client_request(client, 'codeAction/resolve', action, function(err, resolved_action) ---@cast resolved_action rustaceanvim.RACodeAction|rustaceanvim.RACommand if err then vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR) @@ -377,7 +378,13 @@ M.state = { M.code_action_group = function() local context = {} context.diagnostics = require('rustaceanvim.compat').get_line_diagnostics() - local params = vim.lsp.util.make_range_params() + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding) + ---@diagnostic disable-next-line: inject-field params.context = context vim.lsp.buf_request_all(0, 'textDocument/codeAction', params, function(results) diff --git a/lua/rustaceanvim/commands/expand_macro.lua b/lua/rustaceanvim/commands/expand_macro.lua index 26c21d67..a403f81b 100644 --- a/lua/rustaceanvim/commands/expand_macro.lua +++ b/lua/rustaceanvim/commands/expand_macro.lua @@ -3,8 +3,9 @@ local ui = require('rustaceanvim.ui') local M = {} ---@return lsp_position_params -local function get_params() - return vim.lsp.util.make_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 @@ -70,11 +71,14 @@ local function handler(_, result) ui.resize(true, '-25') end -local rl = require('rustaceanvim.rust_analyzer') - --- Sends the request to rust-analyzer to expand the macro under the cursor function M.expand_macro() - rl.buf_request(0, 'rust-analyzer/expandMacro', get_params(), handler) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + ra.buf_request(0, 'rust-analyzer/expandMacro', get_params(clients[1].offset_encoding), handler) end return M.expand_macro diff --git a/lua/rustaceanvim/commands/external_docs.lua b/lua/rustaceanvim/commands/external_docs.lua index 0f644a61..98764c81 100644 --- a/lua/rustaceanvim/commands/external_docs.lua +++ b/lua/rustaceanvim/commands/external_docs.lua @@ -1,14 +1,22 @@ local M = {} -local rl = require('rustaceanvim.rust_analyzer') - function M.open_external_docs() - rl.buf_request(0, 'experimental/externalDocs', vim.lsp.util.make_position_params(), function(_, url) - if url then - local config = require('rustaceanvim.config.internal') - config.tools.open_url(url) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + ra.buf_request( + 0, + 'experimental/externalDocs', + vim.lsp.util.make_position_params(0, clients[1].offset_encoding), + function(_, url) + if url then + local config = require('rustaceanvim.config.internal') + config.tools.open_url(url) + end end - end) + ) end return M.open_external_docs diff --git a/lua/rustaceanvim/commands/hover_range.lua b/lua/rustaceanvim/commands/hover_range.lua index 75d8a876..ae0c8eb0 100644 --- a/lua/rustaceanvim/commands/hover_range.lua +++ b/lua/rustaceanvim/commands/hover_range.lua @@ -42,18 +42,17 @@ local function get_visual_selected_range() return make_lsp_position(row1, math.min(col1, col2), row1, math.max(col1, col2)) end ----@return lsp_range_params -local function get_opts() - local params = vim.lsp.util.make_range_params() +function M.hover_range() + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding) + ---@diagnostic disable-next-line: inject-field params.position = get_visual_selected_range() params.range = nil - return params -end - -local rl = require('rustaceanvim.rust_analyzer') - -function M.hover_range() - rl.buf_request(0, 'textDocument/hover', get_opts()) + ra.buf_request(0, 'textDocument/hover', params) end return M.hover_range diff --git a/lua/rustaceanvim/commands/init.lua b/lua/rustaceanvim/commands/init.lua index 7ec07ecb..93788964 100644 --- a/lua/rustaceanvim/commands/init.lua +++ b/lua/rustaceanvim/commands/init.lua @@ -159,9 +159,13 @@ local rustlsp_command_tbl = { }, joinLines = { impl = function(_, opts) + local cmds = require('rustaceanvim.commands.join_lines') ---@cast opts vim.api.keyset.user_command - local visual_mode = opts.range and opts.range ~= 0 or false - require('rustaceanvim.commands.join_lines')(visual_mode) + if opts.range and opts.range ~= 0 then + cmds.join_lines_visual() + else + cmds.join_lines() + end end, }, moveItem = { @@ -171,9 +175,9 @@ local rustlsp_command_tbl = { return end if args[1] == 'down' then - require('rustaceanvim.commands.move_item')() + require('rustaceanvim.commands.move_item')('Down') elseif args[1] == 'up' then - require('rustaceanvim.commands.move_item')(true) + require('rustaceanvim.commands.move_item')('Up') else vim.notify( 'moveItem: unexpected argument: ' .. vim.inspect(args) .. " expected 'up' or 'down'", @@ -203,9 +207,13 @@ local rustlsp_command_tbl = { ssr = { impl = function(args, opts) ---@cast opts vim.api.keyset.user_command - local visual_mode = opts.range and opts.range > 0 or false local query = args and #args > 0 and table.concat(args, ' ') or nil - require('rustaceanvim.commands.ssr')(query, visual_mode) + local cmds = require('rustaceanvim.commands.ssr') + if opts.range and opts.range > 0 then + cmds.ssr_visual(query) + else + cmds.ssr(query) + end end, }, reloadWorkspace = { diff --git a/lua/rustaceanvim/commands/join_lines.lua b/lua/rustaceanvim/commands/join_lines.lua index 89f42181..1cc8d8a7 100644 --- a/lua/rustaceanvim/commands/join_lines.lua +++ b/lua/rustaceanvim/commands/join_lines.lua @@ -1,16 +1,13 @@ local M = {} ----@alias lsp_join_lines_params { textDocument: lsp_text_document, ranges: lsp_range[] } - ----@param visual_mode boolean ----@return lsp_join_lines_params -local function get_params(visual_mode) - local params = visual_mode and vim.lsp.util.make_given_range_params() or vim.lsp.util.make_range_params() +---@param params { textDocument: lsp_text_document, range: lsp_range } +---@return { textDocument: lsp_text_document, ranges: lsp_range[] } +local function modify_params(params) local range = params.range - params.range = nil + ---@diagnostic disable-next-line: inject-field params.ranges = { range } - + ---@diagnostic disable-next-line: return-type-mismatch return params end @@ -18,13 +15,28 @@ 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) end -local rl = require('rustaceanvim.rust_analyzer') +local ra = require('rustaceanvim.rust_analyzer') + +--- Sends the request to rust-analyzer to get the TextEdits to join the lines +--- under the cursor and applies them (for use in visual mode) +function M.join_lines_visual() + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = modify_params(vim.lsp.util.make_given_range_params(nil, nil, 0, clients[1].offset_encoding)) + ra.buf_request(0, 'experimental/joinLines', params, handler) +end --- Sends the request to rust-analyzer to get the TextEdits to join the lines --- under the cursor and applies them ----@param visual_mode boolean -function M.join_lines(visual_mode) - rl.buf_request(0, 'experimental/joinLines', get_params(visual_mode), handler) +function M.join_lines() + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = modify_params(vim.lsp.util.make_range_params(0, clients[1].offset_encoding)) + ra.buf_request(0, 'experimental/joinLines', params, handler) end -return M.join_lines +return M diff --git a/lua/rustaceanvim/commands/move_item.lua b/lua/rustaceanvim/commands/move_item.lua index 457b57a4..3d1b18ca 100644 --- a/lua/rustaceanvim/commands/move_item.lua +++ b/lua/rustaceanvim/commands/move_item.lua @@ -2,16 +2,6 @@ local M = {} ---@alias lsp_move_items_params { textDocument: lsp_text_document, range: lsp_range, direction: 'Up' | 'Down' } ----@param up boolean ----@return lsp_move_items_params -local function get_params(up) - local direction = up and 'Up' or 'Down' - local params = vim.lsp.util.make_range_params() - params.direction = direction - - return params -end - ---@param prev_text_edit rustaceanvim.lsp.TextEdit ---@param text_edit rustaceanvim.lsp.TextEdit local function text_edit_line_range_diff(prev_text_edit, text_edit) @@ -55,11 +45,18 @@ local function handler(_, text_edits, ctx) vim.api.nvim_win_set_cursor(0, cursor) end -local rl = require('rustaceanvim.rust_analyzer') - -- Sends the request to rust-analyzer to move the item and handle the response -function M.move_item(up) - rl.buf_request(0, 'experimental/moveItem', get_params(up or false), handler) +---@param direction 'Up' | 'Down' +function M.move_item(direction) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding) + ---@diagnostic disable-next-line: inject-field + params.direction = direction + ra.buf_request(0, 'experimental/moveItem', params, handler) end return M.move_item diff --git a/lua/rustaceanvim/commands/parent_module.lua b/lua/rustaceanvim/commands/parent_module.lua index ec624d07..12294b87 100644 --- a/lua/rustaceanvim/commands/parent_module.lua +++ b/lua/rustaceanvim/commands/parent_module.lua @@ -1,12 +1,7 @@ local M = {} -local rl = require('rustaceanvim.rust_analyzer') local compat = require('rustaceanvim.compat') -local function get_params() - return vim.lsp.util.make_position_params(0, nil) -end - local function handler(_, result, ctx) if result == nil or vim.tbl_isempty(result) then vim.api.nvim_out_write("Can't find parent module\n") @@ -27,7 +22,13 @@ end --- Sends the request to rust-analyzer to get the parent modules location and open it function M.parent_module() - rl.buf_request(0, 'experimental/parentModule', get_params(), handler) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding) + ra.buf_request(0, 'experimental/parentModule', params, handler) end return M.parent_module diff --git a/lua/rustaceanvim/commands/ssr.lua b/lua/rustaceanvim/commands/ssr.lua index b2724bfb..7e1e8fee 100644 --- a/lua/rustaceanvim/commands/ssr.lua +++ b/lua/rustaceanvim/commands/ssr.lua @@ -1,17 +1,14 @@ local M = {} ----@param query string ----@param visual_mode boolean -local function get_opts(query, visual_mode) - local opts = vim.lsp.util.make_position_params() - local range = (visual_mode and vim.lsp.util.make_given_range_params() or vim.lsp.util.make_range_params()).range - ---@diagnostic disable-next-line: inject-field - opts.query = query - ---@diagnostic disable-next-line: inject-field - opts.parseOnly = false - ---@diagnostic disable-next-line: inject-field - opts.selections = { range } - return opts +local ra = require('rustaceanvim.rust_analyzer') + +---@params table +---@query string +---@param range table +local function modify_params(params, query, range) + params.query = query + params.parseOnly = false + params.selections = { range } end local function handler(err, result, ctx) @@ -26,19 +23,36 @@ local function handler(err, result, ctx) end end -local rl = require('rustaceanvim.rust_analyzer') - ----@param query string | nil ----@param visual_mode boolean -function M.ssr(query, visual_mode) +---@param query? string +---@param make_range_params fun(bufnr: integer, offset_encoding: string):{ range: table } +local function ssr(query, make_range_params) + local clients = ra.get_active_rustaceanvim_clients(0) + 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 if not query then vim.ui.input({ prompt = 'Enter query: ' }, function(input) query = input end) end + modify_params(params, query, range) if query then - rl.buf_request(0, 'experimental/ssr', get_opts(query, visual_mode), handler) + ra.buf_request(0, 'experimental/ssr', params, handler) end end -return M.ssr +---@param query? string +M.ssr = function(query) + ssr(query, vim.lsp.util.make_range_params) +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) + end) +end + +return M diff --git a/lua/rustaceanvim/commands/syntax_tree.lua b/lua/rustaceanvim/commands/syntax_tree.lua index ba89594d..7571d8f1 100644 --- a/lua/rustaceanvim/commands/syntax_tree.lua +++ b/lua/rustaceanvim/commands/syntax_tree.lua @@ -2,11 +2,6 @@ local ui = require('rustaceanvim.ui') local M = {} ----@return lsp_range_params -local function get_params() - return vim.lsp.util.make_range_params() -end - ---@type integer | nil local latest_buf_id = nil @@ -29,9 +24,14 @@ local function handler(_, result) ui.resize(true, '-25') end -local rl = require('rustaceanvim.rust_analyzer') function M.syntax_tree() - rl.buf_request(0, 'rust-analyzer/syntaxTree', get_params(), handler) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = vim.lsp.util.make_range_params(0, clients[1].offset_encoding) + ra.buf_request(0, 'rust-analyzer/syntaxTree', params, handler) end return M.syntax_tree diff --git a/lua/rustaceanvim/commands/view_ir.lua b/lua/rustaceanvim/commands/view_ir.lua index 617db33f..7471a61f 100644 --- a/lua/rustaceanvim/commands/view_ir.lua +++ b/lua/rustaceanvim/commands/view_ir.lua @@ -41,7 +41,12 @@ end ---@param level rustaceanvim.ir.level function M.viewIR(level) - local position_params = vim.lsp.util.make_position_params(0, nil) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local position_params = vim.lsp.util.make_position_params(0, clients[1].offset_encoding) rl.buf_request(0, 'rust-analyzer/view' .. level, position_params, function(...) return handler(level, ...) end) diff --git a/lua/rustaceanvim/compat.lua b/lua/rustaceanvim/compat.lua index 5e4db56b..46138181 100644 --- a/lua/rustaceanvim/compat.lua +++ b/lua/rustaceanvim/compat.lua @@ -28,4 +28,58 @@ function compat.show_document(location, offset_encoding) return show_document(location, offset_encoding, { focus = true }) end +--- @param client vim.lsp.Client +--- @param method string LSP method name. +--- @param params? table LSP request params. +--- @param handler? lsp.Handler Response |lsp-handler| for this method. +--- @param bufnr? integer Buffer handle. 0 for current (default). +--- @return boolean status indicates whether the request was successful. +--- If it is `false`, then it will always be `false` (the client has shutdown). +--- @return integer? request_id Can be used with |Client:cancel_request()|. +--- `nil` is request failed. +--- to cancel the-request. +function compat.client_request(client, method, params, handler, bufnr) + local info = debug.getinfo(client.request, 'u') + if info.nparams > 0 then + ---@diagnostic disable-next-line: param-type-mismatch + return client:request(method, params, handler, bufnr) + else + ---@diagnostic disable-next-line: param-type-mismatch + return client.request(method, params, handler, bufnr) + end +end + +--- @param client vim.lsp.Client +--- @param method string LSP method name. +--- @param params table? LSP request params. +--- @return boolean status indicating if the notification was successful. +--- If it is false, then the client has shutdown. +function compat.client_notify(client, method, params) + -- Nothing brings me more joy than updating Neovim nightly + -- and discovering that my perfectly functioning plugin has been obliterated because + -- a feature was deprecated without an alternative in stable. + -- Truly, it's the chaos I live for. (╯°□°)╯︵ ┻━┻ + local info = debug.getinfo(client.notify, 'u') + if info.nparams > 0 then + ---@diagnostic disable-next-line: param-type-mismatch + return client:notify(method, params) + else + ---@diagnostic disable-next-line: param-type-mismatch + return client.notify(method, params) + end +end + +---@param client vim.lsp.Client +---@return boolean +function compat.client_is_stopped(client) + local info = debug.getinfo(client.is_stopped, 'u') + if info.nparams > 0 then + ---@diagnostic disable-next-line: param-type-mismatch + return client:is_stopped() + else + ---@diagnostic disable-next-line: missing-parameter + return client.is_stopped() + end +end + return compat diff --git a/lua/rustaceanvim/hover_actions.lua b/lua/rustaceanvim/hover_actions.lua index 586ea0a8..a84c5fcd 100644 --- a/lua/rustaceanvim/hover_actions.lua +++ b/lua/rustaceanvim/hover_actions.lua @@ -3,10 +3,6 @@ local lsp_util = vim.lsp.util local M = {} -local function get_params() - return lsp_util.make_position_params(0, nil) -end - ---@class rustaceanvim.hover_actions.State local _state = { ---@type integer @@ -140,11 +136,15 @@ function M.handler(_, result, ctx) end, { buffer = vim.api.nvim_get_current_buf(), noremap = true, silent = true }) end -local rl = require('rustaceanvim.rust_analyzer') - --- Sends the request to rust-analyzer to get hover actions and handle it function M.hover_actions() - rl.buf_request(0, 'textDocument/hover', get_params(), M.handler) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end + local params = lsp_util.make_position_params(0, clients[1].offset_encoding) + ra.buf_request(0, 'textDocument/hover', params, M.handler) end return M diff --git a/lua/rustaceanvim/lsp/init.lua b/lua/rustaceanvim/lsp/init.lua index e04558a5..b4014e1d 100644 --- a/lua/rustaceanvim/lsp/init.lua +++ b/lua/rustaceanvim/lsp/init.lua @@ -7,6 +7,7 @@ local server_status = require('rustaceanvim.server_status') local cargo = require('rustaceanvim.cargo') local os = require('rustaceanvim.os') local rustc = require('rustaceanvim.rustc') +local compat = require('rustaceanvim.compat') local function override_apply_text_edits() local old_func = vim.lsp.util.apply_text_edits @@ -114,7 +115,7 @@ local function restart(bufnr, filter, callback) local stopped_client_count = 0 timer:start(200, 100, function() for _, client in ipairs(clients) do - if client:is_stopped() then + if compat.client_is_stopped(client) then stopped_client_count = stopped_client_count + 1 vim.schedule(function() -- Execute the callback, if provided, for additional actions before restarting @@ -307,7 +308,7 @@ M.reload_settings = function(bufnr) local settings = get_start_settings(vim.api.nvim_buf_get_name(bufnr), client.config.root_dir, config.server) ---@diagnostic disable-next-line: inject-field client.settings = settings - client.notify('workspace/didChangeConfiguration', { + compat.client_notify(client, 'workspace/didChangeConfiguration', { settings = client.settings, }) end @@ -326,7 +327,7 @@ M.set_target_arch = function(bufnr, target) local ra = client.config.settings['rust-analyzer'] ra.cargo = ra.cargo or {} ra.cargo.target = target - client.notify('workspace/didChangeConfiguration', { settings = client.config.settings }) + compat.client_notify(client, 'workspace/didChangeConfiguration', { settings = client.config.settings }) vim.schedule(function() vim.notify('Target architecture updated successfully to: ' .. target, vim.log.levels.INFO) end) diff --git a/lua/rustaceanvim/runnables.lua b/lua/rustaceanvim/runnables.lua index f5b89c24..519532fe 100644 --- a/lua/rustaceanvim/runnables.lua +++ b/lua/rustaceanvim/runnables.lua @@ -185,8 +185,13 @@ end ---@param runnables rustaceanvim.RARunnable ---@return integer | nil choice function M.get_runnable_at_cursor_position(runnables) + local ra = require('rustaceanvim.rust_analyzer') + local clients = ra.get_active_rustaceanvim_clients(0) + if #clients == 0 then + return + end ---@type lsp.Position - local position = vim.lsp.util.make_position_params().position + local position = vim.lsp.util.make_position_params(0, clients[1].offset_encoding).position ---@type integer|nil, integer|nil local choice, fallback for idx, runnable in ipairs(runnables) do diff --git a/lua/rustaceanvim/rust_analyzer.lua b/lua/rustaceanvim/rust_analyzer.lua index 219600fa..3851bac2 100644 --- a/lua/rustaceanvim/rust_analyzer.lua +++ b/lua/rustaceanvim/rust_analyzer.lua @@ -2,6 +2,7 @@ local os = require('rustaceanvim.os') local rustc = require('rustaceanvim.rustc') +local compat = require('rustaceanvim.compat') ---@class rustaceanvim.rust-analyzer.ClientAdapter local M = {} @@ -60,7 +61,7 @@ M.any_buf_request = function(method, params, handler) end -- No buffer found. Try any client. for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { method = method })) do - client.request(method, params, handler, 0) + compat.client_request(client, method, params, handler, 0) end end @@ -76,7 +77,7 @@ M.buf_request = function(bufnr, method, params, handler) end local client_found = false for _, client in ipairs(M.get_active_rustaceanvim_clients(bufnr, { method = method })) do - client.request(method, params, handler, bufnr) + compat.client_request(client, method, params, handler, 0) client_found = true end return client_found @@ -99,7 +100,7 @@ end M.notify = function(method, params) local client_found = false for _, client in ipairs(M.get_active_rustaceanvim_clients(0, { method = method })) do - client.notify(method, params) + compat.client_notify(client, method, params) client_found = true end if not client_found then