diff --git a/.gitignore b/.gitignore index c3121bab..194fed01 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ tmp node_modules nvim-test deps -busted/fixtures/basic/bin -busted/fixtures/basic/data +spec/fixtures/basic/bin +spec/fixtures/basic/cache +spec/fixtures/basic/data diff --git a/lua/elixir/nextls/init.lua b/lua/elixir/nextls/init.lua index d048f614..053a475c 100644 --- a/lua/elixir/nextls/init.lua +++ b/lua/elixir/nextls/init.lua @@ -5,8 +5,8 @@ if not vim.uv then vim.uv = vim.loop end -M.default_bin = vim.g.next_ls_default_bin or (vim.env.HOME .. "/.cache/elixir-tools/nextls/bin/nextls") -M.default_data = vim.g.next_ls_data_dir or (vim.env.HOME .. "/.data/elixir-tools/nextls") +M.default_bin = vim.g.next_ls_default_bin or (utils.cache_dir() .. "/elixir-tools/nextls/bin/nextls") +M.default_data = vim.g.next_ls_data_dir or (utils.data_dir() .. "/elixir-tools/nextls") local function bufname_valid(bufname) if diff --git a/lua/elixir/utils.lua b/lua/elixir/utils.lua index 5338a547..9fddc4fa 100644 --- a/lua/elixir/utils.lua +++ b/lua/elixir/utils.lua @@ -1,6 +1,14 @@ local Path = require("plenary.path") local M = {} +function M.cache_dir() + return vim.env.XDG_CACHE_HOME or vim.env.HOME .. "/.cache" +end + +function M.data_dir() + return vim.env.XDG_DATA_HOME or vim.env.HOME .. "/.data" +end + ---@param path string function M.safe_path(path) return string.gsub(path, "/", "_") @@ -37,7 +45,7 @@ local arch = { function M.download_nextls(opts) vim.notify("[elixir-tools] Downloading latest version of Next LS") - local default_cache_dir = vim.g.next_ls_cache_dir or vim.env.HOME .. "/.cache/elixir-tools/nextls/bin" + local default_cache_dir = vim.g.next_ls_cache_dir or M.cache_dir() .. "/elixir-tools/nextls/bin" opts = opts or {} local cache_dir = opts.cache_dir or default_cache_dir local os_name = string.lower(vim.uv.os_uname().sysname) @@ -77,7 +85,7 @@ end function M.latest_release(owner, repo, opts) opts = opts or {} local github_host = opts.github_host or "api.github.com" - local cache_dir = opts.cache_dir or "~/.cache/nvim/elixir-tools.nvim/" + local cache_dir = opts.cache_dir or M.cache_dir() .. "/nvim/elixir-tools.nvim/" local curl_response = vim.fn.system { "curl", "--fail", diff --git a/spec/nextls/install_spec.lua b/spec/nextls/install_spec.lua index 51a27914..c23ed6c0 100644 --- a/spec/nextls/install_spec.lua +++ b/spec/nextls/install_spec.lua @@ -4,6 +4,8 @@ local exec_lua = helpers.exec_lua local luv = vim.loop local eq = assert.equal +helpers.options = { verbose = true } + describe("install", function() before_each(function() helpers.clear() @@ -11,6 +13,11 @@ describe("install", function() helpers.fn.delete("./spec/fixtures/basic/data", "rf") helpers.fn.mkdir("./spec/fixtures/basic/data", "p") helpers.fn.mkdir("./spec/fixtures/basic/bin", "p") + exec_lua([[ + vim.g.next_ls_cache_dir = nil + vim.g.next_ls_data_dir = nil + vim.g.next_ls_default_bin = nil + ]]) -- Make plugin available exec_lua([[vim.opt.rtp:append'.']]) exec_lua([[vim.opt.rtp:append'./deps/plenary.nvim/']]) @@ -29,6 +36,20 @@ describe("install", function() eq(luv.fs_stat("./spec/fixtures/basic/bin/nextls").mode, 33523) end) + it("installs nextls into the xdg dirs when set", function() + helpers.fn.writefile({ "" }, "./spec/fixtures/basic/data/.next-ls-force-update-v1") + exec_lua([[ + vim.env.XDG_CACHE_HOME = "./spec/fixtures/basic/cache" + vim.env.XDG_DATA_HOME = "./spec/fixtures/basic/data" + require("elixir.nextls").setup({auto_update = true, cmd = "./spec/fixtures/basic/cache/elixir-tools/nextls/bin/nextls" }) + vim.cmd.edit("./spec/fixtures/basic/lib/basic.ex") + ]]) + + local file = luv.fs_stat("./spec/fixtures/basic/cache/elixir-tools/nextls/bin/nextls") + assert.Table(file) + eq(file.mode, 33523) + end) + it("forces an install if the flag is not set", function() helpers.fn.mkdir("./spec/fixtures/basic/bin", "p") helpers.fn.writefile({ "foobar" }, "./spec/fixtures/basic/bin/nextls")