Skip to content

Commit 67bb9a1

Browse files
authored
Revert "feat: no longer require nvim-lspconfig (#27)" (#30)
This reverts commit 1128046.
1 parent 1128046 commit 67bb9a1

File tree

3 files changed

+53
-57
lines changed

3 files changed

+53
-57
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
}
5555
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
5656
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
57+
git clone --depth 1 https://github.com/neovim/nvim-lspconfig ~/.local/share/nvim/site/pack/vendor/start/nvim-lspconfig
5758
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
5859
5960
- name: Run tests

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Requires 0.7+.
1010

1111
```lua
12-
use({ "mhanberg/elixir.nvim", requires = { "nvim-lua/plenary.nvim" }})
12+
use({ "mhanberg/elixir.nvim", requires = { "neovim/nvim-lspconfig", "nvim-lua/plenary.nvim" }})
1313
```
1414

1515
## Getting Started
@@ -31,7 +31,7 @@ elixir.setup({
3131
-- specify a repository and branch
3232
repo = "mhanberg/elixir-ls", -- defaults to elixir-lsp/elixir-ls
3333
branch = "mh/all-workspace-symbols", -- defaults to nil, just checkouts out the default branch, mutually exclusive with the `tag` option
34-
tag = "v0.10.0", -- defaults to nil, mutually exclusive with the `branch` option
34+
tag = "v0.9.0", -- defaults to nil, mutually exclusive with the `branch` option
3535

3636
-- default settings, use the `settings` function to override settings
3737
settings = elixir.settings({

lua/elixir/init.lua

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
local uv = vim.loop
22

3+
local lspconfig = require("lspconfig")
4+
local lsputil = require("lspconfig.util")
5+
36
local Job = require("plenary.job")
47
local Path = require("plenary.path")
58
local popup = require("plenary.popup")
@@ -9,6 +12,7 @@ local Download = require("elixir.download")
912
local Compile = require("elixir.compile")
1013
local Utils = require("elixir.utils")
1114

15+
local default_config = require("lspconfig.server_configurations.elixirls").default_config
1216
local capabilities = vim.lsp.protocol.make_client_capabilities()
1317
capabilities.textDocument.completion.completionItem.snippetSupport = true
1418

@@ -153,14 +157,15 @@ local function test(command)
153157
end
154158

155159
local root_dir = function(fname)
156-
local child_or_root_path = vim.fs.dirname(vim.fs.find({ "mix.exs", ".git" }, { upward = true, path = fname })[1])
157-
local maybe_umbrella_path =
158-
vim.fs.dirname(vim.fs.find({ "mix.exs" }, { upward = true, path = child_or_root_path })[1])
160+
local path = lsputil.path
161+
local child_or_root_path = lsputil.root_pattern({ "mix.exs", ".git" })(fname)
162+
local maybe_umbrella_path = lsputil.root_pattern({ "mix.exs" })(
163+
uv.fs_realpath(path.join({ child_or_root_path, ".." }))
164+
)
159165

160-
if maybe_umbrella_path then
161-
if not vim.startswith(child_or_root_path, Path:joinpath(maybe_umbrella_path, "apps"):absolute()) then
162-
maybe_umbrella_path = nil
163-
end
166+
local has_ancestral_mix_exs_path = vim.startswith(child_or_root_path, path.join({ maybe_umbrella_path, "apps" }))
167+
if maybe_umbrella_path and not has_ancestral_mix_exs_path then
168+
maybe_umbrella_path = nil
164169
end
165170

166171
local path = maybe_umbrella_path or child_or_root_path or vim.loop.os_homedir()
@@ -180,8 +185,13 @@ M.settings = function(opts)
180185
end
181186

182187
function M.command(params)
183-
local install_path =
184-
Path:new(params.path, params.repo, Utils.safe_path(params.ref), params.versions, "language_server.sh")
188+
local install_path = Path:new(
189+
params.path,
190+
params.repo,
191+
Utils.safe_path(params.ref),
192+
params.versions,
193+
"language_server.sh"
194+
)
185195

186196
return install_path
187197
end
@@ -237,55 +247,40 @@ end
237247

238248
function M.setup(opts)
239249
opts = opts or {}
240-
local elixir_group = vim.api.nvim_create_augroup("elixirnvim", { clear = true })
241-
242-
local start_elixir_ls = function(arg)
243-
fname = Path.new(arg.file):absolute()
244-
245-
local root_dir = opts.root_dir and opts.root_dir(fname) or root_dir(fname)
246-
local new_opts = make_opts(opts)
247-
248-
local cmd = M.command({
249-
path = tostring(install_dir),
250-
repo = new_opts.repo,
251-
ref = new_opts.ref,
252-
versions = Version.get(),
253-
})
254-
255-
if not cmd:exists() then
256-
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
257-
if choice == "Yes" then
258-
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
259-
end
260-
end)
261-
262-
return
263-
elseif root_dir then
264-
vim.lsp.start(vim.tbl_extend("keep", {
265-
name = "ElixirLS",
266-
cmd = { tostring(cmd) },
267-
commands = {
268-
["elixir.lens.test.run"] = test,
269-
},
270-
settings = opts.settings or settings,
271-
capabilities = opts.capabilities or capabilities,
272-
root_dir = root_dir,
273-
on_attach = function(...)
274-
if opts.on_attach then
275-
opts.on_attach(...)
250+
lspconfig.elixirls.setup(vim.tbl_extend("keep", {
251+
on_init = lsputil.add_hook_after(default_config.on_init, function(client)
252+
client.commands["elixir.lens.test.run"] = test
253+
end),
254+
on_new_config = function(new_config, new_root_dir)
255+
new_opts = make_opts(opts)
256+
257+
local cmd = M.command({
258+
path = tostring(install_dir),
259+
repo = new_opts.repo,
260+
ref = new_opts.ref,
261+
versions = Version.get(),
262+
})
263+
264+
if not cmd:exists() then
265+
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
266+
if choice == "Yes" then
267+
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
276268
end
269+
end)
277270

278-
M.on_attach(...)
279-
end,
280-
}, opts))
281-
end
282-
end
271+
return
272+
else
273+
local updated_config = new_config
274+
updated_config.cmd = { tostring(cmd) }
283275

284-
vim.api.nvim_create_autocmd({ "FileType" }, {
285-
group = elixir_group,
286-
pattern = { "elixir" },
287-
callback = start_elixir_ls,
288-
})
276+
return updated_config
277+
end
278+
end,
279+
settings = opts.settings or settings,
280+
capabilities = opts.capabilities or capabilities,
281+
root_dir = opts.root_dir or root_dir,
282+
on_attach = lsputil.add_hook_before(opts.on_attach, M.on_attach),
283+
}, opts))
289284
end
290285

291286
return M

0 commit comments

Comments
 (0)