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-
111--- @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
139
1410--- @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
1820end
1921
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
2359 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 ] = {}
3367 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
3772end
3873
3974--- @private
40- --- @param buf integer
4175--- @param language string
4276--- @param root TSNode
4377--- @return TSNode[]
44- function M . get_conceal_nodes ( buf , language , root )
78+ function Context : compute_conceal_nodes ( language , root )
4579 if not vim .tbl_contains ({ ' markdown' , ' markdown_inline' }, language ) then
4680 return {}
4781 end
@@ -50,38 +84,30 @@ function M.get_conceal_nodes(buf, language, root)
5084 return {}
5185 end
5286 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
5488 if metadata .conceal ~= nil then
5589 table.insert (nodes , node )
5690 end
5791 end
5892 return nodes
5993end
6094
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 = {}
67100
68101--- @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 )
78105end
79106
80107--- @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 ]
85111end
86112
87113return M
0 commit comments