Skip to content

Revert "feat: no longer require nvim-lspconfig (#27)" #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/neovim/nvim-lspconfig ~/.local/share/nvim/site/pack/vendor/start/nvim-lspconfig
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start

- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Requires 0.7+.

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

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

-- default settings, use the `settings` function to override settings
settings = elixir.settings({
Expand Down
105 changes: 50 additions & 55 deletions lua/elixir/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local uv = vim.loop

local lspconfig = require("lspconfig")
local lsputil = require("lspconfig.util")

local Job = require("plenary.job")
local Path = require("plenary.path")
local popup = require("plenary.popup")
Expand All @@ -9,6 +12,7 @@ local Download = require("elixir.download")
local Compile = require("elixir.compile")
local Utils = require("elixir.utils")

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

Expand Down Expand Up @@ -153,14 +157,15 @@ local function test(command)
end

local root_dir = function(fname)
local child_or_root_path = vim.fs.dirname(vim.fs.find({ "mix.exs", ".git" }, { upward = true, path = fname })[1])
local maybe_umbrella_path =
vim.fs.dirname(vim.fs.find({ "mix.exs" }, { upward = true, path = child_or_root_path })[1])
local path = lsputil.path
local child_or_root_path = lsputil.root_pattern({ "mix.exs", ".git" })(fname)
local maybe_umbrella_path = lsputil.root_pattern({ "mix.exs" })(
uv.fs_realpath(path.join({ child_or_root_path, ".." }))
)

if maybe_umbrella_path then
if not vim.startswith(child_or_root_path, Path:joinpath(maybe_umbrella_path, "apps"):absolute()) then
maybe_umbrella_path = nil
end
local has_ancestral_mix_exs_path = vim.startswith(child_or_root_path, path.join({ maybe_umbrella_path, "apps" }))
if maybe_umbrella_path and not has_ancestral_mix_exs_path then
maybe_umbrella_path = nil
end

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

function M.command(params)
local install_path =
Path:new(params.path, params.repo, Utils.safe_path(params.ref), params.versions, "language_server.sh")
local install_path = Path:new(
params.path,
params.repo,
Utils.safe_path(params.ref),
params.versions,
"language_server.sh"
)

return install_path
end
Expand Down Expand Up @@ -237,55 +247,40 @@ end

function M.setup(opts)
opts = opts or {}
local elixir_group = vim.api.nvim_create_augroup("elixirnvim", { clear = true })

local start_elixir_ls = function(arg)
fname = Path.new(arg.file):absolute()

local root_dir = opts.root_dir and opts.root_dir(fname) or root_dir(fname)
local new_opts = make_opts(opts)

local cmd = M.command({
path = tostring(install_dir),
repo = new_opts.repo,
ref = new_opts.ref,
versions = Version.get(),
})

if not cmd:exists() then
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
if choice == "Yes" then
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
end
end)

return
elseif root_dir then
vim.lsp.start(vim.tbl_extend("keep", {
name = "ElixirLS",
cmd = { tostring(cmd) },
commands = {
["elixir.lens.test.run"] = test,
},
settings = opts.settings or settings,
capabilities = opts.capabilities or capabilities,
root_dir = root_dir,
on_attach = function(...)
if opts.on_attach then
opts.on_attach(...)
lspconfig.elixirls.setup(vim.tbl_extend("keep", {
on_init = lsputil.add_hook_after(default_config.on_init, function(client)
client.commands["elixir.lens.test.run"] = test
end),
on_new_config = function(new_config, new_root_dir)
new_opts = make_opts(opts)

local cmd = M.command({
path = tostring(install_dir),
repo = new_opts.repo,
ref = new_opts.ref,
versions = Version.get(),
})

if not cmd:exists() then
vim.ui.select({ "Yes", "No" }, { prompt = "Install ElixirLS" }, function(choice)
if choice == "Yes" then
install_elixir_ls(vim.tbl_extend("force", new_opts, { install_path = cmd:parent() }))
end
end)

M.on_attach(...)
end,
}, opts))
end
end
return
else
local updated_config = new_config
updated_config.cmd = { tostring(cmd) }

vim.api.nvim_create_autocmd({ "FileType" }, {
group = elixir_group,
pattern = { "elixir" },
callback = start_elixir_ls,
})
return updated_config
end
end,
settings = opts.settings or settings,
capabilities = opts.capabilities or capabilities,
root_dir = opts.root_dir or root_dir,
on_attach = lsputil.add_hook_before(opts.on_attach, M.on_attach),
}, opts))
end

return M