From ffc3af2774af979ac45e4bb98519fcb10ac9a187 Mon Sep 17 00:00:00 2001 From: jghauser Date: Sat, 6 Jul 2024 16:19:46 +0200 Subject: [PATCH] refactor(telescope): don't have actions that return fns --- lua/telescope/_extensions/papis.lua | 37 ++++++++---- lua/telescope/_extensions/papis/actions.lua | 66 +++++++++------------ 2 files changed, 56 insertions(+), 47 deletions(-) diff --git a/lua/telescope/_extensions/papis.lua b/lua/telescope/_extensions/papis.lua index a02656a..8e5a6e9 100644 --- a/lua/telescope/_extensions/papis.lua +++ b/lua/telescope/_extensions/papis.lua @@ -86,16 +86,33 @@ local function papis_picker(opts) end, }), sorter = papis_sorter, - attach_mappings = function(_, map) - actions.select_default:replace(papis_actions.ref_insert(format_string)) - map("i", "f", papis_actions.open_file(), { desc = "Open file" }) - map("n", "of", papis_actions.open_file(), { desc = "Open file" }) - map("i", "n", papis_actions.open_note(), { desc = "Open note" }) - map("n", "on", papis_actions.open_note(), { desc = "Open note" }) - map("i", "", papis_actions.open_info(), { desc = "Open info.yaml file" }) - map("n", "e", papis_actions.open_info(), { desc = "Open info.yaml file" }) - map("n", "f", papis_actions.ref_insert_formatted(), { desc = "Insert formatted reference" }) - map("i", "", papis_actions.ref_insert_formatted(), { desc = "Insert formatted reference" }) + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace( + function() papis_actions.ref_insert(prompt_bufnr, format_string) end) + map("i", "f", + function() papis_actions.open_file(prompt_bufnr) end, + { desc = "Open file" }) + map("n", "of", + function() papis_actions.open_file(prompt_bufnr) end, + { desc = "Open file" }) + map("i", "n", + function() papis_actions.open_note(prompt_bufnr) end, + { desc = "Open note" }) + map("n", "on", + function() papis_actions.open_note(prompt_bufnr) end, + { desc = "Open note" }) + map("i", "", + function() papis_actions.open_info(prompt_bufnr) end, + { desc = "Open info.yaml file" }) + map("n", "e", + function() papis_actions.open_info(prompt_bufnr) end, + { desc = "Open info.yaml file" }) + map("n", "f", + function() papis_actions.ref_insert_formatted(prompt_bufnr) end, + { desc = "Insert formatted reference" }) + map("i", "", + function() papis_actions.ref_insert_formatted(prompt_bufnr) end, + { desc = "Insert formatted reference" }) -- Makes sure that the other defaults are still applied return true end, diff --git a/lua/telescope/_extensions/papis/actions.lua b/lua/telescope/_extensions/papis/actions.lua index 0113ed5..3365ba8 100644 --- a/lua/telescope/_extensions/papis/actions.lua +++ b/lua/telescope/_extensions/papis/actions.lua @@ -14,55 +14,47 @@ local utils = require("papis.utils") local M = {} ---This function inserts a formatted reference string at the cursor +---@param prompt_bufnr number @The buffer number of the prompt ---@param format_string string @The string to be inserted ----@return function -M.ref_insert = function(format_string) - return function(prompt_bufnr) - local entry = string.format(format_string, action_state.get_selected_entry().id.ref) - actions.close(prompt_bufnr) - vim.api.nvim_put({ entry }, "", false, true) - end +M.ref_insert = function(prompt_bufnr, format_string) + local entry = string.format(format_string, action_state.get_selected_entry().id.ref) + actions.close(prompt_bufnr) + vim.api.nvim_put({ entry }, "", false, true) end -M.ref_insert_formatted = function() - return function(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) - - vim.api.nvim_put({ reference }, "", false, true) - end +---This function inserts a formatted reference string at the cursor +---@param prompt_bufnr number @The buffer number of the prompt +M.ref_insert_formatted = function(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) + + vim.api.nvim_put({ reference }, "", false, true) end ---This function opens the files attached to the current entry ----@return function -M.open_file = function() - return function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id - actions.close(prompt_bufnr) - utils:do_open_attached_files(papis_id) - end +---@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 + actions.close(prompt_bufnr) + utils:do_open_attached_files(papis_id) end ---This function opens the note attached to the current entry ----@return function -M.open_note = function() - return function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id - actions.close(prompt_bufnr) - utils:do_open_text_file(papis_id, "note") - end +---@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 + actions.close(prompt_bufnr) + utils:do_open_text_file(papis_id, "note") end ---This function opens the info_file containing this entry's information ----@return function -M.open_info = function() - return function(prompt_bufnr) - local papis_id = action_state.get_selected_entry().id.papis_id - actions.close(prompt_bufnr) - utils:do_open_text_file(papis_id, "info") - end +---@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 + actions.close(prompt_bufnr) + utils:do_open_text_file(papis_id, "info") end return M