Skip to content

Commit 354baf4

Browse files
fix: handle all visual modes when getting range
## Details When getting the range of hidden lines for visual mode only the base character-wise mode ('v') was handled. This change adds support for line-wise ('V') and block-wise ('<C-v>'). Minor other changes: - Moves the hidden calculation out of UI and into config - Makes UI cache a class that creates new states if they are missing on get so that logic exists in a single place - Rename `get_config` method in state module to just `get`
1 parent 9f2fb6c commit 354baf4

File tree

11 files changed

+68
-56
lines changed

11 files changed

+68
-56
lines changed

Diff for: CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@
99
- table min width [f84eeae](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/f84eeaebac278e26bd2906fd47747631716a5edb)
1010
- new debug API for development [6f87257](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/6f8725746ecadae0ae5ab3e7a1a445dad6b2e231)
1111
- `render_modes` as a boolean [7493db6](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/7493db6d3fe3f6679549e6020498f72e97cd9b73)
12+
- anti conceal selected range in visual mode [#168](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/168)
13+
[5ff191f](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/5ff191f0c7457ede2fd30ecf76ab16c65118b4ee)
14+
- disable rendering in diff mode [#169](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/169)
15+
[01b38dc](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/01b38dcf7d0a99620547651fb59a3ba521ba12d5)
1216

1317
### Bug Fixes
1418

1519
- indent with block widths [044f2d6](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/044f2d6d76712de69a79b25a7cd8311cb505a9f4)
20+
- nil buffer state [#171](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/171)
21+
[#172](https://github.com/MeanderingProgrammer/render-markdown.nvim/pull/172)
22+
23+
### Collaborator Shoutouts
24+
25+
- @xudyang1
1626

1727
## 7.0.0 (2024-09-13)
1828

Diff for: doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 17
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 18
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

Diff for: lua/render-markdown/config.lua

+17
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,21 @@ function Config:render(mode)
4040
end
4141
end
4242

43+
---@param mode string
44+
---@param row? integer
45+
---@return Range2?
46+
function Config:hidden(mode, row)
47+
-- Anti-conceal is not enabled -> hide nothing
48+
-- Row is not known means buffer is not active -> hide nothing
49+
if not self.anti_conceal.enabled or row == nil then
50+
return nil
51+
end
52+
if vim.tbl_contains({ 'v', 'V', '\22' }, mode) then
53+
local start = vim.fn.getpos('v')[2] - 1
54+
return { math.min(row, start), math.max(row, start) }
55+
else
56+
return { row - self.anti_conceal.above, row + self.anti_conceal.below }
57+
end
58+
end
59+
4360
return Config

Diff for: lua/render-markdown/core/extmark.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function Extmark:overlaps(row)
3030
return not (start_row > row or end_row <= row)
3131
end
3232

33-
---@param hide_range { [1]: integer, [2]: integer }?
34-
function Extmark:render(hide_range)
35-
if self:should_show(hide_range) then
33+
---@param hidden Range2?
34+
function Extmark:render(hidden)
35+
if self:should_show(hidden) then
3636
self:show()
3737
else
3838
self:hide()
@@ -62,15 +62,15 @@ function Extmark:hide()
6262
end
6363

6464
---@private
65-
---@param hide_range { [1]: integer, [2]: integer }?
65+
---@param hidden Range2?
6666
---@return boolean
67-
function Extmark:should_show(hide_range)
68-
if hide_range == nil or not self.mark.conceal then
67+
function Extmark:should_show(hidden)
68+
if hidden == nil or not self.mark.conceal then
6969
return true
7070
end
7171
-- Show mark if it is outside hidden range
7272
local row = self.mark.start_row
73-
return row < hide_range[1] or row > hide_range[2]
73+
return row < hidden[1] or row > hidden[2]
7474
end
7575

7676
return Extmark

Diff for: lua/render-markdown/core/ui.lua

+25-40
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,43 @@ local builtin_handlers = {
1212
latex = require('render-markdown.handler.latex'),
1313
}
1414

15-
---@type table<integer, render.md.BufferState>
16-
local cache = {}
15+
---@class render.md.cache.Ui
16+
---@field states table<integer, render.md.BufferState>
17+
local Cache = {
18+
states = {},
19+
}
20+
21+
---@param buf integer
22+
---@return render.md.BufferState
23+
function Cache.get(buf)
24+
local buffer_state = Cache.states[buf]
25+
if buffer_state == nil then
26+
buffer_state = BufferState.new(buf)
27+
Cache.states[buf] = buffer_state
28+
end
29+
return buffer_state
30+
end
1731

1832
---@class render.md.Ui
1933
local M = {}
2034

2135
M.namespace = vim.api.nvim_create_namespace('render-markdown.nvim')
2236

2337
function M.invalidate_cache()
24-
for buf, buffer_state in pairs(cache) do
38+
for buf, buffer_state in pairs(Cache.states) do
2539
M.clear(buf, buffer_state)
2640
end
27-
cache = {}
41+
Cache.states = {}
2842
end
2943

3044
---@param buf integer
3145
---@return integer, render.md.Mark[]
3246
function M.get_row_marks(buf)
33-
local buffer_state, row = cache[buf], util.cursor_row(buf)
34-
if buffer_state == nil or row == nil then
47+
local row = util.cursor_row(buf)
48+
if row == nil then
3549
return 0, {}
3650
end
37-
local marks = {}
51+
local marks, buffer_state = {}, Cache.get(buf)
3852
for _, extmark in ipairs(buffer_state.marks or {}) do
3953
if extmark:overlaps(row) then
4054
table.insert(marks, extmark.mark)
@@ -61,9 +75,7 @@ function M.debounce_update(buf, win, event, change)
6175
return
6276
end
6377

64-
local config = state.get_config(buf)
65-
local buffer_state = cache[buf] or BufferState.new(buf)
66-
cache[buf] = buffer_state
78+
local config, buffer_state = state.get(buf), Cache.get(buf)
6779

6880
if not change and Context.contains_range(buf, win) then
6981
vim.schedule(function()
@@ -85,12 +97,7 @@ function M.update(buf, win, parse)
8597
return
8698
end
8799

88-
local config = state.get_config(buf)
89-
local buffer_state = cache[buf]
90-
if not buffer_state then
91-
buffer_state = BufferState.new(buf)
92-
cache[buf] = buffer_state
93-
end
100+
local config, buffer_state = state.get(buf), Cache.get(buf)
94101
local mode = vim.fn.mode(true)
95102

96103
local next_state = M.next_state(config, win, mode)
@@ -107,9 +114,9 @@ function M.update(buf, win, parse)
107114
buffer_state.marks = M.parse_buffer(buf, win)
108115
end
109116
local row = util.cursor_row(buf, win)
110-
local hide_range = M.hide_range(config.anti_conceal, mode, row)
117+
local hidden = config:hidden(mode, row)
111118
for _, mark in ipairs(buffer_state.marks) do
112-
mark:render(hide_range)
119+
mark:render(hidden)
113120
end
114121
else
115122
M.clear(buf, buffer_state)
@@ -137,28 +144,6 @@ function M.next_state(config, win, mode)
137144
return 'rendered'
138145
end
139146

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

Diff for: lua/render-markdown/handler/markdown.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Handler.__index = Handler
1717
function Handler.new(buf)
1818
local self = setmetatable({}, Handler)
1919
self.marks = list.new_marks()
20-
self.config = state.get_config(buf)
20+
self.config = state.get(buf)
2121
self.context = Context.get(buf)
2222
self.renderers = {
2323
code = require('render-markdown.render.code'),

Diff for: lua/render-markdown/handler/markdown_inline.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Handler.__index = Handler
1616
function Handler.new(buf)
1717
local self = setmetatable({}, Handler)
1818
self.marks = list.new_marks()
19-
self.config = state.get_config(buf)
19+
self.config = state.get(buf)
2020
self.context = Context.get(buf)
2121
return self
2222
end

Diff for: lua/render-markdown/health.lua

+1-1
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.9'
7+
M.version = '7.0.10'
88

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

Diff for: lua/render-markdown/manager.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function M.attach(buf)
5656
if not M.should_attach(buf) then
5757
return
5858
end
59-
local config = state.get_config(buf)
59+
local config = state.get(buf)
6060
local events = { 'BufWinEnter', 'BufLeave', 'CursorHold', 'CursorMoved' }
6161
local change_events = { 'DiffUpdated', 'ModeChanged', 'TextChanged' }
6262
if config:render('i') then
@@ -95,7 +95,7 @@ function M.should_attach(buf)
9595
return false
9696
end
9797

98-
local config = state.get_config(buf)
98+
local config = state.get(buf)
9999
if not config.enabled then
100100
log.debug_buf(log_name, buf, 'skip', 'state disabled')
101101
return false

Diff for: lua/render-markdown/state.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868

6969
---@param buf integer
7070
---@return render.md.buffer.Config
71-
function M.get_config(buf)
71+
function M.get(buf)
7272
local config = configs[buf]
7373
if config == nil then
7474
local buf_config = M.default_buffer_config()

Diff for: tests/state_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ describe('state', function()
2323
it('valid', function()
2424
eq(0, #validate())
2525
vim.bo.buftype = ''
26-
eq(true, state.get_config(0).sign.enabled)
26+
eq(true, state.get(0).sign.enabled)
2727
state.invalidate_cache()
2828
vim.bo.buftype = 'nofile'
29-
eq(false, state.get_config(0).sign.enabled)
29+
eq(false, state.get(0).sign.enabled)
3030

3131
eq(0, #validate({ callout = { note = { raw = 'value' } } }))
3232

0 commit comments

Comments
 (0)