Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache cells.line_types_entire_buf to avoid repeated calls #90

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lua/jupynium/cells.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ function M.line_type(line)
return "others" -- code
end

local line_types_changedtick_per_buf = {}
local line_types_per_buf = {}

--- Get the line types of the entire buffer
--- Similar to `line_type` but returns a table of line types, and it returns more types
--- It also caches the results so repeated calls are fast
--- i.e. cell content: code, cell content: markdown, cell content: header
--- Useful for highlighting
---@return table
function M.line_types_entire_buf(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local changedtick = vim.api.nvim_buf_get_changedtick(bufnr)
if line_types_changedtick_per_buf[bufnr] == changedtick then
return line_types_per_buf[bufnr]
end

local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

local current_cell_type = "header"
Expand All @@ -60,6 +69,8 @@ function M.line_types_entire_buf(bufnr)
end
end

line_types_changedtick_per_buf[bufnr] = changedtick
line_types_per_buf[bufnr] = line_types
return line_types
end

Expand Down