Skip to content

Commit a91ae53

Browse files
committed
feat(nextls): multi-root workspaces
This patch has a dependency on the `mhanberg/workspace-folders.nvim` plugin. To try out the behavior, intsall that plugin and create a `.code-workspace` file that describes your multi-root workspace (or mono repo as most call it). If your monorepo is called "money-factory-io", with the folders "crypto", "ai-chat-app", and "drop-shipping-cms", your `code-workspace` would be named `money-factory-io.code-workspace` and look like: ```json { "folders": [ {"path": "crypto"}, {"path": "ai-chat-app"}, {"path": "drop-shipping-cms"}, ] } ```
1 parent f98a90e commit a91ae53

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

lua/elixir/nextls/init.lua

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function M.setup(opts)
1717
local cmd = event.data.cmd
1818
local auto_update = event.data.auto_update
1919
local options = event.data.opts
20-
local root_dir = event.data.root_dir
20+
local workspace_folders = event.data.workspace_folders
21+
2122
vim.lsp.start({
2223
name = "NextLS",
2324
cmd = cmd,
@@ -28,9 +29,7 @@ function M.setup(opts)
2829
init_options = options.init_options or vim.empty_dict(),
2930
settings = {},
3031
capabilities = options.capabilities or vim.lsp.protocol.make_client_capabilities(),
31-
workspace_folders = {
32-
{ name = root_dir, uri = vim.uri_from_fname(root_dir) },
33-
},
32+
workspace_folders = workspace_folders,
3433
on_attach = options.on_attach or function() end,
3534
}, {
3635
bufnr = 0,
@@ -50,35 +49,54 @@ function M.setup(opts)
5049
group = nextls_group,
5150
pattern = { "elixir", "eelixir", "heex", "surface" },
5251
callback = function()
53-
local lock_matches = vim.fs.find({ "mix.lock" }, {
54-
stop = vim.uv.os_homedir(),
55-
upward = true,
56-
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
57-
})
52+
local lock_matches
53+
local mix_exs_matches
54+
local workspace_folders
55+
if vim.g.workspace then
56+
local uri = vim.uri_from_bufnr(0)
57+
if
58+
vim.iter(vim.g.workspace.folders):any(function(folder)
59+
return vim.startswith(uri, folder.uri)
60+
end)
61+
then
62+
workspace_folders = vim.g.workspace.folders
63+
end
64+
else
65+
lock_matches = vim.fs.find({ "mix.lock" }, {
66+
stop = vim.uv.os_homedir(),
67+
upward = true,
68+
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
69+
})
5870

59-
local mix_exs_matches = vim.fs.find({ "mix.exs" }, {
60-
stop = vim.uv.os_homedir(),
61-
upward = true,
62-
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
63-
})
71+
mix_exs_matches = vim.fs.find({ "mix.exs" }, {
72+
stop = vim.uv.os_homedir(),
73+
upward = true,
74+
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
75+
})
76+
local file = lock_matches[1] or mix_exs_matches[1]
6477

65-
local file = lock_matches[1] or mix_exs_matches[1]
78+
if file then
79+
local root_dir = vim.fs.dirname(file)
80+
assert(type(root_dir) == "string", "expected root_dir to be a string")
81+
workspace_folders = {
82+
{ name = vim.fs.basename(root_dir), uri = vim.uri_from_fname(root_dir) },
83+
}
84+
end
85+
end
6686

67-
if file then
87+
if workspace_folders then
6888
local cmd
6989
if type(opts.port) == "number" then
7090
cmd = vim.lsp.rpc.connect("127.0.0.1", opts.port)
7191
else
7292
cmd = { opts.cmd, "--stdio" }
7393
end
7494

75-
local root_dir = vim.fs.dirname(file)
76-
assert(type(root_dir) == "string", "expected root_dir to be a string")
7795
local activate = function()
7896
vim.api.nvim_exec_autocmds("User", {
7997
pattern = "ElixirToolsNextLSActivate",
8098
data = {
81-
root_dir = root_dir,
99+
workspace_folders = workspace_folders,
82100
cmd = cmd,
83101
auto_update = opts.auto_update,
84102
opts = opts,
@@ -96,7 +114,7 @@ function M.setup(opts)
96114
utils.download_nextls()
97115
activate()
98116
else
99-
vim.b.elixir_tools_prompted_nextls_install = true
117+
vim.b["elixir_tools_prompted_nextls_install"] = true
100118
end
101119
end)
102120
else

0 commit comments

Comments
 (0)