From 0d1ce78d231773046320caa45fe1a18dfa366761 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Thu, 11 Nov 2021 01:00:24 -0800 Subject: 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 --- lua/lspconfig/util.lua | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'lua/lspconfig/util.lua') diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 3691da68..dbac70fd 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -233,18 +233,24 @@ end)() -- seen before, will call make_config(root_dir) and start a new client. function M.server_per_root_dir_manager(_make_config) local clients = {} + local single_file_clients = {} local manager = {} - function manager.add(root_dir) - if not root_dir then - return - end - if not M.path.is_dir(root_dir) then - return + function manager.add(root_dir, single_file_mode) + local client_id + if single_file_mode then + client_id = single_file_clients[root_dir] + else + if not root_dir then + return + end + if not M.path.is_dir(root_dir) then + return + end + client_id = clients[root_dir] end -- Check if we have a client already or start and store it. - local client_id = clients[root_dir] if not client_id then local new_config = _make_config(root_dir) -- do nothing if the client is not enabled @@ -265,9 +271,20 @@ function M.server_per_root_dir_manager(_make_config) end new_config.on_exit = M.add_hook_before(new_config.on_exit, function() clients[root_dir] = nil + single_file_clients[root_dir] = nil end) + -- Sending rootDirectory and workspaceFolders as null is not explicitly + -- codified in the spec. Certain servers crash if initialized with a NULL + -- root directory. + if single_file_mode then + new_config.root_dir = nil + end client_id = lsp.start_client(new_config) - clients[root_dir] = client_id + if single_file_mode then + single_file_clients[root_dir] = client_id + else + clients[root_dir] = client_id + end end return client_id end -- cgit v1.2.3-70-g09d2