Skip to content

Commit 69a22c2

Browse files
authored
feat(lsp): pass default settings to server.settings function (#291)
1 parent a59b4e0 commit 69a22c2

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [4.13.0] - 2024-03-15
10+
11+
### Added
12+
13+
- LSP: More flexibility when overriding default rust-analyzer settings.
14+
The `server.settings` function can now take a `default_settings` tabe
15+
to be merged.
16+
17+
## [4.12.2] - 2024-03-11
18+
19+
### Fixed
20+
21+
- LSP/Windows: path normalisation preventing lsp from working
22+
`textDocument/definition` to std library [[#285](https://github.com/mrcjkb/rustaceanvim/pull/285)].
23+
Thanks [@tangtang95](https://github.com/tangtang95)!
24+
925
## [4.12.1] - 2024-03-09
1026

1127
### Fixed

doc/rustaceanvim.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ vim.g.rustaceanvim = {
9696
on_attach = function(client, bufnr)
9797
-- Set keybindings, etc. here.
9898
end,
99-
settings = {
99+
default_settings = {
100100
-- rust-analyzer language server configuration
101101
['rust-analyzer'] = {
102102
},
@@ -209,11 +209,11 @@ RustcOpts *RustcOpts*
209209
RustaceanLspClientOpts *RustaceanLspClientOpts*
210210

211211
Fields: ~
212-
{auto_attach?} (boolean|fun(bufnr:integer):boolean) Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
213-
{cmd?} (string[]|fun():string[]) Command and arguments for starting rust-analyzer
214-
{settings?} (table|fun(project_root:string|nil):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.
215-
{standalone?} (boolean) Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time.
216-
{logfile?} (string) The path to the rust-analyzer log file.
212+
{auto_attach?} (boolean|fun(bufnr:integer):boolean) Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
213+
{cmd?} (string[]|fun():string[]) Command and arguments for starting rust-analyzer
214+
{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.
215+
{standalone?} (boolean) Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time.
216+
{logfile?} (string) The path to the rust-analyzer log file.
217217

218218

219219
RustaceanDapOpts *RustaceanDapOpts*
@@ -329,6 +329,7 @@ LoadRASettingsOpts *LoadRASettingsOpts*
329329

330330
Fields: ~
331331
{settings_file_pattern} (string|nil) File name or pattern to search for. Defaults to 'rust-analyzer.json'
332+
{default_settings} (table|nil) Default settings to merge the loaded settings into
332333

333334

334335
*server.load_rust_analyzer_settings*

lua/rustaceanvim/config/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
--- on_attach = function(client, bufnr)
2323
--- -- Set keybindings, etc. here.
2424
--- end,
25-
--- settings = {
25+
--- default_settings = {
2626
--- -- rust-analyzer language server configuration
2727
--- ['rust-analyzer'] = {
2828
--- },
@@ -102,7 +102,7 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
102102
---@class RustaceanLspClientOpts
103103
---@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.
104104
---@field cmd? string[] | fun():string[] Command and arguments for starting rust-analyzer
105-
---@field settings? table | fun(project_root:string|nil):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.
105+
---@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.
106106
---@field standalone? boolean Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time.
107107
---@field logfile? string The path to the rust-analyzer log file.
108108

lua/rustaceanvim/config/internal.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,12 @@ local RustaceanDefaultConfig = {
256256
---@type string The path to the rust-analyzer log file.
257257
logfile = vim.fn.tempname() .. '-rust-analyzer.log',
258258

259-
---@type table | (fun(project_root:string|nil):table) -- The rust-analyzer settings or a function that creates them.
260-
settings = function(project_root)
261-
return require('rustaceanvim.config.server').load_rust_analyzer_settings(project_root)
259+
---@type table | (fun(project_root:string|nil, default_settings: table|nil):table) -- The rust-analyzer settings or a function that creates them.
260+
settings = function(project_root, default_settings)
261+
return require('rustaceanvim.config.server').load_rust_analyzer_settings(
262+
project_root,
263+
{ default_settings = default_settings }
264+
)
262265
end,
263266

264267
--- @type table

lua/rustaceanvim/config/server.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717

1818
---@class LoadRASettingsOpts
1919
---@field settings_file_pattern string|nil File name or pattern to search for. Defaults to 'rust-analyzer.json'
20+
---@field default_settings table|nil Default settings to merge the loaded settings into
2021

2122
--- Load rust-analyzer settings from a JSON file,
2223
--- falling back to the default settings if none is found or if it cannot be decoded.
@@ -28,7 +29,9 @@ function server.load_rust_analyzer_settings(project_root, opts)
2829
local config = require('rustaceanvim.config.internal')
2930
local compat = require('rustaceanvim.compat')
3031

31-
local default_settings = config.server.default_settings
32+
local default_opts = { settings_file_pattern = 'rust-analyzer.json' }
33+
opts = vim.tbl_deep_extend('force', {}, default_opts, opts or {})
34+
local default_settings = opts.default_settings or config.server.default_settings
3235
local use_clippy = config.tools.enable_clippy and vim.fn.executable('cargo-clippy') == 1
3336
---@diagnostic disable-next-line: undefined-field
3437
if default_settings['rust-analyzer'].checkOnSave == nil and use_clippy then
@@ -42,8 +45,6 @@ function server.load_rust_analyzer_settings(project_root, opts)
4245
if not project_root then
4346
return default_settings
4447
end
45-
local default_opts = { settings_file_pattern = 'rust-analyzer.json' }
46-
opts = vim.tbl_deep_extend('force', {}, default_opts, opts or {})
4748
local results = vim.fn.glob(compat.joinpath(project_root, opts.settings_file_pattern), true, true)
4849
if #results == 0 then
4950
return default_settings

lua/rustaceanvim/lsp.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ M.start = function(bufnr)
6565
end
6666

6767
local settings = client_config.settings
68-
local evaluated_settings = type(settings) == 'function' and settings(root_dir) or settings
68+
local evaluated_settings = type(settings) == 'function' and settings(root_dir, client_config.default_settings)
69+
or settings
6970
---@cast evaluated_settings table
7071
lsp_start_config.settings = evaluated_settings
7172

0 commit comments

Comments
 (0)