Skip to content

Commit

Permalink
refactor(telescope): don't have actions that return fns
Browse files Browse the repository at this point in the history
  • Loading branch information
jghauser committed Jul 6, 2024
1 parent a978704 commit ffc3af2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
37 changes: 27 additions & 10 deletions lua/telescope/_extensions/papis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<c-o>f", papis_actions.open_file(), { desc = "Open file" })
map("n", "of", papis_actions.open_file(), { desc = "Open file" })
map("i", "<c-o>n", papis_actions.open_note(), { desc = "Open note" })
map("n", "on", papis_actions.open_note(), { desc = "Open note" })
map("i", "<c-e>", 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", "<c-f>", 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", "<c-o>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", "<c-o>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", "<c-e>",
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", "<c-f>",
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,
Expand Down
66 changes: 29 additions & 37 deletions lua/telescope/_extensions/papis/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ffc3af2

Please sign in to comment.