Skip to content

Commit

Permalink
add crude previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Oct 21, 2024
1 parent a8e91b8 commit b63e3a9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
57 changes: 57 additions & 0 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,7 @@ query($endCursor: String) {
title
url
closed
isAnswered
repository { nameWithOwner }
}
pageInfo {
Expand All @@ -2190,6 +2191,62 @@ query($endCursor: String) {
}
]]

M.discussion_query = [[
query($endCursor: String) {
repository(owner: "%s", name: "%s") {
discussion(number: %d) {
id
number
title
body
createdAt
closedAt
url
repository { nameWithOwner }
author { login }
labels(first: 20) {
nodes {
color
name
}
}
comments(first: 100, after: $endCursor) {
nodes {
id
body
createdAt
lastEditedAt
reactionGroups {
content
viewerHasReacted
users {
totalCount
}
}
author {
login
}
viewerDidAuthor
viewerCanUpdate
viewerCanDelete
replies(first: 10) {
totalCount
nodes {
body
author { login }
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
]]

-- https://docs.github.com/en/graphql/reference/objects#project
M.projects_query = [[
query {
Expand Down
23 changes: 11 additions & 12 deletions lua/octo/pickers/telescope/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local M = {}
--- @field state string
--- @field isDraft boolean
--- @field stateReason string
--- @field isAnswered boolean
--- @field closed boolean

--- @class Entry
--- @field kind string
Expand All @@ -35,7 +37,8 @@ local icons = {
},
discussion = {
open = { "", "OctoGrey" },
closed = { "", "OctoGreen" },
answered = { "", "OctoGreen" },
closed = { "", "OctoRed" },
},
unknown = { "" },
}
Expand Down Expand Up @@ -71,11 +74,14 @@ local function get_icon(entry)
return icons.pull_request.open
end
elseif kind == "discussion" then
local state = entry.obj.state
local closed = entry.obj.closed
local isAnswered = entry.obj.isAnswered

if state == "OPENED" then
if not closed then
return icons.discussion.open
elseif state == "CLOSED" then
elseif isAnswered then
return icons.discussion.answered
else
return icons.discussion.closed
end
end
Expand All @@ -89,16 +95,9 @@ function M.gen_from_discussions(max_number)
return nil
end

local icon
if entry.obj.closed then
icon = icons.discussion.closed
else
icon = icons.discussion.open
end

local columns = {
{ entry.value, "TelescopeResultsNumber" },
icon,
get_icon(entry),
{ entry.obj.title },
}
local layout = {
Expand Down
32 changes: 31 additions & 1 deletion lua/octo/pickers/telescope/previewers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@ local pv_utils = require "telescope.previewers.utils"
local ts_utils = require "telescope.utils"
local defaulter = ts_utils.make_default_callable

local vim = vim

local discussion = defaulter(function(opts)
return previewers.new_buffer_previewer {
title = opts.preview_title,
get_buffer_by_name = function(_, entry)
return entry.value
end,
define_preview = function(self, entry)
return self.state.bufnr, self.state.bufname
local bufnr = self.state.bufnr

if self.state.bufname == entry.value and vim.api.nvim_buf_line_count(bufnr) ~= 1 then
return
end

local number = entry.value
local owner, name = utils.split_repo(entry.repo)
local query = graphql("discussion_query", owner, name, number)

gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output and vim.api.nvim_buf_is_valid(bufnr) then
local result = vim.fn.json_decode(output)
local obj = result.data.repository.discussion

writers.write_title(bufnr, tostring(obj.title), 1)
writers.write_body(bufnr, obj)

vim.api.nvim_buf_set_option(bufnr, "filetype", "octo")
end
end,
}
end,
}
end)
Expand Down
2 changes: 2 additions & 0 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,8 @@ function M.discussions(opts)
opts.cb(selected, prompt_bufnr, type)
end

opts.preview_title = opts.preview_title or ""

pickers
.new(opts, {
finder = finders.new_table {
Expand Down
2 changes: 2 additions & 0 deletions lua/octo/ui/writers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ function M.write_title(bufnr, title, line)
local title_mark = M.write_block(bufnr, { title, "" }, line, true)
vim.api.nvim_buf_add_highlight(bufnr, -1, "OctoIssueTitle", 0, 0, -1)
local buffer = octo_buffers[bufnr]

vim.print(buffer)
if buffer then
buffer.titleMetadata = TitleMetadata:new {
savedBody = title,
Expand Down

0 comments on commit b63e3a9

Please sign in to comment.