From 30cd282ae203ead547b5f6ffc3986d3c06c94c7c Mon Sep 17 00:00:00 2001 From: jghauser Date: Sun, 7 Jul 2024 13:44:58 +0200 Subject: [PATCH] feat: enable acting on multiple selections in telescope --- lua/papis/utils.lua | 8 +- lua/telescope/_extensions/papis/actions.lua | 86 +++++++++++++++++---- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/lua/papis/utils.lua b/lua/papis/utils.lua index 3f1a930..86c0424 100644 --- a/lua/papis/utils.lua +++ b/lua/papis/utils.lua @@ -111,7 +111,7 @@ function M:do_open_attached_files(papis_id) if vim.tbl_isempty(filenames) then vim.notify("This item has no attached files.", vim.log.levels.WARN) elseif #filenames == 1 then - vim.notify("Opening file '" .. filenames[1] .. "' ", vim.log.levels.INFO) + log.info(string.format("Opening file '%s' ", filenames[1])) local path = lookup_tbl[filenames[1]] self:do_open_file_external(path) else @@ -119,7 +119,7 @@ function M:do_open_attached_files(papis_id) prompt = "Select attachment to open:", }, function(choice) if choice then - vim.notify("Opening file '" .. choice .. "' ", vim.log.levels.INFO) + log.info(string.format("Opening file '%s' ", choice)) local path = lookup_tbl[choice] self:do_open_file_external(path) end @@ -216,7 +216,9 @@ function M:do_open_text_file(papis_id, type) nuiline:render(popup.bufnr, -1, k) end - popup:mount() + vim.schedule(function() + popup:mount() + end) end elseif type == "info" then log.debug("Opening an info file") diff --git a/lua/telescope/_extensions/papis/actions.lua b/lua/telescope/_extensions/papis/actions.lua index 3365ba8..446b574 100644 --- a/lua/telescope/_extensions/papis/actions.lua +++ b/lua/telescope/_extensions/papis/actions.lua @@ -11,50 +11,108 @@ local db = require("papis.sqlite-wrapper") local utils = require("papis.utils") +local get_multi = function(prompt_bufnr) + local picker = require('telescope.actions.state').get_current_picker(prompt_bufnr) + local multi = picker:get_multi_selection() + return multi +end + local M = {} ----This function inserts a formatted reference string at the cursor +---This function inserts a formatted ref string at the cursor ---@param prompt_bufnr number @The buffer number of the prompt ---@param format_string string @The string to be inserted M.ref_insert = function(prompt_bufnr, format_string) - local entry = string.format(format_string, action_state.get_selected_entry().id.ref) + local multi = get_multi(prompt_bufnr) + actions.close(prompt_bufnr) - vim.api.nvim_put({ entry }, "", false, true) + local string_to_insert = "" + if vim.tbl_isempty(multi) then + local ref = string.format(format_string, action_state.get_selected_entry().id.ref) + string_to_insert = ref + else + for _, entry in pairs(multi) do + local ref = string.format(format_string, entry.id.ref) + string_to_insert = string_to_insert .. ref .. " " + end + end + vim.api.nvim_put({ string_to_insert }, "", false, true) end ----This function inserts a formatted reference string at the cursor +---This function inserts a formatted full reference at the cursor ---@param prompt_bufnr number @The buffer number of the prompt M.ref_insert_formatted = function(prompt_bufnr) + local multi = get_multi(prompt_bufnr) + actions.close(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id - local entry = db.data:get({ papis_id = papis_id })[1] - local reference = config["formatter"].format_references_fn(entry) + local string_to_insert = "" + if vim.tbl_isempty(multi) then + local papis_id = action_state.get_selected_entry().id.papis_id + local full_entry = db.data:get({ papis_id = papis_id })[1] + local full_reference = config["formatter"].format_references_fn(full_entry) + string_to_insert = full_reference + else + for _, entry in pairs(multi) do + local papis_id = entry.id.papis_id + local full_entry = db.data:get({ papis_id = papis_id })[1] + local full_reference = config["formatter"].format_references_fn(full_entry) + string_to_insert = string_to_insert .. full_reference .. " " + end + end - vim.api.nvim_put({ reference }, "", false, true) + vim.api.nvim_put({ string_to_insert }, "", false, true) end ---This function opens the files attached to the current entry ---@param prompt_bufnr number @The buffer number of the prompt M.open_file = function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id + local multi = get_multi(prompt_bufnr) + actions.close(prompt_bufnr) - utils:do_open_attached_files(papis_id) + if vim.tbl_isempty(multi) then + local papis_id = action_state.get_selected_entry().id.papis_id + utils:do_open_attached_files(papis_id) + else + for _, entry in pairs(multi) do + local papis_id = entry.id.papis_id + utils:do_open_attached_files(papis_id) + end + end end ---This function opens the note attached to the current entry ---@param prompt_bufnr number @The buffer number of the prompt M.open_note = function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id + local multi = get_multi(prompt_bufnr) + actions.close(prompt_bufnr) - utils:do_open_text_file(papis_id, "note") + if vim.tbl_isempty(multi) then + local papis_id = action_state.get_selected_entry().id.papis_id + utils:do_open_text_file(papis_id, "note") + else + for _, entry in pairs(multi) do + -- TODO: this only opens one note if a note needs to be created + local papis_id = entry.id.papis_id + utils:do_open_text_file(papis_id, "note") + end + end end ---This function opens the info_file containing this entry's information ---@param prompt_bufnr number @The buffer number of the prompt M.open_info = function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id + local multi = get_multi(prompt_bufnr) + actions.close(prompt_bufnr) - utils:do_open_text_file(papis_id, "info") + if vim.tbl_isempty(multi) then + local papis_id = action_state.get_selected_entry().id.papis_id + utils:do_open_text_file(papis_id, "info") + else + for _, entry in pairs(multi) do + local papis_id = entry.id.papis_id + utils:do_open_text_file(papis_id, "info") + end + end end return M