Skip to content

Commit 04dc55d

Browse files
feat(lsp): add command to reload lsp settings (#309)
Co-authored-by: Marc Jakobi <[email protected]>
1 parent 597c8d6 commit 04dc55d

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- LSP: `RustAnalyzer reloadSettings` command to reload settings without restarting.
14+
1115
### Fixed
1216

1317
- DAP: Defer automatic registration of nvim-dap configurations on LSP client init,

doc/rustaceanvim.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Commands:
2222
`:RustAnalyzer start` - Start the LSP client.
2323
`:RustAnalyzer stop` - Stop the LSP client.
2424
`:RustAnalyzer restart` - Restart the LSP client.
25+
`:RustAnalyzer reloadSettings` - Reload settings for the LSP client.
2526

2627
The `:RustLsp[!]` command is available after the LSP client has initialized.
2728
It accepts the following subcommands:

lua/rustaceanvim/lsp.lua

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ local function is_in_workspace(client, root_dir)
3535
return false
3636
end
3737

38-
---@Searches upward for a .vscode/settings.json that contains rust-analyzer
39-
--- settings and applies them.
38+
---Searches upward for a .vscode/settings.json that contains rust-analyzer
39+
---settings and returns them.
4040
---@param bufname string
4141
---@return table server_settings or an empty table if no settings were found
4242
local function find_vscode_settings(bufname)
@@ -54,6 +54,25 @@ local function find_vscode_settings(bufname)
5454
return content and require('rustaceanvim.config.json').silent_decode(content) or {}
5555
end
5656

57+
---Generate the settings from config and vscode settings if found.
58+
---settings and returns them.
59+
---@param bufname string
60+
---@param root_dir string | nil
61+
---@param client_config table
62+
---@return table server_settings or an empty table if no settings were found
63+
local function get_start_settings(bufname, root_dir, client_config)
64+
local settings = client_config.settings
65+
local evaluated_settings = type(settings) == 'function' and settings(root_dir, client_config.default_settings)
66+
or settings
67+
68+
if config.server.load_vscode_settings then
69+
local json_settings = find_vscode_settings(bufname)
70+
require('rustaceanvim.config.json').override_with_rust_analyzer_json_keys(evaluated_settings, json_settings)
71+
end
72+
73+
return evaluated_settings
74+
end
75+
5776
---@class LspStartConfig: RustaceanLspClientConfig
5877
---@field root_dir string | nil
5978
---@field init_options? table
@@ -84,17 +103,7 @@ M.start = function(bufnr)
84103
lsp_start_config.init_options = { detachedFiles = { bufname } }
85104
end
86105

87-
local settings = client_config.settings
88-
local evaluated_settings = type(settings) == 'function' and settings(root_dir, client_config.default_settings)
89-
or settings
90-
91-
---@cast evaluated_settings table
92-
lsp_start_config.settings = evaluated_settings
93-
94-
if config.server.load_vscode_settings then
95-
local json_settings = find_vscode_settings(bufname)
96-
require('rustaceanvim.config.json').override_with_rust_analyzer_json_keys(lsp_start_config.settings, json_settings)
97-
end
106+
lsp_start_config.settings = get_start_settings(bufname, root_dir, client_config)
98107

99108
-- Check if a client is already running and add the workspace folder if necessary.
100109
for _, client in pairs(rust_analyzer.get_active_rustaceanvim_clients()) do
@@ -222,6 +231,24 @@ M.stop = function(bufnr)
222231
return clients
223232
end
224233

234+
---Reload settings for the LSP client.
235+
---@param bufnr? number The buffer number, defaults to the current buffer
236+
---@return table[] clients A list of clients that will be have their settings reloaded
237+
M.reload_settings = function(bufnr)
238+
bufnr = bufnr or vim.api.nvim_get_current_buf()
239+
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr)
240+
---@cast clients lsp.Client[]
241+
for _, client in ipairs(clients) do
242+
local settings = get_start_settings(vim.api.nvim_buf_get_name(bufnr), client.config.root_dir, config.server)
243+
---@diagnostic disable-next-line: inject-field
244+
client.settings = settings
245+
client.notify('workspace/didChangeConfiguration', {
246+
settings = client.settings,
247+
})
248+
end
249+
return clients
250+
end
251+
225252
---Restart the LSP client.
226253
---Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
227254
---@param bufnr? number The buffer number (optional), defaults to the current buffer
@@ -262,6 +289,7 @@ local RustAnalyzerCmd = {
262289
start = 'start',
263290
stop = 'stop',
264291
restart = 'restart',
292+
reload_settings = 'reloadSettings',
265293
}
266294

267295
local function rust_analyzer_cmd(opts)
@@ -274,6 +302,8 @@ local function rust_analyzer_cmd(opts)
274302
M.stop()
275303
elseif cmd == RustAnalyzerCmd.restart then
276304
M.restart()
305+
elseif cmd == RustAnalyzerCmd.reload_settings then
306+
M.reload_settings()
277307
end
278308
end
279309

@@ -283,7 +313,7 @@ vim.api.nvim_create_user_command('RustAnalyzer', rust_analyzer_cmd, {
283313
complete = function(arg_lead, cmdline, _)
284314
local clients = rust_analyzer.get_active_rustaceanvim_clients()
285315
---@type RustAnalyzerCmd[]
286-
local commands = #clients == 0 and { 'start' } or { 'stop', 'restart' }
316+
local commands = #clients == 0 and { 'start' } or { 'stop', 'restart', 'reloadSettings' }
287317
if cmdline:match('^RustAnalyzer%s+%w*$') then
288318
return vim.tbl_filter(function(command)
289319
return command:find(arg_lead) ~= nil

0 commit comments

Comments
 (0)