Skip to content

Commit aa5f6a6

Browse files
committed
feat: add silent on error option
1 parent 7610345 commit aa5f6a6

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

doc/lspconfig.txt

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ the following added keys:
7575
- {single_file_support} (`bool`) (default: nil) Determines if a server is
7676
started without a matching root directory. See |lspconfig-single-file-support|.
7777

78+
- {silent} (`bool`) (default: false) Whether to suppress error reporting if the
79+
LSP server fails to start.
80+
7881
- {on_new_config} (`function(new_config, new_root_dir)`) Function executed
7982
after a root directory is detected. This is used to modify the server
8083
configuration (including `cmd` itself). Most commonly, this is used to

lua/lspconfig/configs.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local configs = {}
88
--- @class lspconfig.Config : vim.lsp.ClientConfig
99
--- @field enabled? boolean
1010
--- @field single_file_support? boolean
11+
--- @field silent? boolean
1112
--- @field filetypes? string[]
1213
--- @field filetype? string
1314
--- @field on_new_config? fun(new_config: lspconfig.Config?, new_root_dir: string)
@@ -105,7 +106,7 @@ function configs.__newindex(t, config_name, config_def)
105106
api.nvim_create_autocmd(event_conf.event, {
106107
pattern = event_conf.pattern or '*',
107108
callback = function(opt)
108-
M.manager:try_add(opt.buf)
109+
M.manager:try_add(opt.buf, nil, config.silent)
109110
end,
110111
group = lsp_group,
111112
desc = string.format(
@@ -176,7 +177,7 @@ function configs.__newindex(t, config_name, config_def)
176177
return
177178
end
178179
local pseudo_root = #bufname == 0 and pwd or util.path.dirname(util.path.sanitize(bufname))
179-
M.manager:add(pseudo_root, true, bufnr)
180+
M.manager:add(pseudo_root, true, bufnr, config.silent)
180181
end
181182
end)
182183
end

lua/lspconfig/manager.lua

+12-7
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ end
8585
--- @param new_config lspconfig.Config
8686
--- @param root_dir string
8787
--- @param single_file boolean
88-
function M:_start_client(bufnr, new_config, root_dir, single_file)
88+
--- @param silent boolean
89+
function M:_start_client(bufnr, new_config, root_dir, single_file, silent)
8990
-- do nothing if the client is not enabled
9091
if new_config.enabled == false then
9192
return
@@ -129,6 +130,7 @@ function M:_start_client(bufnr, new_config, root_dir, single_file)
129130

130131
local client_id = lsp.start(new_config, {
131132
bufnr = bufnr,
133+
silent = silent,
132134
reuse_client = function(existing_client)
133135
if vim.tbl_isempty(self._clients) then
134136
return false
@@ -163,9 +165,11 @@ end
163165
---@param root_dir string
164166
---@param single_file boolean
165167
---@param bufnr integer
168+
---@param silent boolean
169+
function M:add(root_dir, single_file, bufnr, silent)
166170
root_dir = util.path.sanitize(root_dir)
167171
local new_config = self.make_config(root_dir)
168-
self:_start_client(bufnr, new_config, root_dir, single_file)
172+
self:_start_client(bufnr, new_config, root_dir, single_file, silent)
169173
end
170174

171175
--- @return vim.lsp.Client[]
@@ -183,7 +187,8 @@ end
183187
--- a new client if one doesn't already exist for `bufnr`.
184188
--- @param bufnr integer
185189
--- @param project_root? string
186-
function M:try_add(bufnr, project_root)
190+
--- @param silent boolean
191+
function M:try_add(bufnr, project_root, silent)
187192
bufnr = bufnr or api.nvim_get_current_buf()
188193

189194
if vim.bo[bufnr].buftype == 'nofile' then
@@ -200,7 +205,7 @@ function M:try_add(bufnr, project_root)
200205
end
201206

202207
if project_root then
203-
self:add(project_root, false, bufnr)
208+
self:add(project_root, false, bufnr, silent)
204209
return
205210
end
206211

@@ -223,10 +228,10 @@ function M:try_add(bufnr, project_root)
223228
end
224229

225230
if root_dir then
226-
self:add(root_dir, false, bufnr)
231+
self:add(root_dir, false, bufnr, silent)
227232
elseif self.config.single_file_support then
228233
local pseudo_root = #bufname == 0 and pwd or util.path.dirname(buf_path)
229-
self:add(pseudo_root, true, bufnr)
234+
self:add(pseudo_root, true, bufnr, silent)
230235
end
231236
end)
232237
end
@@ -239,7 +244,7 @@ function M:try_add_wrapper(bufnr, project_root)
239244
local config = self.config
240245
-- `config.filetypes = nil` means all filetypes are valid.
241246
if not config.filetypes or vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then
242-
self:try_add(bufnr, project_root)
247+
self:try_add(bufnr, project_root, config.silent)
243248
end
244249
end
245250

0 commit comments

Comments
 (0)