Skip to content
Open
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
33 changes: 23 additions & 10 deletions lua/project_config/utils.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
local Path = require("plenary.path")
local sha = require 'project_config.sha2'
local sha = require("project_config.sha2")

local utils = {}

-- calculate file signature, file should be instance of Path
function utils.file_signature(file)
if not file:exists() then return nil end
local content = string.format('%q', table.concat(file:readlines(), "\n"))
if not file:exists() then
return nil
end
local content = string.format("%q", table.concat(file:readlines(), "\n"))
return sha.sha256(content)
end

-- generate the config file for the current cwd
function utils.get_config_file()
local cwd = Path:new(vim.loop.cwd())
local data_dir = Path:new(vim.fn.stdpath('data'))

local data_dir = Path:new(vim.fn.stdpath("data"))
local file = sha.sha256(cwd:absolute()) .. ".vim"

return data_dir:joinpath('project_config', file)
local config_file = data_dir:joinpath("project_config", file)
if config_file:exists() then
return config_file
end

for _, path in pairs(cwd:parents()) do
local parent_config = data_dir:joinpath("project_config", sha.sha256(path) .. ".vim")
if parent_config:exists() then
return parent_config
end
end

return config_file
end

-- get index of value in table, or nil if missing
function utils.index_of(table, value)
local index = {}
for k, v in pairs(table) do index[v] = k end
for k, v in pairs(table) do
index[v] = k
end
return index[value]
end

-- wrapper around vim confirm()
function utils.confirm(dialog, options, default)
local opts = table.concat(options, "\n")
local def = utils.index_of(options, default)
return vim.api.nvim_eval(string.format([[confirm("%s", "%s", "%s")]], dialog,
opts, def))
return vim.api.nvim_eval(string.format([[confirm("%s", "%s", "%s")]], dialog, opts, def))
end

-- source config file
Expand Down