From c71dbe54e66595e9c88564ce709ab856ceba6cf6 Mon Sep 17 00:00:00 2001 From: jghauser Date: Sun, 16 Jun 2024 15:55:04 +0200 Subject: [PATCH] feat(search): make telescope speedier --- lua/papis/search/data.lua | 2 ++ lua/papis/search/init.lua | 49 +++++++++++++++++++++++++++++ lua/telescope/_extensions/papis.lua | 36 +++++---------------- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lua/papis/search/data.lua b/lua/papis/search/data.lua index 78147db..1a40974 100644 --- a/lua/papis/search/data.lua +++ b/lua/papis/search/data.lua @@ -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 = { diff --git a/lua/papis/search/init.lua b/lua/papis/search/init.lua index d015232..48cb345 100644 --- a/lua/papis/search/init.lua +++ b/lua/papis/search/init.lua @@ -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 -- HACK: there must better way to turn this off 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") diff --git a/lua/telescope/_extensions/papis.lua b/lua/telescope/_extensions/papis.lua index c057e23..c6f65ff 100644 --- a/lua/telescope/_extensions/papis.lua +++ b/lua/telescope/_extensions/papis.lua @@ -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") @@ -31,14 +30,17 @@ local function parse_format_string() return cite_format end -local wrap, preview_format, required_db_keys, initial_sort_by_time_added +local wrap, preview_format, initial_sort_by_time_added ---Defines the papis.nvim telescope picker ---@param opts table #Options for the papis picker 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 @@ -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({ @@ -138,9 +119,6 @@ return telescope.register_extension({ wrap = opts["wrap"] initial_sort_by_time_added = opts["initial_sort_by_time_added"] preview_format = opts["preview_format"] - local search_keys = opts["search_keys"] - local results_format = opts["results_format"] - required_db_keys = utils:get_required_db_keys({ { "papis_id" }, search_keys, preview_format, results_format }) end, exports = { papis = papis_picker,