summaryrefslogtreecommitdiff
path: root/lua/telescope/pickers/scroller.lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2021-01-11 13:29:37 -0500
committerGitHub <noreply@github.com>2021-01-11 13:29:37 -0500
commit8783bea06e1e0dfa8dfd4834058923088471d832 (patch)
tree050096ba649a94bb01e7c0b3a029e9b4eb060029 /lua/telescope/pickers/scroller.lua
parentde80a9837cd1d207981c1f6dbf504436f8bfee13 (diff)
feat: quickfix (#293)
* feat: quickfix (not implemented) * [WIP]: Wed 09 Dec 2020 11:11:30 PM EST * somewhat working linked list impl * getting closer * might be working * might be working for real * works and implemented basic example * dont forget to close prompt * fix descending and add more tests * test fixes * fix test * more logging * Fix some more tests * Fix logging messing up tests * fix: lint * fix: multi select stuffs
Diffstat (limited to 'lua/telescope/pickers/scroller.lua')
-rw-r--r--lua/telescope/pickers/scroller.lua93
1 files changed, 56 insertions, 37 deletions
diff --git a/lua/telescope/pickers/scroller.lua b/lua/telescope/pickers/scroller.lua
index 324627b..3169c4b 100644
--- a/lua/telescope/pickers/scroller.lua
+++ b/lua/telescope/pickers/scroller.lua
@@ -1,56 +1,75 @@
local scroller = {}
-local calc_count_fn = function(sorting_strategy)
- if sorting_strategy == 'ascending' then
- return function(a, b) return math.min(a, b) end
- else
- return function(a, b, row)
- if a == b or not row then
- return math.max(a, b)
- else
- local x = a - b
- if row < x then
- return math.max(a, b) - 1, true
- elseif row == a then
- return x, true
- else
- return math.max(a, b)
- end
- end
- end
- end
-end
+local range_calculators = {
+ ascending = function(max_results, num_results)
+ return 0, math.min(max_results, num_results)
+ end,
-scroller.create = function(strategy, sorting_strategy)
- local calc_count = calc_count_fn(sorting_strategy)
+ descending = function(max_results, num_results)
+ return math.max(max_results - num_results, 0), max_results
+ end,
+}
- if strategy == 'cycle' then
+local scroll_calculators = {
+ cycle = function(range_fn)
return function(max_results, num_results, row)
- local count, b = calc_count(max_results, num_results, row)
- if b then return count end
+ local start, finish = range_fn(max_results, num_results)
- if row >= count then
- return 0
- elseif row < 0 then
- return count - 1
+ if row >= finish then
+ return start
+ elseif row < start then
+ return finish - 1
end
return row
end
- elseif strategy == 'limit' or strategy == nil then
+ end,
+
+ limit = function(range_fn)
return function(max_results, num_results, row)
- local count = calc_count(max_results, num_results)
+ local start, finish = range_fn(max_results, num_results)
- if row >= count then
- return count - 1
- elseif row < 0 then
- return 0
+ if row >= finish then
+ return finish - 1
+ elseif row < start then
+ return start
end
return row
end
- else
- error("Unsupported strategy: " .. strategy)
+ end,
+}
+
+scroller.create = function(scroll_strategy, sorting_strategy)
+ local range_fn = range_calculators[sorting_strategy]
+ if not range_fn then
+ error(debug.traceback("Unknown sorting strategy: " .. sorting_strategy))
+ end
+
+ local scroll_fn = scroll_calculators[scroll_strategy]
+ if not scroll_fn then
+ error(debug.traceback("Unknown scroll strategy: " .. (scroll_strategy or '')))
+ end
+
+ local calculator = scroll_fn(range_fn)
+ return function(max_results, num_results, row)
+ local result = calculator(max_results, num_results, row)
+
+ if result < 0 then
+ error(string.format(
+ "Must never return a negative row: { result = %s, args = { %s %s %s } }",
+ result, max_results, num_results, row
+ ))
+ end
+
+ if result >= max_results then
+ error(string.format(
+ "Must never exceed max results: { result = %s, args = { %s %s %s } }",
+ result, max_results, num_results, row
+ ))
+ end
+
+ return result
end
end