| 1 |
local lualine_require = require('lualine_require') |
| 2 |
local modules = lualine_require.lazy_require { |
| 3 |
utils = 'lualine.utils.utils', |
| 4 |
Job = 'lualine.utils.job', |
| 5 |
} |
| 6 |
|
| 7 |
local M = {} |
| 8 |
|
| 9 |
-- Vars |
| 10 |
-- variable to store git diff stats |
| 11 |
local git_diff = nil |
| 12 |
-- accumulates output from diff process |
| 13 |
local diff_output_cache = {} |
| 14 |
-- variable to store git_diff job |
| 15 |
local diff_job = nil |
| 16 |
|
| 17 |
local active_bufnr = '0' |
| 18 |
local diff_cache = {} -- Stores last known value of diff of a buffer |
| 19 |
|
| 20 |
---initialize the module |
| 21 |
---param opts table |
| 22 |
function M.init(opts) |
| 23 |
if type(opts.source) == 'function' then |
| 24 |
M.src = opts.source |
| 25 |
else |
| 26 |
modules.utils.define_autocmd('BufEnter', "lua require'lualine.components.diff.git_diff'.update_diff_args()") |
| 27 |
modules.utils.define_autocmd('BufWritePost', "lua require'lualine.components.diff.git_diff'.update_git_diff()") |
| 28 |
M.update_diff_args() |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
---Api to get git sign count |
| 33 |
---scheme : |
| 34 |
---{ |
| 35 |
--- added = added_count, |
| 36 |
--- modified = modified_count, |
| 37 |
--- removed = removed_count, |
| 38 |
---} |
| 39 |
---error_code = { added = -1, modified = -1, removed = -1 } |
| 40 |
---@param bufnr number|nil |
| 41 |
function M.get_sign_count(bufnr) |
| 42 |
if bufnr then |
| 43 |
return diff_cache[bufnr] |
| 44 |
end |
| 45 |
if M.src then |
| 46 |
git_diff = M.src() |
| 47 |
diff_cache[vim.api.nvim_get_current_buf()] = git_diff |
| 48 |
elseif vim.g.actual_curbuf ~= nil and active_bufnr ~= vim.g.actual_curbuf then |
| 49 |
-- Workaround for https://github.com/nvim-lualine/lualine.nvim/issues/286 |
| 50 |
-- See upstream issue https://github.com/neovim/neovim/issues/15300 |
| 51 |
-- Diff is out of sync re sync it. |
| 52 |
M.update_diff_args() |
| 53 |
end |
| 54 |
return git_diff |
| 55 |
end |
| 56 |
|
| 57 |
---process diff data and update git_diff{ added, removed, modified } |
| 58 |
---@param data string output on stdout od git diff job |
| 59 |
local function process_diff(data) |
| 60 |
-- Adapted from https://github.com/wbthomason/nvim-vcs.lua |
| 61 |
local added, removed, modified = 0, 0, 0 |
| 62 |
for _, line in ipairs(data) do |
| 63 |
if string.find(line, [[^@@ ]]) then |
| 64 |
local tokens = vim.fn.matchlist(line, [[^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)]]) |
| 65 |
local line_stats = { |
| 66 |
mod_count = tokens[3] == nil and 0 or tokens[3] == '' and 1 or tonumber(tokens[3]), |
| 67 |
new_count = tokens[5] == nil and 0 or tokens[5] == '' and 1 or tonumber(tokens[5]), |
| 68 |
} |
| 69 |
|
| 70 |
if line_stats.mod_count == 0 and line_stats.new_count > 0 then |
| 71 |
added = added + line_stats.new_count |
| 72 |
elseif line_stats.mod_count > 0 and line_stats.new_count == 0 then |
| 73 |
removed = removed + line_stats.mod_count |
| 74 |
else |
| 75 |
local min = math.min(line_stats.mod_count, line_stats.new_count) |
| 76 |
modified = modified + min |
| 77 |
added = added + line_stats.new_count - min |
| 78 |
removed = removed + line_stats.mod_count - min |
| 79 |
end |
| 80 |
end |
| 81 |
end |
| 82 |
git_diff = { added = added, modified = modified, removed = removed } |
| 83 |
end |
| 84 |
|
| 85 |
---updates the job args |
| 86 |
function M.update_diff_args() |
| 87 |
-- Don't show git diff when current buffer doesn't have a filename |
| 88 |
active_bufnr = tostring(vim.api.nvim_get_current_buf()) |
| 89 |
if #vim.fn.expand('%') == 0 or vim.fn.executable('git') == 0 then |
| 90 |
M.diff_args = nil |
| 91 |
git_diff = nil |
| 92 |
return |
| 93 |
end |
| 94 |
M.diff_args = { |
| 95 |
cmd = { |
| 96 |
'git', |
| 97 |
'-C', |
| 98 |
vim.fn.expand('%:h'), |
| 99 |
'--no-pager', |
| 100 |
'diff', |
| 101 |
'--no-color', |
| 102 |
'--no-ext-diff', |
| 103 |
'-U0', |
| 104 |
'--', |
| 105 |
vim.fn.expand('%:t'), |
| 106 |
}, |
| 107 |
on_stdout = function(_, data) |
| 108 |
if next(data) then |
| 109 |
diff_output_cache = vim.list_extend(diff_output_cache, data) |
| 110 |
end |
| 111 |
end, |
| 112 |
on_stderr = function(_, data) |
| 113 |
data = table.concat(data, '\n') |
| 114 |
if #data > 0 then |
| 115 |
git_diff = nil |
| 116 |
diff_output_cache = {} |
| 117 |
end |
| 118 |
end, |
| 119 |
on_exit = function() |
| 120 |
if #diff_output_cache > 0 then |
| 121 |
process_diff(diff_output_cache) |
| 122 |
else |
| 123 |
git_diff = { added = 0, modified = 0, removed = 0 } |
| 124 |
end |
| 125 |
diff_cache[vim.api.nvim_get_current_buf()] = git_diff |
| 126 |
end, |
| 127 |
} |
| 128 |
M.update_git_diff() |
| 129 |
end |
| 130 |
|
| 131 |
---update git_diff variable |
| 132 |
function M.update_git_diff() |
| 133 |
if M.diff_args then |
| 134 |
diff_output_cache = {} |
| 135 |
if diff_job then |
| 136 |
diff_job:stop() |
| 137 |
end |
| 138 |
diff_job = modules.Job(M.diff_args) |
| 139 |
if diff_job then |
| 140 |
diff_job:start() |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
return M |