Skip to content

Commit

Permalink
refactor the telescope search logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Feb 25, 2025
1 parent 0292699 commit f74446b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
51 changes: 25 additions & 26 deletions lua/octo/pickers/telescope/previewers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,37 @@ local issue = defaulter(function(opts)
if self.state.bufname ~= entry.value or vim.api.nvim_buf_line_count(bufnr) == 1 then
local number = entry.value
local owner, name = utils.split_repo(entry.repo)
local query

local query, jq
if entry.kind == "issue" then
query = graphql("issue_query", owner, name, number, _G.octo_pv2_fragment)
jq = ".data.repository.issue"
elseif entry.kind == "pull_request" then
query = graphql("pull_request_query", owner, name, number, _G.octo_pv2_fragment)
jq = ".data.repository.pullRequest"
end
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.json.decode(output)
local obj
if entry.kind == "issue" then
obj = result.data.repository.issue
elseif entry.kind == "pull_request" then
obj = result.data.repository.pullRequest
end

local state = utils.get_displayed_state(entry.kind == "issue", obj.state, obj.stateReason)

writers.write_title(bufnr, obj.title, 1)
writers.write_details(bufnr, obj)
writers.write_body(bufnr, obj)
writers.write_state(bufnr, state:upper(), number)
local reactions_line = vim.api.nvim_buf_line_count(bufnr) - 1
writers.write_block(bufnr, { "", "" }, reactions_line)
writers.write_reactions(bufnr, obj.reactionGroups, reactions_line)
vim.api.nvim_buf_set_option(bufnr, "filetype", "octo")
end
end,
gh.api.graphql {
query = query,
jq = jq,
opts = {
cb = gh.create_callback {
failure = vim.api.nvim_err_writeln,
success = function(output)
local obj = vim.json.decode(output)
local state = utils.get_displayed_state(entry.kind == "issue", obj.state, obj.stateReason)

writers.write_title(bufnr, obj.title, 1)
writers.write_details(bufnr, obj)
writers.write_body(bufnr, obj)
writers.write_state(bufnr, state:upper(), number)
local reactions_line = vim.api.nvim_buf_line_count(bufnr) - 1
writers.write_block(bufnr, { "", "" }, reactions_line)
writers.write_reactions(bufnr, obj.reactionGroups, reactions_line)
vim.api.nvim_buf_set_option(bufnr, "filetype", "octo")
end,
},
},
}
end
end,
Expand Down
20 changes: 8 additions & 12 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ local function get_search_query(prompt)
end

local function get_search_size(prompt)
local query = graphql("search_count_query", prompt)
return gh.api.graphql {
query = query,
query = graphql "search_count_query",
fields = { prompt = prompt },
jq = ".data.search.issueCount",
opts = {
mode = "sync",
Expand Down Expand Up @@ -558,17 +558,13 @@ function M.search(opts)
if val then
_prompt = string.format("%s %s", val, _prompt)
end
local query = graphql("search_query", _prompt)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
local output = gh.api.graphql {
query = graphql "search_query",
fields = { prompt = _prompt },
jq = ".data.search.nodes",
opts = { mode = "sync" },
}
if output then
local resp = vim.json.decode(output)
for _, issue in ipairs(resp.data.search.nodes) do
table.insert(results, issue)
end
end
vim.list_extend(results, vim.json.decode(output))
end
return results
end
Expand Down

0 comments on commit f74446b

Please sign in to comment.