Skip to content

Commit 47eb965

Browse files
committed
perf: prevent expensive nvim_win_text_height call on big scroll
1 parent ab3741e commit 47eb965

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

lua/smear_cursor/animation.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ end
521521
local function scroll_buffer_space()
522522
if current_top_row ~= previous_top_row and current_line ~= previous_line then
523523
-- Shift to show smear in buffer space instead of screen space
524-
local shift = screen.get_screen_distance(previous_top_row, current_top_row)
524+
local shift = screen.get_screen_distance(previous_top_row, current_top_row, current_window_id)
525525
local shifted_position = { current_corners[1][1] - shift, current_corners[1][2] }
526526
clamp_to_buffer(shifted_position)
527527
set_corners(current_corners, shifted_position[1], shifted_position[2])

lua/smear_cursor/screen.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,30 @@ M.get_screen_cmd_cursor_position = function()
3636
return row, col
3737
end
3838

39-
M.get_screen_distance = function(row_start, row_end)
39+
M.get_screen_distance = function(row_start, row_end, window_id)
4040
local reversed = false
4141

4242
if row_start > row_end then
4343
row_start, row_end = row_end, row_start
4444
reversed = true
4545
end
4646

47+
local window_height = vim.api.nvim_win_get_height(window_id)
48+
4749
local text_height
48-
local success = pcall(function()
49-
text_height = vim.api.nvim_win_text_height(0, {
50-
start_row = row_start - 1,
51-
end_row = row_end - 1,
52-
})
53-
end)
54-
55-
if not success then -- line is not visible
56-
text_height = { all = 1 }
50+
if row_end - row_start >= window_height then
51+
text_height = { all = window_height }
52+
else
53+
local success = pcall(function()
54+
text_height = vim.api.nvim_win_text_height(0, {
55+
start_row = row_start - 1,
56+
end_row = row_end - 1,
57+
})
58+
end)
59+
60+
if not success then -- line is not visible
61+
text_height = { all = 1 }
62+
end
5763
end
5864

5965
local distance = text_height.all - 1

0 commit comments

Comments
 (0)