Skip to content

Commit aba09c5

Browse files
Optimize custom highlighter to parse only ranges that are not folded
1 parent d3980a5 commit aba09c5

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

lua/orgmode/colors/markup_highlighter.lua

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local config = require('orgmode.config')
2+
local utils = require('orgmode.utils')
23
local ts_utils = require('nvim-treesitter.ts_utils')
34
local query = nil
45

@@ -360,18 +361,37 @@ local function get_matches(bufnr, first_line, last_line)
360361
end
361362

362363
local function apply(namespace, bufnr, _, first_line, last_line, _)
363-
local visible_lines = {}
364364
-- Add some offset to make sure everything is covered
365-
local start_line = math.max(0, first_line - 5)
366-
for i = start_line, last_line do
365+
local line_ranges = { {} }
366+
local current_line_range = 1
367+
local last_valid_line = -1
368+
for i = first_line, last_line do
367369
if vim.fn.foldclosed(i + 1) == -1 then
368-
table.insert(visible_lines, i)
370+
-- Generate list of valid ranges
371+
if last_valid_line < 0 or (i - 1) == last_valid_line then
372+
table.insert(line_ranges[current_line_range], i)
373+
else
374+
current_line_range = current_line_range + 1
375+
line_ranges[current_line_range] = { i }
376+
end
377+
last_valid_line = i
369378
end
370379
end
371-
if #visible_lines == 0 then
380+
381+
-- None of the lines are valid
382+
if last_valid_line < 0 then
372383
return
373384
end
374-
local ranges, link_ranges, latex_ranges = get_matches(bufnr, visible_lines[1], visible_lines[#visible_lines])
385+
local ranges = {}
386+
local link_ranges = {}
387+
local latex_ranges = {}
388+
389+
for _, range in ipairs(line_ranges) do
390+
local r, link_r, latex_r = get_matches(bufnr, math.max(1, range[1] - 5), range[#range] + 5)
391+
utils.concat(ranges, r or {})
392+
utils.concat(link_ranges, link_r or {})
393+
utils.concat(latex_ranges, latex_r or {})
394+
end
375395
local hide_markers = config.org_hide_emphasis_markers
376396

377397
for _, range in ipairs(ranges) do

0 commit comments

Comments
 (0)