Skip to content

Commit ae7fd0d

Browse files
authored
feat: Add status, better debug, and some associated refactors
* [WIP]: Mon 28 Sep 2020 01:08:24 PM EDT * add much much better tracking. so much less hax * status updates, oneshot job updates, etc. * remove temp function * add status function * asdfasdfasdf
1 parent 2ce2369 commit ae7fd0d

22 files changed

+975
-433
lines changed

lua/telescope/actions.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ function actions.move_selection_previous(prompt_bufnr)
3131
actions.shift_current_selection(prompt_bufnr, -1)
3232
end
3333

34+
function actions.add_selection(prompt_bufnr)
35+
local current_picker = actions.get_current_picker(prompt_bufnr)
36+
current_picker:add_selection(current_picker:get_selection_row())
37+
end
38+
3439
--- Get the current entry
3540
function actions.get_selected_entry(prompt_bufnr)
3641
return actions.get_current_picker(prompt_bufnr):get_selection()

lua/telescope/builtin.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ builtin.find_files = function(opts)
518518
opts
519519
),
520520
previewer = previewers.cat.new(opts),
521-
sorter = sorters.get_fuzzy_file(),
521+
sorter = sorters.get_fuzzy_file(opts),
522522
}):find()
523523
end
524524

lua/telescope/config.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ local function first_non_null(...)
1212
end
1313
end
1414

15+
local actions = require('telescope.actions')
16+
1517
-- TODO: Add other major configuration points here.
1618
-- selection_strategy
1719

@@ -42,6 +44,8 @@ function config.set_defaults(defaults)
4244
set("border", {})
4345
set("borderchars", { '', '', '', '', '', '', '', ''})
4446

47+
set("get_status_text", function(self) return string.format("%s / %s", self.stats.processed - self.stats.filtered, self.stats.processed) end)
48+
4549
-- Builtin configuration
4650

4751
-- List that will be executed.
@@ -51,6 +55,50 @@ function config.set_defaults(defaults)
5155
-- TODO: Shortenpath
5256
-- Decide how to propagate that to all the opts everywhere.
5357

58+
-- TODO: Add motions to keybindings
59+
-- TODO: Add relative line numbers?
60+
set("default_mappings", {
61+
i = {
62+
["<C-n>"] = actions.move_selection_next,
63+
["<C-p>"] = actions.move_selection_previous,
64+
65+
["<C-c>"] = actions.close,
66+
67+
["<Down>"] = actions.move_selection_next,
68+
["<Up>"] = actions.move_selection_previous,
69+
70+
["<CR>"] = actions.goto_file_selection_edit,
71+
["<C-x>"] = actions.goto_file_selection_split,
72+
["<C-v>"] = actions.goto_file_selection_vsplit,
73+
["<C-t>"] = actions.goto_file_selection_tabedit,
74+
75+
["<C-u>"] = actions.preview_scrolling_up,
76+
["<C-d>"] = actions.preview_scrolling_down,
77+
78+
-- TODO: When we implement multi-select, you can turn this back on :)
79+
-- ["<Tab>"] = actions.add_selection,
80+
},
81+
82+
n = {
83+
["<esc>"] = actions.close,
84+
["<CR>"] = actions.goto_file_selection_edit,
85+
["<C-x>"] = actions.goto_file_selection_split,
86+
["<C-v>"] = actions.goto_file_selection_vsplit,
87+
["<C-t>"] = actions.goto_file_selection_tabedit,
88+
89+
-- TODO: This would be weird if we switch the ordering.
90+
["j"] = actions.move_selection_next,
91+
["k"] = actions.move_selection_previous,
92+
93+
["<Down>"] = actions.move_selection_next,
94+
["<Up>"] = actions.move_selection_previous,
95+
96+
["<C-u>"] = actions.preview_scrolling_up,
97+
["<C-d>"] = actions.preview_scrolling_down,
98+
},
99+
})
100+
101+
54102
-- NOT STABLE. DO NOT USE
55103
set("horizontal_config", {
56104
get_preview_width = function(columns, _)

lua/telescope/debounce.lua

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
-- Credit: https://gist.github.com/runiq/31aa5c4bf00f8e0843cd267880117201
2+
--
3+
4+
local M = {}
5+
6+
---Validates args for `throttle()` and `debounce()`.
7+
local function td_validate(fn, ms)
8+
vim.validate{
9+
fn = { fn, 'f' },
10+
ms = {
11+
ms,
12+
function(ms)
13+
return type(ms) == 'number' and ms > 0
14+
end,
15+
"number > 0",
16+
},
17+
}
18+
end
19+
20+
--- Throttles a function on the leading edge. Automatically `schedule_wrap()`s.
21+
---
22+
--@param fn (function) Function to throttle
23+
--@param timeout (number) Timeout in ms
24+
--@returns (function, timer) throttled function and timer. Remember to call
25+
---`timer:close()` at the end or you will leak memory!
26+
function M.throttle_leading(fn, ms)
27+
td_validate(fn, ms)
28+
local timer = vim.loop.new_timer()
29+
local running = false
30+
31+
local function wrapped_fn(...)
32+
if not running then
33+
timer:start(ms, 0, function()
34+
running = false
35+
end)
36+
running = true
37+
pcall(vim.schedule_wrap(fn), select(1, ...))
38+
end
39+
end
40+
return wrapped_fn, timer
41+
end
42+
43+
--- Throttles a function on the trailing edge. Automatically
44+
--- `schedule_wrap()`s.
45+
---
46+
--@param fn (function) Function to throttle
47+
--@param timeout (number) Timeout in ms
48+
--@param last (boolean, optional) Whether to use the arguments of the last
49+
---call to `fn` within the timeframe. Default: Use arguments of the first call.
50+
--@returns (function, timer) Throttled function and timer. Remember to call
51+
---`timer:close()` at the end or you will leak memory!
52+
function M.throttle_trailing(fn, ms, last)
53+
td_validate(fn, ms)
54+
local timer = vim.loop.new_timer()
55+
local running = false
56+
57+
local wrapped_fn
58+
if not last then
59+
function wrapped_fn(...)
60+
if not running then
61+
local argv = {...}
62+
local argc = select('#', ...)
63+
64+
timer:start(ms, 0, function()
65+
running = false
66+
pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
67+
end)
68+
running = true
69+
end
70+
end
71+
else
72+
local argv, argc
73+
function wrapped_fn(...)
74+
argv = {...}
75+
argc = select('#', ...)
76+
77+
if not running then
78+
timer:start(ms, 0, function()
79+
running = false
80+
pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
81+
end)
82+
running = true
83+
end
84+
end
85+
end
86+
return wrapped_fn, timer
87+
end
88+
89+
--- Debounces a function on the leading edge. Automatically `schedule_wrap()`s.
90+
---
91+
--@param fn (function) Function to debounce
92+
--@param timeout (number) Timeout in ms
93+
--@returns (function, timer) Debounced function and timer. Remember to call
94+
---`timer:close()` at the end or you will leak memory!
95+
function M.debounce_leading(fn, ms)
96+
td_validate(fn, ms)
97+
local timer = vim.loop.new_timer()
98+
local running = false
99+
100+
local function wrapped_fn(...)
101+
timer:start(ms, 0, function()
102+
running = false
103+
end)
104+
105+
if not running then
106+
running = true
107+
pcall(vim.schedule_wrap(fn), select(1, ...))
108+
end
109+
end
110+
return wrapped_fn, timer
111+
end
112+
113+
--- Debounces a function on the trailing edge. Automatically
114+
--- `schedule_wrap()`s.
115+
---
116+
--@param fn (function) Function to debounce
117+
--@param timeout (number) Timeout in ms
118+
--@param first (boolean, optional) Whether to use the arguments of the first
119+
---call to `fn` within the timeframe. Default: Use arguments of the last call.
120+
--@returns (function, timer) Debounced function and timer. Remember to call
121+
---`timer:close()` at the end or you will leak memory!
122+
function M.debounce_trailing(fn, ms, first)
123+
td_validate(fn, ms)
124+
local timer = vim.loop.new_timer()
125+
local wrapped_fn
126+
127+
if not first then
128+
function wrapped_fn(...)
129+
local argv = {...}
130+
local argc = select('#', ...)
131+
132+
timer:start(ms, 0, function()
133+
pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
134+
end)
135+
end
136+
else
137+
local argv, argc
138+
function wrapped_fn(...)
139+
argv = argv or {...}
140+
argc = argc or select('#', ...)
141+
142+
timer:start(ms, 0, function()
143+
pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
144+
end)
145+
end
146+
end
147+
return wrapped_fn, timer
148+
end
149+
150+
--- Test deferment methods (`{throttle,debounce}_{leading,trailing}()`).
151+
---
152+
--@param bouncer (string) Bouncer function to test
153+
--@param ms (number, optional) Timeout in ms, default 2000.
154+
--@param firstlast (bool, optional) Whether to use the 'other' fn call
155+
---strategy.
156+
function M.test_defer(bouncer, ms, firstlast)
157+
local bouncers = {
158+
tl = M.throttle_leading,
159+
tt = M.throttle_trailing,
160+
dl = M.debounce_leading,
161+
dt = M.debounce_trailing,
162+
}
163+
164+
local timeout = ms or 2000
165+
166+
local bounced = bouncers[bouncer](
167+
function(i) vim.cmd('echom "' .. bouncer .. ': ' .. i .. '"') end,
168+
timeout,
169+
firstlast
170+
)
171+
172+
for i, _ in ipairs{1,2,3,4,5} do
173+
bounced(i)
174+
vim.schedule(function () vim.cmd('echom ' .. i) end)
175+
vim.fn.call("wait", {1000, "v:false"})
176+
end
177+
end
178+
179+
return M

0 commit comments

Comments
 (0)