|
1 | 1 | local config = require('orgmode.config')
|
| 2 | +local utils = require('orgmode.utils') |
2 | 3 | local ts_utils = require('nvim-treesitter.ts_utils')
|
3 | 4 | local query = nil
|
4 | 5 |
|
@@ -360,18 +361,37 @@ local function get_matches(bufnr, first_line, last_line)
|
360 | 361 | end
|
361 | 362 |
|
362 | 363 | local function apply(namespace, bufnr, _, first_line, last_line, _)
|
363 |
| - local visible_lines = {} |
364 | 364 | -- 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 |
367 | 369 | 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 |
369 | 378 | end
|
370 | 379 | end
|
371 |
| - if #visible_lines == 0 then |
| 380 | + |
| 381 | + -- None of the lines are valid |
| 382 | + if last_valid_line < 0 then |
372 | 383 | return
|
373 | 384 | 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 |
375 | 395 | local hide_markers = config.org_hide_emphasis_markers
|
376 | 396 |
|
377 | 397 | for _, range in ipairs(ranges) do
|
|
0 commit comments