Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- Add a validator to config/check.lua
- Accept a string for `root_dir`
- Don't mention the default implementation as the private
  `rustaceanvim.cargo.get_root_dir` function.
- Pass an extra "default" argument with the default implementation.
- Update documentation/comments where relevant.
  • Loading branch information
bgw committed May 27, 2024
1 parent db68179 commit fa3171b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
14 changes: 7 additions & 7 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
10 changes: 8 additions & 2 deletions lua/rustaceanvim/cargo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Expand Down
1 change: 1 addition & 0 deletions lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa3171b

Please sign in to comment.