Skip to content

Commit

Permalink
fix: move some scope calculations out of main loop (#795)
Browse files Browse the repository at this point in the history
fix: move some scope calculations out of main loop
  • Loading branch information
Danielkonge committed Dec 21, 2023
1 parent 38cd858 commit 258b5d8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
5 changes: 4 additions & 1 deletion doc/indent_blankline.txt
Expand Up @@ -231,11 +231,14 @@ config.viewport_buffer *ibl.config.viewport_buffer*
Default: `30` ~

*ibl.config.viewport_buffer.max*
{max} (number)
{max} (number) [deprecated]
Maximum number of lines above and below of what is currently
visible in the window for which indentation guides will
be generated

(This functionality has been deprecated, and the exact offset
can be chosen with `min` instead)

Default: `500` ~

Example: ~
Expand Down
65 changes: 47 additions & 18 deletions lua/ibl/init.lua
Expand Up @@ -187,10 +187,14 @@ M.refresh = function(bufnr)
end
local scope
local scope_start_line, scope_end_line
if not scope_disabled and config.scope.enabled then
scope = scp.get(bufnr, config)
if scope and scope:start() >= 0 then
offset = top_offset - math.min(top_offset - math.min(offset, scope:start()), config.viewport_buffer.max)
local scope_start = scope:start()
local scope_end = scope:end_()
scope_start_line = vim.api.nvim_buf_get_lines(bufnr, scope_start, scope_start + 1, false)[1]
scope_end_line = vim.api.nvim_buf_get_lines(bufnr, scope_end, scope_end + 1, false)[1]
end
end
Expand Down Expand Up @@ -260,6 +264,47 @@ M.refresh = function(bufnr)
end
end
if scope and scope_start_line then
-- find whitespace tables for the start and end lines of scope
local whitespace_start = utils.get_whitespace(scope_start_line)
local whitespace_end = utils.get_whitespace(scope_end_line)
local whitespace_tbl_start = indent.get(whitespace_start, indent_opts, nil)
local whitespace_tbl_end = indent.get(whitespace_end, indent_opts, nil)
local whitespace, whitespace_tbl, scope_row
-- use the smallest whitespace table of the two to determine the scope index
if #whitespace_tbl_end < #whitespace_tbl_start then
whitespace = whitespace_end
whitespace_tbl = whitespace_tbl_end
scope_row = scope_row_end
else
whitespace = whitespace_start
whitespace_tbl = whitespace_tbl_start
scope_row = scope_row_start
end
-- do the same calculations as in the main loop below, but note that the scope
-- start and end will never be on a blankline, so these cases simplify a lot
whitespace_tbl = utils.fix_horizontal_scroll(whitespace_tbl, left_offset)
for _, fn in
pairs(hooks.get(bufnr, hooks.type.WHITESPACE) --[=[@as ibl.hooks.cb.whitespace[]]=])
do
whitespace_tbl = fn(buffer_state.tick, bufnr, scope_row - 1, whitespace_tbl)
end
-- calculate the scope index
scope_col_start = #whitespace
scope_col_start_single = #whitespace_tbl
scope_index = #utils.tbl_filter(function(w)
return indent.is_indent(w)
end, whitespace_tbl) + 1
for _, fn in
pairs(hooks.get(bufnr, hooks.type.SCOPE_HIGHLIGHT) --[=[@as ibl.hooks.cb.scope_highlight[]]=])
do
scope_index = fn(buffer_state.tick, bufnr, scope, scope_index)
end
end
for i, line in ipairs(lines) do
local row = i + offset
if line_skipped[i] then
Expand Down Expand Up @@ -358,11 +403,7 @@ M.refresh = function(bufnr)
end
-- Fix horizontal scroll
local current_left_offset = left_offset
while #whitespace_tbl > 0 and current_left_offset > 0 do
table.remove(whitespace_tbl, 1)
current_left_offset = current_left_offset - 1
end
whitespace_tbl = utils.fix_horizontal_scroll(whitespace_tbl, left_offset)
for _, fn in
pairs(hooks.get(bufnr, hooks.type.WHITESPACE) --[=[@as ibl.hooks.cb.whitespace[]]=])
Expand All @@ -375,18 +416,6 @@ M.refresh = function(bufnr)
-- #### make virtual text ####
local scope_start = row == scope_row_start
local scope_end = row == scope_row_end
if scope_start and scope then
scope_col_start = #whitespace
scope_col_start_single = #whitespace_tbl
scope_index = #utils.tbl_filter(function(w)
return indent.is_indent(w)
end, whitespace_tbl) + 1
for _, fn in
pairs(hooks.get(bufnr, hooks.type.SCOPE_HIGHLIGHT) --[=[@as ibl.hooks.cb.scope_highlight[]]=])
do
scope_index = fn(buffer_state.tick, bufnr, scope, scope_index)
end
end
local whitespace_only = not blankline and line == whitespace
local char_map = vt.get_char_map(config, listchars, whitespace_only, blankline)
Expand Down
12 changes: 12 additions & 0 deletions lua/ibl/utils.lua
Expand Up @@ -348,4 +348,16 @@ M.tbl_get_index = function(list, i)
return list[((i - 1) % #list) + 1]
end

---@param whitespace_tbl ibl.indent.whitespace[]
---@param left_offset number
---@return ibl.indent.whitespace[]
M.fix_horizontal_scroll = function(whitespace_tbl, left_offset)
local current_left_offset = left_offset
while #whitespace_tbl > 0 and current_left_offset > 0 do
table.remove(whitespace_tbl, 1)
current_left_offset = current_left_offset - 1
end
return whitespace_tbl
end

return M

0 comments on commit 258b5d8

Please sign in to comment.