2525
2626local 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
3130end
@@ -36,43 +35,44 @@ local function get_contents (buffer)
3635 return table.concat (lines )
3736end
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 )
4241 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 )
42+ repeat -- Allow continue in for loop
43+ local sline , scol = byte2pos (event .first )
44+ if sline < firstline then break end
45+ local eline , ecol = byte2pos (event .last )
46+ if sline < eline then
47+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , scol , - 1 )
5048 sline = sline + 1
49+ while sline < eline do
50+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , 0 , - 1 )
51+ sline = sline + 1
52+ end
53+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , 0 , ecol )
54+ else
55+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , scol , ecol )
5156 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
57+ until true
5658 end
5759end
5860
5961function commonmarker :detach (buffer )
60- dump (self ._attachments )
6162 self ._attachments [buffer ] = nil
6263 buf_clear_namespace (buffer , self ._namespace , 0 , - 1 )
6364end
6465
6566function commonmarker :attach (buffer )
6667 if self ._attachments [buffer ] then return end
6768 self ._attachments [buffer ] = true
68- highlight (buffer , self ._namespace )
69+ highlight (buffer , self ._namespace , 0 )
6970 buf_attach (buffer , false , {
70- on_lines = function (_ , _ , _ , _ , _ , _ )
71- dump (self )
72- buf_clear_namespace (buffer , self ._namespace , 0 , - 1 )
71+ on_lines = function (_ , _ , _ , firstline , _ , _ )
72+ buf_clear_namespace (buffer , self ._namespace , firstline , - 1 )
7373 -- Returning true here detaches, we thought we should have been already
7474 if not self ._attachments [buffer ] then return true end
75- highlight (buffer , self ._namespace )
75+ highlight (buffer , self ._namespace , firstline )
7676 end ,
7777 on_detach = function (_ )
7878 self ._attachments [buffer ] = nil
0 commit comments