Skip to content

Commit e8befc9

Browse files
committed
Add caching for get_window_edges. #121
1 parent 89ba324 commit e8befc9

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

lua/scrollview.lua

+25-12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ local PROPER_MODE = 2 -- considers folds and wrapped lines
100100
local VIRTUAL_LINE_COUNT_KEY_PREFIX = 0
101101
local PROPER_LINE_COUNT_KEY_PREFIX = 1
102102
local TOPLINE_LOOKUP_KEY_PREFIX = 2
103+
local GET_WINDOW_EDGES = 3
103104

104105
-- Maps window ID to a temporary highlight group name. This is reset on each
105106
-- refresh cycle.
@@ -246,6 +247,8 @@ end
246247
-- Returns the position of window edges, with borders considered part of the
247248
-- window.
248249
local get_window_edges = function(winid)
250+
local memoize_key = table.concat({GET_WINDOW_EDGES, winid}, ':')
251+
if memoize and cache[memoize_key] then return cache[memoize_key] end
249252
local top, left = unpack(fn.win_screenpos(winid))
250253
local bottom = top + get_window_height(winid) - 1
251254
local right = left + fn.winwidth(winid) - 1
@@ -271,7 +274,9 @@ local get_window_edges = function(winid)
271274
right = right + 1
272275
end
273276
end
274-
return top, bottom, left, right
277+
local result = {top, bottom, left, right}
278+
if memoize then cache[memoize_key] = result end
279+
return result
275280
end
276281

277282
-- Return the floating windows that overlap the region corresponding to the
@@ -286,7 +291,7 @@ local get_float_overlaps = function(top, bottom, left, right)
286291
local workspace_win =
287292
fn.getwinvar(winid, WIN_WORKSPACE_BASE_WINID_VAR, -1) ~= -1
288293
if not workspace_win and floating then
289-
local top2, bottom2, left2, right2 = get_window_edges(winid)
294+
local top2, bottom2, left2, right2 = unpack(get_window_edges(winid))
290295
if top <= bottom2
291296
and bottom >= top2
292297
and left <= right2
@@ -2210,18 +2215,26 @@ if to_bool(fn.exists('&mousemoveevent')) then
22102215
mousemove_received = true
22112216
pending_mousemove_callback_count = pending_mousemove_callback_count + 1
22122217
vim.defer_fn(function()
2213-
pending_mousemove_callback_count =
2218+
local resume_memoize = memoize
2219+
start_memoize()
2220+
pcall(function()
2221+
pending_mousemove_callback_count =
22142222
math.max(0, pending_mousemove_callback_count - 1)
2215-
if pending_mousemove_callback_count > 0 then
2216-
-- If there are mousemove callbacks that will occur subsequently,
2217-
-- don't execute this one.
2218-
return
2219-
end
2220-
for _, winid in ipairs(get_scrollview_windows()) do
2221-
local props = api.nvim_win_get_var(winid, PROPS_VAR)
2222-
if not vim.tbl_isempty(props) and props.highlight_fn ~= nil then
2223-
props.highlight_fn(is_mouse_over_scrollview_win(winid))
2223+
if pending_mousemove_callback_count > 0 then
2224+
-- If there are mousemove callbacks that will occur subsequently,
2225+
-- don't execute this one.
2226+
return
2227+
end
2228+
for _, winid in ipairs(get_scrollview_windows()) do
2229+
local props = api.nvim_win_get_var(winid, PROPS_VAR)
2230+
if not vim.tbl_isempty(props) and props.highlight_fn ~= nil then
2231+
props.highlight_fn(is_mouse_over_scrollview_win(winid))
2232+
end
22242233
end
2234+
end)
2235+
if not resume_memoize then
2236+
stop_memoize()
2237+
reset_memoize()
22252238
end
22262239
end, 0)
22272240
end

0 commit comments

Comments
 (0)