Skip to content

Commit 63e0705

Browse files
chore: create node body subclass that node extends
## Details Moves the range and text fields (essentially what a treesitter node encompasses) into a separate `Body` subclass. Use this subclass as the input to various methods in particular the ones required to compute width (conceal + offset). This looser restriction will allow us to compute the widths of arbitrary ranges without needing to create a "fake" node, we'll create a real "body" instead. Currently this capability is not being used, made available for future changes. Functionally no changes occurred.
1 parent 39a330f commit 63e0705

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.8.1'
8+
M.version = '8.8.2'
99

1010
function M.check()
1111
M.start('versions')

lua/render-markdown/lib/node.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ local Position = {
99
last = 'last',
1010
}
1111

12-
---@class render.md.Node
13-
---@field private buf integer
14-
---@field private node TSNode
15-
---@field type string
12+
---@class render.md.node.Body
1613
---@field text string
1714
---@field start_row integer
1815
---@field start_col integer
1916
---@field end_row integer
2017
---@field end_col integer
18+
19+
---@class render.md.Node: render.md.node.Body
20+
---@field private buf integer
21+
---@field private node TSNode
22+
---@field type string
2123
local Node = {}
2224
Node.__index = Node
2325

@@ -226,7 +228,7 @@ function Node:line(position, by)
226228
elseif position == Position.last then
227229
row = self.end_row - (single and 0 or 1) - by
228230
else
229-
error('invalid position: ' .. position)
231+
error(('invalid position: %s'):format(position))
230232
end
231233
return row, vim.api.nvim_buf_get_lines(self.buf, row, row + 1, false)[1]
232234
end

lua/render-markdown/request/conceal.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ function Conceal:width(character)
8787
end
8888
end
8989

90-
---@param node render.md.Node
90+
---@param body render.md.node.Body
9191
---@return boolean
92-
function Conceal:hidden(node)
92+
function Conceal:hidden(body)
9393
-- conceal lines metadata require neovim >= 0.11.0 to function
94-
return compat.has_11 and self:line(node).hidden
94+
return compat.has_11 and self:line(body).hidden
9595
end
9696

97-
---@param node render.md.Node
97+
---@param body render.md.node.Body
9898
---@return integer
99-
function Conceal:get(node)
99+
function Conceal:get(body)
100100
local result = 0
101-
local col = { node.start_col, node.end_col } ---@type render.md.Range
102-
for _, section in ipairs(self:line(node).sections) do
101+
local col = { body.start_col, body.end_col } ---@type render.md.Range
102+
for _, section in ipairs(self:line(body).sections) do
103103
if interval.overlaps(section.col, col, true) then
104104
local width = section.width - self:width(section.character)
105105
result = result + width
@@ -109,14 +109,14 @@ function Conceal:get(node)
109109
end
110110

111111
---@private
112-
---@param node render.md.Node
112+
---@param body render.md.node.Body
113113
---@return render.md.request.conceal.Line
114-
function Conceal:line(node)
114+
function Conceal:line(body)
115115
if not self.computed then
116116
self.computed = true
117117
self:compute()
118118
end
119-
local line = self.lines[node.start_row]
119+
local line = self.lines[body.start_row]
120120
if not line then
121121
line = { hidden = false, sections = {} }
122122
end

lua/render-markdown/request/context.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ function Context.new(buf, win, config, view)
3838
return self
3939
end
4040

41-
---@param node? render.md.Node
41+
---@param body? render.md.node.Body
4242
---@return integer
43-
function Context:width(node)
44-
if not node then
43+
function Context:width(body)
44+
if not body then
4545
return 0
4646
end
47-
return str.width(node.text) + self.offset:get(node) - self.conceal:get(node)
47+
return str.width(body.text) + self.offset:get(body) - self.conceal:get(body)
4848
end
4949

5050
---@class render.md.request.context.Manager

lua/render-markdown/request/offset.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ function Offset:add(row, value)
2727
values[#values + 1] = value
2828
end
2929

30-
---@param node render.md.Node
30+
---@param body render.md.node.Body
3131
---@return integer
32-
function Offset:get(node)
32+
function Offset:get(body)
3333
local result = 0
34-
local values = self.values[node.start_row] or {}
34+
local values = self.values[body.start_row] or {}
3535
for _, value in ipairs(values) do
36-
if node.start_col <= value.col and node.end_col > value.col then
36+
if body.start_col <= value.col and body.end_col > value.col then
3737
result = result + value.width
3838
end
3939
end

0 commit comments

Comments
 (0)