Skip to content

Commit 1c48acd

Browse files
Revert "fix: Use decoration provider on_line callbacks for custom highlights to avoid flickering"
This reverts commit 6d379ee.
1 parent 6d379ee commit 1c48acd

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

lua/orgmode/colors/custom_highlighter.lua

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ local MarkupHighlighter = nil
55
local valid_bufnrs = {}
66

77
---@param bufnr number
8-
local function apply_highlights(bufnr, line)
9-
HideLeadingStars.apply(namespace, bufnr, line)
10-
MarkupHighlighter.apply(namespace, bufnr, line)
8+
---@param first_line number
9+
---@param last_line number
10+
local function apply_highlights(bufnr, first_line, last_line, tick_changed)
11+
local changed_lines = vim.api.nvim_buf_get_lines(bufnr, first_line, last_line, false)
12+
HideLeadingStars.apply(namespace, bufnr, changed_lines, first_line, last_line)
13+
MarkupHighlighter.apply(namespace, bufnr, changed_lines, first_line, last_line, tick_changed)
1114
end
1215

1316
local function setup()
@@ -22,19 +25,18 @@ local function setup()
2225
MarkupHighlighter.setup()
2326

2427
vim.api.nvim_set_decoration_provider(namespace, {
25-
on_start = function(_, tick)
26-
local bufnr = vim.api.nvim_get_current_buf()
27-
if valid_bufnrs[bufnr] == tick or vim.bo[bufnr].filetype ~= 'org' then
28-
return false
28+
on_win = function(_, _, bufnr, topline, botline)
29+
local changedtick = vim.api.nvim_buf_get_var(bufnr, 'changedtick')
30+
local tick_changed = not valid_bufnrs[bufnr] or valid_bufnrs[bufnr] ~= changedtick
31+
if valid_bufnrs[bufnr] then
32+
valid_bufnrs[bufnr] = changedtick
33+
return apply_highlights(bufnr, topline, botline, tick_changed)
34+
end
35+
local ft = vim.api.nvim_buf_get_option(bufnr, 'filetype')
36+
if ft == 'org' then
37+
valid_bufnrs[bufnr] = changedtick
38+
return apply_highlights(bufnr, topline, botline, tick_changed)
2939
end
30-
valid_bufnrs[bufnr] = tick
31-
return true
32-
end,
33-
on_win = function(_, _, bufnr)
34-
return valid_bufnrs[bufnr] ~= nil and vim.bo[bufnr].filetype == 'org'
35-
end,
36-
on_line = function(_, _, bufnr, line)
37-
return apply_highlights(bufnr, line)
3840
end,
3941
})
4042
end
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
local config = require('orgmode.config')
22

3-
local function apply(namespace, bufnr, line_index)
4-
if not config.org_hide_leading_stars then
5-
return
6-
end
7-
local line = vim.api.nvim_buf_get_lines(bufnr, line_index, line_index + 1, false)[1]
3+
local function update_line_highlight(namespace, bufnr, line_index, line)
84
local stars = line:match('^%*+')
95
if stars then
106
vim.api.nvim_buf_set_extmark(bufnr, namespace, line_index, 0, {
@@ -16,6 +12,16 @@ local function apply(namespace, bufnr, line_index)
1612
end
1713
end
1814

15+
local function apply(namespace, bufnr, changed_lines, first_line, _)
16+
if not config.org_hide_leading_stars then
17+
return
18+
end
19+
20+
for i, line in ipairs(changed_lines) do
21+
update_line_highlight(namespace, bufnr, first_line + i - 1, line)
22+
end
23+
end
24+
1925
return {
2026
apply = apply,
2127
}

lua/orgmode/colors/markup_highlighter.lua

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,38 @@ local function get_matches(bufnr, first_line, last_line)
360360
return result, link_result, latex_result
361361
end
362362

363-
local function apply(namespace, bufnr, line_index)
364-
local ranges, link_ranges, latex_ranges = get_matches(bufnr, line_index, line_index + 1)
363+
local function apply(namespace, bufnr, _, first_line, last_line, _)
364+
-- Add some offset to make sure everything is covered
365+
local line_ranges = { {} }
366+
local current_line_range = 1
367+
local last_valid_line = -1
368+
for i = first_line, last_line do
369+
if vim.fn.foldclosed(i + 1) == -1 then
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
378+
end
379+
end
380+
381+
-- None of the lines are valid
382+
if last_valid_line < 0 then
383+
return
384+
end
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
365395
local hide_markers = config.org_hide_emphasis_markers
366396

367397
for _, range in ipairs(ranges) do

0 commit comments

Comments
 (0)