1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-- This config is DEPRECATED.
-- Use the configs in `lsp/` instead (requires Nvim 0.11).
--
-- ALL configs in `lua/lspconfig/configs/` will be DELETED.
-- They exist only to support Nvim 0.10 or older.
-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
local util = require 'lspconfig.util'
local async = require 'lspconfig.async'
local function reload_workspace(bufnr)
bufnr = util.validate_bufnr(bufnr)
local clients = vim.lsp.get_clients { bufnr = bufnr, name = 'rust_analyzer' }
for _, client in ipairs(clients) do
vim.notify 'Reloading Cargo Workspace'
client.request('rust-analyzer/reloadWorkspace', nil, function(err)
if err then
error(tostring(err))
end
vim.notify 'Cargo workspace reloaded'
end, 0)
end
end
local function is_library(fname)
local user_home = vim.fs.normalize(vim.env.HOME)
local cargo_home = os.getenv 'CARGO_HOME' or user_home .. '/.cargo'
local registry = cargo_home .. '/registry/src'
local git_registry = cargo_home .. '/git/checkouts'
local rustup_home = os.getenv 'RUSTUP_HOME' or user_home .. '/.rustup'
local toolchains = rustup_home .. '/toolchains'
for _, item in ipairs { toolchains, registry, git_registry } do
if util.path.is_descendant(item, fname) then
local clients = vim.lsp.get_clients { name = 'rust_analyzer' }
return #clients > 0 and clients[#clients].config.root_dir or nil
end
end
end
return {
default_config = {
cmd = { 'rust-analyzer' },
filetypes = { 'rust' },
single_file_support = true,
root_dir = function(fname)
local reuse_active = is_library(fname)
if reuse_active then
return reuse_active
end
local cargo_crate_dir = util.root_pattern 'Cargo.toml'(fname)
local cargo_workspace_root
if cargo_crate_dir ~= nil then
local cmd = {
'cargo',
'metadata',
'--no-deps',
'--format-version',
'1',
'--manifest-path',
cargo_crate_dir .. '/Cargo.toml',
}
local result = async.run_command(cmd)
if result and result[1] then
result = vim.json.decode(table.concat(result, ''))
if result['workspace_root'] then
cargo_workspace_root = vim.fs.normalize(result['workspace_root'])
end
end
end
return cargo_workspace_root
or cargo_crate_dir
or util.root_pattern 'rust-project.json'(fname)
or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
end,
capabilities = {
experimental = {
serverStatusNotification = true,
},
},
before_init = function(init_params, config)
-- See https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26
if config.settings and config.settings['rust-analyzer'] then
init_params.initializationOptions = config.settings['rust-analyzer']
end
end,
},
commands = {
CargoReload = {
function()
reload_workspace(0)
end,
description = 'Reload current cargo workspace',
},
},
docs = {
description = [[
https://github.com/rust-lang/rust-analyzer
rust-analyzer (aka rls 2.0), a language server for Rust
See [docs](https://rust-analyzer.github.io/book/configuration.html) for extra settings. The settings can be used like this:
```lua
require'lspconfig'.rust_analyzer.setup{
settings = {
['rust-analyzer'] = {
diagnostics = {
enable = false;
}
}
}
}
```
Note: do not set `init_options` for this LS config, it will be automatically populated by the contents of settings["rust-analyzer"] per
https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26.
]],
},
}
|