Skip to content

Commit 2e9c1cb

Browse files
committed
chore: refactor
1 parent bdabccc commit 2e9c1cb

42 files changed

Lines changed: 238 additions & 477 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lua/vgit/core/Window.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ end
6060

6161
function Window:get_cursor()
6262
local ok, cursor = pcall(vim.api.nvim_win_get_cursor, self.win_id)
63-
if not ok or not cursor then return { 1, 1 } end
63+
if not ok or not cursor then return { 1, 0 } end
6464

6565
return cursor
6666
end

lua/vgit/core/Window_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ describe('Window:', function()
8383
assert.are.equal(2, #cursor)
8484
end)
8585

86-
it('should return {1, 1} if get_cursor fails', function()
86+
it('should return {1, 0} if get_cursor fails', function()
8787
win = Window(-1) -- Invalid window
8888

8989
local cursor = win:get_cursor()
90-
eq({ 1, 1 }, cursor)
90+
eq({ 1, 0 }, cursor)
9191
end)
9292
end)
9393

lua/vgit/features/lenses/BlameLensView_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ describe('BlameLensView:', function()
128128
assert.is_nil(view._blame_info_component)
129129
assert.is_nil(view._diff_component)
130130
assert.is_false(view._destroyed)
131-
assert.is_false(view._is_destroying)
132131
end)
133132
end)
134133

lua/vgit/features/lenses/HunkLensView_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ describe('HunkLensView:', function()
7272
local view = HunkLensView()
7373
assert.is_nil(view.diff_component)
7474
assert.is_false(view._destroyed)
75-
assert.is_false(view._is_destroying)
7675
end)
7776
end)
7877

lua/vgit/features/screens/CommitPickerView_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ describe('CommitPickerView:', function()
472472
view:create(make_data())
473473

474474
view._destroyed = false
475-
view._is_destroying = false
475+
476476
view:destroy()
477477
assert.is_true(view._destroyed)
478478

@@ -487,7 +487,7 @@ describe('CommitPickerView:', function()
487487
view:create(make_data())
488488

489489
view._destroyed = false
490-
view._is_destroying = false
490+
491491
view:destroy()
492492
assert.is_true(view._destroyed)
493493
end)

lua/vgit/features/screens/ProjectDiffView_spec.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ describe('ProjectDiffView:', function()
477477
view._component_group = { unmount = function() end }
478478
view._context = { restore_window_options = function() end }
479479
view._destroyed = false
480-
view._is_destroying = false
481480

482481
view:destroy()
483482
view:destroy() -- second call should not error
@@ -489,7 +488,6 @@ describe('ProjectDiffView:', function()
489488
view._component_group = { unmount = function() end }
490489
view._context = { restore_window_options = function() end }
491490
view._destroyed = false
492-
view._is_destroying = false
493491

494492
assert.is_false(view._destroyed)
495493
view:destroy()
@@ -501,7 +499,6 @@ describe('ProjectDiffView:', function()
501499
view._component_group = { unmount = function() end }
502500
view._context = { restore_window_options = function() end }
503501
view._destroyed = false
504-
view._is_destroying = false
505502

506503
local gen_before = view._update_gen
507504
view:destroy()
@@ -523,7 +520,6 @@ describe('ProjectDiffView:', function()
523520
view._component_group = { unmount = function() end }
524521
view._context = { restore_window_options = function() end }
525522
view._destroyed = false
526-
view._is_destroying = false
527523

528524
view:destroy()
529525
eq(1, cleanup1_calls)

lua/vgit/features/screens/StatusDiffView_spec.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,10 +1882,6 @@ describe('StatusDiffView:', function()
18821882
it('should initialize destroyed as false', function()
18831883
assert.is_false(view._destroyed)
18841884
end)
1885-
1886-
it('should initialize is_destroying as false', function()
1887-
assert.is_false(view._is_destroying)
1888-
end)
18891885
end)
18901886

18911887
describe('destroy', function()

lua/vgit/ui/Component.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ local function CreateComponent(config)
163163
self._element:unmount()
164164
self._element = nil
165165
end
166+
if config.viewport then
167+
self._renderer_attached = false
168+
self._viewport_dirty = true
169+
self._last_top = nil
170+
self._last_bot = nil
171+
end
166172
self._mounted = false
167173
end
168174
elseif has_elements then
@@ -179,11 +185,10 @@ local function CreateComponent(config)
179185
function Class:unmount()
180186
if not self._mounted then return end
181187
if config.on_unmount then config.on_unmount(self) end
182-
if self.children then
183-
for _, child in pairs(self.children) do
184-
if child.unmount then child:unmount() end
185-
end
188+
for _, child in pairs(self.children) do
189+
if child.unmount then child:unmount() end
186190
end
191+
self.children = {}
187192
self._mounted = false
188193
end
189194
end

lua/vgit/ui/DiffViewportComponent.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local create_component_element = require('vgit.ui.create_component_element')
77
local element_delegation = require('vgit.ui.element_delegation')
88
local define_single_element_methods = element_delegation.define_single_element_methods
99
local define_getters_with_fallbacks = element_delegation.define_getters_with_fallbacks
10+
local clean_nil_values = require('vgit.ui.Component').clean_nil_values
1011

1112
local DiffViewportComponent = ViewportComponent:extend()
1213

@@ -55,8 +56,7 @@ function DiffViewportComponent:set_props(updates, callback)
5556
local prev_props = utils.object.clone(self.props)
5657
self.props = utils.object.extend(self.props, updates)
5758

58-
local Component = require('vgit.ui.Component')
59-
Component.clean_nil_values(self.props)
59+
clean_nil_values(self.props)
6060

6161
if self._mounted then self:on_props(prev_props) end
6262

@@ -103,7 +103,7 @@ define_single_element_methods(DiffViewportComponent, {
103103
-- (viewport rendering runs hot and cannot nil-check every frame)
104104
define_getters_with_fallbacks(DiffViewportComponent, {
105105
get_lnum = 1,
106-
get_cursor = { 1, 1 },
106+
get_cursor = { 1, 0 },
107107
get_line_count = 0,
108108
get_width = 0,
109109
get_height = 0,

lua/vgit/ui/Extmark.lua

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ function Extmark:highlight_pattern(opts)
5353
local extmark_opts = {
5454
end_col = to,
5555
hl_group = hl,
56+
priority = priority,
5657
}
57-
if priority then extmark_opts.priority = priority end
5858
local ok, value = pcall(vim.api.nvim_buf_set_extmark, self.bufnr, self.ns_id, row, from - 1, extmark_opts)
5959
if not ok then return false, value end
60-
j = from
60+
j = to
61+
if to < from then break end
6162
result[#result + 1] = value
6263
end
6364
else
@@ -71,11 +72,12 @@ function Extmark:highlight_pattern(opts)
7172
local extmark_opts = {
7273
end_col = to,
7374
hl_group = hl,
75+
priority = priority,
7476
}
75-
if priority then extmark_opts.priority = priority end
7677
local ok, value = pcall(vim.api.nvim_buf_set_extmark, self.bufnr, self.ns_id, i - 1, from - 1, extmark_opts)
7778
if not ok then return false, value end
78-
j = from
79+
j = to
80+
if to < from then break end
7981
result[#result + 1] = value
8082
end
8183
end
@@ -101,13 +103,11 @@ function Extmark:highlight_range(opts)
101103
local col_range = opts.col_range
102104
local priority = opts.priority
103105

104-
local extmark_opts = {
106+
return pcall(vim.api.nvim_buf_set_extmark, self.bufnr, self.ns_id, row, col_range.from, {
105107
end_col = col_range.to,
106108
hl_group = hl,
107-
}
108-
if priority then extmark_opts.priority = priority end
109-
110-
return pcall(vim.api.nvim_buf_set_extmark, self.bufnr, self.ns_id, row, col_range.from, extmark_opts)
109+
priority = priority,
110+
})
111111
end
112112

113113
function Extmark:highlight(opts)
@@ -160,6 +160,7 @@ function Extmark:sign(opts)
160160

161161
local id = self:derive_id(row)
162162
local sign_definition = get_sign_definition(name)
163+
if not sign_definition then return false, 'unknown sign: ' .. tostring(name) end
163164
local sign_text = sign_definition.text
164165

165166
return pcall(vim.api.nvim_buf_set_extmark, self.bufnr, self.ns_id, row, 0, {

0 commit comments

Comments
 (0)