Skip to content

Commit 5ff191f

Browse files
feat: anti conceal selected range in visual mode
## Details Request: #168 Rather than removing marks on current row only, for visual mode specifically use the range of the selected text. This allows users to see what's actually being selected rather that still being overlayed by marks. All other modes continue to behave the same as before. Moves the logic for caclulating the hidden range out of `extmark` module and into `ui` since that now seems like a better fit.
1 parent 01b38dc commit 5ff191f

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

lua/render-markdown/core/extmark.lua

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ function Extmark:overlaps(row)
3030
return not (start_row > row or end_row <= row)
3131
end
3232

33-
---@param config render.md.buffer.Config
34-
---@param row? integer
35-
function Extmark:render(config, row)
36-
if self:should_show(config.anti_conceal, row) then
33+
---@param hide_range { [1]: integer, [2]: integer }?
34+
function Extmark:render(hide_range)
35+
if self:should_show(hide_range) then
3736
self:show()
3837
else
3938
self:hide()
@@ -62,27 +61,16 @@ function Extmark:hide()
6261
end
6362
end
6463

65-
---Render marks based on anti-conceal behavior and current row
6664
---@private
67-
---@param anti_conceal render.md.AntiConceal
68-
---@param row? integer
65+
---@param hide_range { [1]: integer, [2]: integer }?
6966
---@return boolean
70-
function Extmark:should_show(anti_conceal, row)
71-
-- Anti-conceal is not enabled -> all marks should be shown
72-
if not anti_conceal.enabled then
67+
function Extmark:should_show(hide_range)
68+
if hide_range == nil or not self.mark.conceal then
7369
return true
7470
end
75-
-- Row is not known means buffer is not active -> all marks should be shown
76-
if row == nil then
77-
return true
78-
end
79-
-- Mark is not concealable -> mark should always be shown
80-
if not self.mark.conceal then
81-
return true
82-
end
83-
-- Show mark if it is outside current row range
84-
local mark_row = self.mark.start_row
85-
return mark_row < row - anti_conceal.above or mark_row > row + anti_conceal.below
71+
-- Show mark if it is outside hidden range
72+
local row = self.mark.start_row
73+
return row < hide_range[1] or row > hide_range[2]
8674
end
8775

8876
return Extmark

lua/render-markdown/core/ui.lua

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ function M.update(buf, win, parse)
8787

8888
local config = state.get_config(buf)
8989
local buffer_state = cache[buf]
90+
local mode = vim.fn.mode(true)
9091

91-
local next_state = M.next_state(config, win)
92+
local next_state = M.next_state(config, win, mode)
9293
if next_state ~= buffer_state.state then
9394
for name, value in pairs(config.win_options) do
9495
util.set_win(win, name, value[next_state])
@@ -102,8 +103,9 @@ function M.update(buf, win, parse)
102103
buffer_state.marks = M.parse_buffer(buf, win)
103104
end
104105
local row = util.cursor_row(buf, win)
106+
local hide_range = M.hide_range(config.anti_conceal, mode, row)
105107
for _, mark in ipairs(buffer_state.marks) do
106-
mark:render(config, row)
108+
mark:render(hide_range)
107109
end
108110
else
109111
M.clear(buf, buffer_state)
@@ -113,8 +115,9 @@ end
113115
---@private
114116
---@param config render.md.buffer.Config
115117
---@param win integer
118+
---@param mode string
116119
---@return 'default'|'rendered'
117-
function M.next_state(config, win)
120+
function M.next_state(config, win, mode)
118121
if not state.enabled then
119122
return 'default'
120123
end
@@ -124,12 +127,34 @@ function M.next_state(config, win)
124127
if not util.view(win).leftcol == 0 then
125128
return 'default'
126129
end
127-
if not config:render(vim.fn.mode(true)) then
130+
if not config:render(mode) then
128131
return 'default'
129132
end
130133
return 'rendered'
131134
end
132135

136+
---@private
137+
---@param config render.md.AntiConceal
138+
---@param mode string
139+
---@param row? integer
140+
---@return { [1]: integer, [2]: integer }?
141+
function M.hide_range(config, mode, row)
142+
-- Anti-conceal is not enabled -> hide nothing
143+
if not config.enabled then
144+
return nil
145+
end
146+
-- Row is not known means buffer is not active -> hide nothing
147+
if row == nil then
148+
return nil
149+
end
150+
if mode == 'v' then
151+
local start = vim.fn.getpos('v')[2] - 1
152+
return { math.min(row, start), math.max(row, start) }
153+
else
154+
return { row - config.above, row + config.below }
155+
end
156+
end
157+
133158
---@private
134159
---@param buf integer
135160
---@param win integer

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.0.8'
7+
M.version = '7.0.9'
88

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

0 commit comments

Comments
 (0)