Skip to content

fix(#418): preserve folder collapse states during git operations #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lua/vgit/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ local controller = {}
function controller.setup(config)
configure_settings(config)

vim.api.nvim_create_user_command("VGit", controller.execute_command, {
nargs = "*",
vim.api.nvim_create_user_command('VGit', controller.execute_command, {
nargs = '*',
complete = controller.autocomplete,
})

Expand Down Expand Up @@ -212,29 +212,29 @@ function controller.execute_command(args)
local commands = controller.commands()

if not args.fargs or #args.fargs == 0 then
vim.notify("Vgit: No command provided", vim.log.levels.ERROR)
vim.notify('Vgit: No command provided', vim.log.levels.ERROR)
return
end

local cmd = args.fargs[1]
local cmd_func = commands[cmd]

if not cmd_func then
vim.notify(string.format("Vgit: Unknown command '%s'", cmd), vim.log.levels.ERROR)
vim.notify(string.format('Vgit: Unknown command \'%s\'', cmd), vim.log.levels.ERROR)
return
end

local ok, err = pcall(cmd_func, unpack(args.fargs, 2))
if not ok then
vim.notify(string.format("Vgit: Error executing command '%s': %s", cmd, err), vim.log.levels.ERROR)
vim.notify(string.format('Vgit: Error executing command \'%s\': %s', cmd, err), vim.log.levels.ERROR)
end
end

function controller.autocomplete(arg_lead, cmd_line, _)
local commands = controller.commands()
local cmd_list = vim.tbl_keys(commands)

local split_cmd = vim.split(cmd_line, "%s+")
local split_cmd = vim.split(cmd_line, '%s+')
if #split_cmd == 2 then
return vim.tbl_filter(function(cmd)
return vim.startswith(cmd, arg_lead)
Expand Down
8 changes: 3 additions & 5 deletions lua/vgit/core/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ function keymap.set(opts, callback)
if type(callback) == 'string' then
local command = callback

if not desc then
desc = 'VGit:' .. command
end
if not desc then desc = 'VGit:' .. command end

vim.api.nvim_set_keymap(mode, key, string.format('<Cmd>lua require("vgit").%s()<CR>', command), {
desc = desc,
Expand Down Expand Up @@ -76,13 +74,13 @@ end
function keymap.define(keymaps)
for commands, callback in pairs(keymaps) do
if type(callback) == 'table' then
local config = callback;
local config = callback
keymap.set(config, config.handler)
else
commands = vim.split(commands, ' ')
local config = {
mode = commands[1],
key = commands[2]
key = commands[2],
}
keymap.set(config, callback)
end
Expand Down
4 changes: 2 additions & 2 deletions lua/vgit/features/buffer/Hunks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ function Hunks:cursor_reset()
for i = 1, #hunks do
local hunk = hunks[i]
if
(lnum >= hunk.top and lnum <= hunk.bot)
or (hunk.top == 0 and hunk.bot == 0 and lnum - 1 == hunk.top and lnum - 1 == hunk.bot)
(lnum >= hunk.top and lnum <= hunk.bot)
or (hunk.top == 0 and hunk.bot == 0 and lnum - 1 == hunk.top and lnum - 1 == hunk.bot)
then
selected_hunk = hunk
selected_hunk_index = i
Expand Down
32 changes: 16 additions & 16 deletions lua/vgit/features/buffer/LiveGutter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ end

function LiveGutter:register_events()
git_buffer_store
.on({ 'attach', 'reload' }, function(buffer)
if not self:is_enabled() then return end
.on({ 'attach', 'reload' }, function(buffer)
if not self:is_enabled() then return end

self:fetch(buffer)
buffer:render_signs()
end)
.on({ 'change' }, function(buffer)
if not self:is_enabled() then return end
self:fetch_debounced(buffer)
end)
.on('sync', function(buffer)
if not self:is_enabled() then return end
self:fetch_debounced(buffer)
end)
.on('detach', function(buffer)
buffer:clear_extmarks()
end)
self:fetch(buffer)
buffer:render_signs()
end)
.on({ 'change' }, function(buffer)
if not self:is_enabled() then return end
self:fetch_debounced(buffer)
end)
.on('sync', function(buffer)
if not self:is_enabled() then return end
self:fetch_debounced(buffer)
end)
.on('detach', function(buffer)
buffer:clear_extmarks()
end)
end

return LiveGutter
10 changes: 5 additions & 5 deletions lua/vgit/features/screens/DiffScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ function DiffScreen:create_app_bar_view(scene, model)
local keymaps = diff_preview_setting:get('keymaps')
if model:is_staged() then
return {
{ 'Unstage', keymaps['buffer_unstage'] },
{ 'Unstage hunk', keymaps['buffer_hunk_unstage'] },
{ 'Unstage', keymaps['buffer_unstage'] },
{ 'Unstage hunk', keymaps['buffer_hunk_unstage'] },
{ 'Switch to Staged View', keymaps['toggle_view'] },
}
end
return {
{ 'Stage', keymaps['buffer_stage'] },
{ 'Stage hunk', keymaps['buffer_hunk_stage'] },
{ 'Reset', keymaps['reset'] },
{ 'Stage', keymaps['buffer_stage'] },
{ 'Stage hunk', keymaps['buffer_hunk_stage'] },
{ 'Reset', keymaps['reset'] },
{ 'Switch to Unstage View', keymaps['toggle_view'] },
}
end,
Expand Down
3 changes: 2 additions & 1 deletion lua/vgit/features/screens/HistoryScreen/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function Model:get_diff()
loop.free_textlock()

if lines_err then
local err_str = string.format("fatal: path '%s' exists on disk, but not in '%s'", self.git_file.filename, commit_hash)
local err_str =
string.format('fatal: path \'%s\' exists on disk, but not in \'%s\'', self.git_file.filename, commit_hash)
if lines_err[1] == err_str then
loop.free_textlock()
lines, lines_err = self.git_file:lines(parent_hash)
Expand Down
2 changes: 1 addition & 1 deletion lua/vgit/features/screens/ProjectCommitsScreen/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Model:fetch(commits, opts)
self.state.statuses[id] = entry

return entry
end)
end),
}
end

Expand Down
25 changes: 12 additions & 13 deletions lua/vgit/features/screens/ProjectDiffScreen/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,20 @@ function Model:fetch()
if #unmerged_files ~= 0 then
entries[#entries + 1] = {
title = 'Merge Changes',
entries = unmerged_files
}
end
if #staged_files ~= 0 then
entries[#entries + 1] = {
title = 'Staged Changes',
entries = staged_files
}
end
if #changed_files ~= 0 then
entries[#entries + 1] = {
title = 'Changes',
entries = changed_files
metadata = { section = 'unmerged' },
entries = unmerged_files,
}
end
if #staged_files ~= 0 then entries[#entries + 1] = {
title = 'Staged Changes',
metadata = { section = 'staged' },
entries = staged_files,
} end
if #changed_files ~= 0 then entries[#entries + 1] = {
title = 'Changes',
metadata = { section = 'unstaged' },
entries = changed_files,
} end

self.state.entries = entries
self.state.reponame = reponame
Expand Down
74 changes: 30 additions & 44 deletions lua/vgit/features/screens/ProjectDiffScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ function ProjectDiffScreen:constructor(opts)
keymaps = function()
local keymaps = project_diff_preview_setting:get('keymaps')
return {
{ 'Stage', keymaps['buffer_stage'] },
{ 'Unstage', keymaps['buffer_unstage'] },
{ 'Reset', keymaps['buffer_reset'] },
{ 'Stage hunk', keymaps['buffer_hunk_stage'] },
{ 'Stage', keymaps['buffer_stage'] },
{ 'Unstage', keymaps['buffer_unstage'] },
{ 'Reset', keymaps['buffer_reset'] },
{ 'Stage hunk', keymaps['buffer_hunk_stage'] },
{ 'Unstage hunk', keymaps['buffer_hunk_unstage'] },
{ 'Stage all', keymaps['stage_all'] },
{ 'Unstage all', keymaps['unstage_all'] },
{ 'Reset all', keymaps['reset_all'] },
{ 'Commit', keymaps['commit'] },
{ 'Stage all', keymaps['stage_all'] },
{ 'Unstage all', keymaps['unstage_all'] },
{ 'Reset all', keymaps['reset_all'] },
{ 'Commit', keymaps['commit'] },
}
end,
}),
Expand Down Expand Up @@ -108,14 +108,12 @@ function ProjectDiffScreen:stage_hunk()
end

self:render(function()
local has_unstaged = false
self.status_list_view:each_status(function(status, entry_type)
if entry_type == 'unstaged' and status.filename == entry.status.filename then has_unstaged = true end
end)
self:move_to(function(status, entry_type)
if has_unstaged and entry_type == 'staged' then return false end
return status.filename == entry.status.filename
local found = self:move_to(function(status, entry_type)
return entry_type == 'unstaged'
end)
if not found then
self.status_list_view:move('down')
end
end)
end

Expand All @@ -136,14 +134,12 @@ function ProjectDiffScreen:unstage_hunk()
end

self:render(function()
local has_staged = false
self.status_list_view:each_status(function(status, entry_type)
if entry_type == 'staged' and status.filename == entry.status.filename then has_staged = true end
end)
self:move_to(function(status, entry_type)
if has_staged and entry_type == 'unstaged' then return false end
return status.filename == entry.status.filename
local found = self:move_to(function(status, entry_type)
return entry_type == 'staged'
end)
if not found then
self.status_list_view:move('down')
end
end)
end

Expand All @@ -161,17 +157,12 @@ function ProjectDiffScreen:stage_file()
end

self:render(function()
local has_unstaged = false
self.status_list_view:each_status(function(status)
if status:is_staged() then
has_unstaged = true
end
end)

self:move_to(function(status)
if has_unstaged then return status:is_unstaged() == true end
return status.filename == entry.status.filename
local found = self:move_to(function(status, entry_type)
return entry_type == 'unstaged'
end)
if not found then
self.status_list_view:move('down')
end
end)
end

Expand All @@ -189,17 +180,12 @@ function ProjectDiffScreen:unstage_file()
end

self:render(function()
local has_staged = false
self.status_list_view:each_status(function(status)
if status:is_staged() then
has_staged = true
end
end)

self:move_to(function(status)
if has_staged then return status:is_staged() == true end
return status.filename == entry.status.filename
local found = self:move_to(function(status, entry_type)
return entry_type == 'staged'
end)
if not found then
self.status_list_view:move('down')
end
end)
end

Expand Down Expand Up @@ -246,7 +232,7 @@ function ProjectDiffScreen:reset_file()

loop.free_textlock()
local decision =
console.input(string.format('Are you sure you want to discard changes in %s? (y/N) ', filename)):lower()
console.input(string.format('Are you sure you want to discard changes in %s? (y/N) ', filename)):lower()

if decision ~= 'yes' and decision ~= 'y' then return end

Expand Down Expand Up @@ -485,7 +471,7 @@ function ProjectDiffScreen:setup_diff_keymaps()
mode = 'n',
mapping = {
key = '<enter>',
desc = 'Open buffer'
desc = 'Open buffer',
},
handler = loop.coroutine(function()
self:enter_view()
Expand Down
10 changes: 4 additions & 6 deletions lua/vgit/features/screens/ProjectLogsScreen/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ function Model:select(log)
self.state.selected.set[log.commit_hash] = nil
for i = 1, #self.state.selected.ordered do
local selected_log = self.state.selected.ordered[i]
if selected_log.commit_hash == log.commit_hash then
self.state.selected.ordered[i] = nil
end
if selected_log.commit_hash == log.commit_hash then self.state.selected.ordered[i] = nil end
end
return
end
Expand All @@ -67,7 +65,7 @@ function Model:fetch()
local pagination = {
skip = 0,
count = 100,
display = string.format('%s-%s', 1, 100)
display = string.format('%s-%s', 1, 100),
}
local logs, err = git_log.list(reponame, { pagination = pagination })
if err then return nil, err end
Expand Down Expand Up @@ -96,7 +94,7 @@ function Model:next()
local pagination = {
count = count,
skip = skip,
display = nil
display = nil,
}
local logs, err = git_log.list(reponame, { pagination = pagination })
if err then return nil, err end
Expand All @@ -122,7 +120,7 @@ function Model:previous()
local pagination = {
count = count,
skip = skip,
display = nil
display = nil,
}
local logs, err = git_log.list(reponame, { pagination = pagination })
if err then return nil, err end
Expand Down
Loading
Loading