From bac20e18a68356488b1031ed36d78de49edd377d Mon Sep 17 00:00:00 2001 From: Will Dean Date: Mon, 16 Dec 2024 00:39:52 +0100 Subject: [PATCH] implement telescope multiselect --- lua/octo/commands.lua | 25 +++++++++++++++++-- lua/octo/gh/graphql.lua | 4 ++-- lua/octo/pickers/telescope/provider.lua | 32 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/lua/octo/commands.lua b/lua/octo/commands.lua index 917fbb112..bd276db06 100644 --- a/lua/octo/commands.lua +++ b/lua/octo/commands.lua @@ -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 @@ -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) diff --git a/lua/octo/gh/graphql.lua b/lua/octo/gh/graphql.lua index 377225f47..e721fd571 100644 --- a/lua/octo/gh/graphql.lua +++ b/lua/octo/gh/graphql.lua @@ -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 @@ -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 diff --git a/lua/octo/pickers/telescope/provider.lua b/lua/octo/pickers/telescope/provider.lua index 765dec964..cc0f04954 100644 --- a/lua/octo/pickers/telescope/provider.lua +++ b/lua/octo/pickers/telescope/provider.lua @@ -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() @@ -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, @@ -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,