summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSenghan Bright <senghan.bright@deltaprojects.com>2021-01-18 17:53:52 +0100
committerGitHub <noreply@github.com>2021-01-18 17:53:52 +0100
commit7d4d3462e990e2af489eb285aa7041d0b787c560 (patch)
treef2e2b2383a8decf179a752ee58e7a950ac3369c0
parent7e241aa0a45369b8187a4a0cf8d5bb9087286ecc (diff)
Substring matcher (#443)
* add substr_matcher * correct function name * use util.split function and remove unused opts
-rw-r--r--lua/telescope/sorters.lua38
1 files changed, 38 insertions, 0 deletions
diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua
index 1ac410f..a85a75b 100644
--- a/lua/telescope/sorters.lua
+++ b/lua/telescope/sorters.lua
@@ -429,4 +429,42 @@ sorters.get_levenshtein_sorter = function()
}
end
+local substr_highlighter = function(_, prompt, display)
+ local highlights = {}
+ display = display:lower()
+
+ local search_terms = util.max_split(prompt, "%s")
+ local hl_start, hl_end
+
+ 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})
+ end
+ end
+
+ return highlights
+end
+
+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
+ end
+ end
+
+ return matched == total_search_terms and entry.index or -1
+ end
+ }
+end
+
return sorters