Swap DiffView with CodeDiff
CodeDiff is supposed to be the successor to DiffView. It also has file watching built-in so I can drop the custom `directory-watcher.lua`.
This commit is contained in:
@@ -1,96 +0,0 @@
|
||||
-- Custom directory watcher using Neovim's built-in libuv bindings
|
||||
-- Source: https://github.com/richardgill/nix/blob/ebdd8260f770d242db7b6163158cfe9ad9784c41/modules/home-manager/dot-files/nvim/lua/custom/directory-watcher.lua
|
||||
|
||||
local M = {}
|
||||
|
||||
local uv = vim.uv
|
||||
local watcher = nil
|
||||
local debounce_timer = nil
|
||||
local on_change_handlers = {}
|
||||
|
||||
-- Debounce helper to prevent callback storms
|
||||
local debounce = function(fn, delay)
|
||||
return function(...)
|
||||
local args = { ... }
|
||||
if debounce_timer then
|
||||
debounce_timer:close()
|
||||
end
|
||||
debounce_timer = vim.defer_fn(function()
|
||||
debounce_timer = nil
|
||||
fn(unpack(args))
|
||||
end, delay)
|
||||
end
|
||||
end
|
||||
|
||||
-- Register a named handler to be called when files change
|
||||
-- If a handler with the same name exists, it will be replaced
|
||||
-- Note: Named handlers are required to support Lua hotreload - when a file is reloaded,
|
||||
-- it re-registers its handler with the same name, replacing the old one instead of
|
||||
-- creating duplicates
|
||||
M.registerOnChangeHandler = function(name, handler)
|
||||
on_change_handlers[name] = handler
|
||||
end
|
||||
|
||||
-- Start watching a directory for file changes
|
||||
M.setup = function(opts)
|
||||
opts = opts or {}
|
||||
local path = opts.path
|
||||
local debounce_delay = opts.debounce or 100 -- ms
|
||||
|
||||
if not path then
|
||||
return false
|
||||
end
|
||||
|
||||
-- Stop existing watcher if any
|
||||
if watcher then
|
||||
M.stop()
|
||||
end
|
||||
|
||||
-- Create fs_event handle
|
||||
local fs_event = uv.new_fs_event()
|
||||
if not fs_event then
|
||||
return false
|
||||
end
|
||||
|
||||
-- Debounced callback for file changes
|
||||
local on_change = debounce(function(err, filename, events)
|
||||
if err then
|
||||
M.stop()
|
||||
return
|
||||
end
|
||||
|
||||
if filename then
|
||||
local full_path = path .. "/" .. filename
|
||||
|
||||
-- Call all registered handlers
|
||||
for _, handler in pairs(on_change_handlers) do
|
||||
pcall(handler, full_path, events)
|
||||
end
|
||||
end
|
||||
end, debounce_delay)
|
||||
|
||||
-- Start watching (wrapped for thread safety)
|
||||
local ok, err = fs_event:start(path, { recursive = false }, vim.schedule_wrap(on_change))
|
||||
|
||||
if ok ~= 0 then
|
||||
return false
|
||||
end
|
||||
|
||||
watcher = fs_event
|
||||
return true
|
||||
end
|
||||
|
||||
-- Stop the watcher and clean up resources
|
||||
M.stop = function()
|
||||
if watcher then
|
||||
watcher:stop()
|
||||
watcher = nil
|
||||
end
|
||||
|
||||
if debounce_timer then
|
||||
debounce_timer:close()
|
||||
debounce_timer = nil
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,39 @@
|
||||
-- VSCode-style diff viewer, replaces sindrets/diffview.nvim.
|
||||
-- The C diff library is downloaded automatically on first use (:CodeDiff install to force).
|
||||
|
||||
return {
|
||||
"esmuellert/codediff.nvim",
|
||||
cmd = "CodeDiff",
|
||||
opts = {
|
||||
diff = {
|
||||
layout = "side-by-side",
|
||||
},
|
||||
explorer = {
|
||||
-- Match diffview's file panel: tree listing with +/- line stats
|
||||
view_mode = "tree",
|
||||
line_stats = {
|
||||
enabled = true,
|
||||
},
|
||||
},
|
||||
keymaps = {
|
||||
view = {
|
||||
-- gq closed every diffview panel, keep it alongside the default q
|
||||
quit = { "q", "gq" },
|
||||
-- Default <leader>b shadows LazyVim's +buffer group, <leader>e/E mirrors
|
||||
-- LazyVim's explorer mnemonic instead
|
||||
toggle_explorer = "<leader>E",
|
||||
focus_explorer = "<leader>e",
|
||||
-- Defaults t and gc shadow the t{char} motion and the gc comment
|
||||
-- operator in the (editable) modified buffer
|
||||
toggle_layout = "gl",
|
||||
toggle_compact = "gz",
|
||||
},
|
||||
},
|
||||
},
|
||||
keys = {
|
||||
{ "<leader>gv", "<cmd>CodeDiff<cr>", desc = "Open [g]it Diff[v]iew tab" },
|
||||
{ "<leader>gl", "<cmd>CodeDiff history %<cr>", desc = "Toggle git log of current file history" },
|
||||
{ "<leader>gl", ":'<,'>CodeDiff history<cr>", mode = "v", desc = "Toggle git log of selected lines" },
|
||||
{ "<leader>gL", "<cmd>CodeDiff history<cr>", desc = "Toggle git log of current branch" },
|
||||
},
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
-- Custom Diffview auto-refresh config from https://richardgill.org/blog/configuring-neovim-coding-agents
|
||||
-- Source: https://github.com/richardgill/nix/blob/ebdd8260f770d242db7b6163158cfe9ad9784c41/modules/home-manager/dot-files/nvim/lua/plugins/git-diff_diffview.lua
|
||||
|
||||
return {
|
||||
"sindrets/diffview.nvim",
|
||||
cmd = { "DiffviewOpen", "DiffviewClose", "DiffviewToggleFiles", "DiffviewFocusFiles" },
|
||||
opts = {
|
||||
enhanced_diff_hl = true,
|
||||
keymaps = {
|
||||
view = {
|
||||
{ "n", "gq", "<cmd>DiffviewClose<cr>", { desc = "Close diffview" } },
|
||||
},
|
||||
file_panel = {
|
||||
{ "n", "gq", "<cmd>DiffviewClose<cr>", { desc = "Close diffview" } },
|
||||
},
|
||||
file_history_panel = {
|
||||
{ "n", "gq", "<cmd>DiffviewClose<cr>", { desc = "Close diffview" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
keys = {
|
||||
{ "<leader>gl", ":'<,'>DiffviewFileHistory<cr>", mode = "v", desc = "Toggle git log of selected lines" },
|
||||
{ "<leader>gL", "<cmd>DiffviewFileHistory<cr>", desc = "Toggle git log of current branch" },
|
||||
{ "<leader>gl", "<cmd>DiffviewFileHistory %<cr>", desc = "Toggle git log of current file history" },
|
||||
{ "<leader>gv", "<cmd>DiffviewOpen<cr>", desc = "Open [g]it Diff[v]iew tab" },
|
||||
},
|
||||
config = function(_, opts)
|
||||
require("diffview").setup(opts)
|
||||
|
||||
local watcher = require("custom.directory-watcher")
|
||||
|
||||
local is_git_ignored = function(filepath)
|
||||
vim.fn.system("git check-ignore -q " .. vim.fn.shellescape(filepath))
|
||||
return vim.v.shell_error == 0
|
||||
end
|
||||
|
||||
local update_left_pane = function()
|
||||
pcall(function()
|
||||
local lib = require("diffview.lib")
|
||||
local view = lib.get_current_view()
|
||||
if view then
|
||||
view:update_files()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Register handler for file changes in watched directory
|
||||
watcher.registerOnChangeHandler("diffview", function(filepath, events)
|
||||
local is_in_dot_git_dir = filepath:match("/%.git/") or filepath:match("^%.git/")
|
||||
|
||||
if is_in_dot_git_dir or not is_git_ignored(filepath) then
|
||||
vim.notify("[diffview] File changed: " .. vim.fn.fnamemodify(filepath, ":t"), vim.log.levels.INFO)
|
||||
update_left_pane()
|
||||
end
|
||||
end)
|
||||
|
||||
-- Start watching the current working directory
|
||||
watcher.setup({ path = vim.fn.getcwd() })
|
||||
|
||||
vim.api.nvim_create_autocmd("FocusGained", {
|
||||
callback = update_left_pane,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "DiffviewViewLeave",
|
||||
callback = function()
|
||||
watcher.stop()
|
||||
vim.cmd(":DiffviewClose")
|
||||
end,
|
||||
})
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user