Skip to content

Commit abc02f3

Browse files
feat: allow anti-conceal margin to be specified and modified with user commands
## Details Adds anti_conceal.above and anti_conceal.below which function as a margin to disable marks added by this plugin when moving cursor. Useful to see more context, more useful in certain files than others. Added 2 new subcommands to RenderMarkdown, expand & contract. These increment and decrement the margin values making sure to never go below 0.
1 parent 166a254 commit abc02f3

File tree

9 files changed

+68
-17
lines changed

9 files changed

+68
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Pre-release
44

5+
### Features
6+
7+
- handle imperfectly spaced tables using max width [166a254](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/166a254aaf5b4333fe015a29a66ad99c276538ea)
8+
59
## 6.1.0 (2024-08-11)
610

711
### Features

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Plugin to improve viewing Markdown files in Neovim
2828
2929
<!-- panvimdoc-ignore-start -->
3030
31-
| | |
32-
| -------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
33-
| ![Heading Code](https://github.com/user-attachments/assets/663a34c5-0438-4688-8204-332065f65835) | ![List Table](https://github.com/user-attachments/assets/162986e1-91f0-4e13-a83f-6183d58b0fcb) |
34-
| ![Box Dash Quote](https://github.com/user-attachments/assets/3f76e73e-b3a0-4cd8-90c1-208c5070659c) | ![LaTeX](https://github.com/user-attachments/assets/9e8909e4-7256-45fc-b481-4aba8850ebc3) |
35-
| ![Callout](https://github.com/user-attachments/assets/4324ea72-a017-4175-9f9d-363da5e5f6ba) | |
31+
| | |
32+
| --------- | ------- |
33+
| ![Heading](https://github.com/user-attachments/assets/663a34c5-0438-4688-8204-332065f65835) | ![Table](https://github.com/user-attachments/assets/162986e1-91f0-4e13-a83f-6183d58b0fcb) |
34+
| ![Quote](https://github.com/user-attachments/assets/3f76e73e-b3a0-4cd8-90c1-208c5070659c) | ![LaTeX](https://github.com/user-attachments/assets/9e8909e4-7256-45fc-b481-4aba8850ebc3) |
35+
| ![Callout](https://github.com/user-attachments/assets/4324ea72-a017-4175-9f9d-363da5e5f6ba) | |
3636
3737
<!-- panvimdoc-ignore-end -->
3838
@@ -127,6 +127,10 @@ use({
127127
- Can also be accessed directly through `require('render-markdown').disable()`
128128
- `:RenderMarkdown toggle` - Switch between enabling & disabling this plugin
129129
- Can also be accessed directly through `require('render-markdown').toggle()`
130+
- `:RenderMarkdown expand` - Increase anti-conceal margin above and below by 1
131+
- Can also be accessed directly through `require('render-markdown').expand()`
132+
- `:RenderMarkdown contract` - Decrease anti-conceal margin above and below by 1
133+
- Can also be accessed directly through `require('render-markdown').contract()`
130134

131135
# Setup
132136

@@ -214,8 +218,11 @@ require('render-markdown').setup({
214218
acknowledge_conflicts = false,
215219
anti_conceal = {
216220
-- This enables hiding any added text on the line the cursor is on
217-
-- This does have a performance penalty as we must listen to the 'CursorMoved' event
218221
enabled = true,
222+
-- Number of lines above cursor to show
223+
above = 0,
224+
-- Number of lines below cursor to show
225+
below = 0,
219226
},
220227
latex = {
221228
-- Whether LaTeX should be rendered, mainly used for health check

doc/render-markdown.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ PACKER.NVIM *render-markdown-install-packer.nvim*
154154
- Can also be accessed directly through `require('render-markdown').disable()`
155155
- `:RenderMarkdown toggle` - Switch between enabling & disabling this plugin
156156
- Can also be accessed directly through `require('render-markdown').toggle()`
157+
- `:RenderMarkdown expand` - Increase anti-conceal margin above and below by 1
158+
- Can also be accessed directly through `require('render-markdown').expand()`
159+
- `:RenderMarkdown contract` - Decrease anti-conceal margin above and below by 1
160+
- Can also be accessed directly through `require('render-markdown').contract()`
157161

158162

159163
==============================================================================
@@ -243,8 +247,11 @@ Full Default Configuration ~
243247
acknowledge_conflicts = false,
244248
anti_conceal = {
245249
-- This enables hiding any added text on the line the cursor is on
246-
-- This does have a performance penalty as we must listen to the 'CursorMoved' event
247250
enabled = true,
251+
-- Number of lines above cursor to show
252+
above = 0,
253+
-- Number of lines below cursor to show
254+
below = 0,
248255
},
249256
latex = {
250257
-- Whether LaTeX should be rendered, mainly used for health check

lua/render-markdown/api.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ function M.toggle()
1616
manager.set_all(not state.enabled)
1717
end
1818

19+
function M.expand()
20+
state.modify_anti_conceal(1)
21+
M.enable()
22+
end
23+
24+
function M.contract()
25+
state.modify_anti_conceal(-1)
26+
M.enable()
27+
end
28+
1929
return M

lua/render-markdown/extmark.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
---@param config render.md.BufferConfig
2323
---@param row? integer
2424
function Extmark:render(config, row)
25-
if self:should_show(config, row) then
25+
if self:should_show(config.anti_conceal, row) then
2626
self:show()
2727
else
2828
self:hide()
@@ -53,12 +53,12 @@ end
5353

5454
---Render marks based on anti-conceal behavior and current row
5555
---@private
56-
---@param config render.md.BufferConfig
56+
---@param anti_conceal render.md.AntiConceal
5757
---@param row? integer
5858
---@return boolean
59-
function Extmark:should_show(config, row)
59+
function Extmark:should_show(anti_conceal, row)
6060
-- Anti-conceal is not enabled -> all marks should be shown
61-
if not config.anti_conceal.enabled then
61+
if not anti_conceal.enabled then
6262
return true
6363
end
6464
-- Row is not known means buffer is not active -> all marks should be shown
@@ -69,8 +69,9 @@ function Extmark:should_show(config, row)
6969
if not self.mark.conceal then
7070
return true
7171
end
72-
-- Show mark if it is not on the current row
73-
return self.mark.start_row ~= row
72+
-- Show mark if it is outside current row range
73+
local mark_row = self.mark.start_row
74+
return mark_row < row - anti_conceal.above or mark_row > row + anti_conceal.below
7475
end
7576

7677
return Extmark

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.1.1'
8+
M.version = '6.1.2'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

lua/render-markdown/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ local M = {}
130130

131131
---@class (exact) render.md.UserAntiConceal
132132
---@field public enabled? boolean
133+
---@field public above? integer
134+
---@field public below? integer
133135

134136
---@class (exact) render.md.UserConfigOverrides
135137
---@field public buftype? table<string, render.md.UserBufferConfig>
@@ -239,8 +241,11 @@ M.default_config = {
239241
acknowledge_conflicts = false,
240242
anti_conceal = {
241243
-- This enables hiding any added text on the line the cursor is on
242-
-- This does have a performance penalty as we must listen to the 'CursorMoved' event
243244
enabled = true,
245+
-- Number of lines above cursor to show
246+
above = 0,
247+
-- Number of lines below cursor to show
248+
below = 0,
244249
},
245250
latex = {
246251
-- Whether LaTeX should be rendered, mainly used for health check

lua/render-markdown/state.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ function M.invalidate_cache()
4444
configs = {}
4545
end
4646

47+
---@param amount integer
48+
function M.modify_anti_conceal(amount)
49+
---@param anti_conceal render.md.AntiConceal
50+
local function modify(anti_conceal)
51+
anti_conceal.above = math.max(anti_conceal.above + amount, 0)
52+
anti_conceal.below = math.max(anti_conceal.below + amount, 0)
53+
end
54+
modify(M.config.anti_conceal)
55+
for _, config in pairs(configs) do
56+
modify(config.anti_conceal)
57+
end
58+
end
59+
4760
---@param buf integer
4861
---@return render.md.BufferConfig
4962
M.get_config = function(buf)
@@ -64,7 +77,7 @@ end
6477
function M.default_buffer_config()
6578
local config = M.config
6679
---@type render.md.BufferConfig
67-
return {
80+
return vim.deepcopy({
6881
enabled = true,
6982
max_file_size = config.max_file_size,
7083
debounce = config.debounce,
@@ -81,7 +94,7 @@ function M.default_buffer_config()
8194
link = config.link,
8295
sign = config.sign,
8396
win_options = config.win_options,
84-
}
97+
}, true)
8598
end
8699

87100
---@return string[]
@@ -188,6 +201,8 @@ function M.validate()
188201
if anti_conceal ~= nil then
189202
append_errors(path .. '.anti_conceal', anti_conceal, {
190203
enabled = { anti_conceal.enabled, 'boolean', nilable },
204+
above = { anti_conceal.above, 'number', nilable },
205+
below = { anti_conceal.below, 'number', nilable },
191206
})
192207
end
193208

lua/render-markdown/types.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@
107107

108108
---@class (exact) render.md.AntiConceal
109109
---@field public enabled boolean
110+
---@field public above integer
111+
---@field public below integer
110112

111113
---@class (exact) render.md.ConfigOverrides
112114
---@field public buftype table<string, render.md.UserBufferConfig>

0 commit comments

Comments
 (0)