Skip to content

Commit

Permalink
feat: support XDG_CACHE_HOME and XDG_DATA_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jan 12, 2025
1 parent 2c8466c commit 8360e17
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 2 additions & 2 deletions lua/elixir/nextls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lua/elixir/utils.lua
Original file line number Diff line number Diff line change
@@ -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, "/", "_")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions spec/nextls/install_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ 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()
helpers.fn.delete("./spec/fixtures/basic/bin", "rf")
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/']])
Expand All @@ -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")
Expand Down

0 comments on commit 8360e17

Please sign in to comment.