Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): make telescope speedier #72

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -- 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")
Expand Down
36 changes: 7 additions & 29 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 @@ -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
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 Expand Up @@ -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,
Expand Down
Loading