Skip to content

Commit

Permalink
feat: add silent on error option
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaSolOs committed Nov 24, 2024
1 parent 8d9fd35 commit 2eccb41
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 3 additions & 0 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ the following added keys:
- {single_file_support} (`bool`) (default: nil) Determines if a server is
started without a matching root directory. See |lspconfig-single-file-support|.

- {silent} (`bool`) (default: false) Whether to suppress error reporting if the
LSP server fails to start.

- {on_new_config} (`function(new_config, new_root_dir)`) Function executed
after a root directory is detected. This is used to modify the server
configuration (including `cmd` itself). Most commonly, this is used to
Expand Down
5 changes: 3 additions & 2 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local configs = {}
--- @class lspconfig.Config : vim.lsp.ClientConfig
--- @field enabled? boolean
--- @field single_file_support? boolean
--- @field silent? boolean
--- @field filetypes? string[]
--- @field filetype? string
--- @field on_new_config? fun(new_config: lspconfig.Config?, new_root_dir: string)
Expand Down Expand Up @@ -105,7 +106,7 @@ function configs.__newindex(t, config_name, config_def)
api.nvim_create_autocmd(event_conf.event, {
pattern = event_conf.pattern or '*',
callback = function(opt)
M.manager:try_add(opt.buf)
M.manager:try_add(opt.buf, nil, config.silent)
end,
group = lsp_group,
desc = string.format(
Expand Down Expand Up @@ -176,7 +177,7 @@ function configs.__newindex(t, config_name, config_def)
return
end
local pseudo_root = #bufname == 0 and pwd or util.path.dirname(util.path.sanitize(bufname))
M.manager:add(pseudo_root, true, bufnr)
M.manager:add(pseudo_root, true, bufnr, config.silent)
end
end)
end
Expand Down
19 changes: 12 additions & 7 deletions lua/lspconfig/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ end
--- @param new_config lspconfig.Config
--- @param root_dir string
--- @param single_file boolean
function M:_start_client(bufnr, new_config, root_dir, single_file)
--- @param silent boolean
function M:_start_client(bufnr, new_config, root_dir, single_file, silent)
-- do nothing if the client is not enabled
if new_config.enabled == false then
return
Expand Down Expand Up @@ -129,6 +130,7 @@ function M:_start_client(bufnr, new_config, root_dir, single_file)

local client_id = lsp.start(new_config, {
bufnr = bufnr,
silent = silent,
reuse_client = function(existing_client)
if (self._clients[root_dir] or {})[existing_client.name] then
self:_notify_workspace_folder_added(root_dir, existing_client)
Expand All @@ -153,9 +155,11 @@ end
---@param root_dir string
---@param single_file boolean
---@param bufnr integer
---@param silent boolean
function M:add(root_dir, single_file, bufnr, silent)
root_dir = util.path.sanitize(root_dir)
local new_config = self.make_config(root_dir)
self:_start_client(bufnr, new_config, root_dir, single_file)
self:_start_client(bufnr, new_config, root_dir, single_file, silent)
end

--- @return vim.lsp.Client[]
Expand All @@ -173,7 +177,8 @@ end
--- a new client if one doesn't already exist for `bufnr`.
--- @param bufnr integer
--- @param project_root? string
function M:try_add(bufnr, project_root)
--- @param silent boolean
function M:try_add(bufnr, project_root, silent)
bufnr = bufnr or api.nvim_get_current_buf()

if vim.bo[bufnr].buftype == 'nofile' then
Expand All @@ -190,7 +195,7 @@ function M:try_add(bufnr, project_root)
end

if project_root then
self:add(project_root, false, bufnr)
self:add(project_root, false, bufnr, silent)
return
end

Expand All @@ -213,10 +218,10 @@ function M:try_add(bufnr, project_root)
end

if root_dir then
self:add(root_dir, false, bufnr)
self:add(root_dir, false, bufnr, silent)
elseif self.config.single_file_support then
local pseudo_root = #bufname == 0 and pwd or util.path.dirname(buf_path)
self:add(pseudo_root, true, bufnr)
self:add(pseudo_root, true, bufnr, silent)
end
end)
end
Expand All @@ -229,7 +234,7 @@ function M:try_add_wrapper(bufnr, project_root)
local config = self.config
-- `config.filetypes = nil` means all filetypes are valid.
if not config.filetypes or vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then
self:try_add(bufnr, project_root)
self:try_add(bufnr, project_root, config.silent)
end
end

Expand Down

0 comments on commit 2eccb41

Please sign in to comment.