Skip to content

Commit

Permalink
feat(project_commit_screen): upgrade to a split
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Feb 6, 2025
1 parent fdf853d commit 74b7e2f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lua/vgit/core/Buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function Buffer:place_extmark_highlight(opts)
end

function Buffer:clear_extmark_texts()
loop.free_textlock()
if not self:is_valid() then return end
return self.text_extmark:clear()
end
Expand Down Expand Up @@ -144,7 +145,6 @@ function Buffer:is_current()
end

function Buffer:is_valid()
loop.free_textlock()
local bufnr = self.bufnr
return vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
end
Expand Down
44 changes: 16 additions & 28 deletions lua/vgit/features/screens/ProjectCommitScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ local loop = require('vgit.core.loop')
local Scene = require('vgit.ui.Scene')
local Object = require('vgit.core.Object')
local console = require('vgit.core.console')
local SimpleView = require('vgit.ui.views.SimpleView')
local KeyHelpBarView = require('vgit.ui.views.KeyHelpBarView')
local SimpleSplit = require('vgit.ui.splits.SimpleSplit')
local Model = require('vgit.features.screens.ProjectCommitScreen.Model')
local project_commit_preview_setting = require('vgit.settings.project_commit_preview')

Expand All @@ -18,16 +17,7 @@ function ProjectCommitScreen:constructor(opts)
name = 'Project Commit Screen',
scene = scene,
model = model,
app_bar_view = KeyHelpBarView(scene, {
keymaps = function()
local keymaps = project_commit_preview_setting:get('keymaps')
return { { 'Save commit', keymaps['save'] } }
end,
}),
view = SimpleView(scene, {
title = function()
return model:get_title()
end,
split = SimpleSplit(scene, {
lines = function()
return model:get_lines()
end,
Expand All @@ -43,6 +33,14 @@ function ProjectCommitScreen:constructor(opts)
}
end

function ProjectCommitScreen:save()
local _, err = self.model:commit(self.split:get_lines())
loop.free_textlock()
if err then return console.debug.error(err).error(err) end
console.info('Successfully committed changes')
self:destroy()
end

function ProjectCommitScreen:create()
loop.free_textlock()
local _, err = self.model:fetch()
Expand All @@ -54,31 +52,21 @@ function ProjectCommitScreen:create()
end

loop.free_textlock()
self.view:define()
self.app_bar_view:define()
self.split:define()

self.app_bar_view:mount()
self.app_bar_view:render()
self.split:mount()
self.split:render()
self.split:set_filetype('gitcommit')

self.view:mount()
self.view:render()
self.view:set_keymap({
self.split:set_keymap({
{
mode = 'n',
mapping = project_commit_preview_setting:get('keymaps').save,
handler = loop.coroutine(function()
local _, commit_err = self.model:commit(self.view:get_lines())
loop.free_textlock()

if commit_err then return console.debug.error(commit_err).error(commit_err) end

console.info('Successfully committed changes')

self:destroy()
self:save()
end),
},
})
self.view:set_filetype('gitcommit')

return true
end
Expand Down
2 changes: 1 addition & 1 deletion lua/vgit/settings/project_commit_preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Config = require('vgit.core.Config')
return Config({
keymaps = {
save = {
key = 'S',
key = '<enter>',
desc = 'Save commit',
},
},
Expand Down
71 changes: 71 additions & 0 deletions lua/vgit/ui/splits/SimpleSplit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local utils = require('vgit.core.utils')
local Object = require('vgit.core.Object')
local Split = require('vgit.ui.Split')
local dimensions = require('vgit.ui.dimensions')

local SimpleSplit = Object:extend()

function SimpleSplit:constructor(scene, props, plot, config)
return {
plot = plot,
scene = scene,
props = props,
config = config or {},
}
end

function SimpleSplit:get_components()
return { self.scene:get('simple_split') }
end

function SimpleSplit:define()
self.scene:set(
'simple_split',
Split({
config = {
elements = utils.object.assign({ header = true, footer = false }, self.config.elements),
buf_options = utils.object.assign({ modifiable = true }, self.config.buf_options),
win_options = {
cursorbind = true,
scrollbind = true,
cursorline = true,
},
win_plot = dimensions.relative_win_plot(self.plot, {
height = '100vh',
width = '100vw',
}),
},
})
)
end

function SimpleSplit:on(event_name, callback)
self.scene:get('simple_split'):on(event_name, callback)
end

function SimpleSplit:set_keymap(configs)
utils.list.each(configs, function(config)
self.scene:get('simple_split'):set_keymap(config, config.handler)
end)
end

function SimpleSplit:set_filetype(filetype)
self.scene:get('simple_split'):set_filetype(filetype)
end

function SimpleSplit:get_lines()
return self.scene:get('simple_split'):get_lines()
end

function SimpleSplit:mount(opts)
self.scene:get('simple_split'):mount(opts)
end

function SimpleSplit:render()
local lines = self.props.lines()
if not lines then return end

self.scene:get('simple_split'):unlock():set_lines(lines):lock()
end

return SimpleSplit

0 comments on commit 74b7e2f

Please sign in to comment.