@@ -12,20 +12,23 @@ local Str = require('render-markdown.lib.str')
1212--- @field sections render.md.conceal.Section[]
1313
1414--- @class render.md.Conceal
15+ --- @field private context render.md.Context
1516--- @field private buf integer
1617--- @field private level integer
1718--- @field private computed boolean
1819--- @field private lines table<integer , render.md.conceal.Line>
1920local Conceal = {}
2021Conceal .__index = Conceal
2122
23+ --- @param context render.md.Context
2224--- @param buf integer
23- --- @param level integer
25+ --- @param win integer
2426--- @return render.md.Conceal
25- function Conceal .new (buf , level )
27+ function Conceal .new (context , buf , win )
2628 local self = setmetatable ({}, Conceal )
29+ self .context = context
2730 self .buf = buf
28- self .level = level
31+ self .level = Env . win . get ( win , ' conceallevel ' )
2932 self .computed = false
3033 self .lines = {}
3134 return self
@@ -49,17 +52,24 @@ function Conceal:add(row, entry)
4952 if type (entry ) == ' boolean' then
5053 line .hidden = entry
5154 else
52- if entry .width <= 0 then
53- return
54- end
5555 -- If the section is covered by an existing one don't add it
56- for _ , section in ipairs (line .sections ) do
57- if section .start_col <= entry .start_col and section .end_col >= entry .end_col then
58- return
59- end
56+ if entry .width > 0 and not self :has (line , entry ) then
57+ table.insert (line .sections , entry )
58+ end
59+ end
60+ end
61+
62+ --- @private
63+ --- @param line render.md.conceal.Line
64+ --- @param entry render.md.conceal.Section
65+ --- @return boolean
66+ function Conceal :has (line , entry )
67+ for _ , section in ipairs (line .sections ) do
68+ if section .start_col <= entry .start_col and section .end_col >= entry .end_col then
69+ return true
6070 end
61- table.insert (line .sections , entry )
6271 end
72+ return false
6373end
6474
6575--- @param width integer
@@ -77,20 +87,18 @@ function Conceal:adjust(width, character)
7787 end
7888end
7989
80- --- @param context render.md.Context
8190--- @param node render.md.Node
8291--- @return boolean
83- function Conceal :hidden (context , node )
92+ function Conceal :hidden (node )
8493 -- conceal lines metadata require neovim >= 0.11.0 to function
85- return Env .has_11 and self :line (context , node ).hidden
94+ return Env .has_11 and self :line (node ).hidden
8695end
8796
88- --- @param context render.md.Context
8997--- @param node render.md.Node
9098--- @return integer
91- function Conceal :get (context , node )
99+ function Conceal :get (node )
92100 local result = 0
93- for _ , section in ipairs (self :line (context , node ).sections ) do
101+ for _ , section in ipairs (self :line (node ).sections ) do
94102 if node .start_col < section .end_col and node .end_col > section .start_col then
95103 local amount = self :adjust (section .width , section .character )
96104 result = result + amount
@@ -100,12 +108,11 @@ function Conceal:get(context, node)
100108end
101109
102110--- @private
103- --- @param context render.md.Context
104111--- @param node render.md.Node
105- function Conceal :line (context , node )
112+ function Conceal :line (node )
106113 if not self .computed then
107- self :compute (context )
108114 self .computed = true
115+ self :compute ()
109116 end
110117 local line = self .lines [node .start_row ]
111118 if line == nil then
116123
117124--- Cached row level implementation of vim.treesitter.get_captures_at_pos
118125--- @private
119- --- @param context render.md.Context
120- function Conceal :compute (context )
126+ function Conceal :compute ()
121127 if not self :enabled () then
122128 return
123129 end
@@ -128,18 +134,17 @@ function Conceal:compute(context)
128134 if parser == nil then
129135 return
130136 end
131- context :parse (parser )
137+ self . context :parse (parser )
132138 parser :for_each_tree (function (tree , language_tree )
133- self :compute_tree (context , language_tree :lang (), tree :root ())
139+ self :compute_tree (language_tree :lang (), tree :root ())
134140 end )
135141end
136142
137143--- @private
138- --- @param context render.md.Context
139144--- @param language string
140145--- @param root TSNode
141- function Conceal :compute_tree (context , language , root )
142- if not context :overlaps_node (root ) then
146+ function Conceal :compute_tree (language , root )
147+ if not self . context :overlaps_node (root ) then
143148 return
144149 end
145150 if not vim .tbl_contains ({ ' markdown' , ' markdown_inline' }, language ) then
@@ -149,7 +154,7 @@ function Conceal:compute_tree(context, language, root)
149154 if query == nil then
150155 return
151156 end
152- context :for_each (function (range )
157+ self . context :for_each (function (range )
153158 for id , node , metadata in query :iter_captures (root , self .buf , range .top , range .bottom ) do
154159 if metadata .conceal_lines ~= nil then
155160 local node_range = self :node_range (id , node , metadata )
0 commit comments