aboutsummaryrefslogtreecommitdiffstats
path: root/lua/lspconfig/configs.lua
diff options
context:
space:
mode:
authorMichael Lingelbach <m.j.lbach@gmail.com>2021-11-11 01:00:24 -0800
committerGitHub <noreply@github.com>2021-11-11 01:00:24 -0800
commit0d1ce78d231773046320caa45fe1a18dfa366761 (patch)
treefde488c8490f93b6ad88ef8a32e77ff9b4e49d34 /lua/lspconfig/configs.lua
parentdocs: make CONTRIBUTING more visible via move (#1396) (diff)
downloadnvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar.gz
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar.bz2
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar.lz
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar.xz
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.tar.zst
nvim-lspconfig-0d1ce78d231773046320caa45fe1a18dfa366761.zip
feat: add single file mode (#1385)
* This adds a "single file mode" option for each language server * Currently, if a root is not detected, a new language server is started for each file opened. * Root directory is set to `nil` in start_client. Some servers will refuse to start, or otherwise panic. This is opt-in per server. * Some servers, such as rust-analyzer, explicitly have a "single file mode", we will not support that until it is officially part of the LSP specification Co-authored-by: Peter Lithammer <peter.lithammer@gmail.com>
Diffstat (limited to 'lua/lspconfig/configs.lua')
-rw-r--r--lua/lspconfig/configs.lua49
1 files changed, 34 insertions, 15 deletions
diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua
index 2f4cd0d7..f69e2673 100644
--- a/lua/lspconfig/configs.lua
+++ b/lua/lspconfig/configs.lua
@@ -78,22 +78,29 @@ function configs.__newindex(t, config_name, config_def)
function M.launch()
local root_dir = get_root_dir(api.nvim_buf_get_name(0), api.nvim_get_current_buf())
- if not root_dir then
- vim.notify(string.format('Launching %s failed: matching root directory not detected.', config_name))
- return
- end
- api.nvim_command(
- string.format(
- "autocmd %s unsilent lua require'lspconfig'[%q].manager.try_add_wrapper()",
- 'BufReadPost ' .. root_dir .. '/*',
- config.name
+ if root_dir then
+ api.nvim_command(
+ string.format(
+ "autocmd %s unsilent lua require'lspconfig'[%q].manager.try_add_wrapper()",
+ 'BufReadPost ' .. root_dir .. '/*',
+ config.name
+ )
)
- )
- for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
- local buf_dir = api.nvim_buf_get_name(bufnr)
- if buf_dir:sub(1, root_dir:len()) == root_dir then
- M.manager.try_add_wrapper(bufnr)
+ for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
+ local buf_dir = api.nvim_buf_get_name(bufnr)
+ if buf_dir:sub(1, root_dir:len()) == root_dir then
+ M.manager.try_add_wrapper(bufnr)
+ end
end
+ elseif config.single_file_support then
+ -- This allows on_new_config to use the parent directory of the file
+ -- Effectively this is the root from lspconfig's perspective, as we use
+ -- this to attach additional files in the same parent folder to the same server.
+ -- We just no longer send rootDirectory or workspaceFolders during initialization.
+ local pseudo_root = util.path.dirname(api.nvim_buf_get_name(0))
+ M.manager.add(pseudo_root, true)
+ else
+ vim.notify(string.format('Autostart for %s failed: matching root directory not detected.', config_name))
end
end
@@ -182,11 +189,23 @@ function configs.__newindex(t, config_name, config_def)
function manager.try_add(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
+
if vim.api.nvim_buf_get_option(bufnr, 'buftype') == 'nofile' then
return
end
+
+ local id
local root_dir = get_root_dir(api.nvim_buf_get_name(bufnr), bufnr)
- local id = manager.add(root_dir)
+
+ if root_dir then
+ id = manager.add(root_dir, false)
+ elseif config.single_file_support then
+ local pseudo_root = util.path.dirname(api.nvim_buf_get_name(0))
+ id = manager.add(pseudo_root, true)
+ else
+ vim.notify(string.format('Autostart for %s failed: matching root directory not detected.', config_name))
+ end
+
if id then
lsp.buf_attach_client(bufnr, id)
end