From 1843a774ba2e20a68b8be6e3507c28cdf1abea99 Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Sat, 11 Nov 2023 00:05:28 -0500 Subject: [PATCH] refactor: config --- lua/git-worktree/config.lua | 43 +++++++++++++------------------------ lua/git-worktree/init.lua | 22 +++++-------------- spec/config_spec.lua | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+), 45 deletions(-) create mode 100644 spec/config_spec.lua diff --git a/lua/git-worktree/config.lua b/lua/git-worktree/config.lua index c2ecbb7..90cfd58 100644 --- a/lua/git-worktree/config.lua +++ b/lua/git-worktree/config.lua @@ -1,36 +1,23 @@ local M = {} ----@class GitWorktreeConfig +---@class GitWorktree.Config +local defaults = { + change_directory_command = 'cd', + update_on_change = true, + update_on_change_command = 'e .', + clearjumps_on_change = true, + confirm_telescope_deletions = true, + autopush = false, +} ----@class GitWorktreePartialConfig ----@field change_directory_command? string ----@field update_on_change? boolean ----@field update_on_change_command? string ----@field clearjumps_on_change? boolean ----@field confirm_telescope_deletions? boolean ----@field autopush? boolean +---@type GitWorktree.Config +M.options = {} ----@return GitWorktreeConfig -function M.get_default_config() - return { - change_directory_command = 'cd', - update_on_change = true, - update_on_change_command = 'e .', - clearjumps_on_change = true, - confirm_telescope_deletions = true, - autopush = false, - } +---@param opts? GitWorktree.Config +function M.setup(opts) + M.options = vim.tbl_deep_extend('force', defaults, opts or {}) end ----@param partial_config GitWorktreePartialConfig ----@param latest_config GitWorktreeConfig? ----@return GitWorktreeConfig -function M.merge_config(partial_config, latest_config) - local config = latest_config or M.get_default_config() - - config = vim.tbl_extend('force', config, partial_config) - - return config -end +M.setup() return M diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index b3964b2..5155367 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -2,7 +2,7 @@ -- local Path = require("plenary.path") -- local Enum = require("git-worktree.enum") -local Config = require('git-worktree.config') +-- local Config = require('git-worktree.config') -- local Git = require("git-worktree.git") -- local Hooks = require("git-worktree.hooks") local Status = require('git-worktree.status') @@ -17,21 +17,9 @@ local status = Status:new() local M = {} -M.__index = M - ----@return GitWorktree -function M:new() - local config = Config.get_default_config() - return setmetatable({ - config = config, - }, self) -end - ----@param partial_config GitWorktreePartialConfig ----@return GitWorktree -function M:setup(partial_config) - self.config = Config.merge_config(partial_config, self.config) - return self +---@param opts? GitWorktree.Config +function M.setup(opts) + require('git-worktree.config').setup(opts) end -- local function change_dirs(path) @@ -405,4 +393,4 @@ end -- M.setup() -- --M.Operations = Enum.Operations -return M:new() +return M diff --git a/spec/config_spec.lua b/spec/config_spec.lua new file mode 100644 index 0000000..7eae061 --- /dev/null +++ b/spec/config_spec.lua @@ -0,0 +1,22 @@ +local stub = require('luassert.stub') + +describe('config', function() + local notify_once = stub(vim, 'notify_once') + local notify = stub(vim, 'notify') + + it('returns the default config', function() + local Config = require('git-worktree.config') + assert.truthy(Config.options.change_directory_command) + end) + + it('can have configuration applied', function() + local Config = require('git-worktree.config') + Config.setup { change_directory_command = 'test' } + assert.equals(Config.options.change_directory_command, 'test') + end) + + it('No notifications at startup.', function() + assert.stub(notify_once).was_not_called() + assert.stub(notify).was_not_called() + end) +end)