1
- local util = require (' render-markdown.util' )
2
-
3
- --- @class render.md.ContextCache
4
- local cache = {
5
- --- @type table<integer , table<integer , [integer , integer][]>>
6
- conceal = {},
7
- --- @type table<integer , table<integer , [integer , integer , string][]>>
8
- inline_links = {},
9
- }
10
-
11
1
--- @class render.md.Context
12
- local M = {}
2
+ --- @field private buf integer
3
+ --- @field private win integer
4
+ --- @field private conceallevel integer
5
+ --- @field private conceal ? table<integer , [integer , integer][]>
6
+ --- @field private links table<integer , [integer , integer , string][]>
7
+ local Context = {}
8
+ Context .__index = Context
13
9
14
10
--- @param buf integer
15
- function M .reset_buf (buf )
16
- cache .conceal [buf ] = {}
17
- cache .inline_links [buf ] = {}
11
+ --- @param win integer
12
+ function Context .new (buf , win )
13
+ local self = setmetatable ({}, Context )
14
+ self .buf = buf
15
+ self .win = win
16
+ self .conceallevel = vim .api .nvim_get_option_value (' conceallevel' , { scope = ' local' , win = win })
17
+ self .conceal = nil
18
+ self .links = {}
19
+ return self
18
20
end
19
21
20
- --- @param buf integer
21
- --- @param parser vim.treesitter.LanguageTree
22
- function M .compute_conceal (buf , parser )
22
+ --- @param info render.md.NodeInfo
23
+ --- @param icon string
24
+ function Context :add_link (info , icon )
25
+ local row = info .start_row
26
+ if self .links [row ] == nil then
27
+ self .links [row ] = {}
28
+ end
29
+ table.insert (self .links [row ], { info .start_col , info .end_col , icon })
30
+ end
31
+
32
+ --- @param row integer
33
+ --- @return [integer , integer , string][]
34
+ function Context :get_links (row )
35
+ return self .links [row ] or {}
36
+ end
37
+
38
+ --- @return integer
39
+ function Context :get_width ()
40
+ return vim .api .nvim_win_get_width (self .win )
41
+ end
42
+
43
+ --- @param row integer
44
+ --- @return [integer , integer][]
45
+ function Context :get_conceal (row )
46
+ if self .conceal == nil then
47
+ self .conceal = self :compute_conceal ()
48
+ end
49
+ return self .conceal [row ] or {}
50
+ end
51
+
52
+ --- Cached row level implementation of vim.treesitter.get_captures_at_pos
53
+ --- @private
54
+ --- @return table<integer , [integer , integer][]>
55
+ function Context :compute_conceal ()
56
+ if self .conceallevel == 0 then
57
+ return {}
58
+ end
23
59
local ranges = {}
24
- if util .get_win (util .buf_to_win (buf ), ' conceallevel' ) > 0 then
25
- parser :for_each_tree (function (tree , language_tree )
26
- local nodes = M .get_conceal_nodes (buf , language_tree :lang (), tree :root ())
27
- for _ , node in ipairs (nodes ) do
28
- local row , start_col , _ , end_col = node :range ()
29
- if ranges [row ] == nil then
30
- ranges [row ] = {}
31
- end
32
- table.insert (ranges [row ], { start_col , end_col })
60
+ local parser = vim .treesitter .get_parser (self .buf )
61
+ parser :for_each_tree (function (tree , language_tree )
62
+ local nodes = self :compute_conceal_nodes (language_tree :lang (), tree :root ())
63
+ for _ , node in ipairs (nodes ) do
64
+ local row , start_col , _ , end_col = node :range ()
65
+ if ranges [row ] == nil then
66
+ ranges [row ] = {}
33
67
end
34
- end )
35
- end
36
- cache .conceal [buf ] = ranges
68
+ table.insert (ranges [row ], { start_col , end_col })
69
+ end
70
+ end )
71
+ return ranges
37
72
end
38
73
39
74
--- @private
40
- --- @param buf integer
41
75
--- @param language string
42
76
--- @param root TSNode
43
77
--- @return TSNode[]
44
- function M . get_conceal_nodes ( buf , language , root )
78
+ function Context : compute_conceal_nodes ( language , root )
45
79
if not vim .tbl_contains ({ ' markdown' , ' markdown_inline' }, language ) then
46
80
return {}
47
81
end
@@ -50,38 +84,30 @@ function M.get_conceal_nodes(buf, language, root)
50
84
return {}
51
85
end
52
86
local nodes = {}
53
- for _ , node , metadata in query :iter_captures (root , buf ) do
87
+ for _ , node , metadata in query :iter_captures (root , self . buf ) do
54
88
if metadata .conceal ~= nil then
55
89
table.insert (nodes , node )
56
90
end
57
91
end
58
92
return nodes
59
93
end
60
94
61
- --- @param buf integer
62
- --- @param row integer
63
- --- @return [integer , integer][]
64
- function M .concealed (buf , row )
65
- return cache .conceal [buf ][row ] or {}
66
- end
95
+ --- @type table<integer , render.md.Context>
96
+ local cache = {}
97
+
98
+ --- @class render.md.ContextManager
99
+ local M = {}
67
100
68
101
--- @param buf integer
69
- --- @param info render.md.NodeInfo
70
- --- @param icon string
71
- function M .add_inline_link (buf , info , icon )
72
- local inline_links = cache .inline_links [buf ]
73
- local row = info .start_row
74
- if inline_links [row ] == nil then
75
- inline_links [row ] = {}
76
- end
77
- table.insert (inline_links [row ], { info .start_col , info .end_col , icon })
102
+ --- @param win integer
103
+ function M .reset (buf , win )
104
+ cache [buf ] = Context .new (buf , win )
78
105
end
79
106
80
107
--- @param buf integer
81
- --- @param row integer
82
- --- @return [integer , integer , string][]
83
- function M .inline_links (buf , row )
84
- return cache .inline_links [buf ][row ] or {}
108
+ --- @return render.md.Context
109
+ function M .get (buf )
110
+ return cache [buf ]
85
111
end
86
112
87
113
return M
0 commit comments