Skip to content

Commit 258da4b

Browse files
feat(options)! allow all window options to be configured, set concealcursor
# Details Make the `conceal` behavior more generic by wrapping it in a table of options. All options set a value to use when rendered and when not rendered. This plugin will handle switching between them. This is a breaking change for anyone currently setting a custom `conceal` value like this: ```lua require('render-markdown').setup({ conceal = { default = <value_1>, rendered = <value_2>, }, }) ``` Should instead now do: ```lua require('render-markdown').setup({ win_options = { conceallevel = { default = <value_1>, rendered = <value_2>, }, }, }) ``` Along with this change have added a default value for `concealcursor` to continue concealing text in all modes when rendered, otherwise use the user setting. This way text only changes on mode change rather than mode change + when your cursor enters a line, though not everyone prefers this, it is something that can be disabled in config. With concealed text now being more consistent change the list marker before checkmarks to be concealed rather than overlayed with spaces. This resolves: #31.
1 parent 473e48d commit 258da4b

File tree

11 files changed

+82
-52
lines changed

11 files changed

+82
-52
lines changed

Diff for: README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,22 @@ require('render-markdown').setup({
138138
warning = ' Warning',
139139
caution = '󰳦 Caution',
140140
},
141-
-- See :h 'conceallevel' for more information about meaning of values
142-
conceal = {
143-
-- conceallevel used for buffer when not being rendered, get user setting
144-
default = vim.opt.conceallevel:get(),
145-
-- conceallevel used for buffer when being rendered
146-
rendered = 3,
141+
-- Window options to use that change between rendered and raw view
142+
win_options = {
143+
-- See :h 'conceallevel'
144+
conceallevel = {
145+
-- Used when not being rendered, get user setting
146+
default = vim.api.nvim_get_option_value('conceallevel', {}),
147+
-- Used when being rendered, concealed text is completely hidden
148+
rendered = 3,
149+
},
150+
-- See :h 'concealcursor'
151+
concealcursor = {
152+
-- Used when not being rendered, get user setting
153+
default = vim.api.nvim_get_option_value('concealcursor', {}),
154+
-- Used when being rendered, conceal text in all modes
155+
rendered = 'nvic',
156+
},
147157
},
148158
-- Determines how tables are rendered
149159
-- full: adds a line above and below tables + normal behavior

Diff for: demo/demo.gif

69.6 KB
Loading

Diff for: doc/render-markdown.txt

+16-6
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,22 @@ modified by the user.
169169
warning = ' Warning',
170170
caution = '󰳦 Caution',
171171
},
172-
-- See :h 'conceallevel' for more information about meaning of values
173-
conceal = {
174-
-- conceallevel used for buffer when not being rendered, get user setting
175-
default = vim.opt.conceallevel:get(),
176-
-- conceallevel used for buffer when being rendered
177-
rendered = 3,
172+
-- Window options to use that change between rendered and raw view
173+
win_options = {
174+
-- See :h 'conceallevel'
175+
conceallevel = {
176+
-- Used when not being rendered, get user setting
177+
default = vim.api.nvim_get_option_value('conceallevel', {}),
178+
-- Used when being rendered, concealed text is completely hidden
179+
rendered = 3,
180+
},
181+
-- See :h 'concealcursor'
182+
concealcursor = {
183+
-- Used when not being rendered, get user setting
184+
default = vim.api.nvim_get_option_value('concealcursor', {}),
185+
-- Used when being rendered, conceal text in all modes
186+
rendered = 'nvic',
187+
},
178188
},
179189
-- Determines how tables are rendered
180190
-- full: adds a line above and below tables + normal behavior

Diff for: justfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
init := "tests/minimal.lua"
2-
default_zoom := "-2"
2+
default_zoom := "0"
33

44
test:
55
nvim --headless --noplugin -u {{init}} \

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,29 @@ M.render = function(namespace, root, buf)
5151
hl_eol = true,
5252
})
5353
elseif capture == 'list_marker' then
54-
local list_marker_overlay = ''
5554
if M.is_sibling_checkbox(node) then
5655
-- Hide the list marker for checkboxes rather than replacing with a bullet point
57-
list_marker_overlay = string.rep(' ', vim.fn.strdisplaywidth(value))
56+
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
57+
end_row = end_row,
58+
end_col = end_col,
59+
conceal = '',
60+
})
5861
else
5962
-- List markers from tree-sitter should have leading spaces removed, however there are known
6063
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
6164
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
6265
local _, leading_spaces = value:find('^%s*')
6366
local level = M.calculate_list_level(node)
6467
local bullet = list.cycle(state.config.bullets, level)
65-
list_marker_overlay = string.rep(' ', leading_spaces or 0) .. bullet
66-
end
6768

68-
local virt_text = { list_marker_overlay, highlights.bullet }
69-
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
70-
end_row = end_row,
71-
end_col = end_col,
72-
virt_text = { virt_text },
73-
virt_text_pos = 'overlay',
74-
})
69+
local virt_text = { string.rep(' ', leading_spaces or 0) .. bullet, highlights.bullet }
70+
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
71+
end_row = end_row,
72+
end_col = end_col,
73+
virt_text = { virt_text },
74+
virt_text_pos = 'overlay',
75+
})
76+
end
7577
elseif capture == 'quote_marker' then
7678
local virt_text = { value:gsub('>', state.config.quote), highlights.quote }
7779
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ local function check_keys(t1, t2, path)
4141
table.insert(errors, string.format('Invalid type: %s, expected %s but found %s', key, type(v1), type(v2)))
4242
elseif type(v1) == 'table' and type(v2) == 'table' then
4343
-- Some tables are meant to have unrestricted keys
44-
if k ~= 'custom_handlers' then
44+
if not vim.list_contains({ 'win_options', 'custom_handlers' }, k) then
4545
vim.list_extend(errors, check_keys(v1, v2, key_path))
4646
end
4747
end

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

+20-10
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ local M = {}
3737
---@class render.md.Handler
3838
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
3939

40-
---@class render.md.UserConceal
41-
---@field public default? integer
42-
---@field public rendered? integer
40+
---@class render.md.WindowOption
41+
---@field public default any
42+
---@field public rendered any
4343

4444
---@class render.md.UserCheckbox
4545
---@field public unchecked? string
@@ -59,7 +59,7 @@ local M = {}
5959
---@field public checkbox? render.md.UserCheckbox
6060
---@field public quote? string
6161
---@field public callout? render.md.UserCallout
62-
---@field public conceal? render.md.UserConceal
62+
---@field public win_options? table<string, render.md.WindowOption>
6363
---@field public table_style? 'full'|'normal'|'none'
6464
---@field public custom_handlers? table<string, render.md.Handler>
6565
---@field public highlights? render.md.UserHighlights
@@ -139,12 +139,22 @@ M.default_config = {
139139
warning = ' Warning',
140140
caution = '󰳦 Caution',
141141
},
142-
-- See :h 'conceallevel' for more information about meaning of values
143-
conceal = {
144-
-- conceallevel used for buffer when not being rendered, get user setting
145-
default = vim.opt.conceallevel:get(),
146-
-- conceallevel used for buffer when being rendered
147-
rendered = 3,
142+
-- Window options to use that change between rendered and raw view
143+
win_options = {
144+
-- See :h 'conceallevel'
145+
conceallevel = {
146+
-- Used when not being rendered, get user setting
147+
default = vim.api.nvim_get_option_value('conceallevel', {}),
148+
-- Used when being rendered, concealed text is completely hidden
149+
rendered = 3,
150+
},
151+
-- See :h 'concealcursor'
152+
concealcursor = {
153+
-- Used when not being rendered, get user setting
154+
default = vim.api.nvim_get_option_value('concealcursor', {}),
155+
-- Used when being rendered, conceal text in all modes
156+
rendered = 'nvic',
157+
},
148158
},
149159
-- Determines how tables are rendered
150160
-- full: adds a line above and below tables + normal behavior

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
---@field public quote string
2929
---@field public callout render.md.Callout
3030

31-
---@class render.md.Conceal
32-
---@field public default integer
33-
---@field public rendered integer
34-
3531
---@class render.md.Checkbox
3632
---@field public unchecked string
3733
---@field public checked string
@@ -50,7 +46,7 @@
5046
---@field public checkbox render.md.Checkbox
5147
---@field public quote string
5248
---@field public callout render.md.Callout
53-
---@field public conceal render.md.Conceal
49+
---@field public win_options table<string, render.md.WindowOption>
5450
---@field public table_style 'full'|'normal'|'none'
5551
---@field public custom_handlers table<string, render.md.Handler>
5652
---@field public highlights render.md.Highlights

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ M.refresh = function(buf)
2525
end
2626

2727
logger.start()
28-
util.set_conceal(buf, state.config.conceal.rendered)
29-
28+
for name, value in pairs(state.config.win_options) do
29+
util.set_win_option(buf, name, value.rendered)
30+
end
3031
-- Make sure injections are processed
3132
local parser = vim.treesitter.get_parser(buf)
3233
parser:parse(true)
@@ -67,7 +68,9 @@ M.clear_valid = function(buf)
6768
if not vim.api.nvim_win_is_valid(win) then
6869
return false
6970
end
70-
util.set_conceal(buf, state.config.conceal.default)
71+
for name, value in pairs(state.config.win_options) do
72+
util.set_win_option(buf, name, value.default)
73+
end
7174
local leftcol = vim.api.nvim_win_call(win, function()
7275
return vim.fn.winsaveview().leftcol
7376
end)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ M.buf_to_win = function(buf)
1515
end
1616

1717
---@param buf integer
18-
---@param value integer
19-
M.set_conceal = function(buf, value)
20-
local name = 'conceallevel'
18+
---@param name string
19+
---@param value any
20+
M.set_win_option = function(buf, name, value)
2121
local opts = { scope = 'local', win = M.buf_to_win(buf) }
2222
local before = vim.api.nvim_get_option_value(name, opts)
2323
vim.api.nvim_set_option_value(name, value, opts)

Diff for: tests/init_spec.lua

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async_tests.describe('init', function()
2222
col = { col, details.end_col },
2323
hl_eol = details.hl_eol,
2424
hl_group = details.hl_group,
25+
conceal = details.conceal,
2526
virt_text = details.virt_text,
2627
virt_text_pos = details.virt_text_pos,
2728
virt_lines = details.virt_lines,
@@ -229,12 +230,11 @@ async_tests.describe('init', function()
229230

230231
-- Checkboxes
231232
vim.list_extend(expected, {
232-
-- Unchecked, list marker
233+
-- Unchecked, conceal list marker
233234
{
234235
row = { 41, 41 },
235236
col = { 0, 2 },
236-
virt_text = { { ' ', 'Normal' } },
237-
virt_text_pos = 'overlay',
237+
conceal = '',
238238
},
239239
-- Unchecked, checkbox
240240
{
@@ -243,12 +243,11 @@ async_tests.describe('init', function()
243243
virt_text = { { ' 󰄱 ', '@markup.list.unchecked' } },
244244
virt_text_pos = 'overlay',
245245
},
246-
-- Checked, list marker
246+
-- Checked, conceal list marker
247247
{
248248
row = { 42, 42 },
249249
col = { 0, 2 },
250-
virt_text = { { ' ', 'Normal' } },
251-
virt_text_pos = 'overlay',
250+
conceal = '',
252251
},
253252
-- Checked, checkbox
254253
{

0 commit comments

Comments
 (0)