Skip to content

Commit

Permalink
refactor(live_signs): simplify live signs logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Apr 5, 2024
1 parent 7b7f27d commit df6d0ea
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 252 deletions.
130 changes: 42 additions & 88 deletions lua/vgit/features/buffer/LiveGutter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,128 +8,82 @@ local live_gutter_setting = require('vgit.settings.live_gutter')
local LiveGutter = Object:extend()

function LiveGutter:constructor()
return {
name = 'Live Gutter',
}
end

LiveGutter.clear = loop.coroutine(function(self, buffer)
loop.free_textlock()

if buffer:is_rendering() then
return self
end

buffer:sign_unplace()

return self
end)

function LiveGutter:reset()
local buffers = GitBuffer:list()

for i = 1, #buffers do
local buffer = buffers[i]

if buffer then
self:clear(buffer)
end
end
return { name = 'Live Gutter' }
end

LiveGutter.fetch = loop.debounce_coroutine(function(self, buffer)
loop.free_textlock()
if not buffer:is_valid() then
return self
end

loop.free_textlock()
local live_signs = buffer:get_cached_live_signs()
if not buffer:is_valid() then return self end

loop.free_textlock()
buffer:clear_cached_live_signs()
local err = buffer:live_hunks()

loop.free_textlock()
local err = buffer.git_object:live_hunks(buffer:get_lines())

if err then
loop.free_textlock()
buffer:set_cached_live_signs(live_signs)
console.debug.error(err)

return self
end

local hunks = buffer.git_object.hunks

if not hunks then
loop.free_textlock()
buffer:set_cached_live_signs(live_signs)

return self
else
local diff_status = buffer.git_object:generate_diff_status()

loop.free_textlock()
buffer:set_var('vgit_status', diff_status)
end

loop.free_textlock()
self:clear(buffer)

for i = 1, #hunks do
loop.free_textlock()
buffer:cache_live_sign(hunks[i])
end
buffer:sign_unplace()
buffer:generate_status()

return self
end, 50)
end, 10)

function LiveGutter:render(buffer, top, bot)
if not live_gutter_setting:get('enabled') then
return self
end
if not live_gutter_setting:get('enabled') then return self end

local hunks = buffer.git_object.hunks
if not hunks then return self end

if not hunks then
return self
local signs = {}
for i = top, bot do
signs[#signs + 1] = buffer.signs[i]
end

local cached_live_signs = buffer:get_cached_live_signs()
local gutter_signs = {}
buffer:sign_placelist(signs)

for i = top, bot do
gutter_signs[#gutter_signs + 1] = cached_live_signs[i]
return self
end

function LiveGutter:reset()
local buffers = GitBuffer:list()

for i = 1, #buffers do
local buffer = buffers[i]

if buffer then
buffer:sign_unplace()
end
end

buffer:sign_placelist(gutter_signs)
return self
end

function LiveGutter:register_events()
git_buffer_store
.attach('attach', function(git_buffer) self:fetch(git_buffer) end)
.attach('reload', function(git_buffer)
loop.free_textlock()
self:fetch(git_buffer)
.attach('attach', function(buffer)
self:fetch(buffer)
end)
.attach('change', function(git_buffer, p_lnum, n_lnum, byte_count)
if p_lnum == n_lnum and byte_count == 0 then
return
end
loop.free_textlock()
self:fetch(git_buffer)
.attach('reload', function(buffer)
self:fetch(buffer)
end)
.attach('change', function(buffer)
self:fetch(buffer)
end)
.attach('watch', function(git_buffer)
git_buffer:sync()
self:fetch(git_buffer)
.attach('watch', function(buffer)
buffer:sync()
self:fetch(buffer)
end)
.attach('git_watch', function(git_buffers)
for i = 1, #git_buffers do
self:fetch(git_buffers[i])
.attach('git_watch', function(buffers)
for i = 1, #buffers do
local buffer = buffers[i]
self:fetch(buffer)
end
end)
.attach('render', function(git_buffer, top, bot) self:render(git_buffer, top, bot) end)
.attach('render', function(buffer, top, bot)
self:render(buffer, top, bot)
end)

return self
end
Expand Down
93 changes: 32 additions & 61 deletions lua/vgit/git/GitBuffer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local loop = require('vgit.core.loop')
local Buffer = require('vgit.core.Buffer')
local GitObject = require('vgit.git.GitObject')
local signs_setting = require('vgit.settings.signs')
Expand All @@ -8,85 +7,57 @@ local GitBuffer = Buffer:extend()
function GitBuffer:sync()
Buffer.sync(self)

self.git_object = GitObject(self.filename)
self.live_signs = {}
self.signs = {}
self.is_processing = false
self.is_showing_lens = false
self.git_object = GitObject(self.filename)

return self
end

function GitBuffer:set_cached_live_signs(live_signs)
self.state.live_signs = live_signs

return self
end

function GitBuffer:get_cached_live_signs() return self.state.live_signs end

function GitBuffer:clear_cached_live_signs()
self.state.live_signs = {}

return self
function GitBuffer:is_ignored()
return self.git_object:is_ignored()
end

function GitBuffer:cache_live_sign(hunk)
local bufnr = self.bufnr
local live_signs = self:get_cached_live_signs()
local sign_priority = signs_setting:get('priority')
local sign_group = self.namespace:get_sign_ns_id(self)
local sign_types = signs_setting:get('usage').main

for j = hunk.top, hunk.bot do
local lnum = (hunk.type == 'remove' and j == 0) and 1 or j

live_signs[lnum] = {
id = lnum,
lnum = lnum,
buffer = bufnr,
group = sign_group,
name = sign_types[hunk.type],
priority = sign_priority,
}
end

return self
function GitBuffer:is_tracked()
return self.git_object:tracked_filename() ~= ''
end

function GitBuffer:is_inside_git_dir()
loop.free_textlock()
local is_inside_git_dir = self.git_object:is_inside_git_dir()
loop.free_textlock()

if not is_inside_git_dir then
return false
end

return true
return self.git_object:is_inside_git_dir()
end

function GitBuffer:is_ignored()
loop.free_textlock()
local is_ignored = self.git_object:is_ignored()
loop.free_textlock()
function GitBuffer:generate_status()
return self:set_var('vgit_status', self.git_object:generate_status())
end

if is_ignored then
return true
end
function GitBuffer:live_hunks()
local lines = self:get_lines()
local err, hunks = self.git_object:live_hunks(lines)

return false
end
if err then return err end

function GitBuffer:is_tracked()
loop.free_textlock()
local tracked_filename = self.git_object:tracked_filename()
loop.free_textlock()
local sign_types = signs_setting:get('usage').main
local sign_priority = signs_setting:get('priority')
local sign_group = self.namespace:get_sign_ns_id(self)

if tracked_filename == '' then
return false
self.signs = {}
for i = 1, #hunks do
local hunk = hunks[i]
for j = hunk.top, hunk.bot do
local lnum = (hunk.type == 'remove' and j == 0) and 1 or j
self.signs[lnum] = {
id = lnum,
lnum = lnum,
buffer = self.bufnr,
group = sign_group,
name = sign_types[hunk.type],
priority = sign_priority,
}
end
end

return true
return nil, hunks
end

return GitBuffer
Loading

0 comments on commit df6d0ea

Please sign in to comment.