From ff377fe97f45dc967317e6bed76e4b3dc746976f Mon Sep 17 00:00:00 2001 From: Mauricio Trajano Date: Fri, 26 Jul 2024 13:44:05 -0400 Subject: [PATCH] perf(highlight): Skip redraw over folded lines Fixes: #285 Currently on a window update such as `WinScroll` highlight_win is looping through every line from from `w0` to `w$` to update the highlights. On large files with very large folded regions these two lines can be very far apart and highlighting every single line can be costly even though they are not in view. This update adds a check to skip over any lines hidden in a fold. --- lua/todo-comments/highlight.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/todo-comments/highlight.lua b/lua/todo-comments/highlight.lua index f4c7674..856c115 100644 --- a/lua/todo-comments/highlight.lua +++ b/lua/todo-comments/highlight.lua @@ -101,8 +101,14 @@ function M.redraw(buf, first, last) first = math.max(first - Config.options.highlight.multiline_context, 0) last = math.min(last + Config.options.highlight.multiline_context, vim.api.nvim_buf_line_count(buf)) local state = M.get_state(buf) - for i = first, last do - state.dirty[i] = true + while first <= last do + -- skip over lines that are hidden under a fold + if vim.fn.foldclosed(first) == -1 then + state.dirty[first] = true + else + first = vim.fn.foldclosedend(first) + end + first = first + 1 end if not M.timer then M.timer = vim.defer_fn(M.update, Config.options.highlight.throttle)