summaryrefslogtreecommitdiff
path: root/lua/telescope/sorters.lua
diff options
context:
space:
mode:
authorSimon Hauser <Simon-Hauser@outlook.de>2021-07-23 17:42:37 +0200
committerGitHub <noreply@github.com>2021-07-23 11:42:37 -0400
commit79644ab67731c7ba956c354bf0545282f34e10cc (patch)
treee65dbf73b3442ab1aa9fc59fc56a35b4c9edf1e0 /lua/telescope/sorters.lua
parent664690029fdb302bee8d3f27a458383e8477add7 (diff)
chore: use stylua for formatting (#1040)
* chore: stylua job and config * reformat with stylua
Diffstat (limited to 'lua/telescope/sorters.lua')
-rw-r--r--lua/telescope/sorters.lua178
1 files changed, 98 insertions, 80 deletions
diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua
index 46a6cc8..8874287 100644
--- a/lua/telescope/sorters.lua
+++ b/lua/telescope/sorters.lua
@@ -1,5 +1,5 @@
-local log = require('telescope.log')
-local util = require('telescope.utils')
+local log = require "telescope.log"
+local util = require "telescope.utils"
local sorters = {}
@@ -12,7 +12,7 @@ local ngram_highlighter = function(ngram_len, prompt, display)
if prompt:find(char, 1, true) then
table.insert(highlights, {
start = disp_index,
- finish = disp_index + ngram_len - 1
+ finish = disp_index + ngram_len - 1,
})
end
end
@@ -22,7 +22,6 @@ end
local FILTERED = -1
-
local Sorter = {}
Sorter.__index = Sorter
@@ -51,9 +50,9 @@ function Sorter:new(opts)
tags = opts.tags,
-- State management
- init = opts.init,
- start = opts.start,
- finish = opts.finish,
+ init = opts.init,
+ start = opts.start,
+ finish = opts.finish,
destroy = opts.destroy,
filter_function = opts.filter_function,
@@ -62,17 +61,21 @@ function Sorter:new(opts)
discard = opts.discard,
_discard_state = {
filtered = {},
- prompt = '',
+ prompt = "",
},
}, Sorter)
end
function Sorter:_init()
- if self.init then self:init() end
+ if self.init then
+ self:init()
+ end
end
function Sorter:_destroy()
- if self.destroy then self:destroy() end
+ if self.destroy then
+ self:destroy()
+ end
end
-- TODO: We could make this a bit smarter and cache results "as we go" and where they got filtered.
@@ -81,7 +84,9 @@ end
-- as he did in his example.
-- Example can be found in ./scratch/prime_prompt_cache.lua
function Sorter:_start(prompt)
- if self.start then self:start(prompt) end
+ if self.start then
+ self:start(prompt)
+ end
if not self.discard then
return
@@ -91,10 +96,10 @@ function Sorter:_start(prompt)
local len_previous = #previous
if #prompt < len_previous then
- log.debug("Reset discard because shorter prompt")
+ log.debug "Reset discard because shorter prompt"
self._discard_state.filtered = {}
elseif string.sub(prompt, 1, len_previous) ~= previous then
- log.debug("Reset discard no match")
+ log.debug "Reset discard no match"
self._discard_state.filtered = {}
end
@@ -102,13 +107,17 @@ function Sorter:_start(prompt)
end
function Sorter:_finish(prompt)
- if self.finish then self:finish(prompt) end
+ if self.finish then
+ self:finish(prompt)
+ end
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, cb_add, cb_filter)
- if not entry or not entry.ordinal then return end
+ if not entry or not entry.ordinal then
+ return
+ end
local ordinal = entry.ordinal
if self:_was_discarded(prompt, ordinal) then
@@ -117,7 +126,9 @@ function Sorter:score(prompt, entry, cb_add, cb_filter)
local filter_score
if self.filter_function ~= nil then
- if self.tags then self.tags:insert(entry) end
+ if self.tags then
+ self.tags:insert(entry)
+ end
filter_score, prompt = self:filter_function(prompt, entry)
end
@@ -159,7 +170,7 @@ sorters.Sorter = Sorter
TelescopeCachedTails = TelescopeCachedTails or nil
if not TelescopeCachedTails then
local os_sep = util.get_separator()
- local match_string = '[^' .. os_sep .. ']*$'
+ local match_string = "[^" .. os_sep .. "]*$"
TelescopeCachedTails = setmetatable({}, {
__index = function(t, k)
local tail = string.match(k, match_string)
@@ -170,20 +181,21 @@ if not TelescopeCachedTails then
})
end
-TelescopeCachedUppers = TelescopeCachedUppers or setmetatable({}, {
- __index = function(t, k)
- local obj = {}
- for i = 1, #k do
- local s_byte = k:byte(i, i)
- if s_byte <= 90 and s_byte >= 65 then
- obj[s_byte] = true
+TelescopeCachedUppers = TelescopeCachedUppers
+ or setmetatable({}, {
+ __index = function(t, k)
+ local obj = {}
+ for i = 1, #k do
+ local s_byte = k:byte(i, i)
+ if s_byte <= 90 and s_byte >= 65 then
+ obj[s_byte] = true
+ end
end
- end
- rawset(t, k, obj)
- return obj
- end
-})
+ rawset(t, k, obj)
+ return obj
+ end,
+ })
TelescopeCachedNgrams = TelescopeCachedNgrams or {}
@@ -201,7 +213,7 @@ sorters.get_fuzzy_file = function(opts)
local R = {}
for i = 1, s:len() - n + 1 do
- R[#R+1] = s:sub(i, i+n-1)
+ R[#R + 1] = s:sub(i, i + n - 1)
end
if not TelescopeCachedNgrams[s] then
@@ -267,19 +279,17 @@ sorters.get_fuzzy_file = function(opts)
end
local denominator = (
- (10 * match_count / #prompt_lower_ngrams)
- -- biases for shorter strings
- + 3 * match_count * ngram_len / #line
- + consecutive_matches
- + N / (contains_string or (2 * #line))
-
- -- + 30/(c1 or 2*N)
-
- -- TODO: It might be possible that this too strongly correlates,
- -- but it's unlikely for people to type capital letters without actually
- -- wanting to do something with a capital letter in it.
- + uppers_matching
- ) * tail_modifier
+ (10 * match_count / #prompt_lower_ngrams)
+ -- biases for shorter strings
+ + 3 * match_count * ngram_len / #line
+ + consecutive_matches
+ + N / (contains_string or (2 * #line))
+ -- + 30/(c1 or 2*N)
+ -- TODO: It might be possible that this too strongly correlates,
+ -- but it's unlikely for people to type capital letters without actually
+ -- wanting to do something with a capital letter in it.
+ + uppers_matching
+ ) * tail_modifier
if denominator == 0 or denominator ~= denominator then
return -1
@@ -310,7 +320,7 @@ sorters.get_generic_fuzzy_sorter = function(opts)
local R = {}
for i = 1, s:len() - n + 1 do
- R[#R+1] = s:sub(i, i+n-1)
+ R[#R + 1] = s:sub(i, i + n - 1)
end
if not TelescopeCachedNgrams[s] then
@@ -359,15 +369,15 @@ sorters.get_generic_fuzzy_sorter = function(opts)
-- TODO: Copied from ashkan.
local denominator = (
- (10 * match_count / #prompt_ngrams)
- -- biases for shorter strings
- -- TODO(ashkan): this can bias towards repeated finds of the same
- -- subpattern with overlapping_ngrams
- + 3 * match_count * ngram_len / #line
- + consecutive_matches
- + N / (contains_string or (2 * #line))
- -- + 30/(c1 or 2*N)
- )
+ (10 * match_count / #prompt_ngrams)
+ -- biases for shorter strings
+ -- TODO(ashkan): this can bias towards repeated finds of the same
+ -- subpattern with overlapping_ngrams
+ + 3 * match_count * ngram_len / #line
+ + consecutive_matches
+ + N / (contains_string or (2 * #line)) -- + 30/(c1 or 2*N)
+
+ )
if denominator == 0 or denominator ~= denominator then
return -1
@@ -406,17 +416,17 @@ sorters.fuzzy_with_index_bias = function(opts)
else
return math.min(math.pow(entry.index, 0.25), 2) * base_score
end
- end
+ end,
}
end
-- Sorter using the fzy algorithm
sorters.get_fzy_sorter = function(opts)
opts = opts or {}
- local fzy = opts.fzy_mod or require('telescope.algos.fzy')
+ local fzy = opts.fzy_mod or require "telescope.algos.fzy"
local OFFSET = -fzy.get_score_floor()
- return sorters.Sorter:new{
+ return sorters.Sorter:new {
discard = true,
scoring_function = function(_, prompt, line)
@@ -454,10 +464,12 @@ end
sorters.highlighter_only = function(opts)
opts = opts or {}
- local fzy = opts.fzy_mod or require('telescope.algos.fzy')
+ local fzy = opts.fzy_mod or require "telescope.algos.fzy"
return Sorter:new {
- scoring_function = function() return 0 end,
+ scoring_function = function()
+ return 0
+ end,
highlighter = function(_, prompt, display)
return fzy.positions(prompt, display)
@@ -467,7 +479,9 @@ end
sorters.empty = function()
return Sorter:new {
- scoring_function = function() return 0 end,
+ scoring_function = function()
+ return 0
+ end,
}
end
@@ -475,8 +489,8 @@ end
sorters.get_levenshtein_sorter = function()
return Sorter:new {
scoring_function = function(_, prompt, line)
- return require('telescope.algos.string_distance')(prompt, line)
- end
+ return require "telescope.algos.string_distance"(prompt, line)
+ end,
}
end
@@ -490,7 +504,7 @@ local substr_highlighter = function(_, prompt, display)
for _, word in pairs(search_terms) do
hl_start, hl_end = display:find(word, 1, true)
if hl_start then
- table.insert(highlights, {start = hl_start, finish = hl_end})
+ table.insert(highlights, { start = hl_start, finish = hl_end })
end
end
@@ -501,20 +515,20 @@ sorters.get_substr_matcher = function()
return Sorter:new {
highlighter = substr_highlighter,
scoring_function = function(_, prompt, _, entry)
- local display = entry.ordinal:lower()
-
- local search_terms = util.max_split(prompt, "%s")
- local matched = 0
- local total_search_terms = 0
- for _, word in pairs(search_terms) do
- total_search_terms = total_search_terms + 1
- if display:find(word, 1, true) then
- matched = matched + 1
+ local display = entry.ordinal:lower()
+
+ local search_terms = util.max_split(prompt, "%s")
+ local matched = 0
+ local total_search_terms = 0
+ for _, word in pairs(search_terms) do
+ total_search_terms = total_search_terms + 1
+ if display:find(word, 1, true) then
+ matched = matched + 1
+ end
end
- end
- return matched == total_search_terms and entry.index or -1
- end
+ return matched == total_search_terms and entry.index or -1
+ end,
}
end
@@ -552,25 +566,29 @@ local filter_function = function(opts)
end
local function create_tag_set(tag)
- tag = vim.F.if_nil(tag, 'ordinal')
+ tag = vim.F.if_nil(tag, "ordinal")
local set = {}
return setmetatable(set, {
__index = {
insert = function(set_, entry)
local value = entry[tag]
- if not set_[value] then set_[value] = true end
- end
- }
+ if not set_[value] then
+ set_[value] = true
+ end
+ end,
+ },
})
end
sorters.prefilter = function(opts)
local sorter = opts.sorter
- opts.delimiter = util.get_default(opts.delimiter, ':')
+ opts.delimiter = util.get_default(opts.delimiter, ":")
sorter._delimiter = opts.delimiter
sorter.tags = create_tag_set(opts.tag)
sorter.filter_function = filter_function(opts)
- sorter._was_discarded = function() return false end
+ sorter._was_discarded = function()
+ return false
+ end
return sorter
end