Skip to content

Commit 8bb0d47

Browse files
chore: move find pattern ranges logic to node class
## Details Finding the range of string patterns relative to a node really belongs in the node class since it depends on the node's position along with its text. Remove the old usage of `Str.find_all` + helper. Will make using patterns in more places easier if we choose to do so. Minor style change, inline `@type` annotations rather than having them be on separate lines in a few contexts. Update changelog.
1 parent 91d40c2 commit 8bb0d47

File tree

10 files changed

+55
-61
lines changed

10 files changed

+55
-61
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
[c283dec](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/c283dec1ea94947499c36bb17443e15d6acf5cda)
1212
- wrap `nvim_buf_set_extmark` in pcall use notify_once if it errors [#382](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/382)
1313
[1e2e9a3](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/1e2e9a386fbe41b869d3d0e000e19db72284585b)
14+
- ignore option, checked before attaching [05e6a6d](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/05e6a6d119f90b99829ecb7eb85428a226c0c05f)
15+
- on.initial option, called before adding marks for the first time [#396](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/396)
16+
[91d40c2](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/91d40c2f37a4373ec29a47fcf3ce656408d302dc)
1417

1518
## 8.2.0 (2025-03-31)
1619

lua/render-markdown/handler/latex.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ function M.parse(ctx)
3737
cache[node.text] = raw_expression
3838
end
3939

40-
---@type string[]
41-
local expressions = {}
40+
local expressions = {} ---@type string[]
4241
for _ = 1, latex.top_pad do
4342
expressions[#expressions + 1] = ''
4443
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 state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.2.9'
8+
M.version = '8.2.10'
99

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

lua/render-markdown/integ/coq.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ end
2929
---@param map table<integer, table>
3030
---@return integer
3131
function M.new_uid(map)
32-
---@type integer|nil
33-
local key = nil
32+
local key = nil ---@type integer?
3433
while true do
3534
if not key or map[key] then
3635
key = math.floor(math.random() * 10000)

lua/render-markdown/lib/node.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,40 @@ function Node:widths()
204204
return Iter.list.map(lines, Str.width)
205205
end
206206

207+
---@param pattern string
208+
---@return Range4[]
209+
function Node:find(pattern)
210+
local result = {} ---@type Range4[]
211+
local index = 1 ---@type integer?
212+
while index ~= nil do
213+
local start_index, end_index = self.text:find(pattern, index)
214+
if start_index == nil or end_index == nil then
215+
index = nil
216+
else
217+
index = end_index + 1
218+
-- start : 1-based inclusive -> 0-based inclusive = -1 offset
219+
-- end : 1-based inclusive -> 0-based exclusive = 0 offset
220+
local start_row, start_col = self:position(start_index, -1)
221+
local end_row, end_col = self:position(end_index, 0)
222+
result[#result + 1] = { start_row, start_col, end_row, end_col }
223+
end
224+
end
225+
return result
226+
end
227+
228+
---@private
229+
---@param index integer
230+
---@param offset integer
231+
---@return integer, integer
232+
function Node:position(index, offset)
233+
local lines = Str.split(self.text:sub(1, index), '\n')
234+
-- start row includes first line
235+
local row = self.start_row + #lines - 1
236+
local col = #lines[#lines] + offset
237+
if row == self.start_row then
238+
col = col + self.start_col
239+
end
240+
return row, col
241+
end
242+
207243
return Node

lua/render-markdown/lib/str.lua

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,4 @@ function M.pad(n)
4848
return string.rep(' ', n)
4949
end
5050

51-
---@param s string
52-
---@param pattern string
53-
---@return Range2[]
54-
function M.find_all(s, pattern)
55-
local result = {}
56-
---@type integer?
57-
local index = 1
58-
while index ~= nil do
59-
local start_index, end_index = s:find(pattern, index)
60-
if start_index == nil or end_index == nil then
61-
index = nil
62-
else
63-
result[#result + 1] = { start_index, end_index }
64-
index = end_index + 1
65-
end
66-
end
67-
return result
68-
end
69-
7051
return M

lua/render-markdown/render/inline_highlight.lua

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local Base = require('render-markdown.render.base')
2-
local Str = require('render-markdown.lib.str')
32

43
---@class render.md.render.InlineHighlight: render.md.Renderer
54
---@field private info render.md.inline.highlight.Config
@@ -16,36 +15,20 @@ function Render:setup()
1615
end
1716

1817
function Render:render()
19-
for _, range in ipairs(Str.find_all(self.node.text, '==[^=]+==')) do
20-
local start_row, start_col = self:row_col(range[1], 1)
21-
local end_row, end_col = self:row_col(range[2], 0)
18+
for _, range in ipairs(self.node:find('==[^=]+==')) do
2219
-- Hide first 2 equal signs
23-
self:hide_equals(start_row, start_col)
20+
self:hide_equals(range[1], range[2])
2421
-- Highlight contents
25-
self.marks:add(false, start_row, start_col, {
26-
end_row = end_row,
27-
end_col = end_col,
22+
self.marks:add(false, range[1], range[2], {
23+
end_row = range[3],
24+
end_col = range[4],
2825
hl_group = self.info.highlight,
2926
})
3027
-- Hide last 2 equal signs
31-
self:hide_equals(end_row, end_col - 2)
28+
self:hide_equals(range[3], range[4] - 2)
3229
end
3330
end
3431

35-
---@private
36-
---@param index integer
37-
---@param offset integer
38-
---@return integer, integer
39-
function Render:row_col(index, offset)
40-
local lines = Str.split(self.node.text:sub(1, index), '\n')
41-
local row = self.node.start_row + #lines - 1
42-
local col = #lines[#lines] - offset
43-
if row == self.node.start_row then
44-
col = col + self.node.start_col
45-
end
46-
return row, col
47-
end
48-
4932
---@private
5033
---@param row integer
5134
---@param col integer

lua/render-markdown/render/table.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ function Render:setup()
5858
return false
5959
end
6060

61-
---@type render.md.table.DelimRow?
62-
local delim = nil
63-
---@type render.md.Node[]
64-
local table_rows = {}
61+
local delim = nil ---@type render.md.table.DelimRow?
62+
local table_rows = {} ---@type render.md.Node[]
6563
self.node:for_each_child(function(row)
6664
if row.type == 'pipe_table_delimiter_row' then
6765
delim = self:parse_delim(row)
@@ -84,8 +82,7 @@ function Render:setup()
8482
return false
8583
end
8684

87-
---@type render.md.table.Row[]
88-
local rows = {}
85+
local rows = {} ---@type render.md.table.Row[]
8986
table.sort(table_rows)
9087
for _, table_row in ipairs(table_rows) do
9188
local row = self:parse_row(table_row, #delim.columns)
@@ -131,8 +128,7 @@ function Render:parse_delim(row)
131128
if pipes == nil or cells == nil then
132129
return nil
133130
end
134-
---@type render.md.table.DelimColumn[]
135-
local columns = {}
131+
local columns = {} ---@type render.md.table.DelimColumn[]
136132
for i, cell in ipairs(cells) do
137133
local width = pipes[i + 1].start_col - pipes[i].end_col
138134
if width < 0 then
@@ -178,8 +174,7 @@ function Render:parse_row(row, num_columns)
178174
if pipes == nil or cells == nil or #cells ~= num_columns then
179175
return nil
180176
end
181-
---@type render.md.table.Column[]
182-
local columns = {}
177+
local columns = {} ---@type render.md.table.Column[]
183178
for i, cell in ipairs(cells) do
184179
local start_col, end_col = pipes[i].end_col, pipes[i + 1].start_col
185180
-- Account for double width glyphs by replacing cell range with its width

tests/helpers/marks.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function Marks:add(start_row, end_row, start_col, end_col, opts)
2626
local mark = opts
2727
mark.row = { start_row, end_row }
2828
mark.col = { start_col, end_col }
29-
---@diagnostic disable-next-line: assign-type-mismatch
30-
self.marks[#self.marks + 1] = opts
29+
self.marks[#self.marks + 1] = mark
3130
end
3231

3332
return Marks

tests/util.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ function M.actual_marks()
399399
local marks = vim.api.nvim_buf_get_extmarks(0, ui.ns, 0, -1, {
400400
details = true,
401401
})
402-
---@type render.md.test.MarkDetails[]
403-
local actual = {}
402+
local actual = {} ---@type render.md.test.MarkDetails[]
404403
for _, mark in ipairs(marks) do
405404
local _, row, col, details = unpack(mark)
406405
local info = require('tests.helpers.details').new(row, col, details)

0 commit comments

Comments
 (0)