Skip to content

Commit

Permalink
refactor: config
Browse files Browse the repository at this point in the history
  • Loading branch information
polarmutex committed Nov 11, 2023
1 parent 2eafc1d commit 1843a77
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
43 changes: 15 additions & 28 deletions lua/git-worktree/config.lua
Original file line number Diff line number Diff line change
@@ -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
22 changes: 5 additions & 17 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -405,4 +393,4 @@ end
-- M.setup()
-- --M.Operations = Enum.Operations

return M:new()
return M
22 changes: 22 additions & 0 deletions spec/config_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1843a77

Please sign in to comment.