fix: wrong data displayed by diff component (#117)

Sometimes async process outputs partial data. Previously that data was
processed imediatly causing wrong results. Now the data is processed
after async processes finishes
This commit is contained in:
Shadman 2021-02-24 23:03:24 +06:00 committed by GitHub
parent f47f68cee6
commit ec47bb0447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 10 deletions

View File

@ -7,6 +7,8 @@ local highlight = require"lualine.highlight"
-- variable to store git diff stats -- variable to store git diff stats
local git_diff = nil local git_diff = nil
-- accumulates async output to process in the end
local diff_data = ''
-- process diff data and update git_diff{ added, removed, modified} -- process diff data and update git_diff{ added, removed, modified}
local function process_diff(data) local function process_diff(data)
@ -37,8 +39,6 @@ end
-- variable to store git_diff getter async function -- variable to store git_diff getter async function
local get_git_diff = nil local get_git_diff = nil
-- flag to see if async job exited before updating git_diff
local updated = false
-- Updates the async function for current file -- Updates the async function for current file
local function update_git_diff_getter() local function update_git_diff_getter()
@ -51,22 +51,20 @@ local function update_git_diff_getter()
,vim.fn.expand('%:h'), vim.fn.expand('%:t')), ,vim.fn.expand('%:h'), vim.fn.expand('%:t')),
on_stdout = function(_, data) on_stdout = function(_, data)
if data then if data then
process_diff(data) diff_data = diff_data .. data
updated = true
end end
end, end,
on_stderr = function (_, data) on_stderr = function (_, data)
if data then if data then
git_diff = nil git_diff = nil
updated = true diff_data = ''
end end
end, end,
on_exit = function() on_exit = function()
if not updated then if diff_data ~= '' then
-- updated not set means git exited without emmiting anything on stdout process_diff(diff_data)
-- or stderr means file is unchanged else
git_diff = {0, 0, 0} git_diff = {0, 0, 0}
updated = true
end end
end end
}) })
@ -76,7 +74,7 @@ end
local function update_git_diff() local function update_git_diff()
vim.schedule_wrap(function() vim.schedule_wrap(function()
if get_git_diff then if get_git_diff then
updated = false diff_data = ''
get_git_diff:start() get_git_diff:start()
end end
end)() end)()