Skip to content

Commit 091e6df

Browse files
author
jghauser
committed
feat: enable acting on multiple selections in telescope
1 parent 7319769 commit 091e6df

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

lua/papis/utils.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ function M:do_open_attached_files(papis_id)
111111
if vim.tbl_isempty(filenames) then
112112
vim.notify("This item has no attached files.", vim.log.levels.WARN)
113113
elseif #filenames == 1 then
114-
vim.notify("Opening file '" .. filenames[1] .. "' ", vim.log.levels.INFO)
114+
log.info(string.format("Opening file '%s' ", filenames[1]))
115115
local path = lookup_tbl[filenames[1]]
116116
self:do_open_file_external(path)
117117
else
118118
vim.ui.select(filenames, {
119119
prompt = "Select attachment to open:",
120120
}, function(choice)
121121
if choice then
122-
vim.notify("Opening file '" .. choice .. "' ", vim.log.levels.INFO)
122+
log.info(string.format("Opening file '%s' ", choice))
123123
local path = lookup_tbl[choice]
124124
self:do_open_file_external(path)
125125
end
@@ -216,7 +216,9 @@ function M:do_open_text_file(papis_id, type)
216216
nuiline:render(popup.bufnr, -1, k)
217217
end
218218

219-
popup:mount()
219+
vim.schedule(function()
220+
popup:mount()
221+
end)
220222
end
221223
elseif type == "info" then
222224
log.debug("Opening an info file")

lua/telescope/_extensions/papis/actions.lua

+72-14
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,108 @@ local db = require("papis.sqlite-wrapper")
1111

1212
local utils = require("papis.utils")
1313

14+
local get_multi = function(prompt_bufnr)
15+
local picker = require('telescope.actions.state').get_current_picker(prompt_bufnr)
16+
local multi = picker:get_multi_selection()
17+
return multi
18+
end
19+
1420
local M = {}
1521

16-
---This function inserts a formatted reference string at the cursor
22+
---This function inserts a formatted ref string at the cursor
1723
---@param prompt_bufnr number @The buffer number of the prompt
1824
---@param format_string string @The string to be inserted
1925
M.ref_insert = function(prompt_bufnr, format_string)
20-
local entry = string.format(format_string, action_state.get_selected_entry().id.ref)
26+
local multi = get_multi(prompt_bufnr)
27+
2128
actions.close(prompt_bufnr)
22-
vim.api.nvim_put({ entry }, "", false, true)
29+
local string_to_insert = ""
30+
if vim.tbl_isempty(multi) then
31+
local ref = string.format(format_string, action_state.get_selected_entry().id.ref)
32+
string_to_insert = ref
33+
else
34+
for _, entry in pairs(multi) do
35+
local ref = string.format(format_string, entry.id.ref)
36+
string_to_insert = string_to_insert .. ref .. " "
37+
end
38+
end
39+
vim.api.nvim_put({ string_to_insert }, "", false, true)
2340
end
2441

25-
---This function inserts a formatted reference string at the cursor
42+
---This function inserts a formatted full reference at the cursor
2643
---@param prompt_bufnr number @The buffer number of the prompt
2744
M.ref_insert_formatted = function(prompt_bufnr)
45+
local multi = get_multi(prompt_bufnr)
46+
2847
actions.close(prompt_bufnr)
29-
local papis_id = action_state.get_selected_entry().id.papis_id
30-
local entry = db.data:get({ papis_id = papis_id })[1]
31-
local reference = config["formatter"].format_references_fn(entry)
48+
local string_to_insert = ""
49+
if vim.tbl_isempty(multi) then
50+
local papis_id = action_state.get_selected_entry().id.papis_id
51+
local full_entry = db.data:get({ papis_id = papis_id })[1]
52+
local full_reference = config["formatter"].format_references_fn(full_entry)
53+
string_to_insert = full_reference
54+
else
55+
for _, entry in pairs(multi) do
56+
local papis_id = entry.id.papis_id
57+
local full_entry = db.data:get({ papis_id = papis_id })[1]
58+
local full_reference = config["formatter"].format_references_fn(full_entry)
59+
string_to_insert = string_to_insert .. full_reference .. " "
60+
end
61+
end
3262

33-
vim.api.nvim_put({ reference }, "", false, true)
63+
vim.api.nvim_put({ string_to_insert }, "", false, true)
3464
end
3565

3666
---This function opens the files attached to the current entry
3767
---@param prompt_bufnr number @The buffer number of the prompt
3868
M.open_file = function(prompt_bufnr)
39-
local papis_id = action_state.get_selected_entry().id.papis_id
69+
local multi = get_multi(prompt_bufnr)
70+
4071
actions.close(prompt_bufnr)
41-
utils:do_open_attached_files(papis_id)
72+
if vim.tbl_isempty(multi) then
73+
local papis_id = action_state.get_selected_entry().id.papis_id
74+
utils:do_open_attached_files(papis_id)
75+
else
76+
for _, entry in pairs(multi) do
77+
local papis_id = entry.id.papis_id
78+
utils:do_open_attached_files(papis_id)
79+
end
80+
end
4281
end
4382

4483
---This function opens the note attached to the current entry
4584
---@param prompt_bufnr number @The buffer number of the prompt
4685
M.open_note = function(prompt_bufnr)
47-
local papis_id = action_state.get_selected_entry().id.papis_id
86+
local multi = get_multi(prompt_bufnr)
87+
4888
actions.close(prompt_bufnr)
49-
utils:do_open_text_file(papis_id, "note")
89+
if vim.tbl_isempty(multi) then
90+
local papis_id = action_state.get_selected_entry().id.papis_id
91+
utils:do_open_text_file(papis_id, "note")
92+
else
93+
for _, entry in pairs(multi) do
94+
-- TODO: this only opens one note if a note needs to be created
95+
local papis_id = entry.id.papis_id
96+
utils:do_open_text_file(papis_id, "note")
97+
end
98+
end
5099
end
51100

52101
---This function opens the info_file containing this entry's information
53102
---@param prompt_bufnr number @The buffer number of the prompt
54103
M.open_info = function(prompt_bufnr)
55-
local papis_id = action_state.get_selected_entry().id.papis_id
104+
local multi = get_multi(prompt_bufnr)
105+
56106
actions.close(prompt_bufnr)
57-
utils:do_open_text_file(papis_id, "info")
107+
if vim.tbl_isempty(multi) then
108+
local papis_id = action_state.get_selected_entry().id.papis_id
109+
utils:do_open_text_file(papis_id, "info")
110+
else
111+
for _, entry in pairs(multi) do
112+
local papis_id = entry.id.papis_id
113+
utils:do_open_text_file(papis_id, "info")
114+
end
115+
end
58116
end
59117

60118
return M

0 commit comments

Comments
 (0)