From 7f6377f9bee516be95414bb122277c07465be96b Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Wed, 8 Nov 2023 20:57:11 -0500 Subject: [PATCH] chore: luachecks --- .luacheckrc | 8 ++++++++ flake.nix | 2 +- lua/git-worktree/enum.lua | 2 +- lua/git-worktree/git.lua | 4 ++-- lua/git-worktree/hooks.lua | 17 +++++++++------ lua/git-worktree/init.lua | 24 ++++++++++------------ lua/telescope/_extensions/git_worktree.lua | 2 -- spec/git_spec.lua | 1 + tests/worktree_spec.lua | 5 +++-- 9 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 .luacheckrc diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..60b5814 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,8 @@ +ignore = { +} +read_globals = { + "vim", + "describe", + "it", + "assert" +} diff --git a/flake.nix b/flake.nix index 4fee52d..69addd1 100644 --- a/flake.nix +++ b/flake.nix @@ -48,7 +48,7 @@ hooks = { alejandra.enable = true; stylua.enable = true; - #luacheck.enable = true; + luacheck.enable = true; #markdownlint.enable = true; }; }; diff --git a/lua/git-worktree/enum.lua b/lua/git-worktree/enum.lua index d58150a..9a6e59f 100644 --- a/lua/git-worktree/enum.lua +++ b/lua/git-worktree/enum.lua @@ -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, }) diff --git a/lua/git-worktree/git.lua b/lua/git-worktree/git.lua index d0d323c..04fca66 100644 --- a/lua/git-worktree/git.lua +++ b/lua/git-worktree/git.lua @@ -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() @@ -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({ diff --git a/lua/git-worktree/hooks.lua b/lua/git-worktree/hooks.lua index 6f9718e..13c9ec7 100644 --- a/lua/git-worktree/hooks.lua +++ b/lua/git-worktree/hooks.lua @@ -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"]) @@ -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 diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index 0dc0120..af9faf9 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -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 = {} @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index 33c7720..a044f15 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -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") diff --git a/spec/git_spec.lua b/spec/git_spec.lua index 1b23045..043452c 100644 --- a/spec/git_spec.lua +++ b/spec/git_spec.lua @@ -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() diff --git a/tests/worktree_spec.lua b/tests/worktree_spec.lua index 11d1431..d3b96b8 100644 --- a/tests/worktree_spec.lua +++ b/tests/worktree_spec.lua @@ -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 @@ -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)