1
1
package.loaded [" rust" ] = nil -- Force module reload during dev
2
2
local rust = require (" libvim_commonmark" )
3
3
4
- local rustymarks = vim .api .nvim_create_namespace (" rustymarks" )
5
-
6
4
-- The Lua API is verbose and repetative
7
5
local call_function = vim .api .nvim_call_function
8
6
local buf_add_highlight = vim .api .nvim_buf_add_highlight
9
7
local buf_attach = vim .api .nvim_buf_attach
10
8
local buf_get_lines = vim .api .nvim_buf_get_lines
11
9
local buf_clear_namespace = vim .api .nvim_buf_clear_namespace
12
10
13
- local _attachments = {}
11
+ local commonmarker = {
12
+ _attachments = {},
13
+ _namespace = vim .api .nvim_create_namespace (" rustymarks" )
14
+ }
14
15
15
16
-- luacheck: ignore dump
16
17
local function dump (...)
@@ -35,42 +36,49 @@ local function get_contents (buffer)
35
36
return table.concat (lines )
36
37
end
37
38
38
- local function highlight (buffer )
39
+ local function highlight (buffer , namespace )
39
40
local contents = get_contents (buffer )
40
41
local events = rust .get_offsets (contents )
41
42
for _ , event in ipairs (events ) do
42
43
local sline , scol = byte2pos (event .first )
43
44
local eline , ecol = byte2pos (event .last )
44
45
if sline < eline then
45
- buf_add_highlight (buffer , rustymarks , event .group , sline - 1 , scol , - 1 )
46
+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , scol , - 1 )
46
47
sline = sline + 1
47
48
while sline < eline do
48
- buf_add_highlight (buffer , rustymarks , event .group , sline - 1 , 0 , - 1 )
49
+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , 0 , - 1 )
49
50
sline = sline + 1
50
51
end
51
- buf_add_highlight (buffer , rustymarks , event .group , sline - 1 , 0 , ecol )
52
+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , 0 , ecol )
52
53
else
53
- buf_add_highlight (buffer , rustymarks , event .group , sline - 1 , scol , ecol )
54
+ buf_add_highlight (buffer , namespace , event .group , sline - 1 , scol , ecol )
54
55
end
55
56
end
56
57
end
57
58
58
- local function attach (buffer )
59
- if _attachments [buffer ] then return end
60
- _attachments [buffer ] = true
61
- highlight (buffer )
59
+ commonmarker .detach = function (self , buffer )
60
+ dump (self ._attachments )
61
+ self ._attachments [buffer ] = nil
62
+ buf_clear_namespace (buffer , self ._namespace , 0 , - 1 )
63
+ end
64
+
65
+ commonmarker .attach = function (self , buffer )
66
+ if self ._attachments [buffer ] then return end
67
+ self ._attachments [buffer ] = true
68
+ highlight (buffer , self ._namespace )
62
69
buf_attach (buffer , false , {
63
70
on_lines = function (_ , _ , _ , _ , _ , _ )
64
- if not _attachments [buffer ] then return end
65
- buf_clear_namespace (buffer , rustymarks , 0 , - 1 )
66
- highlight (buffer )
71
+ dump (self )
72
+ buf_clear_namespace (buffer , self ._namespace , 0 , - 1 )
73
+ -- Returning true here detaches, we thought we should have been already
74
+ if not self ._attachments [buffer ] then return true end
75
+ highlight (buffer , self ._namespace )
67
76
end ,
68
- on_detach = function ()
69
- _attachments [buffer ] = nil
77
+ on_detach = function (_ )
78
+ self ._attachments [buffer ] = nil
79
+ self :detach (buffer )
70
80
end
71
81
})
72
82
end
73
83
74
- return {
75
- attach = attach ,
76
- }
84
+ return commonmarker
0 commit comments