Skip to content

Commit

Permalink
implement telescope multiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Dec 15, 2024
1 parent 9080a9b commit bac20e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
25 changes: 23 additions & 2 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,22 @@ function M.create_label(label)
}
end

local function format(str)
return string.format('"%s"', str)
end

local function create_list(values, fmt)
if type(values) == "string" then
return fmt(values)
end

local formatted_values = {}
for _, value in ipairs(values) do
table.insert(formatted_values, fmt(value))
end
return "[" .. table.concat(formatted_values, ", ") .. "]"
end

local function label_action(opts)
local label = opts.label

Expand All @@ -1515,8 +1531,13 @@ local function label_action(opts)
utils.error "Cannot get issue/pr id"
end

local cb = function(label_id)
local query = graphql(opts.query_name, iid, label_id)
local cb = function(labels)
local label_ids = {}
for _, lbl in ipairs(labels) do
table.insert(label_ids, lbl.id)
end

local query = graphql(opts.query_name, iid, create_list(label_ids, format))
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ M.create_label_mutation = [[
-- https://docs.github.com/en/graphql/reference/mutations#removelabelsfromlabelable
M.add_labels_mutation = [[
mutation {
addLabelsToLabelable(input: {labelableId: "%s", labelIds: ["%s"]}) {
addLabelsToLabelable(input: {labelableId: "%s", labelIds: %s}) {
labelable {
... on Issue {
id
Expand All @@ -2546,7 +2546,7 @@ M.add_labels_mutation = [[
-- https://docs.github.com/en/graphql/reference/mutations#removelabelsfromlabelable
M.remove_labels_mutation = [[
mutation {
removeLabelsFromLabelable(input: {labelableId: "%s", labelIds: ["%s"]}) {
removeLabelsFromLabelable(input: {labelableId: "%s", labelIds: %s}) {
labelable {
... on Issue {
id
Expand Down
32 changes: 26 additions & 6 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,30 @@ end
--
-- LABELS
--

local function select(opts)
local prompt_bufnr = opts.bufnr
local single_cb = opts.single_cb
local multiple_cb = opts.multiple_cb

local picker = action_state.get_current_picker(prompt_bufnr)
local selections = picker:get_multi_selection()
local cb
local labels = {}
if #selections == 0 then
local selected_label = action_state.get_selected_entry(prompt_bufnr)
table.insert(labels, selected_label.label)
cb = single_cb
else
for _, selection in ipairs(selections) do
table.insert(labels, selection.label)
end
cb = multiple_cb
end
actions.close(prompt_bufnr)
cb(labels)
end

function M.select_label(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -767,9 +791,7 @@ function M.select_label(cb)
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
select { bufnr = prompt_bufnr, single_cb = cb, multiple_cb = cb }
end)
return true
end,
Expand Down Expand Up @@ -812,9 +834,7 @@ function M.select_assigned_label(cb)
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
select { bufnr = prompt_bufnr, single_cb = cb, multiple_cb = cb }
end)
return true
end,
Expand Down

0 comments on commit bac20e1

Please sign in to comment.