Skip to content

Commit

Permalink
refactor,feat: overhaul gh.nvim issue handling
Browse files Browse the repository at this point in the history
this commit reworks how issues are handling in gh.nvim.

now, they are a completely independent function of `gh.nvim` which have
minimal ties to the "pr" features.

issues for the opened repo can be browsed, previewed, and jumped to,
with no limitations.
  • Loading branch information
ldelossa committed May 24, 2022
1 parent f72b461 commit f253b52
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 818 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.luarc.json
5 changes: 3 additions & 2 deletions lua/litee/gh/commands.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local pr = require('litee.gh.pr')
local issues = require('litee.gh.issues')
local dv = require('litee.gh.pr.diff_view')
local pr_handlers = require('litee.gh.pr.handlers')
local issues = require('litee.gh.issues')
Expand Down Expand Up @@ -30,9 +29,11 @@ function M.setup()
-- expand the node within the Review panel
vim.api.nvim_create_user_command("GHExpandReview", pr.expand_pr_review, {})
-- refresh all details of the PR
vim.api.nvim_create_user_command("GHRefreshPR", pr_handlers.global_refresh, {})
vim.api.nvim_create_user_command("GHRefreshPR", pr_handlers.on_refresh, {})
-- refresh just comments, useful to fresh convo buffers quicker.
vim.api.nvim_create_user_command("GHRefreshComments", pr_handlers.refresh_comments, {})
-- refresh any open issue buffers, if a PR is opened, this will be ran as part of "GHRefreshPR"
vim.api.nvim_create_user_command("GHRefreshIssues", issues.on_refresh, {})
-- start a code review
vim.api.nvim_create_user_command("GHStartReview", pr.start_review, {})
-- submit all pending comments in a code review
Expand Down
24 changes: 24 additions & 0 deletions lua/litee/gh/ghcli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ function M.list_all_repo_issues_async(on_read)
async_request(args, on_read, true)
end

function M.get_user()
local args = {"gh", "api", "/user"}
return gh_exec(args)
end

function M.get_user_async(on_read)
local args = {"api", "/user"}
async_request(args, on_read)
Expand Down Expand Up @@ -308,6 +313,18 @@ function M.get_issue_comments_async(number, on_read)
async_request(args, on_read, true)
end

function M.get_issue_comment_reactions_async(id, on_read)
local args = {
'api',
'-X',
'GET',
'-F',
'per_page=100',
string.format('/repos/{owner}/{repo}/issues/comments/%s/reactions', id)
}
async_request(args, on_read, true)
end

function M.get_review_threads_async(pull_number, on_read)
local args = {
'api',
Expand Down Expand Up @@ -690,4 +707,11 @@ function M.get_git_protocol()
return protocol:gsub("[\r\n]", "")
end

-- on module load, immediately cache our user.
M.user = {}
local function init()
M.user = M.get_user()
end
init()

return M
40 changes: 40 additions & 0 deletions lua/litee/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ local config = require('litee.gh.config').config
local pr_buffer = require('litee.gh.pr.buffer')
local pr_marshallers = require('litee.gh.pr.marshal')
local pr = require('litee.gh.pr')
local pr_state = require('litee.gh.pr.state')
local pr_handlers = require('litee.gh.pr.handlers')
local issues = require('litee.gh.issues')

-- register_pr_component registers the "pr" litee component.
--
Expand Down Expand Up @@ -206,4 +209,41 @@ function M.setup(user_config)
commands.setup()
end

function M.refresh()
if pr_state.pull_state ~= nil then
-- will refresh any open issues too
pr_handlers.on_refresh()
return
else
issues.on_refresh()
end
end

-- refresh all data
vim.api.nvim_create_user_command("GHRefresh", M.refresh, {})

M.refresh_timer = nil

function M.start_refresh_timer(now)
if M.refresh_timer == nil then
M.refresh_timer = vim.loop.new_timer()
end
if now then
M.refresh()
end
vim.schedule(function() vim.api.nvim_echo({{"[gh.nvim] started backround refresh with interval " .. 180000/1000/60 .. " minutes", "LTInfo"}}, false, {}) end)
M.refresh_timer:start(180000, 180000, function()
M.refresh()
end)
end

function M.stop_refresh_timer()
if M.refresh_timer == nil then
return
end
vim.loop.timer_stop(M.refresh_timer)
end

M.start_refresh_timer()

return M
87 changes: 32 additions & 55 deletions lua/litee/gh/issues/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local lib_notify = require('litee.lib.notify')
local lib_icons = require('litee.lib.icons')
local lib_path = require('litee.lib.util.path')

local config = require('litee.gh.config').config
local ghcli = require('litee.gh.ghcli')
local issue_buffer = require('litee.gh.issues.issue_buffer')
local ghcli = require('litee.gh.ghcli')
local issue_buffer = require('litee.gh.issues.issue_buffer')
local preview = require('litee.gh.issues.preview')

local M = {}

Expand All @@ -26,11 +28,26 @@ local function extract_issue_cur_line()
return format
end

function M.open_issue_by_number(number)
function M.open_issue_by_number(number, cur_win)
-- if we are already displaying this issue, just open that win, don't spam
-- neovim with multiple issue buffers of the same issue.
local buf_name = "issue #" .. number
for _, win in ipairs(vim.api.nvim_list_wins()) do
local buf = vim.api.nvim_win_get_buf(win)
local name = lib_path.basename(vim.api.nvim_buf_get_name(buf))
if buf_name == name then
vim.api.nvim_set_current_win(win)
return
end
end
issue_buffer.load_issue(number, vim.schedule_wrap(function()
local buf = issue_buffer.render_issue(number)
vim.cmd("tabnew")
vim.api.nvim_win_set_buf(0, buf)
if cur_win then
vim.api.nvim_win_set_buf(0, buf)
else
vim.cmd("tabnew")
vim.api.nvim_win_set_buf(0, buf)
end
local iss_state = issue_buffer.state_by_number[number]
iss_state.win = vim.api.nvim_get_current_win()
end))
Expand All @@ -41,60 +58,15 @@ function M.open_issue_under_cursor()
if issue == nil then
return
end
local iss_state = issue_buffer.state_by_number[issue]


if
iss_state ~= nil and
iss_state.win ~= nil and
vim.api.nvim_win_is_valid(iss_state.win)
then
vim.api.nvim_set_current_win(iss_state.win)
return
end

M.open_issue_by_number(issue)
end

function M.preview_issue_under_cursor()
local issue = extract_issue_cur_line()
if issue == nil then
local number = extract_issue_cur_line()
if number == nil then
return
end
issue_buffer.load_issue(issue, vim.schedule_wrap(function ()
local buf = issue_buffer.render_issue(issue, true)
local width = 20
local line_count = 0
for i, line in ipairs(vim.api.nvim_buf_get_lines(buf, 0, -1, false)) do
local line_width = vim.fn.strdisplaywidth(line)
if line_width > width then
width = line_width
end
line_count = i
end

local popup_conf = vim.lsp.util.make_floating_popup_options(
width,
line_count,
{
border= "rounded",
focusable= false,
zindex = 99,
relative = "cursor"
}
)
local cur_win = vim.api.nvim_get_current_win()
local win = vim.api.nvim_open_win(buf, false, popup_conf)
local id = vim.api.nvim_create_autocmd({"CursorMoved"}, {
buffer = vim.api.nvim_win_get_buf(cur_win),
callback = function()
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end,
})
end, true)
)
preview.preview_issue(number)
end

function M.open_issue(args)
Expand All @@ -104,7 +76,8 @@ function M.open_issue(args)
end

if args["args"] ~= "" then
M.open_issue_by_number(args["args"])
M.open_issue_by_number(args["args"])
return
end

ghcli.list_all_repo_issues_async(function(err, data)
Expand All @@ -125,12 +98,16 @@ function M.open_issue(args)
if idx == nil then
return
end
M.open_issue_by_number(data[idx]["number"])
M.open_issue_by_number(data[idx]["number"])
end
)
end)
end

function M.on_refresh()
issue_buffer.on_refresh()
end

-- set the issue buffer's calls backs.
issue_buffer.set_callbacks({
preview_cb = M.preview_issue_under_cursor,
Expand Down
Loading

0 comments on commit f253b52

Please sign in to comment.