Skip to content

Commit

Permalink
feat: add ability to toggle viewed state of a file
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeLagrange authored and ldelossa committed Nov 8, 2023
1 parent 244eab9 commit 509e196
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/gh-nvm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ local commands = {
{name = "GHNextThread", callback = dv.next_thread, opts = {}},
-- when cursor is on a line which can be commented in a diff view, create a comment
{name = "GHCreateThread", callback = dv.create_comment, opts = {range=true}},
-- toggle viewed state of the file being diffed
{name = "GHToggleViewed", callback = dv.toggle_file_viewed, opts = {}},
-- close a PR and cleanup any state associated with it (happens on tab and neovim close as well)
{name = "GHClosePR", callback = pr.close_pull, opts = {}},
-- close the Commit panel
Expand Down
2 changes: 2 additions & 0 deletions lua/litee/gh/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ local commands = {
{name = "GHNextThread", callback = dv.next_thread, opts = {}},
-- when cursor is on a line which can be commented in a diff view, create a comment
{name = "GHCreateThread", callback = dv.create_comment, opts = {range=true}},
-- toggle viewed state of the file being diffed
{name = "GHToggleViewed", callback = dv.toggle_file_viewed, opts = {}},
-- close a PR and cleanup any state associated with it (happens on tab and neovim close as well)
{name = "GHClosePR", callback = pr.close_pull, opts = {}},
-- close the Commit panel
Expand Down
16 changes: 16 additions & 0 deletions lua/litee/gh/ghcli/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,20 @@ query($name: String!, $owner: String!, $pull_number: Int!, $cursor: String!) {
}
]]

M.mark_file_as_viewed = [[
mutation ($pull_request_id: ID!, $path: String!) {
markFileAsViewed( input: {pullRequestId: $pull_request_id, path: $path}) {
clientMutationId
}
}
]]

M.mark_file_as_unviewed = [[
mutation ($pull_request_id: ID!, $path: String!) {
unmarkFileAsViewed( input: {pullRequestId: $pull_request_id, path: $path}) {
clientMutationId
}
}
]]

return M
30 changes: 30 additions & 0 deletions lua/litee/gh/ghcli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,36 @@ function M.get_pull_files_viewed_state_async(pull_number, on_read)
async_request(args, paginate)
end

function M.mark_file_as_viewed(pull_request_id, path, on_read)
local args = {
"api",
"graphql",
"-F",
string.format("pull_request_id=%s", pull_request_id),
"-F",
string.format("path=%s", path),
"-f",
string.format("query=%s", graphql.mark_file_as_viewed),
}

async_request(args, on_read)
end

function M.mark_file_as_unviewed(pull_request_id, path, on_read)
local args = {
"api",
"graphql",
"-F",
string.format("pull_request_id=%s", pull_request_id),
"-F",
string.format("path=%s", path),
"-f",
string.format("query=%s", graphql.mark_file_as_unviewed),
}

async_request(args, on_read)
end

-- because graphql really sucks, write a special paginated function for this.
function M.get_review_threads_async_paginated(pull_number, on_read)
local args = {
Expand Down
25 changes: 25 additions & 0 deletions lua/litee/gh/pr/diff_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local config = require('litee.gh.config')

local s = require('litee.gh.pr.state')
local thread_buffer = require('litee.gh.pr.thread_buffer')
local ghcli = require('litee.gh.ghcli')
local gitcli = require('litee.gh.gitcli')

-- works as a "once" flag to lazy init signs.
Expand Down Expand Up @@ -792,4 +793,28 @@ function M.create_comment(args)
}
end

function M.toggle_file_viewed()
local cur_win = vim.api.nvim_get_current_win()
-- return if not in one of the diff buffers
if state == nil or (cur_win ~= state.lwin and cur_win ~= state.rwin) then
return
end

local filename = state.file["filename"]
local toggle_viewed_state = nil
if s.pull_state.files_by_name[filename].viewed_state == 'VIEWED' then
toggle_viewed_state = ghcli.mark_file_as_unviewed
else
toggle_viewed_state = ghcli.mark_file_as_viewed
end

toggle_viewed_state(
s.pull_state.pr_raw["node_id"],
state.file["filename"],
function()
vim.cmd("GHRefreshPR")
end
)
end

return M

0 comments on commit 509e196

Please sign in to comment.