From 5c7f482312e01d30934e9b7dd283641ea3ed1831 Mon Sep 17 00:00:00 2001 From: Alessandro Martini <18130319+martini97@users.noreply.github.com> Date: Sat, 23 Oct 2021 16:35:42 -0300 Subject: [PATCH] feat: walk up dir tree to find project config Closes #4 --- lua/project_config/utils.lua | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lua/project_config/utils.lua b/lua/project_config/utils.lua index a757817..155324c 100644 --- a/lua/project_config/utils.lua +++ b/lua/project_config/utils.lua @@ -1,29 +1,43 @@ 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 @@ -31,8 +45,7 @@ end 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