Skip to content

Commit

Permalink
feat(search): make telescope speedier
Browse files Browse the repository at this point in the history
  • Loading branch information
jghauser committed Jun 16, 2024
1 parent 31304b0 commit a420295
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
2 changes: 2 additions & 0 deletions lua/papis/search/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ local function init_tbl()

local timestamp = make_timestamp(entry)

require("papis.search").update_precalc(entry)

self:__update({
where = { id = id },
set = {
Expand Down
49 changes: 49 additions & 0 deletions lua/papis/search/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,59 @@ if not has_telescope then
log.error("The plugin telescope.nvim wasn't found but the search module is enabled and requires it.")
return nil
end
local entry_display = require("telescope.pickers.entry_display")
entry_display.truncate = function(a, ...) return a end
local config = require("papis.config")
local db = require("papis.sqlite-wrapper")
if not db then
return nil
end

local telescope_precalc = {}

local entry_maker = function(entry)
local entry_pre_calc = db["search"]:get(entry["id"])[1]
local timestamp = entry_pre_calc["timestamp"]
local items = entry_pre_calc["items"]

local displayer_tbl = entry_pre_calc["displayer_tbl"]
local displayer = entry_display.create({
separator = "",
items = items,
})

local make_display = function()
return displayer(displayer_tbl)
end

local search_string = entry_pre_calc["search_string"]
return {
value = search_string,
ordinal = search_string,
display = make_display,
timestamp = timestamp,
id = entry,
}
end

local M = {}

function M.update_precalc(entry)
local id = entry["id"]
telescope_precalc[id] = entry_maker(entry)
end

function M.get_precalc()
if vim.tbl_isempty(telescope_precalc) then
local entries = db.data:get()
for _, entry in ipairs(entries) do
local id = entry["id"]
telescope_precalc[id] = entry_maker(entry)
end
end
return telescope_precalc
end

---Sets up the papis.nvim telescope extension
function M.setup()
log.debug("Search: setting up module")
Expand Down
31 changes: 6 additions & 25 deletions lua/telescope/_extensions/papis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ local pickers = require("telescope.pickers")
local actions = require("telescope.actions")
local previewers = require("telescope.previewers")
local telescope_config = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")
local papis_actions = require("telescope._extensions.papis.actions")

local utils = require("papis.utils")
Expand All @@ -38,7 +37,10 @@ local wrap, preview_format, required_db_keys, initial_sort_by_time_added
local function papis_picker(opts)
opts = opts or {}

local results = db.data:get(nil, required_db_keys)
-- get precalculated entries for the telescope picker
local telescope_precalc = require("papis.search").get_precalc()

-- local results = db.data:get(nil, required_db_keys)
local format_string = parse_format_string()

-- amend the generic_sorter so that we can change initial sorting
Expand Down Expand Up @@ -67,30 +69,9 @@ local function papis_picker(opts)
.new(opts, {
prompt_title = "Papis References",
finder = finders.new_table({
results = results,
results = telescope_precalc,
entry_maker = function(entry)
local entry_pre_calc = db["search"]:get(entry["id"])[1]
local timestamp = entry_pre_calc["timestamp"]
local items = entry_pre_calc["items"]

local displayer_tbl = entry_pre_calc["displayer_tbl"]
local displayer = entry_display.create({
separator = "",
items = items,
})

local make_display = function()
return displayer(displayer_tbl)
end

local search_string = entry_pre_calc["search_string"]
return {
value = search_string,
ordinal = search_string,
display = make_display,
timestamp = timestamp,
id = entry,
}
return entry
end,
}),
previewer = previewers.new_buffer_previewer({
Expand Down

0 comments on commit a420295

Please sign in to comment.