Skip to content
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
31 changes: 26 additions & 5 deletions lua/octo/reviews/file-entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ M._null_buffer = {}
---@field right_winid? integer
---@field left_comment_ranges? [integer, integer][]
---@field right_comment_ranges? [integer, integer][]
---@field left_fetching boolean
---@field right_fetching boolean
---@field associated_bufs integer[]
---@field diffhunks? string[]
---@field viewed_state ViewedState
Expand Down Expand Up @@ -103,6 +105,8 @@ function FileEntry:new(opt)
right_lines = {},
left_fetched = false,
right_fetched = false,
left_fetching = false,
right_fetching = false,
diffhunks = diffhunks,
associated_bufs = {},
viewed_state = pr.files[opt.path],
Expand Down Expand Up @@ -209,18 +213,29 @@ function FileEntry:get_buf(split)
end

---Fetch file content locally or from GitHub.
function FileEntry:fetch()
---@param sync boolean
function FileEntry:fetch(sync)
local right_path = self.path
local left_path = self.path
local current_review = require("octo.reviews").get_current_review()
if not current_review then
return
end
local conf = config.values
if self.left_fetching or self.right_fetching then
if sync then
vim.wait(conf.timeout, function()
return self:is_ready_to_render()
end)
end
return
end
self.left_fetching = true
self.right_fetching = true
local right_sha = current_review.layout.right.commit
local left_sha = current_review.layout.left.commit
local right_abbrev = current_review.layout.right:abbrev()
local left_abbrev = current_review.layout.left:abbrev()
local conf = config.values

-- handle renamed files
if self.status == "R" and self.previous_path then
Expand All @@ -232,11 +247,13 @@ function FileEntry:fetch()
utils.get_file_at_commit(right_path, right_sha, function(lines)
self.right_lines = lines
self.right_fetched = true
self.right_fetching = false
end)
else
utils.get_file_contents(self.pull_request.repo, right_abbrev, right_path, function(lines)
self.right_lines = lines
self.right_fetched = true
self.right_fetching = false
end)
end

Expand All @@ -245,18 +262,22 @@ function FileEntry:fetch()
utils.get_file_at_commit(left_path, left_sha, function(lines)
self.left_lines = lines
self.left_fetched = true
self.left_fetching = false
end)
else
utils.get_file_contents(self.pull_request.repo, left_abbrev, left_path, function(lines)
self.left_lines = lines
self.left_fetched = true
self.left_fetching = false
end)
end

-- wait until we have both versions
return vim.wait(conf.timeout, function()
return self:is_ready_to_render()
end)
if sync then
vim.wait(conf.timeout, function()
return self:is_ready_to_render()
end)
end
end

---Determines whether the file content has been loaded and the file is ready to render
Expand Down
5 changes: 4 additions & 1 deletion lua/octo/reviews/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ function Review:set_files_and_select_first(files)

self.layout.files = files
if selected_file_idx then
files[selected_file_idx]:fetch()
files[selected_file_idx]:fetch(true)
self.layout.selected_file_idx = selected_file_idx
end
for _, file in ipairs(files) do
file:fetch(false)
end
self.layout:update_files()
end

Expand Down
2 changes: 1 addition & 1 deletion lua/octo/reviews/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function Layout:set_current_file(file, focus)
end
if found then
if not file:is_ready_to_render() then
local result = file:fetch()
local result = file:fetch(true)
if not result then
utils.print_err("Timeout fetching " .. file.path)
return
Expand Down