Skip to content

Commit

Permalink
perf: cache cells.line_types_entire_buf to avoid repeated calls (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Aug 11, 2023
1 parent 0e55a37 commit 05ef99a
Showing 1 changed file with 11 additions and 0 deletions.
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

0 comments on commit 05ef99a

Please sign in to comment.