From 136b2ed42c22e9694acbdd51aa5ab26b3198a402 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 17 Dec 2024 11:44:07 +0800 Subject: [PATCH] fix(previewer): avoid buffer previewer rendering flicker when scrolling up/down (#756) --- lua/fzfx/detail/popup/buffer_popup_window.lua | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lua/fzfx/detail/popup/buffer_popup_window.lua b/lua/fzfx/detail/popup/buffer_popup_window.lua index c7e777d81..14452519b 100644 --- a/lua/fzfx/detail/popup/buffer_popup_window.lua +++ b/lua/fzfx/detail/popup/buffer_popup_window.lua @@ -10,6 +10,11 @@ local buffer_popup_window_helpers = require("fzfx.detail.popup.buffer_popup_wind local M = {} +--- @return integer +local function minimal_line_step() + return math.max(30, vim.o.lines) +end + --- @alias fzfx.BufferPreviewerOpts {fzf_preview_window_opts:fzfx.FzfPreviewWindowOpts,fzf_border_opts:string} --- @param win_opts fzfx.WindowOpts --- @param buffer_previewer_opts fzfx.BufferPreviewerOpts @@ -688,14 +693,12 @@ function BufferPopupWindow:render_file_contents(file_content, content_view, on_c local LINES = file_content.contents local LINES_COUNT = #LINES - local LARGE_FILE = LINES_COUNT > 50 - -- local TOP_LINE = content_view - -- local BOTTOM_LINE = math.min(WIN_HEIGHT + TOP_LINE, LINES_COUNT) + local IS_LARGE_FILE = LINES_COUNT > 500 local FIRST_LINE = 1 local LAST_LINE = LINES_COUNT local line_index = FIRST_LINE if line_step == nil then - line_step = LARGE_FILE and math.max(math.ceil(math.sqrt(LINES_COUNT)), 10) or 10 + line_step = math.max(math.ceil(math.sqrt(LINES_COUNT)), minimal_line_step()) end -- log.debug( -- string.format( @@ -707,7 +710,7 @@ function BufferPopupWindow:render_file_contents(file_content, content_view, on_c -- ) -- ) - local lines_been_cleared = false + -- local lines_been_cleared = false local function set_buf_lines() vim.defer_fn(function() @@ -761,10 +764,10 @@ function BufferPopupWindow:render_file_contents(file_content, content_view, on_c -- ) -- ) - if not lines_been_cleared then - vim.api.nvim_buf_set_lines(self.previewer_bufnr, 0, -1, false, {}) - lines_been_cleared = true - end + -- if not lines_been_cleared then + -- vim.api.nvim_buf_set_lines(self.previewer_bufnr, 0, -1, false, {}) + -- lines_been_cleared = true + -- end vim.api.nvim_buf_set_lines(self.previewer_bufnr, set_start, set_end, false, buf_lines) if hi_line then @@ -803,12 +806,14 @@ function BufferPopupWindow:render_file_contents(file_content, content_view, on_c if line_index <= content_view.bottom then set_buf_lines() else + -- Render complete, removes other bottom lines in the buffer. + vim.api.nvim_buf_set_lines(self.previewer_bufnr, set_end, -1, false, {}) self:_do_view(content_view) self._saved_previewing_file_content_view = content_view do_complete(true) falsy_rendering() end - end, LARGE_FILE and math.max(10 - string.len(tostring(LINES_COUNT)) * 2, 1) or 10) + end, IS_LARGE_FILE and math.max(10 - string.len(tostring(LINES_COUNT)) * 2, 1) or 10) end set_buf_lines() end, 10) @@ -1055,7 +1060,12 @@ function BufferPopupWindow:scroll_by(percent, up) return end - self:render_file_contents(file_content, view, falsy_scrolling, math.max(LINES_COUNT, 30)) + self:render_file_contents( + file_content, + view, + falsy_scrolling, + math.max(LINES_COUNT, minimal_line_step()) + ) end function BufferPopupWindow:preview_page_down()