Skip to content

Commit 7421415

Browse files
committed
Only update higlights from the first changed line
1 parent 666318f commit 7421415

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

lua/commonmarker/init.lua

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ end
2525

2626
local function byte2pos (byte)
2727
local line = call_function("byte2line", { byte })
28-
-- local col = byte - vim.api.nvim_buf_get_offset(buffer, line)
2928
local col = byte - call_function("line2byte", { line })
3029
return line, col
3130
end
@@ -36,23 +35,27 @@ local function get_contents (buffer)
3635
return table.concat(lines)
3736
end
3837

39-
local function highlight (buffer, namespace)
38+
local function highlight (buffer, namespace, firstline)
4039
local contents = get_contents(buffer)
4140
local events = rust.get_offsets(contents)
41+
dump(firstline)
4242
for _, event in ipairs(events) do
43-
local sline, scol = byte2pos(event.first)
44-
local eline, ecol = byte2pos(event.last)
45-
if sline < eline then
46-
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, -1)
47-
sline = sline + 1
48-
while sline < eline do
49-
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, -1)
43+
repeat -- Allow continue in for loop
44+
local sline, scol = byte2pos(event.first)
45+
if sline < firstline then break end
46+
local eline, ecol = byte2pos(event.last)
47+
if sline < eline then
48+
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, -1)
5049
sline = sline + 1
50+
while sline < eline do
51+
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, -1)
52+
sline = sline + 1
53+
end
54+
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, ecol)
55+
else
56+
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, ecol)
5157
end
52-
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, ecol)
53-
else
54-
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, ecol)
55-
end
58+
until true
5659
end
5760
end
5861

@@ -65,14 +68,14 @@ end
6568
commonmarker.attach = function (self, buffer)
6669
if self._attachments[buffer] then return end
6770
self._attachments[buffer] = true
68-
highlight(buffer, self._namespace)
71+
highlight(buffer, self._namespace, 0)
6972
buf_attach(buffer, false, {
70-
on_lines = function (_, _, _, _, _, _)
73+
on_lines = function (_, _, firstline, _, _, _)
7174
dump(self)
7275
buf_clear_namespace(buffer, self._namespace, 0, -1)
7376
-- Returning true here detaches, we thought we should have been already
7477
if not self._attachments[buffer] then return true end
75-
highlight(buffer, self._namespace)
78+
highlight(buffer, self._namespace, firstline)
7679
end,
7780
on_detach = function (_)
7881
self._attachments[buffer] = nil

0 commit comments

Comments
 (0)