Skip to content

Commit c66ce8f

Browse files
committed
chore: more refactor
1 parent 2e9c1cb commit c66ce8f

83 files changed

Lines changed: 663 additions & 1652 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/cli/commands/show.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local lazy = require('vgit.core.lazy')
33
local event = lazy('vgit.core.event')
44
local console = lazy('vgit.core.console')
55
local GitTree = lazy('vgit.git.GitTree')
6+
local GitCommit = lazy('vgit.git.GitCommit')
67
local repository = lazy('vgit.git.repository')
78
local scene_setting = lazy('vgit.settings.scene')
89
local display_service = lazy('vgit.ui.display_service')
@@ -74,9 +75,8 @@ show_command.execute = event.async(function(args)
7475
local layout_type = scene_setting:get('diff_preference') or 'unified'
7576

7677
-- Get parent hash for diff comparison
77-
local EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
7878
local parent_hash = commit.parent_hash or ''
79-
local from_ref = parent_hash ~= '' and parent_hash or EMPTY_TREE
79+
local from_ref = parent_hash ~= '' and parent_hash or GitCommit.EMPTY_TREE_HASH
8080
local to_ref = commit.commit_hash or commit.hash
8181

8282
-- Build entries for ProjectDiffView (parallel)

lua/vgit/core/Buffer.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ function Buffer:constructor(bufnr)
2424
}
2525
end
2626

27-
function Buffer:set_state(state)
28-
for key, value in pairs(state) do
29-
self.state[key] = value
30-
end
31-
end
32-
3327
function Buffer:call(callback)
3428
vim.api.nvim_buf_call(self.bufnr, callback)
3529
return self
@@ -97,19 +91,25 @@ function Buffer:place_extmark_highlight(opts)
9791
end
9892

9993
function Buffer:clear_extmark_texts()
100-
if not self:is_valid() then return end
101-
return self._text_extmark:clear()
94+
if not self:is_valid() then return self end
95+
self._text_extmark:clear()
96+
return self
10297
end
10398

10499
function Buffer:clear_extmark_lnums()
105-
return self._lnum_extmark:clear()
100+
if not self:is_valid() then return self end
101+
self._lnum_extmark:clear()
102+
return self
106103
end
107104

108105
function Buffer:clear_extmark_signs()
109-
return self._sign_extmark:clear()
106+
if not self:is_valid() then return self end
107+
self._sign_extmark:clear()
108+
return self
110109
end
111110

112111
function Buffer:clear_extmark_highlights(from, to)
112+
if not self:is_valid() then return self end
113113
self._highlight_extmark:clear(from, to)
114114
return self
115115
end
@@ -156,7 +156,7 @@ end
156156

157157
function Buffer:delete(opts)
158158
opts = opts or {}
159-
vim.tbl_extend('keep', opts, { force = true })
159+
opts = vim.tbl_extend('keep', opts, { force = true })
160160
vim.api.nvim_buf_delete(self.bufnr, opts)
161161

162162
return self
@@ -207,7 +207,7 @@ function Buffer:assign_options(options)
207207

208208
for key, value in pairs(options) do
209209
if key == 'modifiable' then self._modifiable = value end
210-
vim.api.nvim_set_option_value(key, value, { buf = bufnr })
210+
pcall(vim.api.nvim_set_option_value, key, value, { buf = bufnr })
211211
end
212212

213213
return self

lua/vgit/core/Buffer_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('Buffer:', function()
104104
describe('get_name', function()
105105
it('should return buffer name', function()
106106
vim.api.nvim_buf_set_name(bufnr, 'test')
107-
assert.equals(buffer:get_name(), string.format('%s/test', vim.loop.cwd()))
107+
assert.equals(buffer:get_name(), string.format('%s/test', vim.uv.cwd()))
108108
end)
109109
end)
110110

@@ -116,7 +116,7 @@ describe('Buffer:', function()
116116

117117
it('should return relative name for named buffer', function()
118118
local named_buf = Buffer(vim.api.nvim_create_buf(false, true))
119-
vim.api.nvim_buf_set_name(named_buf.bufnr, vim.loop.cwd() .. '/lua/vgit/init.lua')
119+
vim.api.nvim_buf_set_name(named_buf.bufnr, vim.uv.cwd() .. '/lua/vgit/init.lua')
120120
assert.are.equal('lua/vgit/init.lua', named_buf:get_relative_name())
121121
end)
122122
end)

lua/vgit/core/Color.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function Color:_load()
2525
if self.hex then return self.hex end
2626

2727
local spec = self.spec
28-
local attribute = spec.attribute == 'fg' and 'foreground' or 'background'
29-
local ok, hl = pcall(highlight.get_hl_by_name, spec.name, true)
28+
local attribute = spec.attribute
29+
local ok, hl = pcall(highlight.get_hl_by_name, spec.name)
3030

3131
if ok and hl and hl[attribute] then self.hex = '#' .. bit.tohex(hl[attribute], 6) end
3232

lua/vgit/core/Config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Config:assign(config)
4646
self.data[key] = value
4747
end
4848
else
49-
console.debug.warning(string.format('Unknown config key "%s" will be ignored', key))
49+
console.debug.warn(string.format('Unknown config key "%s" will be ignored', key))
5050
end
5151
end
5252

lua/vgit/core/File.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
local Object = require('vgit.core.Object')
1+
local lazy = require('vgit.core.lazy')
2+
3+
local Object = lazy('vgit.core.Object')
24

35
local File = Object:extend()
46

lua/vgit/core/Spawn.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ function Spawn:flush(buffer, cb)
9393
end
9494

9595
function Spawn:start()
96-
local stdout = vim.loop.new_pipe(false)
97-
local stderr = vim.loop.new_pipe(false)
96+
local stdout = vim.uv.new_pipe(false)
97+
local stderr = vim.uv.new_pipe(false)
9898

9999
local on_stdout = function(_, chunk)
100100
self:process_chunk(chunk, self.stdout_buffer, self.spec.on_stdout)
@@ -104,25 +104,35 @@ function Spawn:start()
104104
self:process_chunk(chunk, self.stderr_buffer, self.spec.on_stderr)
105105
end
106106

107+
local handle
108+
107109
local on_exit = event.async(function(code, signal)
108110
stdout:read_stop()
109111
stderr:read_stop()
110112
stdout:close()
111113
stderr:close()
114+
if handle and not handle:is_closing() then handle:close() end
112115

113116
self:flush(self.stdout_buffer, self.spec.on_stdout)
114117
self:flush(self.stderr_buffer, self.spec.on_stderr)
115118

116119
if self.spec.on_exit then self.spec.on_exit(code, signal) end
117120
end)
118121

119-
vim.loop.spawn(self.spec.command, {
122+
handle = vim.uv.spawn(self.spec.command, {
120123
args = self.spec.args,
121124
stdio = { nil, stdout, stderr },
122125
cwd = self.spec.cwd,
123126
env = self.spec.env,
124127
}, on_exit)
125128

129+
if not handle then
130+
stdout:close()
131+
stderr:close()
132+
if self.spec.on_exit then self.spec.on_exit(1) end
133+
return self
134+
end
135+
126136
stdout:read_start(on_stdout)
127137
stderr:read_start(on_stderr)
128138

lua/vgit/core/Window.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ function Window:open(buffer, opts)
4848
local mode = opts.mode or 'floating'
4949
local focus = opts.focus
5050

51-
if opts.mode ~= nil then opts.mode = nil end
52-
if opts.focus ~= nil then opts.focus = nil end
51+
opts = vim.tbl_extend('force', opts, {})
52+
opts.mode = nil
53+
opts.focus = nil
5354

5455
if mode == 'screen' or mode == 'split' then return Window.open_screen(buffer, opts) end
5556

@@ -119,7 +120,8 @@ function Window:set_width(width)
119120
end
120121

121122
function Window:set_config(config)
122-
if config.focus ~= nil then config.focus = nil end
123+
config = vim.tbl_extend('force', config, {})
124+
config.focus = nil
123125
vim.api.nvim_win_set_config(self.win_id, config)
124126
return self
125127
end
@@ -130,7 +132,7 @@ end
130132

131133
function Window:assign_options(options)
132134
for key, value in pairs(options) do
133-
vim.api.nvim_set_option_value(key, value, { win = self.win_id })
135+
pcall(vim.api.nvim_set_option_value, key, value, { win = self.win_id })
134136
end
135137
return self
136138
end

lua/vgit/core/async.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ local co = coroutine
33
local async = {}
44

55
local function is_callable(fn)
6-
return type(fn) == 'function' or (type(fn) == 'table' and type(getmetatable(fn).__call) == 'function')
6+
if type(fn) == 'function' then return true end
7+
if type(fn) ~= 'table' then return false end
8+
local mt = getmetatable(fn)
9+
return mt ~= nil and type(mt.__call) == 'function'
710
end
811

912
local function rotate(nargs, ...)

lua/vgit/core/console/LogFormatter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function LogFormatter:add_indentation(line)
7070
end
7171

7272
function LogFormatter:format(log_type, fn_source, fn_name)
73-
if self._is_formatted then return end
73+
if self._is_formatted then return self end
7474

7575
self._is_formatted = true
7676

0 commit comments

Comments
 (0)