Skip to content

Commit

Permalink
Merge pull request #83 from SuperBo/fix-git-graph-issues
Browse files Browse the repository at this point in the history
Fix git graph issues
  • Loading branch information
SuperBo authored Jul 5, 2024
2 parents 8c00326 + 2a7a7ff commit e8b262d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lua/fugit2/view/components/file_tree_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function GitStatusTree:init(ns_id, top_title, bottom_title, min_width)

self.popup = NuiPopup {
ns_id = ns_id,
enter = true,
enter = false,
focusable = true,
zindex = 50,
border = {
Expand Down
35 changes: 30 additions & 5 deletions lua/fugit2/view/git_base_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ function GitStatusDiffBase:_remove_cached_states(node) end

function GitStatusDiffBase:_refresh_views() end

---@param tree NuiTree
---@param node NuiTree.Node?
---@return NuiTree.Node[]
local function get_leaves(tree, node)
local parent_id = node and node:get_id() or nil
local nodes = {}
local children = tree:get_nodes(parent_id)
for _, child in ipairs(children) do
if not child:has_children() then
nodes[#nodes + 1] = child
else
local sub_nodes = get_leaves(tree, child)
vim.list_extend(nodes, sub_nodes)
end
end

return nodes
end

---@param is_visual_mode boolean
---@param action Fugit2IndexAction
function GitStatusDiffBase:_index_add_reset_discard(is_visual_mode, action)
Expand All @@ -50,7 +69,13 @@ function GitStatusDiffBase:_index_add_reset_discard(is_visual_mode, action)

if not is_visual_mode then
local node, _ = tree.tree:get_node()
nodes = iterators.iter { node }

if not node:has_children() then
nodes = iterators.iter { node }
else
node:expand()
nodes = iterators.iter(get_leaves(tree.tree, node))
end
else
local cursor_start = vim.fn.getpos("v")[2]
local cursor_end = vim.fn.getpos(".")[2]
Expand All @@ -62,13 +87,13 @@ function GitStatusDiffBase:_index_add_reset_discard(is_visual_mode, action)
return tree.tree:get_node(linenr)
end)

nodes = nodes:filter(function(node)
return not node:has_children()
end)

vim.api.nvim_feedkeys(utils.KEY_ESC, "n", false)
end

nodes = nodes:filter(function(node)
return not node:has_children()
end)

local results = nodes
:map(function(node)
local is_updated, is_refresh = tree:index_add_reset_discard(self.repo, self.index, node, action)
Expand Down
12 changes: 10 additions & 2 deletions lua/fugit2/view/git_graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function GitGraph:init(ns_id, repo)

local views = {
branch = BranchView(ns_id, BRANCH_WINDOW_WIDTH),
log = LogView(ns_id, " 󱁉 Commits Log ", true),
log = LogView(ns_id, " 󱁉 Commits Log ", false),
}
if self._views then
self._views = vim.tbl_extend("keep", views, self._views)
Expand Down Expand Up @@ -390,14 +390,21 @@ function GitGraph:load_more_log(n)
self._views.log:update(commit_list, self._git.remote_icons)
end

---Renders content for NuiGitGraph.
-- Renders content for NuiGitGraph.
function GitGraph:render()
self._views.branch:render()
self._views.log:render()
end

-- Focus log view
function GitGraph:focus_log()
self._views.log:focus()
end

function GitGraph:mount()
self._layout:mount()
self:focus_log()

local linenr = self._views.branch:scroll_to_active_branch()
if linenr then
self._states.last_branch_linenr = linenr
Expand All @@ -412,6 +419,7 @@ function GitGraph:unmount()
self.repo = nil
self._git = nil
self._layout = nil
self.closed = true
end

---Set callback to be called when user select commit
Expand Down
1 change: 1 addition & 0 deletions lua/fugit2/view/git_status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ end

function GitStatus:mount()
self._layout:mount()
self:focus_file()
end

---Exit function
Expand Down
6 changes: 6 additions & 0 deletions lua/fugit2/view/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
local M = {}

local last_status_window = nil
local last_graph_window = nil

-- Creates Fugit2 Main Floating Window
---@param namespace integer Nvim namespace
Expand All @@ -28,10 +29,15 @@ end
---@param repo GitRepository
---@return NuiLayout
function M.new_fugit2_graph_window(namespace, repo)
if last_graph_window and not last_graph_window.closed then
return last_graph_window
end

-- Status content
local GitGraph = require "fugit2.view.git_graph"
local graph = GitGraph(namespace, repo)
graph:render()
last_graph_window = graph

return graph
end
Expand Down

0 comments on commit e8b262d

Please sign in to comment.