Skip to content

Commit

Permalink
feat: enable acting on multiple selections in telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
jghauser committed Jul 7, 2024
1 parent ffc3af2 commit 30cd282
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
8 changes: 5 additions & 3 deletions lua/papis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ 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
vim.ui.select(filenames, {
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
Expand Down Expand Up @@ -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")
Expand Down
86 changes: 72 additions & 14 deletions lua/telescope/_extensions/papis/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 30cd282

Please sign in to comment.