Skip to content
Merged
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
11 changes: 4 additions & 7 deletions lua/diffview/api/views/diff/diff_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function CDiffView:init(opt)
self.right = opt.right
self.options = opt.options
self.files = FileDict()
self.file_idx = 1
self.fetch_files = opt.update_files
self.get_file_data = opt.get_file_data
self.panel = FilePanel(
Expand All @@ -53,14 +52,14 @@ function CDiffView:init(opt)
self.rev_arg or git.rev_to_pretty_string(self.left, self.right)
)

local files, selected = self:create_file_entries(opt.files)
self.file_idx = selected
local files = self:create_file_entries(opt.files)

for kind, entries in pairs(files) do
for _, entry in ipairs(entries) do
table.insert(self.files[kind], entry)
end
end
self.files:update_file_trees()
end

---@Override
Expand All @@ -70,7 +69,6 @@ end

function CDiffView:create_file_entries(files)
local entries = {}
local i, file_idx = 1, 1

local sections = {
{ kind = "working", files = files.working, left = self.left, right = self.right },
Expand Down Expand Up @@ -103,13 +101,12 @@ function CDiffView:create_file_entries(files)
)

if file_data.selected == true then
file_idx = i
self.panel.cur_file = entries[v.kind][#entries[v.kind]]
end
i = i + 1
end
end

return entries, file_idx
return entries
end

M.CDiffView = CDiffView
Expand Down
4 changes: 4 additions & 0 deletions lua/diffview/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ M.defaults = {
signs = {
fold_closed = "",
fold_open = "",
folder_closed = "",
folder_open = "",
},
file_panel = {
position = "left",
width = 35,
height = 10,
listing_style = "tree",
},
file_history_panel = {
position = "bottom",
Expand Down Expand Up @@ -63,6 +66,7 @@ M.defaults = {
["gf"] = cb("goto_file"),
["<C-w><C-f>"] = cb("goto_file_split"),
["<C-w>gf"] = cb("goto_file_tab"),
["i"] = cb("listing_style"),
["<leader>e"] = cb("focus_files"),
["<leader>b"] = cb("toggle_files"),
},
Expand Down
19 changes: 18 additions & 1 deletion lua/diffview/git/file_dict.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
local oop = require("diffview.oop")
local FileTree = require("diffview.views.file_tree.file_tree").FileTree
local M = {}

---@type table<integer, FileEntry>
---@class FileDict
---@field working FileEntry[]
---@field staged FileEntry[]
---@field working_tree FileTree
---@field staged_tree FileTree
local FileDict = oop.Object
FileDict = oop.create_class("FileDict")

Expand All @@ -28,6 +32,19 @@ function FileDict:init()
end
end

function FileDict:update_file_trees()
if #self.working > 0 then
self.working_tree = FileTree(self.working)
else
self.working_tree = nil
end
if #self.staged > 0 then
self.staged_tree = FileTree(self.staged)
else
self.staged_tree = nil
end
end

function FileDict:size()
return #self.working + #self.staged
end
Expand All @@ -46,10 +63,10 @@ end
function FileDict:ipairs()
local i = 0
local n = #self.working + #self.staged
---@return integer, FileEntry
return function()
i = i + 1
if i <= n then
---@type integer, FileEntry
return i, self[i]
end
end
Expand Down
1 change: 1 addition & 0 deletions lua/diffview/git/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ function M.diff_file_list(git_root, left, right, path_args, opt)
files.staged = tracked_files(git_root, left_rev, right_rev, "--cached HEAD" .. p_args, "staged")
end

files:update_file_trees()
return files
end

Expand Down
121 changes: 110 additions & 11 deletions lua/diffview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ local uid_counter = 0
---@field first integer 0 indexed, inclusive
---@field last integer Exclusive

---@class CompStruct
---@field _name string
---@field comp RenderComponent
local CompStruct

---@class RenderComponent
---@field name string
---@field parent RenderComponent
Expand All @@ -28,7 +33,7 @@ RenderComponent = oop.create_class("RenderComponent")
---RenderComponent constructor.
---@return RenderComponent
function RenderComponent:init(name)
self.name = name
self.name = name or RenderComponent.next_uid()
self.lines = {}
self.hl = {}
self.components = {}
Expand Down Expand Up @@ -61,24 +66,43 @@ function RenderComponent.next_uid()
return uid
end

---Create and add a new component.
---@param schema any
---@return RenderComponent|any
function RenderComponent:create_component(schema)
---Create a new compoenent
---@param schema table
---@return RenderComponent, CompStruct
function RenderComponent.create_static_component(schema)
local comp_struct
local new_comp = RenderComponent(schema and schema.name or RenderComponent.next_uid())
table.insert(self.components, new_comp)
local new_comp = RenderComponent(schema and schema.name or nil)

if schema then
new_comp.context = schema.context
comp_struct = { _name = new_comp.name, comp = new_comp }
create_subcomponents(new_comp, comp_struct, schema)
end

return new_comp, comp_struct
end

---Create and add a new component.
---@param schema table
---@return RenderComponent|CompStruct
function RenderComponent:create_component(schema)
local new_comp, comp_struct = RenderComponent.create_static_component(schema)
self:add_component(new_comp)

if comp_struct then
return comp_struct
end

return new_comp
end

---@param component RenderComponent
function RenderComponent:add_component(component)
component.parent = self
table.insert(self.components, component)
end

---@param component RenderComponent
function RenderComponent:remove_component(component)
for i, c in ipairs(self.components) do
if c == component then
Expand All @@ -90,10 +114,15 @@ function RenderComponent:remove_component(component)
return false
end

---@param line string
function RenderComponent:add_line(line)
table.insert(self.lines, line)
end

---@param group string
---@param line_idx integer
---@param first integer
---@param last integer
function RenderComponent:add_hl(group, line_idx, first, last)
table.insert(self.hl, {
group = group,
Expand Down Expand Up @@ -136,6 +165,66 @@ function RenderComponent:get_comp_on_line(line)
return recurse(self)
end

---@param callback function(comp: RenderComponent, i: integer, parent: RenderComponent)
function RenderComponent:some(callback)
for i, child in ipairs(self.components) do
if callback(child, i, self) then
return
end
end
end

function RenderComponent:deep_some(callback)
local function wrap(comp, i, parent)
if callback(comp, i, parent) then
return true
else
return comp:some(wrap)
end
end
self:some(wrap)
end

function RenderComponent:leaves()
local leaves = {}
self:deep_some(function(comp)
if #comp.components == 0 then
leaves[#leaves + 1] = comp
end
return false
end)

return leaves
end

function RenderComponent:pretty_print()
local keys = { "name", "lstart", "lend" }

local function recurse(depth, comp)
local outer_padding = string.rep(" ", depth * 2)
print(outer_padding .. "{")

local inner_padding = outer_padding .. " "
for _, k in ipairs(keys) do
print(string.format("%s%s = %s,", inner_padding, k, vim.inspect(comp[k])))
end
if #comp.lines > 0 then
print(string.format("%slines = {", inner_padding))
for _, line in ipairs(comp.lines) do
print(string.format("%s %s,", inner_padding, vim.inspect(line)))
end
print(string.format("%s},", inner_padding))
end
for _, child in ipairs(comp.components) do
recurse(depth + 1, child)
end

print(outer_padding .. "},")
end

recurse(0, self)
end

---@class RenderData
---@field lines string[]
---@field hl HlData[]
Expand All @@ -154,12 +243,12 @@ function RenderData:init(ns_name)
end

---Create and add a new component.
---@param schema any
---@return RenderComponent|any
---@param schema table
---@return RenderComponent|CompStruct
function RenderData:create_component(schema)
local comp_struct
local new_comp = RenderComponent(schema and schema.name or RenderComponent.next_uid())
table.insert(self.components, new_comp)
local new_comp = RenderComponent(schema and schema.name or nil)
self:add_component(new_comp)

if schema then
new_comp.context = schema.context
Expand All @@ -171,6 +260,12 @@ function RenderData:create_component(schema)
return new_comp
end

---@param component RenderComponent
function RenderData:add_component(component)
table.insert(self.components, component)
end

---@param component RenderComponent
function RenderData:remove_component(component)
for i, c in ipairs(self.components) do
if c == component then
Expand All @@ -182,6 +277,10 @@ function RenderData:remove_component(component)
return false
end

---@param group string
---@param line_idx integer
---@param first integer
---@param last integer
function RenderData:add_hl(group, line_idx, first, last)
table.insert(self.hl, {
group = group,
Expand Down
Loading