|
| 1 | +-- Inline diff rendering orchestrator |
| 2 | +-- Manages per-buffer state, async git pipeline, and global mode autocmds. |
| 3 | +-- Completely independent from :CodeDiff sessions (uses separate state). |
| 4 | +local M = {} |
| 5 | + |
| 6 | +local DEFAULT_BASE = ":0" -- git index (staged content), matches gitsigns default |
| 7 | + |
| 8 | +-- Per-buffer state: keyed by bufnr |
| 9 | +local inline_state = {} |
| 10 | + |
| 11 | +-- Global mode state |
| 12 | +local global_state = { |
| 13 | + enabled = false, |
| 14 | + base = nil, -- nil = use DEFAULT_BASE |
| 15 | + augroup = nil, |
| 16 | +} |
| 17 | + |
| 18 | +-- Guard for lazy highlight initialization |
| 19 | +local highlights_initialized = false |
| 20 | + |
| 21 | +local function ensure_highlights() |
| 22 | + if not highlights_initialized then |
| 23 | + local highlights = require("codediff.ui.highlights") |
| 24 | + highlights.setup() |
| 25 | + highlights_initialized = true |
| 26 | + end |
| 27 | +end |
| 28 | + |
| 29 | +-- Check if a buffer is part of an active :CodeDiff session. |
| 30 | +-- The codediff-inline namespace is shared, so rendering on a buffer |
| 31 | +-- that already has a :CodeDiff inline view would cause conflicts. |
| 32 | +local function is_in_codediff_session(bufnr) |
| 33 | + local ok, lifecycle = pcall(require, "codediff.ui.lifecycle") |
| 34 | + if not ok then |
| 35 | + return false |
| 36 | + end |
| 37 | + -- find_tabpage_by_buffer returns tabpage if buffer is in an active session |
| 38 | + return lifecycle.find_tabpage_by_buffer(bufnr) ~= nil |
| 39 | +end |
| 40 | + |
| 41 | +-- Internal: fetch git content, compute diff, and render |
| 42 | +function M.render_buf(bufnr, base) |
| 43 | + base = base or global_state.base or DEFAULT_BASE |
| 44 | + |
| 45 | + if not vim.api.nvim_buf_is_valid(bufnr) then |
| 46 | + return |
| 47 | + end |
| 48 | + |
| 49 | + local file_path = vim.api.nvim_buf_get_name(bufnr) |
| 50 | + if file_path == "" then |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | + if is_in_codediff_session(bufnr) then |
| 55 | + return |
| 56 | + end |
| 57 | + |
| 58 | + local git = require("codediff.core.git") |
| 59 | + git.get_buf_file_content(file_path, base, function(err, original_lines, git_root, rel_path) |
| 60 | + vim.schedule(function() |
| 61 | + if not vim.api.nvim_buf_is_valid(bufnr) then |
| 62 | + return |
| 63 | + end |
| 64 | + |
| 65 | + if err then |
| 66 | + -- Non-git buffer or git error: silent no-op |
| 67 | + if err:match("Not in a git repository") then |
| 68 | + return |
| 69 | + end |
| 70 | + -- File not in base revision (new file): treat as all-added |
| 71 | + if err:match("not found in revision") then |
| 72 | + original_lines = {} |
| 73 | + else |
| 74 | + vim.notify("[codediff] " .. err, vim.log.levels.WARN) |
| 75 | + return |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + ensure_highlights() |
| 80 | + |
| 81 | + local modified_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 82 | + local config = require("codediff.config") |
| 83 | + local diff_result = require("codediff.core.diff").compute_diff(original_lines, modified_lines, { |
| 84 | + max_computation_time_ms = config.options.diff.max_computation_time_ms, |
| 85 | + ignore_trim_whitespace = config.options.diff.ignore_trim_whitespace, |
| 86 | + }) |
| 87 | + |
| 88 | + require("codediff.ui.inline").render_inline_diff(bufnr, diff_result, original_lines, modified_lines) |
| 89 | + |
| 90 | + inline_state[bufnr] = { active = true, base = base, git_root = git_root, rel_path = rel_path } |
| 91 | + end) |
| 92 | + end) |
| 93 | +end |
| 94 | + |
| 95 | +-- Clear inline diff decorations and remove state for a buffer |
| 96 | +function M.clear_buf(bufnr) |
| 97 | + local inline = require("codediff.ui.inline") |
| 98 | + inline.clear(bufnr) |
| 99 | + inline_state[bufnr] = nil |
| 100 | +end |
| 101 | + |
| 102 | +-- Toggle inline diff rendering |
| 103 | +-- @param show boolean|nil: true = show, false = hide, nil = toggle |
| 104 | +-- @param global boolean|nil: if true, applies to all git-tracked buffers |
| 105 | +function M.toggle(show, global) |
| 106 | + if global then |
| 107 | + -- Resolve toggle |
| 108 | + if show == nil then |
| 109 | + show = not global_state.enabled |
| 110 | + end |
| 111 | + if show then |
| 112 | + M.enable_global() |
| 113 | + else |
| 114 | + M.disable_global() |
| 115 | + end |
| 116 | + else |
| 117 | + local bufnr = vim.api.nvim_get_current_buf() |
| 118 | + -- Resolve toggle |
| 119 | + if show == nil then |
| 120 | + show = not (inline_state[bufnr] and inline_state[bufnr].active) |
| 121 | + end |
| 122 | + if show then |
| 123 | + M.render_buf(bufnr) |
| 124 | + else |
| 125 | + M.clear_buf(bufnr) |
| 126 | + end |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | +-- Change base revision for inline diff |
| 131 | +-- @param base string|nil: revision (e.g. "HEAD", "~1", "main"), nil = reset to index |
| 132 | +-- @param global boolean|nil: if true, applies to all buffers |
| 133 | +function M.change_base(base, global) |
| 134 | + base = base or DEFAULT_BASE |
| 135 | + |
| 136 | + if global then |
| 137 | + global_state.base = base |
| 138 | + for buf, state in pairs(inline_state) do |
| 139 | + if state.active and vim.api.nvim_buf_is_valid(buf) then |
| 140 | + M.render_buf(buf, base) |
| 141 | + end |
| 142 | + end |
| 143 | + else |
| 144 | + local bufnr = vim.api.nvim_get_current_buf() |
| 145 | + if inline_state[bufnr] and inline_state[bufnr].active then |
| 146 | + M.render_buf(bufnr, base) |
| 147 | + end |
| 148 | + end |
| 149 | +end |
| 150 | + |
| 151 | +-- Enable global mode: render inline diffs on all file buffers, auto-render on BufEnter |
| 152 | +function M.enable_global() |
| 153 | + if global_state.enabled then |
| 154 | + return |
| 155 | + end |
| 156 | + global_state.enabled = true |
| 157 | + |
| 158 | + local augroup = vim.api.nvim_create_augroup("codediff_inline_global", { clear = true }) |
| 159 | + global_state.augroup = augroup |
| 160 | + |
| 161 | + -- Render current buffer immediately |
| 162 | + local bufnr = vim.api.nvim_get_current_buf() |
| 163 | + if vim.bo[bufnr].buftype == "" then |
| 164 | + M.render_buf(bufnr) |
| 165 | + end |
| 166 | + |
| 167 | + vim.api.nvim_create_autocmd("BufEnter", { |
| 168 | + group = augroup, |
| 169 | + callback = function(args) |
| 170 | + if vim.bo[args.buf].buftype == "" and not (inline_state[args.buf] and inline_state[args.buf].active) then |
| 171 | + M.render_buf(args.buf) |
| 172 | + end |
| 173 | + end, |
| 174 | + }) |
| 175 | + |
| 176 | + vim.api.nvim_create_autocmd("BufWritePost", { |
| 177 | + group = augroup, |
| 178 | + callback = function(args) |
| 179 | + if inline_state[args.buf] and inline_state[args.buf].active then |
| 180 | + M.render_buf(args.buf) |
| 181 | + end |
| 182 | + end, |
| 183 | + }) |
| 184 | + |
| 185 | + vim.api.nvim_create_autocmd({ "BufDelete", "BufWipeout" }, { |
| 186 | + group = augroup, |
| 187 | + callback = function(args) |
| 188 | + inline_state[args.buf] = nil |
| 189 | + end, |
| 190 | + }) |
| 191 | + |
| 192 | + vim.api.nvim_create_autocmd("ColorScheme", { |
| 193 | + group = augroup, |
| 194 | + callback = function() |
| 195 | + highlights_initialized = false |
| 196 | + for buf, state in pairs(inline_state) do |
| 197 | + if state.active and vim.api.nvim_buf_is_valid(buf) then |
| 198 | + M.render_buf(buf) |
| 199 | + end |
| 200 | + end |
| 201 | + end, |
| 202 | + }) |
| 203 | +end |
| 204 | + |
| 205 | +-- Disable global mode: clear all inline diffs and remove autocmds |
| 206 | +function M.disable_global() |
| 207 | + if not global_state.enabled then |
| 208 | + return |
| 209 | + end |
| 210 | + |
| 211 | + local inline = require("codediff.ui.inline") |
| 212 | + for buf, _ in pairs(inline_state) do |
| 213 | + if vim.api.nvim_buf_is_valid(buf) then |
| 214 | + inline.clear(buf) |
| 215 | + end |
| 216 | + end |
| 217 | + inline_state = {} |
| 218 | + |
| 219 | + if global_state.augroup then |
| 220 | + vim.api.nvim_del_augroup_by_id(global_state.augroup) |
| 221 | + global_state.augroup = nil |
| 222 | + end |
| 223 | + |
| 224 | + global_state.enabled = false |
| 225 | +end |
| 226 | + |
| 227 | +return M |
0 commit comments