Skip to content

Commit 9c4af1d

Browse files
Refactor move parsing into a setup function per renderer
1 parent e919a42 commit 9c4af1d

File tree

11 files changed

+334
-425
lines changed

11 files changed

+334
-425
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 26
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 27
22

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

lua/render-markdown/core/component.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
local state = require('render-markdown.state')
2-
31
---@class render.md.ComponentHelper
42
local M = {}
53

6-
---@param buf integer
4+
---@param config render.md.BufferConfig
75
---@param text string
86
---@param comparison 'exact'|'contains'
97
---@return render.md.CustomComponent?
10-
function M.callout(buf, text, comparison)
8+
function M.callout(config, text, comparison)
119
---@param callout render.md.CustomComponent
1210
---@return boolean
1311
local function matches(callout)
@@ -19,19 +17,19 @@ function M.callout(buf, text, comparison)
1917
error(string.format('Unhandled comparison: %s', comparison))
2018
end
2119
end
22-
for _, callout in pairs(state.get_config(buf).callout) do
20+
for _, callout in pairs(config.callout) do
2321
if matches(callout) then
2422
return callout
2523
end
2624
end
2725
return nil
2826
end
2927

30-
---@param buf integer
28+
---@param config render.md.BufferConfig
3129
---@param text string
3230
---@param comparison 'exact'|'starts'
3331
---@return render.md.CustomComponent?
34-
function M.checkbox(buf, text, comparison)
32+
function M.checkbox(config, text, comparison)
3533
---@param checkbox render.md.CustomComponent
3634
---@return boolean
3735
local function matches(checkbox)
@@ -43,7 +41,7 @@ function M.checkbox(buf, text, comparison)
4341
error(string.format('Unhandled comparison: %s', comparison))
4442
end
4543
end
46-
for _, checkbox in pairs(state.get_config(buf).checkbox.custom) do
44+
for _, checkbox in pairs(config.checkbox.custom) do
4745
if matches(checkbox) then
4846
return checkbox
4947
end

lua/render-markdown/core/context.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local NodeInfo = require('render-markdown.core.node_info')
2+
local logger = require('render-markdown.core.logger')
13
local str = require('render-markdown.core.str')
24
local util = require('render-markdown.core.util')
35

@@ -99,11 +101,13 @@ end
99101

100102
---@param root TSNode
101103
---@param query vim.treesitter.Query
102-
---@param callback fun(capture: string, node: TSNode, metadata: vim.treesitter.query.TSMetadata)
104+
---@param callback fun(capture: string, node: render.md.NodeInfo)
103105
function Context:query(root, query, callback)
104-
for id, node, metadata in query:iter_captures(root, self.buf, self.top, self.bottom) do
106+
for id, node in query:iter_captures(root, self.buf, self.top, self.bottom) do
105107
local capture = query.captures[id]
106-
callback(capture, node, metadata)
108+
local info = NodeInfo.new(self.buf, node)
109+
logger.debug_node_info(capture, info)
110+
callback(capture, info)
107111
end
108112
end
109113

@@ -188,11 +192,11 @@ function Context:compute_conceal_nodes(language, root)
188192
return {}
189193
end
190194
local nodes = {}
191-
self:query(root, query, function(_, node, metadata)
195+
for _, node, metadata in query:iter_captures(root, self.buf, self.top, self.bottom) do
192196
if metadata.conceal ~= nil then
193197
table.insert(nodes, node)
194198
end
195-
end)
199+
end
196200
return nodes
197201
end
198202

lua/render-markdown/handler/markdown.lua

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
local Context = require('render-markdown.core.context')
2-
local NodeInfo = require('render-markdown.core.node_info')
3-
local RenderCode = require('render-markdown.render.code')
4-
local RenderHeading = require('render-markdown.render.heading')
5-
local RenderQuote = require('render-markdown.render.quote')
6-
local RenderTable = require('render-markdown.render.table')
72
local component = require('render-markdown.core.component')
83
local list = require('render-markdown.core.list')
94
local logger = require('render-markdown.core.logger')
105
local state = require('render-markdown.state')
116
local str = require('render-markdown.core.str')
127

138
---@class render.md.handler.buf.Markdown
14-
---@field private buf integer
159
---@field private marks render.md.Marks
1610
---@field private config render.md.BufferConfig
1711
---@field private context render.md.Context
@@ -23,29 +17,28 @@ Handler.__index = Handler
2317
---@return render.md.handler.buf.Markdown
2418
function Handler.new(buf)
2519
local self = setmetatable({}, Handler)
26-
self.buf = buf
2720
self.marks = list.new_marks()
2821
self.config = state.get_config(buf)
2922
self.context = Context.get(buf)
3023
self.renderers = {
31-
code = RenderCode.new(buf, self.marks, self.config, self.context),
32-
heading = RenderHeading.new(buf, self.marks, self.config, self.context),
33-
quote = RenderQuote.new(buf, self.marks, self.config, self.context),
34-
table = RenderTable.new(buf, self.marks, self.config, self.context),
24+
code = require('render-markdown.render.code'),
25+
heading = require('render-markdown.render.heading'),
26+
quote = require('render-markdown.render.quote'),
27+
table = require('render-markdown.render.table'),
3528
}
3629
return self
3730
end
3831

3932
---@param root TSNode
4033
---@return render.md.Mark[]
4134
function Handler:parse(root)
42-
self.context:query(root, state.markdown_query, function(capture, node)
43-
local info = NodeInfo.new(self.buf, node)
44-
logger.debug_node_info(capture, info)
45-
35+
self.context:query(root, state.markdown_query, function(capture, info)
4636
local renderer = self.renderers[capture]
4737
if renderer ~= nil then
48-
renderer:render(info)
38+
local render = renderer.new(self.marks, self.config, self.context, info)
39+
if render:setup() then
40+
render:render()
41+
end
4942
elseif capture == 'section' then
5043
self:section(info)
5144
elseif capture == 'dash' then
@@ -120,7 +113,7 @@ function Handler:list_marker(info)
120113
return true
121114
end
122115
local paragraph = info:sibling('paragraph')
123-
return paragraph ~= nil and component.checkbox(self.buf, paragraph.text, 'starts') ~= nil
116+
return paragraph ~= nil and component.checkbox(self.config, paragraph.text, 'starts') ~= nil
124117
end
125118
if sibling_checkbox() then
126119
-- Hide the list marker for checkboxes rather than replacing with a bullet point

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
local Context = require('render-markdown.core.context')
2-
local NodeInfo = require('render-markdown.core.node_info')
32
local component = require('render-markdown.core.component')
43
local list = require('render-markdown.core.list')
54
local logger = require('render-markdown.core.logger')
65
local state = require('render-markdown.state')
76
local str = require('render-markdown.core.str')
87

98
---@class render.md.handler.buf.MarkdownInline
10-
---@field private buf integer
119
---@field private marks render.md.Marks
1210
---@field private config render.md.BufferConfig
1311
---@field private context render.md.Context
@@ -18,7 +16,6 @@ Handler.__index = Handler
1816
---@return render.md.handler.buf.MarkdownInline
1917
function Handler.new(buf)
2018
local self = setmetatable({}, Handler)
21-
self.buf = buf
2219
self.marks = list.new_marks()
2320
self.config = state.get_config(buf)
2421
self.context = Context.get(buf)
@@ -28,10 +25,7 @@ end
2825
---@param root TSNode
2926
---@return render.md.Mark[]
3027
function Handler:parse(root)
31-
self.context:query(root, state.inline_query, function(capture, node)
32-
local info = NodeInfo.new(self.buf, node)
33-
logger.debug_node_info(capture, info)
34-
28+
self.context:query(root, state.inline_query, function(capture, info)
3529
if capture == 'code' then
3630
self:code(info)
3731
elseif capture == 'shortcut' then
@@ -62,13 +56,13 @@ end
6256
---@private
6357
---@param info render.md.NodeInfo
6458
function Handler:shortcut(info)
65-
local callout = component.callout(self.buf, info.text, 'exact')
59+
local callout = component.callout(self.config, info.text, 'exact')
6660
if callout ~= nil then
6761
self:callout(info, callout)
6862
return
6963
end
7064

71-
local checkbox = component.checkbox(self.buf, info.text, 'exact')
65+
local checkbox = component.checkbox(self.config, info.text, 'exact')
7266
if checkbox ~= nil then
7367
self:checkbox(info, checkbox)
7468
return

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.2.7'
8+
M.version = '6.2.8'
99

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

0 commit comments

Comments
 (0)