Skip to content

Commit

Permalink
feat(lsp): pass default settings to server.settings function (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Mar 15, 2024
1 parent a59b4e0 commit 69a22c2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.13.0] - 2024-03-15

### Added

- LSP: More flexibility when overriding default rust-analyzer settings.
The `server.settings` function can now take a `default_settings` tabe
to be merged.

## [4.12.2] - 2024-03-11

### Fixed

- LSP/Windows: path normalisation preventing lsp from working
`textDocument/definition` to std library [[#285](https://github.com/mrcjkb/rustaceanvim/pull/285)].
Thanks [@tangtang95](https://github.com/tangtang95)!

## [4.12.1] - 2024-03-09

### Fixed
Expand Down
13 changes: 7 additions & 6 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ vim.g.rustaceanvim = {
on_attach = function(client, bufnr)
-- Set keybindings, etc. here.
end,
settings = {
default_settings = {
-- rust-analyzer language server configuration
['rust-analyzer'] = {
},
Expand Down Expand Up @@ -209,11 +209,11 @@ 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
{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.
{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.
{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
{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.


RustaceanDapOpts *RustaceanDapOpts*
Expand Down Expand Up @@ -329,6 +329,7 @@ LoadRASettingsOpts *LoadRASettingsOpts*

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


*server.load_rust_analyzer_settings*
Expand Down
4 changes: 2 additions & 2 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
--- on_attach = function(client, bufnr)
--- -- Set keybindings, etc. here.
--- end,
--- settings = {
--- default_settings = {
--- -- rust-analyzer language server configuration
--- ['rust-analyzer'] = {
--- },
Expand Down Expand Up @@ -102,7 +102,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 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.
---@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
9 changes: 6 additions & 3 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,12 @@ local RustaceanDefaultConfig = {
---@type string The path to the rust-analyzer log file.
logfile = vim.fn.tempname() .. '-rust-analyzer.log',

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

--- @type table
Expand Down
7 changes: 4 additions & 3 deletions lua/rustaceanvim/config/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end

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

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

local default_settings = config.server.default_settings
local default_opts = { settings_file_pattern = 'rust-analyzer.json' }
opts = vim.tbl_deep_extend('force', {}, default_opts, opts or {})
local default_settings = opts.default_settings or config.server.default_settings
local use_clippy = config.tools.enable_clippy and vim.fn.executable('cargo-clippy') == 1
---@diagnostic disable-next-line: undefined-field
if default_settings['rust-analyzer'].checkOnSave == nil and use_clippy then
Expand All @@ -42,8 +45,6 @@ function server.load_rust_analyzer_settings(project_root, opts)
if not project_root then
return default_settings
end
local default_opts = { settings_file_pattern = 'rust-analyzer.json' }
opts = vim.tbl_deep_extend('force', {}, default_opts, opts or {})
local results = vim.fn.glob(compat.joinpath(project_root, opts.settings_file_pattern), true, true)
if #results == 0 then
return default_settings
Expand Down
3 changes: 2 additions & 1 deletion lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ M.start = function(bufnr)
end

local settings = client_config.settings
local evaluated_settings = type(settings) == 'function' and settings(root_dir) or settings
local evaluated_settings = type(settings) == 'function' and settings(root_dir, client_config.default_settings)
or settings
---@cast evaluated_settings table
lsp_start_config.settings = evaluated_settings

Expand Down

0 comments on commit 69a22c2

Please sign in to comment.