Skip to content

Commit

Permalink
chore: add helper method for finding all ranges of pattern in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
MeanderingProgrammer committed Feb 2, 2025
1 parent d15a144 commit 46b0d41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lua/render-markdown/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local state = require('render-markdown.state')
local M = {}

---@private
M.version = '7.8.15'
M.version = '7.8.16'

function M.check()
M.start('version')
Expand Down
19 changes: 19 additions & 0 deletions lua/render-markdown/lib/str.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,23 @@ function M.pad_to(target, s)
return M.pad(M.width(target) - M.width(s))
end

---@param s string
---@param pattern string
---@return Range2[]
function M.find_all(s, pattern)
local result = {}
---@type integer?
local index = 1
while index ~= nil do
local start_index, end_index = s:find(pattern, index)
if start_index == nil or end_index == nil then
index = nil
else
table.insert(result, { start_index, end_index })
index = end_index + 1
end
end
return result
end

return M
34 changes: 13 additions & 21 deletions lua/render-markdown/render/inline_highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,19 @@ function Render:setup()
end

function Render:render()
---@type integer|nil
local index = 1
while index ~= nil do
local start_index, end_index = self.node.text:find('==[^=]+==', index)
if start_index == nil or end_index == nil then
index = nil
else
local start_row, start_col = self:row_col(start_index, 1)
local end_row, end_col = self:row_col(end_index, 0)
-- Hide first 2 equal signs
self:hide_equals(start_row, start_col)
-- Highlight contents
self.marks:add(false, start_row, start_col, {
end_row = end_row,
end_col = end_col,
hl_group = self.highlight.highlight,
})
-- Hide last 2 equal signs
self:hide_equals(end_row, end_col - 2)
index = end_index + 1
end
for _, range in ipairs(Str.find_all(self.node.text, '==[^=]+==')) do
local start_row, start_col = self:row_col(range[1], 1)
local end_row, end_col = self:row_col(range[2], 0)
-- Hide first 2 equal signs
self:hide_equals(start_row, start_col)
-- Highlight contents
self.marks:add(false, start_row, start_col, {
end_row = end_row,
end_col = end_col,
hl_group = self.highlight.highlight,
})
-- Hide last 2 equal signs
self:hide_equals(end_row, end_col - 2)
end
end

Expand Down

0 comments on commit 46b0d41

Please sign in to comment.