diff --git a/doc/rustaceanvim.txt b/doc/rustaceanvim.txt index 109a4ab1..6e6234c3 100644 --- a/doc/rustaceanvim.txt +++ b/doc/rustaceanvim.txt @@ -234,13 +234,13 @@ RustcOpts *RustcOpts* RustaceanLspClientOpts *RustaceanLspClientOpts* 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 - {root_dir?} (fun(filename:string):string|nil) Should return the directory to use for the attached LSP, or nil if no server should attach. Defaults to `rustaceanvim.cargo.get_root_dir`. - {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. - {standalone?} (boolean) 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. - {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: false + {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 + {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. + {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. + {standalone?} (boolean) 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. + {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: false RustaceanDapOpts *RustaceanDapOpts* diff --git a/lua/rustaceanvim/cargo.lua b/lua/rustaceanvim/cargo.lua index 3585fc97..f12d130d 100644 --- a/lua/rustaceanvim/cargo.lua +++ b/lua/rustaceanvim/cargo.lua @@ -28,7 +28,7 @@ local function get_mb_active_client_root(file_name) end ---Attempts to find the root for an existing active client. If no existing ----client root is found, returns the result of `config.root_dir(file_name)`. +---client root is found, returns the result of evaluating `config.root_dir`. ---@param config RustaceanLspClientConfig ---@param file_name string ---@return string | nil root_dir @@ -37,7 +37,13 @@ function cargo.get_config_root_dir(config, file_name) if reuse_active then return reuse_active end - return config.root_dir(file_name) + + local config_root_dir = config.root_dir + if type(config_root_dir) == 'function' then + return config_root_dir(file_name, cargo.get_root_dir) + else + return config_root_dir + end end ---The default implementation used for `vim.g.rustaceanvim.server.root_dir` diff --git a/lua/rustaceanvim/config/check.lua b/lua/rustaceanvim/config/check.lua index 41910d83..cd5aa025 100644 --- a/lua/rustaceanvim/config/check.lua +++ b/lua/rustaceanvim/config/check.lua @@ -89,6 +89,7 @@ function M.validate(cfg) cmd = { server.cmd, { 'function', 'table' } }, standalone = { server.standalone, 'boolean' }, settings = { server.settings, { 'function', 'table' }, true }, + root_dir = { server.root_dir, { 'function', 'string' } }, }) if not ok then return false, err diff --git a/lua/rustaceanvim/config/init.lua b/lua/rustaceanvim/config/init.lua index 84c08064..5b2fe2c7 100644 --- a/lua/rustaceanvim/config/init.lua +++ b/lua/rustaceanvim/config/init.lua @@ -110,7 +110,7 @@ vim.g.rustaceanvim = vim.g.rustaceanvim ---@class RustaceanLspClientOpts ---@field auto_attach? boolean | fun(bufnr: integer):boolean Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found. ---@field cmd? string[] | fun():string[] Command and arguments for starting rust-analyzer ----@field root_dir? fun(filename: string):string|nil Should return the directory to use for the attached LSP, or nil if no server should attach. Defaults to `rustaceanvim.cargo.get_root_dir`. +---@field 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. ---@field 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. ---@field standalone? boolean Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time. ---@field logfile? string The path to the rust-analyzer log file. diff --git a/lua/rustaceanvim/config/internal.lua b/lua/rustaceanvim/config/internal.lua index 2a3ec323..8f68710d 100644 --- a/lua/rustaceanvim/config/internal.lua +++ b/lua/rustaceanvim/config/internal.lua @@ -277,7 +277,7 @@ local RustaceanDefaultConfig = { return { 'rust-analyzer', '--log-file', RustaceanConfig.server.logfile } end, - ---@type fun(filename: string):string|nil + ---@type string | fun(filename: string, default: fun(filename: string):string|nil):string|nil root_dir = cargo.get_root_dir, --- standalone file support