Skip to content

Commit d723bfd

Browse files
chore(refactor): share overlap logic between debug & render
## Details Moves top level render method in Extmark out into ui module which decides based on overlap whether to show or hide each mark. Moves namespace and buf out of Extmark, instead passing it in to methods as needed. Updates debug method to use the same overlap method as the main update call that does rendering. Effectively means that everything hidden on the current line is what gets returned when debugging + those marks which are not concealable and would be hidden based on range overlap.
1 parent a0777ec commit d723bfd

File tree

5 files changed

+44
-68
lines changed

5 files changed

+44
-68
lines changed

lua/render-markdown/api.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function M.contract()
3333
end
3434

3535
function M.debug()
36-
local buf = vim.api.nvim_get_current_buf()
37-
local row, marks = require('render-markdown.core.ui').get_row_marks(buf)
36+
local buf, win = vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win()
37+
local row, marks = require('render-markdown.core.ui').get_row_marks(buf, win)
3838
require('render-markdown.debug.marks').debug(row, marks)
3939
end
4040

lua/render-markdown/core/extmark.lua

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,45 @@
11
---@class render.md.Extmark
2-
---@field private namespace integer
3-
---@field private buf integer
42
---@field private id? integer
53
---@field mark render.md.Mark
64
local Extmark = {}
75
Extmark.__index = Extmark
86

9-
---@param namespace integer
10-
---@param buf integer
117
---@param mark render.md.Mark
128
---@return render.md.Extmark
13-
function Extmark.new(namespace, buf, mark)
9+
function Extmark.new(mark)
1410
local self = setmetatable({}, Extmark)
15-
self.namespace = namespace
16-
self.buf = buf
1711
self.id = nil
1812
self.mark = mark
1913
return self
2014
end
2115

22-
---@param row integer
23-
---@return boolean
24-
function Extmark:overlaps(row)
25-
local start_row = self.mark.start_row
26-
local end_row = self.mark.opts.end_row or start_row
27-
if start_row == end_row then
28-
end_row = end_row + 1
29-
end
30-
return not (start_row > row or end_row <= row)
31-
end
32-
3316
---@param hidden Range2?
34-
function Extmark:render(hidden)
35-
if self:should_show(hidden) then
36-
self:show()
37-
else
38-
self:hide()
17+
---@return boolean
18+
function Extmark:overlaps(hidden)
19+
if hidden == nil then
20+
return false
3921
end
22+
local row = self.mark.start_row
23+
return row >= hidden[1] and row <= hidden[2]
4024
end
4125

42-
---@private
43-
function Extmark:show()
26+
---@param ns_id integer
27+
---@param buf integer
28+
function Extmark:show(ns_id, buf)
4429
if self.id == nil then
45-
self.mark.opts.strict = false
46-
self.id = vim.api.nvim_buf_set_extmark(
47-
self.buf,
48-
self.namespace,
49-
self.mark.start_row,
50-
self.mark.start_col,
51-
self.mark.opts
52-
)
30+
local mark = self.mark
31+
mark.opts.strict = false
32+
self.id = vim.api.nvim_buf_set_extmark(buf, ns_id, mark.start_row, mark.start_col, mark.opts)
5333
end
5434
end
5535

56-
---@private
57-
function Extmark:hide()
36+
---@param ns_id integer
37+
---@param buf integer
38+
function Extmark:hide(ns_id, buf)
5839
if self.id ~= nil then
59-
vim.api.nvim_buf_del_extmark(self.buf, self.namespace, self.id)
40+
vim.api.nvim_buf_del_extmark(buf, ns_id, self.id)
6041
self.id = nil
6142
end
6243
end
6344

64-
---@private
65-
---@param hidden Range2?
66-
---@return boolean
67-
function Extmark:should_show(hidden)
68-
if hidden == nil or not self.mark.conceal then
69-
return true
70-
end
71-
-- Show mark if it is outside hidden range
72-
local row = self.mark.start_row
73-
return row < hidden[1] or row > hidden[2]
74-
end
75-
7645
return Extmark

lua/render-markdown/core/ui.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ function M.invalidate_cache()
4242
end
4343

4444
---@param buf integer
45+
---@param win integer
4546
---@return integer, render.md.Mark[]
46-
function M.get_row_marks(buf)
47-
local row = util.cursor_row(buf)
48-
if row == nil then
49-
return 0, {}
50-
end
51-
local marks, buffer_state = {}, Cache.get(buf)
47+
function M.get_row_marks(buf, win)
48+
local config, buffer_state = state.get(buf), Cache.get(buf)
49+
local mode, row = util.mode(), util.row(buf, win)
50+
local hidden = config:hidden(mode, row)
51+
assert(row ~= nil and hidden ~= nil, 'Row & range must be known to get marks')
52+
53+
local marks = {}
5254
for _, extmark in ipairs(buffer_state.marks or {}) do
53-
if extmark:overlaps(row) then
55+
if extmark:overlaps(hidden) then
5456
table.insert(marks, extmark.mark)
5557
end
5658
end
@@ -98,7 +100,7 @@ function M.update(buf, win, parse)
98100
end
99101

100102
local config, buffer_state = state.get(buf), Cache.get(buf)
101-
local mode = vim.fn.mode(true)
103+
local mode, row = util.mode(), util.row(buf, win)
102104

103105
local next_state = M.next_state(config, win, mode)
104106
if next_state ~= buffer_state.state then
@@ -113,10 +115,13 @@ function M.update(buf, win, parse)
113115
M.clear(buf, buffer_state)
114116
buffer_state.marks = M.parse_buffer(buf, win)
115117
end
116-
local row = util.cursor_row(buf, win)
117118
local hidden = config:hidden(mode, row)
118-
for _, mark in ipairs(buffer_state.marks) do
119-
mark:render(hidden)
119+
for _, extmark in ipairs(buffer_state.marks) do
120+
if extmark.mark.conceal and extmark:overlaps(hidden) then
121+
extmark:hide(M.namespace, buf)
122+
else
123+
extmark:show(M.namespace, buf)
124+
end
120125
end
121126
else
122127
M.clear(buf, buffer_state)
@@ -171,9 +176,7 @@ function M.parse_buffer(buf, win)
171176
for _, root in ipairs(markdown_roots) do
172177
vim.list_extend(marks, M.parse_tree(buf, 'markdown', root))
173178
end
174-
return vim.tbl_map(function(mark)
175-
return Extmark.new(M.namespace, buf, mark)
176-
end, marks)
179+
return vim.tbl_map(Extmark.new, marks)
177180
end
178181

179182
---Run user & builtin handlers when available. User handler is always executed,

lua/render-markdown/core/util.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ function M.valid(buf, win)
1010
return vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_win_is_valid(win)
1111
end
1212

13+
---@return string
14+
function M.mode()
15+
return vim.fn.mode(true)
16+
end
17+
1318
---@param buf integer
14-
---@param win? integer
19+
---@param win integer
1520
---@return integer?
16-
function M.cursor_row(buf, win)
21+
function M.row(buf, win)
1722
if vim.api.nvim_get_current_buf() ~= buf then
1823
return nil
1924
end
20-
win = win or vim.api.nvim_get_current_win()
2125
return vim.api.nvim_win_get_cursor(win)[1] - 1
2226
end
2327

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.0'
7+
M.version = '7.1.1'
88

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

0 commit comments

Comments
 (0)