Skip to content

Commit 57b96df

Browse files
chore: refactor to node info class rather than ts module
1 parent 1c7b5ee commit 57b96df

File tree

10 files changed

+137
-137
lines changed

10 files changed

+137
-137
lines changed

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 August 06
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 07
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/context.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ end
6969

7070
---@param root TSNode
7171
---@param query vim.treesitter.Query
72-
---@param cb fun(capture: string, node: TSNode, metadata: vim.treesitter.query.TSMetadata)
73-
function Context:query(root, query, cb)
72+
---@param callback fun(capture: string, node: TSNode, metadata: vim.treesitter.query.TSMetadata)
73+
function Context:query(root, query, callback)
7474
for id, node, metadata in query:iter_captures(root, self.buf, self.top, self.bottom) do
7575
local capture = query.captures[id]
76-
cb(capture, node, metadata)
76+
callback(capture, node, metadata)
7777
end
7878
end
7979

lua/render-markdown/handler/latex.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
local NodeInfo = require('render-markdown.node_info')
12
local list = require('render-markdown.list')
23
local logger = require('render-markdown.logger')
34
local state = require('render-markdown.state')
45
local str = require('render-markdown.str')
5-
local ts = require('render-markdown.ts')
66

77
---@type table<string, string[]>
88
local cache = {}
@@ -23,7 +23,7 @@ function M.parse(root, buf)
2323
return {}
2424
end
2525

26-
local info = ts.info(root, buf)
26+
local info = NodeInfo.new(buf, root)
2727
logger.debug_node_info('latex', info)
2828

2929
local expressions = cache[info.text]

lua/render-markdown/handler/markdown.lua

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local NodeInfo = require('render-markdown.node_info')
12
local code_block_parser = require('render-markdown.parser.code_block')
23
local colors = require('render-markdown.colors')
34
local component = require('render-markdown.component')
@@ -8,7 +9,6 @@ local logger = require('render-markdown.logger')
89
local pipe_table_parser = require('render-markdown.parser.pipe_table')
910
local state = require('render-markdown.state')
1011
local str = require('render-markdown.str')
11-
local ts = require('render-markdown.ts')
1212

1313
---@class render.md.handler.buf.Markdown
1414
---@field private buf integer
@@ -31,7 +31,7 @@ end
3131
---@return render.md.Mark[]
3232
function Handler:parse(root)
3333
context.get(self.buf):query(root, state.markdown_query, function(capture, node)
34-
local info = ts.info(node, self.buf)
34+
local info = NodeInfo.new(self.buf, node)
3535
logger.debug_node_info(capture, info)
3636
if capture == 'heading' then
3737
self:heading(info)
@@ -46,8 +46,8 @@ function Handler:parse(root)
4646
elseif capture == 'checkbox_checked' then
4747
self:checkbox(info, self.config.checkbox.checked)
4848
elseif capture == 'quote' then
49-
context.get(self.buf):query(info.node, state.markdown_quote_query, function(nested_capture, nested_node)
50-
local nested_info = ts.info(nested_node, self.buf)
49+
context.get(self.buf):query(node, state.markdown_quote_query, function(nested_capture, nested_node)
50+
local nested_info = NodeInfo.new(self.buf, nested_node)
5151
logger.debug_node_info(nested_capture, nested_info)
5252
if nested_capture == 'quote_marker' then
5353
self:quote_marker(nested_info, info)
@@ -101,9 +101,9 @@ function Handler:heading(info)
101101
if heading.width == 'block' then
102102
-- Overwrite anything beyond left_pad + heading width + right_pad with Normal
103103
local width = heading.left_pad + icon_width + heading.right_pad
104-
local content = ts.sibling(self.buf, info, 'inline')
104+
local content = info:sibling('inline')
105105
if content ~= nil then
106-
width = width + str.width(content.text) - ts.concealed(self.buf, content)
106+
width = width + str.width(content.text) - content:concealed()
107107
end
108108
self:add(true, info.start_row, 0, {
109109
priority = 0,
@@ -134,7 +134,7 @@ function Handler:heading_icon(info, level, foreground, background)
134134
-- Available width is level + 1 - concealed, where level = number of `#` characters, one
135135
-- is added to account for the space after the last `#` but before the heading title,
136136
-- and concealed text is subtracted since that space is not usable
137-
local width = level + 1 - ts.concealed(self.buf, info)
137+
local width = level + 1 - info:concealed()
138138
if icon == nil then
139139
return width
140140
end
@@ -230,7 +230,7 @@ function Handler:language(code_block, add_background)
230230
end
231231
if code.position == 'left' then
232232
local icon_text = icon .. ' '
233-
if ts.hidden(self.buf, info) then
233+
if info:hidden() then
234234
-- Code blocks will pick up varying amounts of leading white space depending on the
235235
-- context they are in. This gets lumped into the delimiter node and as a result,
236236
-- after concealing, the extmark will be left shifted. Logic below accounts for this.
@@ -263,18 +263,14 @@ function Handler:code_background(code_block, icon_added)
263263

264264
if code.border == 'thin' then
265265
local border_width = code_block.width - code_block.col
266-
if
267-
not icon_added
268-
and ts.hidden(self.buf, code_block.code_info)
269-
and ts.hidden(self.buf, code_block.start_delim)
270-
then
266+
if not icon_added and code_block.code_info_hidden and code_block.start_delim_hidden then
271267
self:add(true, code_block.start_row, code_block.col, {
272268
virt_text = { { code.above:rep(border_width), colors.inverse(code.highlight) } },
273269
virt_text_pos = 'overlay',
274270
})
275271
code_block.start_row = code_block.start_row + 1
276272
end
277-
if ts.hidden(self.buf, code_block.end_delim) then
273+
if code_block.end_delim_hidden then
278274
self:add(true, code_block.end_row - 1, code_block.col, {
279275
virt_text = { { code.below:rep(border_width), colors.inverse(code.highlight) } },
280276
virt_text_pos = 'overlay',
@@ -336,13 +332,13 @@ function Handler:list_marker(info)
336332
if not self.config.checkbox.enabled then
337333
return false
338334
end
339-
if ts.sibling(self.buf, info, 'task_list_marker_unchecked') ~= nil then
335+
if info:sibling('task_list_marker_unchecked') ~= nil then
340336
return true
341337
end
342-
if ts.sibling(self.buf, info, 'task_list_marker_checked') ~= nil then
338+
if info:sibling('task_list_marker_checked') ~= nil then
343339
return true
344340
end
345-
local paragraph = ts.sibling(self.buf, info, 'paragraph')
341+
local paragraph = info:sibling('paragraph')
346342
if paragraph == nil then
347343
return false
348344
end
@@ -360,7 +356,7 @@ function Handler:list_marker(info)
360356
if not bullet.enabled then
361357
return
362358
end
363-
local level = ts.level_in_section(info, 'list')
359+
local level = info:level_in_section('list')
364360
local icon = list.cycle(bullet.icons, level)
365361
if icon == nil then
366362
return
@@ -452,7 +448,7 @@ function Handler:pipe_table(info)
452448
if not pipe_table.enabled or pipe_table.style == 'none' then
453449
return
454450
end
455-
local parsed_table = pipe_table_parser.parse(self.buf, info)
451+
local parsed_table = pipe_table_parser.parse(info)
456452
if parsed_table == nil then
457453
return
458454
end
@@ -512,8 +508,7 @@ end
512508
function Handler:table_row(row, highlight)
513509
local pipe_table = self.config.pipe_table
514510
if vim.tbl_contains({ 'raw', 'padded' }, pipe_table.cell) then
515-
for cell_node in row.node:iter_children() do
516-
local cell = ts.info(cell_node, self.buf)
511+
row:for_each_child(function(cell)
517512
if cell.type == '|' then
518513
self:add(true, cell.start_row, cell.start_col, {
519514
end_row = cell.end_row,
@@ -534,7 +529,7 @@ function Handler:table_row(row, highlight)
534529
else
535530
logger.unhandled_type('markdown', 'cell', cell.type)
536531
end
537-
end
532+
end)
538533
elseif pipe_table.cell == 'overlay' then
539534
self:add(true, row.start_row, row.start_col, {
540535
end_row = row.end_row,
@@ -598,7 +593,7 @@ end
598593
---@param info render.md.NodeInfo
599594
---@return integer
600595
function Handler:table_visual_offset(info)
601-
local result = ts.concealed(self.buf, info)
596+
local result = info:concealed()
602597
local icon_ranges = context.get(self.buf):get_links(info.start_row)
603598
for _, icon_range in ipairs(icon_ranges) do
604599
if info.start_col < icon_range[2] and info.end_col > icon_range[1] then

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
local NodeInfo = require('render-markdown.node_info')
12
local component = require('render-markdown.component')
23
local context = require('render-markdown.context')
34
local list = require('render-markdown.list')
45
local logger = require('render-markdown.logger')
56
local state = require('render-markdown.state')
67
local str = require('render-markdown.str')
7-
local ts = require('render-markdown.ts')
88

99
---@class render.md.handler.buf.MarkdownInline
1010
---@field private buf integer
@@ -27,7 +27,7 @@ end
2727
---@return render.md.Mark[]
2828
function Handler:parse(root)
2929
context.get(self.buf):query(root, state.inline_query, function(capture, node)
30-
local info = ts.info(node, self.buf)
30+
local info = NodeInfo.new(self.buf, node)
3131
logger.debug_node_info(capture, info)
3232
if capture == 'code' then
3333
self:code(info)
@@ -80,7 +80,7 @@ function Handler:shortcut(info)
8080
return
8181
end
8282

83-
local line = vim.api.nvim_buf_get_lines(self.buf, info.start_row, info.start_row + 1, false)[1]
83+
local line = info:lines()[1]
8484
if line:find('[' .. info.text .. ']', 1, true) ~= nil then
8585
self:wiki_link(info)
8686
return
@@ -98,7 +98,7 @@ function Handler:callout(info, callout)
9898
---Support for overriding title: https://help.obsidian.md/Editing+and+formatting/Callouts#Change+the+title
9999
---@return string, string?
100100
local function custom_title()
101-
local content = ts.parent(self.buf, info, 'inline')
101+
local content = info:parent('inline')
102102
if content ~= nil then
103103
local line = str.split(content.text, '\n')[1]
104104
if #line > #callout.raw and vim.startswith(line:lower(), callout.raw:lower()) then
@@ -180,7 +180,7 @@ function Handler:link_virt_text(info)
180180
if info.type == 'image' then
181181
return link.image, link.highlight
182182
elseif info.type == 'inline_link' then
183-
local destination = ts.child(self.buf, info, 'link_destination')
183+
local destination = info:child('link_destination')
184184
if destination ~= nil then
185185
return self:dest_virt_text(destination.text)
186186
end

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.0.5'
8+
M.version = '6.0.6'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

0 commit comments

Comments
 (0)