Skip to content

Commit

Permalink
chore(tests): add unit test for Buffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 13, 2024
1 parent cfd4e0f commit 588af83
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 106 deletions.
52 changes: 9 additions & 43 deletions lua/vgit/core/Buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ function Buffer:on_render(top, bot)
return self
end

function Buffer:is_rendering()
return self.rendering
end

function Buffer:is_in_disk()
return fs.exists(self:get_name())
end
Expand Down Expand Up @@ -112,34 +108,22 @@ function Buffer:get_name()
end

function Buffer:add_highlight(opts)
self.namespace:add_highlight(self, {
return self.namespace:add_highlight(self, {
hl = opts.hl,
row = opts.row,
col_range = {
from = opts.col_range.from,
to = opts.col_range.to,
},
})

return self
end

function Buffer:add_pattern_highlight(pattern, hl)
self.namespace:add_pattern_highlight(self, pattern, hl)

return self
end

function Buffer:clear_highlight(row_range)
self.namespace:clear(self, row_range)

return self
return self.namespace:add_pattern_highlight(self, pattern, hl)
end

function Buffer:sign_place(lnum, sign_name)
self.namespace:sign_place(self, lnum, sign_name)

return self
return self.namespace:sign_place(self, lnum, sign_name)
end

function Buffer:sign_placelist(signs)
Expand All @@ -149,60 +133,48 @@ function Buffer:sign_placelist(signs)
end

function Buffer:sign_unplace()
self.namespace:sign_unplace(self)

return self
return self.namespace:sign_unplace(self)
end

function Buffer:transpose_virtual_text(opts)
self.namespace:transpose_virtual_text(self, {
return self.namespace:transpose_virtual_text(self, {
text = opts.text,
hl = opts.hl,
row = opts.row,
col = opts.col,
pos = opts.pos,
priority = opts.priority,
})

return self
end

function Buffer:transpose_virtual_line(opts)
self.namespace:transpose_virtual_line(self, {
return self.namespace:transpose_virtual_line(self, {
texts = opts.texts,
row = opts.row,
pos = opts.pos,
priority = opts.priority,
})

return self
end

function Buffer:transpose_virtual_line_number(opts)
self.namespace:transpose_virtual_line_number(self, {
return self.namespace:transpose_virtual_line_number(self, {
row = opts.row,
hl = opts.hl,
text = opts.text,
})

return self
end

function Buffer:insert_virtual_line(opts)
self.namespace:insert_virtual_line(self, {
return self.namespace:insert_virtual_line(self, {
text = opts.text,
hl = opts.hl,
row = opts.row,
priority = opts.priority,
})

return self
end

function Buffer:clear_namespace()
self.namespace:clear(self)

return self
return self.namespace:clear(self)
end

function Buffer:create(listed, scratch)
Expand Down Expand Up @@ -293,12 +265,6 @@ function Buffer:editing()
return self:get_option('modified')
end

function Buffer:update()
return self:call(function()
vim.cmd('update')
end)
end

function Buffer:filetype()
return fs.detect_filetype(self:get_name())
end
Expand Down
25 changes: 12 additions & 13 deletions lua/vgit/core/Namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,40 @@ function Namespace:add_highlight(buffer, opts)
local row = opts.row
local col_range = opts.col_range

return pcall(vim.api.nvim_buf_add_highlight, buffer.bufnr, self.ns_id, hl, row, col_range.from, col_range.to)
return pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, col_range.from, {
end_col = col_range.to,
hl_group = hl,
})
end

function Namespace:add_pattern_highlight(buffer, pattern, hl)
local lines = buffer:get_lines()

local err
local is_successful = true
local result = {}

local lines = buffer:get_lines()
for i = 1, #lines do
local line = lines[i]

local j = 0
while true do
local from, to = line:find(pattern, j + 1)
j = from

if from == nil then break end
local success, hl_err = self:add_highlight(buffer, {

local ok, value = self:add_highlight(buffer, {
hl = hl,
row = i - 1,
col_range = {
from = from - 1,
to = to,
},
})
if not ok then return false, value end

if not success then
err = hl_err
is_successful = false
end
j = from
result[#result + 1] = value
end
end

return is_successful, err
return true, result
end

function Namespace:transpose_virtual_text(buffer, opts)
Expand Down
6 changes: 0 additions & 6 deletions lua/vgit/ui/Component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ function Component:add_pattern_highlight(pattern, hl)
return self
end

function Component:clear_highlight(row_range)
self.buffer:clear_highlight(row_range)

return self
end

function Component:sign_place(lnum, sign_name)
self.buffer:sign_place(lnum, sign_name)

Expand Down
3 changes: 2 additions & 1 deletion lua/vgit/ui/components/FoldableListComponent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ function FoldableListComponent:paint()
end

function FoldableListComponent:sync()
self.buffer:clear_namespace():set_lines(self:generate_lines())
self.buffer:clear_namespace()
self.buffer:set_lines(self:generate_lines())

loop.free_textlock()
self:paint()
Expand Down
Loading

0 comments on commit 588af83

Please sign in to comment.