Skip to content

Commit

Permalink
chore: luachecks
Browse files Browse the repository at this point in the history
  • Loading branch information
polarmutex authored and Brian Ryall committed Dec 28, 2023
1 parent b63cfd5 commit 7f6377f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ignore = {
}
read_globals = {
"vim",
"describe",
"it",
"assert"
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
hooks = {
alejandra.enable = true;
stylua.enable = true;
#luacheck.enable = true;
luacheck.enable = true;
#markdownlint.enable = true;
};
};
Expand Down
2 changes: 1 addition & 1 deletion lua/git-worktree/enum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Enum = function(tbl)
error(string.format("%s does not exist for this enum.", key))
end,

__newindex = function(t, key, value)
__newindex = function(_, _, _)
error("Enums are immutable. You are not able to set new values")
end,
})
Expand Down
4 changes: 2 additions & 2 deletions lua/git-worktree/git.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Job = require("plenary").job
local Path = require("plenary.path")
--local Path = require("plenary.path")
local Status = require("git-worktree.status")

local status = Status:new()
Expand Down Expand Up @@ -55,7 +55,7 @@ function M.is_worktree()
end
end

--- @param is_worktree boolean
-- @param is_worktree boolean
--- @return string|nil
function M.find_git_dir()
local job = Job:new({
Expand Down
17 changes: 11 additions & 6 deletions lua/git-worktree/hooks.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
local Enum = require("git-worktree.enum")
local Status = require("git-worktree.status")
local status = Status:new()

--- @class GitWorktreeHooks
local M = {}

local function on_tree_change_handler(op, metadata)
function M.on_tree_change_handler(op, metadata)
if M._config.update_on_change then
if op == Enum.Operations.Switch then
local changed = M.update_current_buffer(metadata["prev_path"])
Expand All @@ -15,13 +19,14 @@ local function on_tree_change_handler(op, metadata)
end
end

local function emit_on_change(op, metadata)
function M.emit_on_change(op, metadata)
-- TODO: We don't have a way to async update what is running
status:next_status(string.format("Running post %s callbacks", op))
on_tree_change_handler(op, metadata)
for idx = 1, #on_change_callbacks do
on_change_callbacks[idx](op, metadata)
end
print(metadata)
-- on_tree_change_handler(op, metadata)
-- for idx = 1, #on_change_callbacks do
-- on_change_callbacks[idx](op, metadata)
-- end
end

return M
24 changes: 11 additions & 13 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local Path = require("plenary.path")
local Enum = require("git-worktree.enum")

local Status = require("git-worktree.status")
local Hooks = require("git-worktree.hooks")
local Git = require("git-worktree.git")

local status = Status:new()
local M = {}
Expand Down Expand Up @@ -196,14 +198,14 @@ local function create_worktree(path, branch, upstream, found_branch)
end

vim.schedule(function()
emit_on_change(Enum.Operations.Create, { path = path, branch = branch, upstream = upstream })
Hooks.emit_on_change(Enum.Operations.Create, { path = path, branch = branch, upstream = upstream })
M.switch_worktree(path)
end)
end)
else
create:after(function()
vim.schedule(function()
emit_on_change(Enum.Operations.Create, { path = path, branch = branch, upstream = upstream })
Hooks.emit_on_change(Enum.Operations.Create, { path = path, branch = branch, upstream = upstream })
M.switch_worktree(path)
end)
end)
Expand All @@ -216,7 +218,7 @@ M.create_worktree = function(path, branch, upstream)
status:reset(8)

if upstream == nil then
if has_origin() then
if Git.has_origin() then
upstream = "origin"
end
end
Expand All @@ -228,7 +230,7 @@ M.create_worktree = function(path, branch, upstream)
status:error("worktree already exists")
end

has_branch(branch, function(found_branch)
Git.has_branch(branch, function(found_branch)
create_worktree(path, branch, upstream, found_branch)
end)
end)
Expand All @@ -244,7 +246,7 @@ M.switch_worktree = function(path)

vim.schedule(function()
local prev_path = change_dirs(path)
emit_on_change(Enum.Operations.Switch, { path = path, prev_path = prev_path })
Hooks.emit_on_change(Enum.Operations.Switch, { path = path, prev_path = prev_path })
end)
end)
end
Expand Down Expand Up @@ -274,7 +276,7 @@ M.delete_worktree = function(path, force, opts)

local delete = Job:new(cmd)
delete:after_success(vim.schedule_wrap(function()
emit_on_change(Enum.Operations.Delete, { path = path })
Hooks.emit_on_change(Enum.Operations.Delete, { path = path })
if opts.on_success then
opts.on_success()
end
Expand Down Expand Up @@ -313,12 +315,12 @@ M.update_current_buffer = function(prev_path)
end

local name = Path:new(current_buf_name):absolute()
local start, fin = string.find(name, cwd .. Path.path.sep, 1, true)
if start ~= nil then
local start1, _ = string.find(name, cwd .. Path.path.sep, 1, true)
if start1 ~= nil then
return true
end

start, fin = string.find(name, prev_path, 1, true)
local start, fin = string.find(name, prev_path, 1, true)
if start == nil then
return false
end
Expand Down Expand Up @@ -374,10 +376,6 @@ M.setup = function(config)
}, config)
end

M.set_status = function(msg)
-- TODO: make this so #1
end

M.setup()
M.Operations = Enum.Operations

Expand Down
2 changes: 0 additions & 2 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local Path = require("plenary.path")
local Window = require("plenary.window.float")
local strings = require("plenary.strings")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
Expand Down
1 change: 1 addition & 0 deletions spec/git_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local Status = require("git-worktree.status")

local status = Status:new()

-- luacheck: globals repo_dir
describe("git-worktree git operations", function()
describe("finds git toplevel in normal repo", function()
before_each(function()
Expand Down
5 changes: 3 additions & 2 deletions tests/worktree_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local in_repo_from_local_no_worktrees = harness.in_repo_from_local_no_worktrees
local in_bare_repo_from_origin_2_worktrees = harness.in_bare_repo_from_origin_2_worktrees
local in_repo_from_origin_2_worktrees = harness.in_repo_from_origin_2_worktrees
local in_bare_repo_from_origin_2_similar_named_worktrees = harness.in_bare_repo_from_origin_2_similar_named_worktrees
local in_repo_from_origin_2_similar_named_worktrees = harness.in_repo_from_origin_2_similar_named_worktrees
--local in_repo_from_origin_2_similar_named_worktrees = harness.in_repo_from_origin_2_similar_named_worktrees
local check_git_worktree_exists = harness.check_git_worktree_exists
local check_branch_upstream = harness.check_branch_upstream

Expand Down Expand Up @@ -492,7 +492,8 @@ describe("git-worktree", function()
)

it(
"in a featB worktree(non bare) with file B open, switch to featC and switch to worktree root in other worktree",
"in a featB worktree(non bare) with file B open, switch to featC and switch to worktree"
.. " root in other worktree",
in_repo_from_origin_2_worktrees(function()
local random_str = git_worktree.get_root():sub(git_worktree.get_root():len() - 4)

Expand Down

0 comments on commit 7f6377f

Please sign in to comment.