From 64e59060b1750d0c86761693b6847c3db07afcd2 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Thu, 8 Apr 2021 10:35:44 -0400 Subject: feat: asyncify pickers - except for live_grep (#709) * something kind of works already * yayayayayayayayayayayayayayayayayayayayayayayayayayayayayayayayaya * use async for everything besides live jobs * fix: fixup autocmds previewer * fix: lints for prime * temp: Add example of how we can think about async sorters * feat: Allow picker to decide when to cancel * fix: simplify scoring logic and tests * fixup: name * fix: Move back towards more backwards compat methods * fixup: Remove results from opts * fixup: remove trailing quote * fixup: Attempt to clean up some more async items. Next is status * wip: Add todo for when bfredl implements extmarks over the EOL * wip * fixup: got em * fixup: cleaning * fixup: docs --- lua/telescope/sorters.lua | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'lua/telescope/sorters.lua') diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index 5ac7086..4147f59 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -32,12 +32,17 @@ Sorter.__index = Sorter --- --- Lower number is better (because it's like a closer match) --- But, any number below 0 means you want that line filtered out. ---- @field scoring_function function Function that has the interface: --- (sorter, prompt, line): number +---@field scoring_function function: Function that has the interface: (sorter, prompt, line): number +---@field tags table: Unique tags collected at filtering for tag completion +---@field filter_function function: Function that can filter results +---@field highlighter function: Highlights results to display them pretty +---@field discard boolean: Whether this is a discardable style sorter or not. +---@field score function: Override the score function if desired. function Sorter:new(opts) opts = opts or {} return setmetatable({ + score = opts.score, state = {}, tags = opts.tags, filter_function = opts.filter_function, @@ -77,13 +82,12 @@ end -- TODO: Consider doing something that makes it so we can skip the filter checks -- if we're not discarding. Also, that means we don't have to check otherwise as well :) -function Sorter:score(prompt, entry) - if not entry or not entry.ordinal then return -1 end +function Sorter:score(prompt, entry, cb_add, cb_filter) + if not entry or not entry.ordinal then return end local ordinal = entry.ordinal - if self:_was_discarded(prompt, ordinal) then - return FILTERED + return cb_filter(entry) end local filter_score @@ -92,14 +96,21 @@ function Sorter:score(prompt, entry) filter_score, prompt = self:filter_function(prompt, entry) end - local score = (filter_score == FILTERED and FILTERED or - self:scoring_function(prompt or "", ordinal, entry)) + if filter_score == FILTERED then + return cb_filter(entry) + end + local score = self:scoring_function(prompt or "", ordinal, entry) if score == FILTERED then self:_mark_discarded(prompt, ordinal) + return cb_filter(entry) end - return score + if cb_add then + return cb_add(score, entry) + else + return score + end end function Sorter:_was_discarded(prompt, ordinal) -- cgit v1.2.3