Skip to content

Commit af51e3a

Browse files
chore(refactor): replace remaining usage of Range2
## Details Replaces remaining use of the Range2 class with internal Range class. Remove overlap method from Extmark and call it directly on Range instance. Remove method for checking overlap with NodeInfo and use method for overlap with TSNode in all places. Make clear the distinction between contains and overlaps via names and use them consistently.
1 parent 767707e commit af51e3a

File tree

8 files changed

+24
-41
lines changed

8 files changed

+24
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
& border virtual option [#183](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/183)
2020
[aad1a12](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/aad1a1220dc9da5757e3af3befbc7fc3869dd334)
2121
- config command to debug configurations [a9643f4](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/a9643f4377f39f4abf943fbc73be69f33f5f2f1d)
22+
- same buffer in multiple windows [#184](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/184)
23+
[767707e](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/767707e928389996e8860f03552cf962afb0bfb2)
2224

2325
### Bug Fixes
2426

lua/render-markdown/config.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local Range = require('render-markdown.core.range')
2+
13
---@class render.md.component.Config
24
---@field callout table<string, render.md.CustomComponent>
35
---@field checkbox table<string, render.md.CustomComponent>
@@ -42,7 +44,7 @@ end
4244

4345
---@param mode string
4446
---@param row? integer
45-
---@return Range2?
47+
---@return render.md.Range?
4648
function Config:hidden(mode, row)
4749
-- Anti-conceal is not enabled -> hide nothing
4850
-- Row is not known means buffer is not active -> hide nothing
@@ -51,9 +53,9 @@ function Config:hidden(mode, row)
5153
end
5254
if vim.tbl_contains({ 'v', 'V', '\22' }, mode) then
5355
local start = vim.fn.getpos('v')[2] - 1
54-
return { math.min(row, start), math.max(row, start) }
56+
return Range.new(math.min(row, start), math.max(row, start))
5557
else
56-
return { row - self.anti_conceal.above, row + self.anti_conceal.below }
58+
return Range.new(row - self.anti_conceal.above, row + self.anti_conceal.below)
5759
end
5860
end
5961

lua/render-markdown/core/context.lua

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,17 @@ end
140140
function Context:contains_window(win)
141141
local window_range = Context.compute_range(self.buf, win, 0)
142142
for _, range in ipairs(self.ranges) do
143-
if range:contains(window_range) then
143+
if range:contains(window_range.top, window_range.bottom) then
144144
return true
145145
end
146146
end
147147
return false
148148
end
149149

150-
---@private
151-
---@param top integer
152-
---@param bottom integer
150+
---@param node TSNode
153151
---@return boolean
154-
function Context:overlap(top, bottom)
152+
function Context:overlaps_node(node)
153+
local top, _, bottom, _ = node:range()
155154
for _, range in ipairs(self.ranges) do
156155
if range:overlaps(top, bottom) then
157156
return true
@@ -160,19 +159,6 @@ function Context:overlap(top, bottom)
160159
return false
161160
end
162161

163-
---@param node TSNode
164-
---@return boolean
165-
function Context:contains_node(node)
166-
local top, _, bottom, _ = node:range()
167-
return self:overlap(top, bottom)
168-
end
169-
170-
---@param info render.md.NodeInfo
171-
---@return boolean
172-
function Context:contains_info(info)
173-
return self:overlap(info.start_row, info.end_row)
174-
end
175-
176162
---@param parser vim.treesitter.LanguageTree
177163
function Context:parse(parser)
178164
for _, range in ipairs(self.ranges) do
@@ -260,7 +246,7 @@ end
260246
---@param root TSNode
261247
---@return TSNode[]
262248
function Context:compute_conceal_nodes(language, root)
263-
if not self:contains_node(root) then
249+
if not self:overlaps_node(root) then
264250
return {}
265251
end
266252
if not vim.tbl_contains({ 'markdown', 'markdown_inline' }, language) then

lua/render-markdown/core/extmark.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ function Extmark:get_mark()
1818
return self.mark
1919
end
2020

21-
---@param hidden Range2?
22-
---@return boolean
23-
function Extmark:overlaps(hidden)
24-
if hidden == nil then
25-
return false
26-
end
27-
local row = self.mark.start_row
28-
return row >= hidden[1] and row <= hidden[2]
29-
end
30-
3121
---@param ns_id integer
3222
---@param buf integer
3323
function Extmark:show(ns_id, buf)

lua/render-markdown/core/range.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ function Range.__lt(a, b)
2525
end
2626
end
2727

28-
---@param other render.md.Range
28+
---@param top integer
29+
---@param bottom integer
2930
---@return boolean
30-
function Range:contains(other)
31-
return self.top <= other.top and self.bottom >= other.bottom
31+
function Range:contains(top, bottom)
32+
return self.top <= top and self.bottom >= bottom
3233
end
3334

3435
---@param top integer

lua/render-markdown/core/ui.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ function M.get_row_marks(buf, win)
5252

5353
local marks = {}
5454
for _, extmark in ipairs(buffer_state:get_marks()) do
55-
if extmark:overlaps(hidden) then
56-
table.insert(marks, extmark:get_mark())
55+
local mark = extmark:get_mark()
56+
if hidden:contains(mark.start_row, mark.start_row) then
57+
table.insert(marks, mark)
5758
end
5859
end
5960
return row, marks
@@ -115,7 +116,8 @@ function M.update(buf, win, parse)
115116
end
116117
local hidden = config:hidden(mode, row)
117118
for _, extmark in ipairs(buffer_state:get_marks()) do
118-
if extmark:get_mark().conceal and extmark:overlaps(hidden) then
119+
local mark = extmark:get_mark()
120+
if mark.conceal and hidden ~= nil and hidden:contains(mark.start_row, mark.start_row) then
119121
extmark:hide(M.namespace, buf)
120122
else
121123
extmark:show(M.namespace, buf)
@@ -186,7 +188,7 @@ end
186188
---@return render.md.Mark[]
187189
function M.parse_tree(buf, language, root)
188190
log.buf('debug', 'language', buf, language)
189-
if not Context.get(buf):contains_node(root) then
191+
if not Context.get(buf):overlaps_node(root) then
190192
return {}
191193
end
192194

lua/render-markdown/health.lua

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

66
---@private
7-
M.version = '7.1.13'
7+
M.version = '7.1.14'
88

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

lua/render-markdown/render/table.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function Render:setup()
5656
self.info:for_each_child(function(row)
5757
if row.type == 'pipe_table_delimiter_row' then
5858
delim = self:parse_delim(row)
59-
elseif self.context:contains_info(row) then
59+
elseif self.context:overlaps_node(row:get_node()) then
6060
if vim.tbl_contains({ 'pipe_table_header', 'pipe_table_row' }, row.type) then
6161
table.insert(table_rows, row)
6262
else

0 commit comments

Comments
 (0)