2021-02-23 12:54:04 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-09-03 18:28:20 +00:00
|
|
|
local lualine_require = require 'lualine_require'
|
|
|
|
local modules = lualine_require.lazy_require {
|
|
|
|
utils = 'lualine.utils.utils',
|
2021-08-30 11:54:23 +00:00
|
|
|
utils_notices = 'lualine.utils.notices',
|
2021-09-03 18:28:20 +00:00
|
|
|
highlight = 'lualine.highlight',
|
|
|
|
Job = 'lualine.utils.job',
|
2021-08-30 11:54:23 +00:00
|
|
|
}
|
|
|
|
local Diff = lualine_require.require('lualine.component'):new()
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-09-14 15:14:23 +00:00
|
|
|
local function check_deprecated_options(options)
|
|
|
|
if options.color_added or options.color_modified or options.color_removed then
|
|
|
|
options.diagnostics_color = options.diagnostics_color or {}
|
|
|
|
require('lualine.utils.notices').add_notice(string.format [[
|
|
|
|
### diff.options.colors
|
|
|
|
Previously colors in diff section was set with color_added, color_modified..
|
|
|
|
separate options . They've been unified under diff_color option.
|
|
|
|
Now it should be something like:
|
|
|
|
```lua
|
|
|
|
{ 'diff',
|
|
|
|
diff_color = {
|
|
|
|
added = color_added,
|
|
|
|
modified = color_modified,
|
|
|
|
removed = color_removed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
]])
|
|
|
|
options.diff_color.added = options.color_added
|
|
|
|
options.diff_color.modified = options.color_modified
|
|
|
|
options.diff_color.removed = options.color_removed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Vars
|
2021-02-23 12:54:04 +00:00
|
|
|
-- variable to store git diff stats
|
2021-04-11 08:20:41 +00:00
|
|
|
Diff.git_diff = nil
|
2021-08-08 08:47:20 +00:00
|
|
|
-- accumulates output from diff process
|
|
|
|
Diff.diff_output_cache = {}
|
|
|
|
-- variable to store git_diff job
|
|
|
|
Diff.diff_job = nil
|
2021-08-09 09:04:15 +00:00
|
|
|
Diff.active_bufnr = '0'
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-08-08 16:03:58 +00:00
|
|
|
local diff_cache = {} -- Stores last known value of diff of a buffer
|
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
local default_options = {
|
|
|
|
colored = true,
|
2021-09-03 18:28:20 +00:00
|
|
|
symbols = { added = '+', modified = '~', removed = '-' },
|
2021-09-14 15:14:23 +00:00
|
|
|
diff_color = {
|
|
|
|
added = {
|
|
|
|
fg = modules.utils.extract_highlight_colors('DiffAdd', 'fg') or '#f0e130',
|
|
|
|
},
|
|
|
|
modified = {
|
|
|
|
fg = modules.utils.extract_highlight_colors('DiffChange', 'fg') or '#ff0038',
|
|
|
|
},
|
|
|
|
removed = {
|
|
|
|
fg = modules.utils.extract_highlight_colors('DiffDelete', 'fg') or '#ff0038',
|
|
|
|
},
|
2021-09-03 12:04:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Initializer
|
|
|
|
Diff.new = function(self, options, child)
|
2021-04-20 03:02:21 +00:00
|
|
|
local new_instance = self._parent:new(options, child or Diff)
|
2021-09-03 18:28:20 +00:00
|
|
|
new_instance.options = vim.tbl_deep_extend('keep', new_instance.options or {}, default_options)
|
2021-09-14 15:14:23 +00:00
|
|
|
check_deprecated_options(new_instance.options)
|
2021-04-11 08:20:41 +00:00
|
|
|
-- create highlights and save highlight_name in highlights table
|
2021-04-20 03:02:21 +00:00
|
|
|
if new_instance.options.colored then
|
|
|
|
new_instance.highlights = {
|
2021-08-30 11:54:23 +00:00
|
|
|
added = modules.highlight.create_component_highlight_group(
|
2021-09-14 15:14:23 +00:00
|
|
|
new_instance.options.diff_color.added,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diff_added',
|
|
|
|
new_instance.options
|
|
|
|
),
|
2021-08-30 11:54:23 +00:00
|
|
|
modified = modules.highlight.create_component_highlight_group(
|
2021-09-14 15:14:23 +00:00
|
|
|
new_instance.options.diff_color.modified,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diff_modified',
|
|
|
|
new_instance.options
|
|
|
|
),
|
2021-08-30 11:54:23 +00:00
|
|
|
removed = modules.highlight.create_component_highlight_group(
|
2021-09-14 15:14:23 +00:00
|
|
|
new_instance.options.diff_color.removed,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diff_removed',
|
|
|
|
new_instance.options
|
|
|
|
),
|
2021-04-11 08:20:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-08-30 06:03:07 +00:00
|
|
|
Diff.diff_checker_enabled = type(new_instance.options.source) ~= 'function'
|
|
|
|
|
|
|
|
if Diff.diff_checker_enabled then
|
2021-08-03 11:41:37 +00:00
|
|
|
-- setup internal source
|
2021-09-06 12:14:14 +00:00
|
|
|
modules.utils.define_autocmd('BufEnter', "lua require'lualine.components.diff'.update_diff_args()")
|
2021-08-30 11:54:23 +00:00
|
|
|
modules.utils.define_autocmd('BufWritePost', "lua require'lualine.components.diff'.update_git_diff()")
|
2021-08-30 06:03:07 +00:00
|
|
|
Diff.update_diff_args()
|
2021-08-03 11:41:37 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-04-20 03:02:21 +00:00
|
|
|
return new_instance
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Function that runs everytime statusline is updated
|
2021-08-08 16:03:58 +00:00
|
|
|
Diff.update_status = function(self, is_focused)
|
2021-08-30 06:03:07 +00:00
|
|
|
local git_diff
|
|
|
|
if Diff.diff_checker_enabled then
|
|
|
|
if Diff.active_bufnr ~= vim.g.actual_curbuf then
|
2021-09-06 12:14:14 +00:00
|
|
|
-- Workaround for https://github.com/hoob3rt/lualine.nvim/issues/286
|
|
|
|
-- See upstream issue https://github.com/neovim/neovim/issues/15300
|
|
|
|
-- Diff is out of sync re sync it.
|
2021-08-30 06:03:07 +00:00
|
|
|
Diff.update_diff_args()
|
|
|
|
end
|
|
|
|
git_diff = Diff.git_diff
|
|
|
|
else
|
2021-08-08 16:03:58 +00:00
|
|
|
git_diff = self.options.source()
|
2021-08-03 11:41:37 +00:00
|
|
|
end
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
if not is_focused then
|
|
|
|
git_diff = diff_cache[vim.fn.bufnr()] or {}
|
|
|
|
end
|
|
|
|
if git_diff == nil then
|
|
|
|
return ''
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
|
|
|
local colors = {}
|
|
|
|
if self.options.colored then
|
|
|
|
-- load the highlights and store them in colors table
|
|
|
|
for name, highlight_name in pairs(self.highlights) do
|
2021-08-30 11:54:23 +00:00
|
|
|
colors[name] = modules.highlight.component_format_highlight(highlight_name)
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = {}
|
|
|
|
-- loop though data and load available sections in result table
|
2021-09-03 18:28:20 +00:00
|
|
|
for _, name in ipairs { 'added', 'modified', 'removed' } do
|
2021-08-08 16:03:58 +00:00
|
|
|
if git_diff[name] and git_diff[name] > 0 then
|
2021-04-11 08:20:41 +00:00
|
|
|
if self.options.colored then
|
2021-09-03 18:28:20 +00:00
|
|
|
table.insert(result, colors[name] .. self.options.symbols[name] .. git_diff[name])
|
2021-04-11 08:20:41 +00:00
|
|
|
else
|
2021-08-08 16:03:58 +00:00
|
|
|
table.insert(result, self.options.symbols[name] .. git_diff[name])
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #result > 0 then
|
|
|
|
return table.concat(result, ' ')
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Api to get git sign count
|
|
|
|
-- scheme :
|
|
|
|
-- {
|
|
|
|
-- added = added_count,
|
|
|
|
-- modified = modified_count,
|
|
|
|
-- removed = removed_count,
|
|
|
|
-- }
|
|
|
|
-- error_code = { added = -1, modified = -1, removed = -1 }
|
|
|
|
function Diff.get_sign_count()
|
2021-09-03 18:28:20 +00:00
|
|
|
if Diff.diff_checker_enabled then
|
|
|
|
Diff.update_diff_args()
|
|
|
|
end
|
|
|
|
return Diff.git_diff or { added = -1, modified = -1, removed = -1 }
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-02-23 12:54:04 +00:00
|
|
|
|
2021-03-02 00:10:23 +00:00
|
|
|
-- process diff data and update git_diff{ added, removed, modified }
|
2021-04-11 08:20:41 +00:00
|
|
|
function Diff.process_diff(data)
|
2021-02-23 12:54:04 +00:00
|
|
|
-- Adapted from https://github.com/wbthomason/nvim-vcs.lua
|
|
|
|
local added, removed, modified = 0, 0, 0
|
2021-08-08 08:47:20 +00:00
|
|
|
for _, line in ipairs(data) do
|
2021-02-23 12:54:04 +00:00
|
|
|
if string.find(line, [[^@@ ]]) then
|
2021-09-03 18:28:20 +00:00
|
|
|
local tokens = vim.fn.matchlist(line, [[^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)]])
|
2021-02-23 12:54:04 +00:00
|
|
|
local line_stats = {
|
2021-10-02 09:04:14 +00:00
|
|
|
mod_count = tokens[3] == nil and 0 or tokens[3] == '' and 1 or tonumber(tokens[3]),
|
|
|
|
new_count = tokens[5] == nil and 0 or tokens[5] == '' and 1 or tonumber(tokens[5]),
|
2021-02-23 12:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if line_stats.mod_count == 0 and line_stats.new_count > 0 then
|
|
|
|
added = added + line_stats.new_count
|
|
|
|
elseif line_stats.mod_count > 0 and line_stats.new_count == 0 then
|
|
|
|
removed = removed + line_stats.mod_count
|
|
|
|
else
|
|
|
|
local min = math.min(line_stats.mod_count, line_stats.new_count)
|
|
|
|
modified = modified + min
|
2021-03-15 23:37:46 +00:00
|
|
|
added = added + line_stats.new_count - min
|
2021-02-23 12:54:04 +00:00
|
|
|
removed = removed + line_stats.mod_count - min
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-09-03 18:28:20 +00:00
|
|
|
Diff.git_diff = { added = added, modified = modified, removed = removed }
|
2021-02-23 12:54:04 +00:00
|
|
|
end
|
|
|
|
|
2021-08-08 08:47:20 +00:00
|
|
|
-- Updates the job args
|
|
|
|
function Diff.update_diff_args()
|
2021-02-23 12:54:04 +00:00
|
|
|
-- Donn't show git diff when current buffer doesn't have a filename
|
2021-08-09 09:04:15 +00:00
|
|
|
Diff.active_bufnr = tostring(vim.fn.bufnr())
|
2021-09-03 18:28:20 +00:00
|
|
|
if #vim.fn.expand '%' == 0 then
|
|
|
|
Diff.diff_args = nil
|
|
|
|
Diff.git_diff = nil
|
2021-03-15 23:37:46 +00:00
|
|
|
return
|
|
|
|
end
|
2021-08-08 08:47:20 +00:00
|
|
|
Diff.diff_args = {
|
2021-03-15 23:37:46 +00:00
|
|
|
cmd = string.format(
|
2021-09-03 18:28:20 +00:00
|
|
|
[[git -C %s --no-pager diff --no-color --no-ext-diff -U0 -- %s]],
|
|
|
|
vim.fn.expand '%:h',
|
|
|
|
vim.fn.expand '%:t'
|
|
|
|
),
|
2021-02-23 12:54:04 +00:00
|
|
|
on_stdout = function(_, data)
|
2021-08-08 08:47:20 +00:00
|
|
|
if next(data) then
|
|
|
|
Diff.diff_output_cache = vim.list_extend(Diff.diff_output_cache, data)
|
|
|
|
end
|
2021-02-23 12:54:04 +00:00
|
|
|
end,
|
2021-03-15 23:37:46 +00:00
|
|
|
on_stderr = function(_, data)
|
2021-08-08 08:47:20 +00:00
|
|
|
data = table.concat(data, '\n')
|
|
|
|
if #data > 1 or (#data == 1 and #data[1] > 0) then
|
2021-04-11 08:20:41 +00:00
|
|
|
Diff.git_diff = nil
|
2021-08-08 08:47:20 +00:00
|
|
|
Diff.diff_output_cache = {}
|
2021-02-23 12:54:04 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_exit = function()
|
2021-08-08 08:47:20 +00:00
|
|
|
if #Diff.diff_output_cache > 0 then
|
|
|
|
Diff.process_diff(Diff.diff_output_cache)
|
2021-02-24 17:03:24 +00:00
|
|
|
else
|
2021-09-03 18:28:20 +00:00
|
|
|
Diff.git_diff = { added = 0, modified = 0, removed = 0 }
|
2021-02-23 12:54:04 +00:00
|
|
|
end
|
2021-08-08 16:03:58 +00:00
|
|
|
diff_cache[vim.fn.bufnr()] = Diff.git_diff
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-08-08 08:47:20 +00:00
|
|
|
}
|
2021-08-03 06:19:12 +00:00
|
|
|
Diff.update_git_diff()
|
2021-02-23 12:54:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Update git_diff veriable
|
2021-04-11 08:20:41 +00:00
|
|
|
function Diff.update_git_diff()
|
2021-08-08 08:47:20 +00:00
|
|
|
if Diff.diff_args then
|
|
|
|
Diff.diff_output_cache = {}
|
2021-09-03 18:28:20 +00:00
|
|
|
if Diff.diff_job then
|
|
|
|
Diff.diff_job:stop()
|
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
Diff.diff_job = modules.Job(Diff.diff_args)
|
2021-09-03 18:28:20 +00:00
|
|
|
if Diff.diff_job then
|
|
|
|
Diff.diff_job:start()
|
|
|
|
end
|
2021-08-03 06:19:12 +00:00
|
|
|
end
|
2021-02-23 12:54:04 +00:00
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
return Diff
|