Skip to content

Commit fb7f81e

Browse files
Iterate through table children instead of using query
## Details Currently we use separate queries for processing different parts of a markdown pipe table. Instead, since we already get the top level table, we can iterate through the children which will give us the exact same nodes as from the captures. Update the default query to remove the unneeded capture groups. Add a NodeInfo class to store text value and range in table.
1 parent e099bd8 commit fb7f81e

File tree

8 files changed

+254
-228
lines changed

8 files changed

+254
-228
lines changed

lua/render-markdown/component.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,46 @@ local state = require('render-markdown.state')
66

77
local M = {}
88

9-
---@param value string
9+
---@param text string
1010
---@param comparison 'exact'|'contains'
1111
---@return render.md.Component?
12-
M.callout = function(value, comparison)
13-
---@param text string
12+
M.callout = function(text, comparison)
13+
---@param callout render.md.CustomComponent
1414
---@return boolean
15-
local function matches(text)
15+
local function matches(callout)
1616
if comparison == 'exact' then
17-
return value == text
17+
return text == callout.raw
1818
elseif comparison == 'contains' then
19-
return value:find(text, 1, true) ~= nil
19+
return text:find(callout.raw, 1, true) ~= nil
2020
else
2121
error(string.format('Unhandled comparison: %s', comparison))
2222
end
2323
end
2424
for _, callout in pairs(state.config.callout) do
25-
if matches(callout.raw) then
25+
if matches(callout) then
2626
return { text = callout.rendered, highlight = callout.highlight }
2727
end
2828
end
2929
return nil
3030
end
3131

32-
---@param value string
32+
---@param text string
3333
---@param comparison 'exact'|'starts'
3434
---@return render.md.Component?
35-
M.checkbox = function(value, comparison)
36-
---@param text string
35+
M.checkbox = function(text, comparison)
36+
---@param checkbox render.md.CustomComponent
3737
---@return boolean
38-
local function matches(text)
38+
local function matches(checkbox)
3939
if comparison == 'exact' then
40-
return value == text
40+
return text == checkbox.raw
4141
elseif comparison == 'starts' then
42-
return vim.startswith(value, text)
42+
return vim.startswith(text, checkbox.raw)
4343
else
4444
error(string.format('Unhandled comparison: %s', comparison))
4545
end
4646
end
4747
for _, checkbox in pairs(state.config.checkbox.custom) do
48-
if matches(checkbox.raw) then
48+
if matches(checkbox) then
4949
return { text = checkbox.rendered, highlight = checkbox.highlight }
5050
end
5151
end

lua/render-markdown/handler/latex.lua

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local logger = require('render-markdown.logger')
22
local state = require('render-markdown.state')
3+
local ts = require('render-markdown.ts')
34

45
---@class render.md.Cache
56
---@field expressions table<string, string[]>
@@ -15,40 +16,32 @@ local M = {}
1516
---@param root TSNode
1617
---@param buf integer
1718
M.render = function(namespace, root, buf)
18-
if not state.config.latex.enabled then
19+
local latex = state.config.latex
20+
if not latex.enabled then
1921
return
2022
end
21-
local converter = state.config.latex.converter
22-
if vim.fn.executable(converter) ~= 1 then
23-
logger.debug('Executable not found: ' .. converter)
24-
else
25-
M.render_node(namespace, buf, root, converter)
23+
if vim.fn.executable(latex.converter) ~= 1 then
24+
logger.debug('Executable not found: ' .. latex.converter)
25+
return
2626
end
27-
end
2827

29-
---@param namespace integer
30-
---@param buf integer
31-
---@param node TSNode
32-
---@param converter string
33-
M.render_node = function(namespace, buf, node, converter)
34-
local value = vim.treesitter.get_node_text(node, buf)
35-
local start_row, start_col, end_row, end_col = node:range()
36-
logger.debug_node('latex', node, buf)
37-
38-
local expressions = cache.expressions[value]
28+
local info = ts.info(root, buf)
29+
logger.debug_node_info('latex', info)
30+
31+
local expressions = cache.expressions[info.text]
3932
if expressions == nil then
40-
local raw_expression = vim.fn.system(converter, value)
33+
local raw_expression = vim.fn.system(latex.converter, info.text)
4134
local parsed_expressions = vim.split(vim.trim(raw_expression), '\n', { plain = true })
4235
expressions = vim.tbl_map(vim.trim, parsed_expressions)
43-
cache.expressions[value] = expressions
36+
cache.expressions[info.text] = expressions
4437
end
4538

4639
local latex_lines = vim.tbl_map(function(expression)
47-
return { { expression, state.config.latex.highlight } }
40+
return { { expression, latex.highlight } }
4841
end, expressions)
49-
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
50-
end_row = end_row,
51-
end_col = end_col,
42+
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
43+
end_row = info.end_row,
44+
end_col = info.end_col,
5245
virt_lines = latex_lines,
5346
virt_lines_above = true,
5447
})

0 commit comments

Comments
 (0)