Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
milogert committed Mar 27, 2024
1 parent 47c92fc commit f66d4cb
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 79 deletions.
2 changes: 2 additions & 0 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function M.setup()
prompt = prompt .. k .. ":" .. v .. " "
end
opts.prompt = prompt
opts.kind = "issue"
picker.search(opts)
end,
reload = function()
Expand Down Expand Up @@ -152,6 +153,7 @@ function M.setup()
prompt = prompt .. k .. ":" .. v .. " "
end
opts.prompt = prompt
opts.kind = "pull_request"
picker.search(opts)
end,
reload = function()
Expand Down
6 changes: 6 additions & 0 deletions lua/octo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ function M.get_default_values()
copy_url = { lhs = "<C-y>", desc = "copy url to system clipboard" },
checkout_pr = { lhs = "<C-o>", desc = "checkout pull request" },
merge_pr = { lhs = "<C-r>", desc = "merge pull request" },
open_or_send_to_qf = { lhs = "<CR>", desc = "open item or send to quickfix list" },
open_vertical_split = { lhs = "<C-v>", desc = "open item in vertical split" },
open_horizontal_split = { lhs = "<C-s>", desc = "open item in horizontal split" },
open_tab = { lhs = "<C-t>", desc = "open item in new tab" },
send_to_qf = { lhs = "<C-q>", desc = "send item to quickfix list" },
send_to_ll = { lhs = "<C-l>", desc = "send item to location list" },
},
},
default_remote = { "upstream", "origin" },
Expand Down
16 changes: 16 additions & 0 deletions lua/octo/pickers/fzf-lua/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ function M.gen_from_issue(issue_table)
}
end

function M.entry_string_from_issue_or_pr(tbl, prefix_fn)
if not tbl or vim.tbl_isempty(tbl) then
return nil
end
local kind = tbl.__typename == "Issue" and "issue" or "pull_request"
local filename
if kind == "issue" then
filename = utils.get_issue_uri(tbl.repository.nameWithOwner, tbl.number)
else
filename = utils.get_pull_request_uri(tbl.repository.nameWithOwner, tbl.number)
end
local prefix = prefix_fn(tbl)
return string.format("%s %s %s %s %s", filename, tbl.url, tbl.repository.nameWithOwner, prefix, tbl.title)
end

function M.gen_from_git_commits(entry)
if not entry then
return nil
Expand Down Expand Up @@ -210,6 +225,7 @@ function M.gen_from_repo(repo)
local metadata = string.format("(%s)", table.concat({ fork_str, access_str }, ", "))
local description = fzf.utils.ansi_from_hl("Comment", entry.repo.description)
local entry_str = table.concat({
entry.filename,
name,
metadata,
description,
Expand Down
51 changes: 49 additions & 2 deletions lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local picker_utils = require "octo.pickers.fzf-lua.pickers.utils"
local octo_config = require "octo.config"
local utils = require "octo.utils"
local log = require "octo.pickers.fzf-lua.log"
local navigation = require "octo.navigation"
local fzf_actions = require "fzf-lua.actions"
local M = {}

M.common_buffer_actions = function(formatted_items)
Expand All @@ -11,10 +13,10 @@ M.common_buffer_actions = function(formatted_items)
vim.fn.setqflist({}, " ", {
title = "Octo",
lines = vim.tbl_map(function(i)
log.info('adding to quickfix list', formatted_items[i])
log.info("adding to quickfix list", formatted_items[i])
return formatted_items[i].filename .. "#0#" .. formatted_items[i].ordinal
end, selected),
efm = "%f#%l#%m"
efm = "%f#%l#%m",
})

vim.cmd "copen"
Expand Down Expand Up @@ -46,4 +48,49 @@ M.common_open_actions = function(formatted_items)
})
end

M.common_buffer_actions_v2 = function()
local cfg = octo_config.values
local default = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_or_send_to_qf.lhs)
local open_splits = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_horizontal_split.lhs)
local open_vsplits = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_vertical_split.lhs)
local open_tab = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_tab.lhs)
local send_to_qf = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.send_to_qf.lhs)
local send_to_ll = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.send_to_ll.lhs)

return {
[default] = function(selected, opts)
fzf_actions.file_edit_or_qf(selected, opts)
end,
[open_splits] = function(selected, opts)
fzf_actions.file_split(selected, opts)
end,
[open_vsplits] = function(selected, opts)
fzf_actions.file_vsplit(selected, opts)
end,
[open_tab] = function(selected, opts)
fzf_actions.file_tabedit(selected, opts)
end,
[send_to_qf] = fzf_actions.file_sel_to_qf,
[send_to_ll] = fzf_actions.file_sel_to_ll,
}
end

M.common_open_actions_v2 = function()
local cfg = octo_config.values
return vim.tbl_extend("force", M.common_buffer_actions_v2(), {
[utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_in_browser.lhs)] = function(selected, opts)
for _, s in ipairs(selected) do
local split = vim.split(s, " ")
local filename_split = vim.split(split[1], "/")
navigation.open_in_browser(opts.kind, filename_split[3] .. "/" .. filename_split[4], split[-1])
end
end,
[utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.copy_url.lhs)] = function(selected, opts)
-- Only copy the first one. Multiselect in this case makes no sense.
local split = vim.split(selected[1], " ")
picker_utils.copy_url_raw(split[2])
end,
})
end

return M
45 changes: 24 additions & 21 deletions lua/octo/pickers/fzf-lua/pickers/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local octo_config = require "octo.config"
local picker_utils = require "octo.pickers.fzf-lua.pickers.utils"
local previewers = require "octo.pickers.fzf-lua.previewers"
local utils = require "octo.utils"
local fzf_actions = require "fzf-lua.actions"
local fzf_actions = require "octo.pickers.fzf-lua.pickers.fzf_actions"

return function(opts)
opts = opts or {}
Expand All @@ -28,8 +28,6 @@ return function(opts)

local query = graphql("issues_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })

local formatted_issues = {}

local get_contents = function(fzf_cb)
gh.run {
args = {
Expand All @@ -50,12 +48,12 @@ return function(opts)
local issues = resp.data.repository.issues.nodes

for _, issue in ipairs(issues) do
local entry = entry_maker.gen_from_issue(issue)
local entry_string = entry_maker.entry_string_from_issue_or_pr(issue, function(tbl)
return fzf.utils.ansi_from_hl("Comment", tbl.number)
end)

if entry ~= nil then
formatted_issues[entry.filename.." "..entry.ordinal] = entry
local prefix = fzf.utils.ansi_from_hl("Comment", entry.value)
fzf_cb(entry.filename .. " " .. prefix .. " " .. entry.obj.title)
if entry_string ~= nil then
fzf_cb(entry_string)
end
end
end
Expand All @@ -68,31 +66,36 @@ return function(opts)

fzf.fzf_exec(get_contents, {
prompt = picker_utils.get_prompt(opts.prompt_title),
previewer = previewers.issue(formatted_issues),
previewer = previewers.pr_and_issue(),
fzf_opts = {
["--header"] = opts.results_title,
["--info"] = "default",
["--multi"] = true,
["--delimiter"] = " ",
["--with-nth"] = "2..",
["--with-nth"] = "4..",
},
winopts = {
title = opts.window_title or "Issues",
title_pos = "center",
},
actions = {
["default"] = fzf_actions.file_edit_or_qf,
["ctrl-s"] = fzf_actions.file_split,
["ctrl-v"] = fzf_actions.file_vsplit,
["ctrl-t"] = fzf_actions.file_tabedit,
["alt-q"] = fzf_actions.file_sel_to_qf,
["alt-l"] = fzf_actions.file_sel_to_ll,
},
actions = fzf_actions.common_buffer_actions_v2(),
_fmt = {
from = function (entry)
from = function(entry)
local split = vim.split(entry, " ")
return split[1]..":1:1:"..table.concat(split, " ", 2)
end
return split[1] .. ":1:1:" .. table.concat(split, " ", 2)
end,
},
parse_entry = function(entry)
local split = vim.split(entry, " ")
local number = split[4]
local owner, name = utils.split_repo(split[3])
return {
kind = "issue",
number = number,
owner = owner,
name = name,
previewer_title = split[3],
}
end,
})
end
52 changes: 36 additions & 16 deletions lua/octo/pickers/fzf-lua/pickers/prs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ return function(opts)
local order_by = cfg.pull_requests.order_by

local query =
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })

local formatted_pulls = {}
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })

local get_contents = function(fzf_cb)
gh.run {
Expand All @@ -55,18 +53,18 @@ return function(opts)
local pull_requests = resp.data.repository.pullRequests.nodes

for _, pull in ipairs(pull_requests) do
local entry = entry_maker.gen_from_issue(pull)

if entry ~= nil then
formatted_pulls[entry.ordinal] = entry
local entry_string = entry_maker.entry_string_from_issue_or_pr(pull, function(tbl)
local highlight
if entry.obj.isDraft then
if tbl.isDraft then
highlight = "OctoSymbol"
else
highlight = "OctoStateOpen"
end
local prefix = fzf.utils.ansi_from_hl(highlight, entry.value)
fzf_cb(prefix .. " " .. entry.obj.title)
return fzf.utils.ansi_from_hl(highlight, tbl.number)
end)

if entry_string ~= nil then
fzf_cb(entry_string)
end
end
end
Expand All @@ -77,18 +75,40 @@ return function(opts)
}
end

local checkout_pr_mapping = utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.checkout_pr.lhs)

fzf.fzf_exec(get_contents, {
prompt = picker_utils.get_prompt(opts.prompt_title),
previewer = previewers.issue(formatted_pulls),
previewer = previewers.pr_and_issue(),
fzf_opts = {
["--no-multi"] = "", -- TODO this can support multi, maybe.
["--info"] = "default",
["--multi"] = true,
["--delimiter"] = " ",
["--with-nth"] = "4..",
},
actions = vim.tbl_extend("force", fzf_actions.common_open_actions(formatted_pulls), {
[utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.checkout_pr.lhs)] = function(selected)
local entry = formatted_pulls[selected[1]]
checkout_pull_request(entry)
actions = vim.tbl_extend("force", fzf_actions.common_open_actions_v2(), {
[checkout_pr_mapping] = function(selected, _opts)
local split = vim.split(selected[1], " ")
utils.checkout_pr(split[3])
end,
}),
_fmt = {
from = function(entry)
local split = vim.split(entry, " ")
return split[1] .. ":1:1:" .. table.concat(split, " ", 2)
end,
},
parse_entry = function(entry)
local split = vim.split(entry, " ")
local number = split[4]
local owner, name = utils.split_repo(split[3])
return {
kind = "pull_request",
number = number,
owner = owner,
name = name,
previewer_title = split[3],
}
end,
})
end
4 changes: 2 additions & 2 deletions lua/octo/pickers/fzf-lua/pickers/repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ return function(opts)
fzf_opts = {
["--no-multi"] = "", -- TODO this can support multi, maybe.
["--info"] = "default",
-- ["--delimiter"] = "' '",
-- ["--with-nth"] = "1..5",
["--delimiter"] = "' '",
["--with-nth"] = "2..",
},
actions = fzf_actions.common_open_actions(formatted_repos),
})
Expand Down
49 changes: 34 additions & 15 deletions lua/octo/pickers/fzf-lua/pickers/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ local graphql = require "octo.gh.graphql"
local picker_utils = require "octo.pickers.fzf-lua.pickers.utils"
local utils = require "octo.utils"
local previewers = require "octo.pickers.fzf-lua.previewers"
local log = require "octo.pickers.fzf-lua.log"

local handle_entry = function(fzf_cb, issue, max_id_length, formatted_issues, co)
local entry = entry_maker.gen_from_issue(issue)
if entry ~= nil then
local owner, name = utils.split_repo(entry.repo)
local raw_number = picker_utils.pad_string(entry.obj.number, max_id_length)
local number = fzf.utils.ansi_from_hl("Comment", raw_number)
local ordinal_entry = string.format("%s %s %s %s %s", entry.kind, owner, name, raw_number, entry.obj.title)
local string_entry = string.format("%s %s %s %s %s", entry.kind, owner, name, number, entry.obj.title)
formatted_issues[ordinal_entry] = entry
fzf_cb(string_entry, function()
local handle_entry = function(fzf_cb, issue, max_id_length, co)
local entry_string = entry_maker.entry_string_from_issue_or_pr(issue, function (tbl)
local raw_number = picker_utils.pad_string(tbl.number, max_id_length)
return fzf.utils.ansi_from_hl("Comment", raw_number)
end)
if entry_string ~= nil then
log.info('entry_string', entry_string)
fzf_cb(entry_string, function()
coroutine.resume(co)
end)
end
Expand All @@ -25,8 +24,6 @@ end
return function(opts)
opts = opts or {}

local formatted_items = {}

local contents = function(query)
return function(fzf_cb)
coroutine.wrap(function()
Expand Down Expand Up @@ -65,7 +62,7 @@ return function(opts)

for _, issue in ipairs(resp.data.search.nodes) do
vim.schedule(function()
handle_entry(fzf_cb, issue, max_id_length, formatted_items, co)
handle_entry(fzf_cb, issue, max_id_length, co)
end)
coroutine.yield()
end
Expand All @@ -81,12 +78,34 @@ return function(opts)
prompt = picker_utils.get_prompt(opts.prompt_title),
func_async_callback = false,
previewer = previewers.search(),
query_delay = 500,
query_delay = 250,
fzf_opts = {
["--info"] = "default",
["--multi"] = true,
["--delimiter"] = " ",
["--with-nth"] = "4..",
},
actions = fzf_actions.common_open_actions(formatted_items),
actions = fzf_actions.common_open_actions_v2(),
_fmt = {
from = function(entry)
local split = vim.split(entry, " ")
return split[1] .. ":1:1:" .. table.concat(split, " ", 2)
end,
},
parse_entry = function(entry)
log.info('entry here', entry)
local match = string.gmatch(entry, "[^%s]+")
local _ = match()
local _ = match()
local repo = match()
local owner, name = utils.split_repo(repo)
local number = match()
return {
kind = opts.kind,
number = number,
owner = owner,
name = name,
}
end,
})
end
Loading

0 comments on commit f66d4cb

Please sign in to comment.