|
1 |
| -local NodeInfo = require('render-markdown.core.node_info') |
| 1 | +local Node = require('render-markdown.lib.node') |
2 | 2 | local Range = require('render-markdown.core.range')
|
| 3 | +local Str = require('render-markdown.lib.str') |
3 | 4 | local log = require('render-markdown.core.log')
|
4 |
| -local str = require('render-markdown.core.str') |
5 | 5 | local util = require('render-markdown.core.util')
|
6 | 6 |
|
7 | 7 | ---@class render.md.Context
|
@@ -29,9 +29,9 @@ function Context.new(buf, win, mode, offset)
|
29 | 29 | self.win = win
|
30 | 30 |
|
31 | 31 | local ranges = { Context.compute_range(self.buf, self.win, offset) }
|
32 |
| - for _, window_id in ipairs(vim.fn.win_findbuf(buf)) do |
33 |
| - if window_id ~= self.win then |
34 |
| - table.insert(ranges, Context.compute_range(self.buf, window_id, offset)) |
| 32 | + for _, buf_win in ipairs(vim.fn.win_findbuf(buf)) do |
| 33 | + if buf_win ~= self.win then |
| 34 | + table.insert(ranges, Context.compute_range(self.buf, buf_win, offset)) |
35 | 35 | end
|
36 | 36 | end
|
37 | 37 | self.ranges = Range.coalesce(ranges)
|
|
52 | 52 | ---@param offset integer
|
53 | 53 | ---@return render.md.Range
|
54 | 54 | function Context.compute_range(buf, win, offset)
|
55 |
| - local top = math.max(util.win_view(win).topline - 1 - offset, 0) |
| 55 | + local top = math.max(util.view(win).topline - 1 - offset, 0) |
56 | 56 |
|
57 | 57 | local bottom = top
|
58 | 58 | local lines = vim.api.nvim_buf_line_count(buf)
|
59 | 59 | local size = vim.api.nvim_win_get_height(win) + (2 * offset)
|
60 | 60 | while bottom < lines and size > 0 do
|
61 | 61 | bottom = bottom + 1
|
62 |
| - if util.win_visible(win, bottom) then |
| 62 | + if util.row_visible(win, bottom) then |
63 | 63 | size = size - 1
|
64 | 64 | end
|
65 | 65 | end
|
66 | 66 |
|
67 | 67 | return Range.new(top, bottom)
|
68 | 68 | end
|
69 | 69 |
|
70 |
| ----@param info render.md.NodeInfo |
| 70 | +---@param node render.md.Node |
71 | 71 | ---@return render.md.CustomCallout?
|
72 |
| -function Context:get_callout(info) |
73 |
| - return self.callouts[info.start_row] |
| 72 | +function Context:get_callout(node) |
| 73 | + return self.callouts[node.start_row] |
74 | 74 | end
|
75 | 75 |
|
76 |
| ----@param info render.md.NodeInfo |
| 76 | +---@param node render.md.Node |
77 | 77 | ---@param callout render.md.CustomCallout
|
78 |
| -function Context:add_callout(info, callout) |
79 |
| - self.callouts[info.start_row] = callout |
| 78 | +function Context:add_callout(node, callout) |
| 79 | + self.callouts[node.start_row] = callout |
80 | 80 | end
|
81 | 81 |
|
82 |
| ----@param info render.md.NodeInfo |
| 82 | +---@param node render.md.Node |
83 | 83 | ---@return render.md.CustomCheckbox?
|
84 |
| -function Context:get_checkbox(info) |
85 |
| - return self.checkboxes[info.start_row] |
| 84 | +function Context:get_checkbox(node) |
| 85 | + return self.checkboxes[node.start_row] |
86 | 86 | end
|
87 | 87 |
|
88 |
| ----@param info render.md.NodeInfo |
| 88 | +---@param node render.md.Node |
89 | 89 | ---@param checkbox render.md.CustomCheckbox
|
90 |
| -function Context:add_checkbox(info, checkbox) |
91 |
| - self.checkboxes[info.start_row] = checkbox |
| 90 | +function Context:add_checkbox(node, checkbox) |
| 91 | + self.checkboxes[node.start_row] = checkbox |
92 | 92 | end
|
93 | 93 |
|
94 |
| ----@param info? render.md.NodeInfo |
| 94 | +---@param node? render.md.Node |
95 | 95 | ---@return integer
|
96 |
| -function Context:width(info) |
97 |
| - if info == nil then |
| 96 | +function Context:width(node) |
| 97 | + if node == nil then |
98 | 98 | return 0
|
99 | 99 | end
|
100 |
| - return str.width(info.text) + self:get_offset(info) - self:concealed(info) |
| 100 | + return Str.width(node.text) + self:get_offset(node) - self:concealed(node) |
101 | 101 | end
|
102 | 102 |
|
103 |
| ----@param info render.md.NodeInfo |
| 103 | +---@param node render.md.Node |
104 | 104 | ---@param amount integer
|
105 |
| -function Context:add_offset(info, amount) |
| 105 | +function Context:add_offset(node, amount) |
106 | 106 | if amount == 0 then
|
107 | 107 | return
|
108 | 108 | end
|
109 |
| - local row = info.start_row |
| 109 | + local row = node.start_row |
110 | 110 | if self.links[row] == nil then
|
111 | 111 | self.links[row] = {}
|
112 | 112 | end
|
113 |
| - table.insert(self.links[row], { info.start_col, info.end_col, amount }) |
| 113 | + table.insert(self.links[row], { node.start_col, node.end_col, amount }) |
114 | 114 | end
|
115 | 115 |
|
116 | 116 | ---@private
|
117 |
| ----@param info render.md.NodeInfo |
| 117 | +---@param node render.md.Node |
118 | 118 | ---@return integer
|
119 |
| -function Context:get_offset(info) |
| 119 | +function Context:get_offset(node) |
120 | 120 | local result = 0
|
121 |
| - for _, offset_range in ipairs(self.links[info.start_row] or {}) do |
122 |
| - if info.start_col < offset_range[2] and info.end_col > offset_range[1] then |
| 121 | + for _, offset_range in ipairs(self.links[node.start_row] or {}) do |
| 122 | + if node.start_col < offset_range[2] and node.end_col > offset_range[1] then |
123 | 123 | result = result + offset_range[3]
|
124 | 124 | end
|
125 | 125 | end
|
|
142 | 142 | ---@return integer
|
143 | 143 | function Context:get_width()
|
144 | 144 | if self.window_width == nil then
|
145 |
| - self.window_width = vim.api.nvim_win_get_width(self.win) - util.win_textoff(self.win) |
| 145 | + self.window_width = vim.api.nvim_win_get_width(self.win) - util.textoff(self.win) |
146 | 146 | end
|
147 | 147 | return self.window_width
|
148 | 148 | end
|
@@ -180,38 +180,38 @@ end
|
180 | 180 |
|
181 | 181 | ---@param root TSNode
|
182 | 182 | ---@param query vim.treesitter.Query
|
183 |
| ----@param callback fun(capture: string, node: render.md.NodeInfo) |
| 183 | +---@param callback fun(capture: string, node: render.md.Node) |
184 | 184 | function Context:query(root, query, callback)
|
185 | 185 | for _, range in ipairs(self.ranges) do
|
186 |
| - for id, node in query:iter_captures(root, self.buf, range.top, range.bottom) do |
| 186 | + for id, ts_node in query:iter_captures(root, self.buf, range.top, range.bottom) do |
187 | 187 | local capture = query.captures[id]
|
188 |
| - local info = NodeInfo.new(self.buf, node) |
189 |
| - log.node_info(capture, info) |
190 |
| - callback(capture, info) |
| 188 | + local node = Node.new(self.buf, ts_node) |
| 189 | + log.node(capture, node) |
| 190 | + callback(capture, node) |
191 | 191 | end
|
192 | 192 | end
|
193 | 193 | end
|
194 | 194 |
|
195 |
| ----@param info? render.md.NodeInfo |
| 195 | +---@param node? render.md.Node |
196 | 196 | ---@return boolean
|
197 |
| -function Context:hidden(info) |
198 |
| - return info == nil or str.width(info.text) == self:concealed(info) |
| 197 | +function Context:hidden(node) |
| 198 | + return node == nil or Str.width(node.text) == self:concealed(node) |
199 | 199 | end
|
200 | 200 |
|
201 |
| ----@param info render.md.NodeInfo |
| 201 | +---@param node render.md.Node |
202 | 202 | ---@return integer
|
203 |
| -function Context:concealed(info) |
204 |
| - local ranges = self:get_conceal(info.start_row) |
| 203 | +function Context:concealed(node) |
| 204 | + local ranges = self:get_conceal(node.start_row) |
205 | 205 | if #ranges == 0 then
|
206 | 206 | return 0
|
207 | 207 | end
|
208 |
| - local result, col = 0, info.start_col |
209 |
| - for _, index in ipairs(vim.fn.str2list(info.text)) do |
| 208 | + local result, col = 0, node.start_col |
| 209 | + for _, index in ipairs(vim.fn.str2list(node.text)) do |
210 | 210 | local ch = vim.fn.nr2char(index)
|
211 | 211 | for _, range in ipairs(ranges) do
|
212 | 212 | -- Essentially vim.treesitter.is_in_node_range but only care about column
|
213 | 213 | if col >= range[1] and col + 1 <= range[2] then
|
214 |
| - result = result + str.width(ch) |
| 214 | + result = result + Str.width(ch) |
215 | 215 | end
|
216 | 216 | end
|
217 | 217 | col = col + #ch
|
|
233 | 233 | ---@private
|
234 | 234 | ---@return table<integer, [integer, integer][]>
|
235 | 235 | function Context:compute_conceal()
|
236 |
| - local conceallevel = util.get_win(self.win, 'conceallevel') |
| 236 | + local conceallevel = util.get('win', self.win, 'conceallevel') |
237 | 237 | if conceallevel == 0 then
|
238 | 238 | return {}
|
239 | 239 | end
|
@@ -270,15 +270,15 @@ function Context:compute_conceal_ranges(language, root)
|
270 | 270 | end
|
271 | 271 | local result = {}
|
272 | 272 | for _, range in ipairs(self.ranges) do
|
273 |
| - for id, node, metadata in query:iter_captures(root, self.buf, range.top, range.bottom) do |
| 273 | + for id, ts_node, metadata in query:iter_captures(root, self.buf, range.top, range.bottom) do |
274 | 274 | if metadata.conceal ~= nil then
|
275 | 275 | local node_range = metadata.range
|
276 | 276 | if node_range == nil and metadata[id] ~= nil then
|
277 | 277 | node_range = metadata[id].range
|
278 | 278 | end
|
279 | 279 | if node_range == nil then
|
280 | 280 | ---@diagnostic disable-next-line: missing-fields
|
281 |
| - node_range = { node:range() } |
| 281 | + node_range = { ts_node:range() } |
282 | 282 | end
|
283 | 283 | local row, start_col, _, end_col = unpack(node_range)
|
284 | 284 | table.insert(result, { row, start_col, end_col })
|
|
306 | 306 | ---@return boolean
|
307 | 307 | function M.contains_range(buf, win)
|
308 | 308 | local context = cache[buf]
|
309 |
| - if context == nil then |
310 |
| - return false |
311 |
| - end |
312 |
| - return context:contains_window(win) |
| 309 | + return context ~= nil and context:contains_window(win) |
313 | 310 | end
|
314 | 311 |
|
315 | 312 | ---@param buf integer
|
|
0 commit comments