Skip to content

Commit 2f05c7a

Browse files
committed
No longer depend on nvim-lspconfig
Revert "Revert "feat: no longer require nvim-lspconfig (#27)" (#30)" This reverts commit 67bb9a1. This reverts the #30, which is a revert of #27. We can merge this when `vim.fs` and `vim.lsp.start` make it into stable. Closes #5 Lose dependency on lspconfig
1 parent 62a312e commit 2f05c7a

File tree

5 files changed

+79
-75
lines changed

5 files changed

+79
-75
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ 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
5857
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
5958
6059
- name: Run tests

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Requires 0.7+.
1616

1717
```lua
18-
use({ "mhanberg/elixir.nvim", requires = { "neovim/nvim-lspconfig", "nvim-lua/plenary.nvim" }})
18+
use({ "mhanberg/elixir.nvim", requires = { "nvim-lua/plenary.nvim" }})
1919
```
2020

2121
## Getting Started
@@ -30,14 +30,16 @@ require("elixir").setup()
3030

3131
While the plugin works with a minimal setup, it is much more useful if you add some personal configuration.
3232

33+
Note: Not specifying the `repo`, `branch`, or `tag` options will default to the latest release.
34+
3335
```lua
3436
local elixir = require("elixir")
3537

3638
elixir.setup({
3739
-- specify a repository and branch
3840
repo = "mhanberg/elixir-ls", -- defaults to elixir-lsp/elixir-ls
3941
branch = "mh/all-workspace-symbols", -- defaults to nil, just checkouts out the default branch, mutually exclusive with the `tag` option
40-
tag = "v0.9.0", -- defaults to nil, mutually exclusive with the `branch` option
42+
tag = "v0.11.0", -- defaults to nil, mutually exclusive with the `branch` option
4143

4244
-- alternatively, point to an existing elixir-ls installation (optional)
4345
cmd = "/usr/local/bin/elixir-ls.sh",

lua/elixir/language_server/init.lua

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
local lspconfig = require("lspconfig")
2-
local lsputil = require("lspconfig.util")
3-
4-
local Job = require("plenary.job")
51
local Path = require("plenary.path")
62
local popup = require("plenary.popup")
73

@@ -10,10 +6,13 @@ local Download = require("elixir.language_server.download")
106
local Compile = require("elixir.language_server.compile")
117
local Utils = require("elixir.utils")
128

13-
local default_config = require("lspconfig.server_configurations.elixirls").default_config
149
local capabilities = vim.lsp.protocol.make_client_capabilities()
1510
capabilities.textDocument.completion.completionItem.snippetSupport = true
1611

12+
local default_install_tag = "tags/v0.11.0"
13+
14+
local elixir_nvim_output_bufnr
15+
1716
local M = {}
1817

1918
local get_cursor_position = function()
@@ -29,12 +28,10 @@ function M.open_floating_window(buf)
2928
local lines = vim.o.lines
3029
local width = math.ceil(columns * 0.8)
3130
local height = math.ceil(lines * 0.8 - 4)
32-
-- local left = math.ceil((columns - width) * 0.5)
33-
-- local top = math.ceil((lines - height) * 0.5 - 1)
3431

3532
local bufnr = buf or vim.api.nvim_create_buf(false, true)
3633

37-
local win_id = popup.create(bufnr, {
34+
popup.create(bufnr, {
3835
line = 0,
3936
col = 0,
4037
minwidth = width,
@@ -118,9 +115,8 @@ local nil_buf_id = 999999
118115
local term_buf_id = nil_buf_id
119116

120117
local function test(command)
121-
local row, col = get_cursor_position()
118+
local row, _col = get_cursor_position()
122119
local args = command.arguments[1]
123-
local current_buf_id = vim.api.nvim_get_current_buf()
124120

125121
-- delete the current buffer if it's still open
126122
if vim.api.nvim_buf_is_valid(term_buf_id) then
@@ -219,7 +215,7 @@ local function install_elixir_ls(opts)
219215
local source_path = Download.clone(tostring(download_dir:absolute()), opts)
220216
local bufnr = M.open_floating_window()
221217

222-
local result = Compile.compile(
218+
Compile.compile(
223219
download_dir:joinpath(source_path):absolute(),
224220
opts.install_path:absolute(),
225221
vim.tbl_extend("force", opts, { bufnr = bufnr })
@@ -237,7 +233,7 @@ local function make_opts(opts)
237233
if opts.repo then -- if we specified a repo in our conifg, then let's default to HEAD
238234
ref = "HEAD"
239235
else -- else, let's checkout the latest stable release
240-
ref = "tags/v0.10.0"
236+
ref = default_install_tag
241237
end
242238
end
243239

@@ -253,52 +249,63 @@ function M.setup(opts)
253249
vim.api.nvim_buf_set_name(elixir_nvim_output_bufnr, "ElixirLS Output Panel")
254250
end
255251

256-
opts = opts or {}
257-
lspconfig.elixirls.setup(vim.tbl_extend("keep", {
258-
filetypes = { "elixir", "eelixir", "heex", "surface" },
259-
260-
on_init = lsputil.add_hook_after(default_config.on_init, function(client)
261-
client.commands["elixir.lens.test.run"] = test
262-
end),
263-
on_new_config = function(new_config, new_root_dir)
264-
new_opts = make_opts(opts)
265-
266-
if not opts["cmd"] then
267-
local cmd = M.command {
268-
path = tostring(install_dir),
269-
repo = new_opts.repo,
270-
ref = new_opts.ref,
271-
versions = Version.get(),
272-
}
273-
274-
if not cmd:exists() then
275-
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
276-
if choice == "Yes" then
277-
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
278-
end
279-
end)
280-
281-
return
282-
else
283-
local updated_config = new_config
284-
updated_config.cmd = { tostring(cmd) }
285-
286-
return updated_config
252+
local elixir_group = vim.api.nvim_create_augroup("elixirnvim", { clear = true })
253+
254+
local start_elixir_ls = function(arg)
255+
local fname = Path.new(arg.file):absolute()
256+
257+
local root_dir = opts.root_dir and opts.root_dir(fname) or Utils.root_dir(fname)
258+
local new_opts = make_opts(opts)
259+
260+
local cmd = M.command {
261+
path = tostring(install_dir),
262+
repo = new_opts.repo,
263+
ref = new_opts.ref,
264+
versions = Version.get(),
265+
}
266+
267+
if not cmd:exists() then
268+
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
269+
if choice == "Yes" then
270+
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
287271
end
288-
end
289-
end,
290-
handlers = {
291-
["window/logMessage"] = function(err, result, ...)
292-
message = vim.split("[" .. vim.lsp.protocol.MessageType[result.type] .. "] " .. result.message, "\n")
293-
294-
vim.api.nvim_buf_set_lines(elixir_nvim_output_bufnr, -1, -1, false, message)
295-
end,
296-
},
297-
settings = opts.settings or settings,
298-
capabilities = opts.capabilities or capabilities,
299-
root_dir = opts.root_dir or Utils.root_dir,
300-
on_attach = lsputil.add_hook_before(opts.on_attach, M.on_attach),
301-
}, opts))
272+
end)
273+
274+
return
275+
elseif root_dir then
276+
vim.lsp.start(vim.tbl_extend("keep", {
277+
name = "ElixirLS",
278+
cmd = { tostring(cmd) },
279+
commands = {
280+
["elixir.lens.test.run"] = test,
281+
},
282+
settings = opts.settings or M.settings {},
283+
capabilities = opts.capabilities or capabilities,
284+
root_dir = root_dir,
285+
handlers = {
286+
["window/logMessage"] = function(_err, result)
287+
local message =
288+
vim.split("[" .. vim.lsp.protocol.MessageType[result.type] .. "] " .. result.message, "\n")
289+
290+
vim.api.nvim_buf_set_lines(elixir_nvim_output_bufnr, -1, -1, false, message)
291+
end,
292+
},
293+
on_attach = function(...)
294+
if opts.on_attach then
295+
opts.on_attach(...)
296+
end
297+
298+
M.on_attach(...)
299+
end,
300+
}, opts))
301+
end
302+
end
303+
304+
vim.api.nvim_create_autocmd({ "FileType" }, {
305+
group = elixir_group,
306+
pattern = { "elixir", "eelixir", "heex", "surface" },
307+
callback = start_elixir_ls,
308+
})
302309
end
303310

304311
return M

lua/elixir/utils.lua

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
local lspconfig = require("lspconfig")
2-
local lsputil = require("lspconfig.util")
3-
4-
local uv = vim.loop
5-
1+
local Path = require("plenary.path")
62
local M = {}
73

84
function M.safe_path(path)
@@ -20,15 +16,15 @@ function M.root_dir(fname)
2016
fname = vim.fn.getcwd()
2117
end
2218

23-
local path = lsputil.path
24-
local child_or_root_path = lsputil.root_pattern { "mix.exs", ".git" }(fname)
19+
local child_or_root_path =
20+
vim.fs.dirname(vim.fs.find({ "mix.exs", ".git" }, { upward = true, path = fname })[1])
2521
local maybe_umbrella_path =
26-
lsputil.root_pattern { "mix.exs" }(uv.fs_realpath(path.join { child_or_root_path, ".." }))
22+
vim.fs.dirname(vim.fs.find({ "mix.exs" }, { upward = true, path = child_or_root_path })[1])
2723

28-
local has_ancestral_mix_exs_path =
29-
vim.startswith(child_or_root_path, path.join { maybe_umbrella_path, "apps" })
30-
if maybe_umbrella_path and not has_ancestral_mix_exs_path then
31-
maybe_umbrella_path = nil
24+
if maybe_umbrella_path then
25+
if not vim.startswith(child_or_root_path, Path:joinpath(maybe_umbrella_path, "apps"):absolute()) then
26+
maybe_umbrella_path = nil
27+
end
3228
end
3329

3430
local path = maybe_umbrella_path or child_or_root_path or vim.loop.os_homedir()

tests/download_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ describe("download", function()
4242
it("can checkout a different tag", function()
4343
local download_dir = "tmp/downloads"
4444

45-
local result = Download.clone(download_dir, { repo = "elixir-lsp/elixir-ls", ref = "tags/v0.9.0" })
45+
local result = Download.clone(download_dir, { repo = "elixir-lsp/elixir-ls", ref = "tags/v0.11.0" })
4646

47-
eq("elixir-lsp/elixir-ls/tags_v0.9.0", result)
48-
assert.True(Path:new(download_dir, "elixir-lsp/elixir-ls/tags_v0.9.0", "mix.exs"):exists())
47+
eq("elixir-lsp/elixir-ls/tags_v0.11.0", result)
48+
assert.True(Path:new(download_dir, "elixir-lsp/elixir-ls/tags_v0.11.0", "mix.exs"):exists())
4949
end)
5050
end)

0 commit comments

Comments
 (0)