Skip to content

Commit

Permalink
fix(lsp): don't eagerly evaluate server.cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Dec 24, 2024
1 parent 549cd67 commit 7a1511b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
38 changes: 20 additions & 18 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,32 @@ rustaceanvim.lsp.ClientOpts *rustaceanvim.lsp.ClientOpts*

Fields: ~
{auto_attach?} (boolean|fun(bufnr:integer):boolean)
Whether to automatically attach the LSP client.
Defaults to `true` if the `rust-analyzer` executable is found.
{cmd?} (string[]|fun():string[])
Command and arguments for starting rust-analyzer
Whether to automatically attach the LSP client.
Defaults to `true` if the `rust-analyzer` executable is found.
{cmd?} (string[]|fun():string[]|fun(dispatchers:vim.lsp.rpc.Dispatchers):vim.lsp.rpc.PublicClient)
Command and arguments for starting rust-analyzer
Can be a list of arguments, a function that returns a list of arguments,
or a function that returns an LSP RPC client factory (see |vim.lsp.rpc.connect|).
{root_dir?} (string|fun(filename:string,default:fun(filename:string):string|nil):string|nil)
The directory to use for the attached LSP.
Can be a function, which may return nil if no server should attach.
The second argument contains the default implementation, which can be used for fallback behavior.
The directory to use for the attached LSP.
Can be a function, which may return nil if no server should attach.
The second argument contains the default implementation, which can be used for fallback behavior.
{settings?} (table|fun(project_root:string|nil,default_settings:table):table)
Setting passed to rust-analyzer.
Defaults to a function that looks for a `rust-analyzer.json` file or returns an empty table.
See https://rust-analyzer.github.io/manual.html#configuration.
Setting passed to rust-analyzer.
Defaults to a function that looks for a `rust-analyzer.json` file or returns an empty table.
See https://rust-analyzer.github.io/manual.html#configuration.
{standalone?} (boolean)
Standalone file support (enabled by default).
Disabling it may improve rust-analyzer's startup time.
Standalone file support (enabled by default).
Disabling it may improve rust-analyzer's startup time.
{logfile?} (string)
The path to the rust-analyzer log file.
The path to the rust-analyzer log file.
{load_vscode_settings?} (boolean)
Whether to search (upward from the buffer) for rust-analyzer settings in .vscode/settings json.
If found, loaded settings will override configured options.
Default: `true`
Whether to search (upward from the buffer) for rust-analyzer settings in .vscode/settings json.
If found, loaded settings will override configured options.
Default: `true`
{status_notify_level?} (rustaceanvim.server.status_notify_level)
Server status warning level to notify at.
Default: 'error'
Server status warning level to notify at.
Default: 'error'


rustaceanvim.server.status_notify_level*rustaceanvim.server.status_notify_level*
Expand Down
4 changes: 3 additions & 1 deletion lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---@field auto_attach? boolean | fun(bufnr: integer):boolean
---
---Command and arguments for starting rust-analyzer
---@field cmd? string[] | fun():string[]
---Can be a list of arguments, a function that returns a list of arguments,
---or a function that returns an LSP RPC client factory (see |vim.lsp.rpc.connect|).
---@field cmd? string[] | fun():(string[]|fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient)
---
---The directory to use for the attached LSP.
---Can be a function, which may return nil if no server should attach.
Expand Down
6 changes: 5 additions & 1 deletion lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,15 @@ local RustaceanDefaultConfig = {
return false
end
local cmd = types.evaluate(RustaceanConfig.server.cmd)
if type(cmd) == 'function' then
-- This could be a function that connects via a TCP socket, so we don't want to evaluate it.
return true
end
---@cast cmd string[]
local rs_bin = cmd[1]
return vim.fn.executable(rs_bin) == 1
end,
---@type string[] | fun():string[]
---@type string[] | fun():(string[]|fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient)
cmd = function()
return { 'rust-analyzer', '--log-file', RustaceanConfig.server.logfile }
end,
Expand Down
34 changes: 20 additions & 14 deletions lua/rustaceanvim/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,26 @@ Starting rust-analyzer client in detached/standalone mode (with reduced function
-- that allows you to override the path via .vscode/settings.json
local server_path = vim.tbl_get(lsp_start_config.settings, 'rust-analyzer', 'server', 'path')
if type(server_path) == 'string' then
rust_analyzer_cmd[1] = server_path
if type(rust_analyzer_cmd) == 'table' then
rust_analyzer_cmd[1] = server_path
else
rust_analyzer_cmd = { server_path }
end
--
end
if #rust_analyzer_cmd == 0 then
vim.schedule(function()
vim.notify('rust-analyzer command is not set!', vim.log.levels.ERROR)
end)
return
end
if vim.fn.executable(rust_analyzer_cmd[1]) ~= 1 then
vim.schedule(function()
vim.notify(('%s is not executable'):format(rust_analyzer_cmd[1]), vim.log.levels.ERROR)
end)
return
if type(rust_analyzer_cmd) == 'table' then
if #rust_analyzer_cmd == 0 then
vim.schedule(function()
vim.notify('rust-analyzer command is not set!', vim.log.levels.ERROR)
end)
return
end
if vim.fn.executable(rust_analyzer_cmd[1]) ~= 1 then
vim.schedule(function()
vim.notify(('%s is not executable'):format(rust_analyzer_cmd[1]), vim.log.levels.ERROR)
end)
return
end
end
---@cast rust_analyzer_cmd string[]
lsp_start_config.cmd = rust_analyzer_cmd
Expand Down Expand Up @@ -362,7 +368,7 @@ local RustAnalyzerCmd = {
target = 'target',
}

local function rust_analyzer_cmd(opts)
local function rust_analyzer_user_cmd(opts)
local fargs = opts.fargs
local cmd = fargs[1]
---@cast cmd RustAnalyzerCmd
Expand All @@ -380,7 +386,7 @@ local function rust_analyzer_cmd(opts)
end
end

vim.api.nvim_create_user_command('RustAnalyzer', rust_analyzer_cmd, {
vim.api.nvim_create_user_command('RustAnalyzer', rust_analyzer_user_cmd, {
nargs = '+',
desc = 'Starts, stops the rust-analyzer LSP client or changes the target',
complete = function(arg_lead, cmdline, _)
Expand Down

0 comments on commit 7a1511b

Please sign in to comment.