From e0e239a3e78dae19d1ea35be660016005351561a Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Sun, 10 Jul 2022 11:44:29 -0400 Subject: [PATCH] Revert "feat: no longer require nvim-lspconfig (#27)" This reverts commit 112804679eebe3609cdd68484a4958b8e5781602. --- .github/workflows/ci.yml | 1 + README.md | 4 +- lua/elixir/init.lua | 105 +++++++++++++++++++-------------------- 3 files changed, 53 insertions(+), 57 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f274d19..a10e6e68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index 9d573d97..5da74a5b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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({ diff --git a/lua/elixir/init.lua b/lua/elixir/init.lua index 0d254e4c..a37213e1 100644 --- a/lua/elixir/init.lua +++ b/lua/elixir/init.lua @@ -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") @@ -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 @@ -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() @@ -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 @@ -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