Skip to content

Commit 3c36a25

Browse files
feature! Add more user commands, breaks existing toggle command
## Details Rather than the single toggle command add explicit enable, disable, and toggle commands. So as not to pollute the command space nest these under a single top level 'RenderMarkdown' command which takes a subcommand argument. By default run enable if no arugments are passed in. To fix the breaking change modify any user commands: - 'RenderMarkdownToggle' -> 'RenderMarkdown toggle' Related issue: #72 This should enable lazy loading on the RenderMarkdown command. Unrelated change: modify how demos are recorded, slow them down.
1 parent 4ab8359 commit 3c36a25

16 files changed

+122
-61
lines changed

Diff for: README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ use({
8888

8989
# Commands
9090

91-
`:RenderMarkdownToggle` - Switch between enabling & disabling this plugin
92-
93-
- Function can also be accessed directly through `require('render-markdown').toggle()`
91+
- `:RenderMarkdown` | `:RenderMarkdown enable` - Enable this plugin
92+
- Can also be accessed directly through `require('render-markdown').enable()`
93+
- `:RenderMarkdown disable` - Disable this plugin
94+
- Can also be accessed directly through `require('render-markdown').disable()`
95+
- `:RenderMarkdown toggle` - Switch between enabling & disabling this plugin
96+
- Can also be accessed directly through `require('render-markdown').toggle()`
9497

9598
# Setup
9699

Diff for: demo/box_dash_quote.gif

4.19 KB
Loading

Diff for: demo/callout.gif

-8.16 KB
Loading

Diff for: demo/heading_code.gif

6.44 KB
Loading

Diff for: demo/latex.gif

6.64 KB
Loading

Diff for: demo/list_table.gif

-4.67 KB
Loading

Diff for: demo/record.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def main(cols: int, rows: int, file: str, cast: str, content: str) -> None:
1010
pyautogui.hotkey("`", "c")
1111
time.sleep(1.0)
1212

13-
# Get length of file so we can scroll down it
14-
num_lines = len(Path(file).read_text().splitlines())
13+
# Get lines so we know how to scroll down, account for starting on first line
14+
lines: list[str] = Path(file).read_text().splitlines()[1:]
1515

1616
# Start recording demo file
1717
# https://docs.asciinema.org/manual/cli/usage/
@@ -29,17 +29,18 @@ def main(cols: int, rows: int, file: str, cast: str, content: str) -> None:
2929
pyautogui.press("o")
3030
pyautogui.press("enter")
3131
pyautogui.write(content, interval=0.1)
32-
3332
# Enter normal mode
3433
pyautogui.press("esc")
3534
time.sleep(2.0)
3635

3736
insert_normal(1)
3837

39-
# Slowly scroll down
40-
for _ in range(num_lines):
38+
# Scroll down
39+
for line in lines:
4140
pyautogui.press("j")
42-
time.sleep(0.1)
41+
skip = (" ", "def", "if")
42+
duration = 0 if len(line) == 0 or line.startswith(skip) else 0.75
43+
time.sleep(duration)
4344

4445
insert_normal(1)
4546

@@ -53,11 +54,11 @@ def main(cols: int, rows: int, file: str, cast: str, content: str) -> None:
5354
pyautogui.press("enter")
5455

5556

56-
def insert_normal(seconds: float) -> None:
57+
def insert_normal(duration: float) -> None:
5758
pyautogui.press("i")
58-
time.sleep(seconds)
59+
time.sleep(duration)
5960
pyautogui.press("esc")
60-
time.sleep(seconds)
61+
time.sleep(duration)
6162

6263

6364
if __name__ == "__main__":

Diff for: doc/limitations.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66

77
`LaTeX` formula evaluations are placed above text rather than overlayed.
88

9+
# Resolved Limitations
10+
911
## Telescope Opening File
1012

13+
[FIX 4ab8359](https://github.com/MeanderingProgrammer/markdown.nvim/commit/4ab835985de62b46b6785ae160f5f709b77a0f92)
14+
15+
Should no longer be an issue on any version of neovim if up to date.
16+
1117
Since `telescope` performs several mode change operations to enable previewing and
1218
other nice things like setting `marks` when changing buffers there are scenarios
1319
where a `markdown` file will not render when it is initially opened through `telescope`.
@@ -23,8 +29,6 @@ Something about the way these are done causes the file to appear be opened in `i
2329
mode despite being in `normal` mode. Additionally there is no `ModeChanged` event
2430
that occurs after this to go back to `normal` mode.
2531

26-
# Resolved Limitations
27-
2832
## Text Boundaries
2933

3034
[FIX 5ce3566](https://github.com/MeanderingProgrammer/markdown.nvim/commit/5ce35662725b1024c6dddc8d0bc03befc5abc878)

Diff for: doc/render-markdown.txt

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

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -123,9 +123,12 @@ PACKER.NVIM *render-markdown-install-packer.nvim*
123123
==============================================================================
124124
5. Commands *render-markdown-commands*
125125

126-
`:RenderMarkdownToggle` - Switch between enabling & disabling this plugin
127-
128-
- Function can also be accessed directly through `require('render-markdown').toggle()`
126+
- `:RenderMarkdown` | `:RenderMarkdown enable` - Enable this plugin
127+
- Can also be accessed directly through `require('render-markdown').enable()`
128+
- `:RenderMarkdown disable` - Disable this plugin
129+
- Can also be accessed directly through `require('render-markdown').disable()`
130+
- `:RenderMarkdown toggle` - Switch between enabling & disabling this plugin
131+
- Can also be accessed directly through `require('render-markdown').toggle()`
129132

130133

131134
==============================================================================

Diff for: justfile

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ test:
99
health:
1010
nvim -c "checkhealth render-markdown" -- .
1111

12-
demo-all: demo-heading demo-list demo-box demo-latex demo-callout
12+
demo: demo-heading demo-list demo-box demo-latex demo-callout
1313

1414
demo-heading:
15-
just demo "heading_code" "30" "## Heading 2"
15+
just demo-file "heading_code" "30" "## Heading 2"
1616

1717
demo-list:
18-
just demo "list_table" "30" ""
18+
just demo-file "list_table" "30" ""
1919

2020
demo-box:
21-
just demo "box_dash_quote" "15" ""
21+
just demo-file "box_dash_quote" "15" ""
2222

2323
demo-latex:
24-
just demo "latex" "15" ""
24+
just demo-file "latex" "15" ""
2525

2626
demo-callout:
27-
just demo "callout" "40" ""
27+
just demo-file "callout" "40" ""
2828

29-
demo file rows content:
29+
demo-file file rows content:
3030
rm -f demo/{{file}}.gif
3131
python demo/record.py \
3232
--cols "60" \

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ M.render_sign = function(buf, info, text, highlight)
414414
if not sign.enabled then
415415
return nil
416416
end
417-
if vim.tbl_contains(sign.exclude.buftypes, util.get_buftype(buf)) then
417+
if vim.tbl_contains(sign.exclude.buftypes, util.get_buf(buf, 'buftype')) then
418418
return nil
419419
end
420420
---@type render.md.Mark

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

+45-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local manager = require('render-markdown.manager')
22
local state = require('render-markdown.state')
33

4+
---@class render.md.Init
45
local M = {}
56

67
---@class render.md.Mark
@@ -405,16 +406,54 @@ function M.setup(opts)
405406
state.inline_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_query)
406407
state.inline_link_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_link_query)
407408
end)
409+
408410
manager.setup()
409-
vim.api.nvim_create_user_command(
410-
'RenderMarkdownToggle',
411-
M.toggle,
412-
{ desc = 'Switch between enabling & disabling render markdown plugin' }
413-
)
411+
412+
vim.api.nvim_create_user_command('RenderMarkdown', M.command, {
413+
nargs = '*',
414+
desc = 'markdown.nvim commands',
415+
complete = function(_, cmdline)
416+
if cmdline:find('RenderMarkdown%s+%S+%s+.*') then
417+
return {}
418+
elseif cmdline:find('RenderMarkdown%s+') then
419+
return { 'enable', 'disable', 'toggle' }
420+
else
421+
return {}
422+
end
423+
end,
424+
})
425+
end
426+
427+
---@param opts any
428+
M.command = function(opts)
429+
local args = opts.fargs
430+
if #args == 0 then
431+
M.enable()
432+
elseif #args == 1 then
433+
if args[1] == 'enable' then
434+
M.enable()
435+
elseif args[1] == 'disable' then
436+
M.disable()
437+
elseif args[1] == 'toggle' then
438+
M.toggle()
439+
else
440+
vim.notify('markdown.nvim: unexpected command: ' .. args[1], vim.log.levels.ERROR)
441+
end
442+
else
443+
vim.notify('markdown.nvim: unexpected # arguments: ' .. #args, vim.log.levels.ERROR)
444+
end
445+
end
446+
447+
M.enable = function()
448+
manager.set_all(true)
449+
end
450+
451+
M.disable = function()
452+
manager.set_all(false)
414453
end
415454

416455
M.toggle = function()
417-
manager.toggle()
456+
manager.set_all(not state.enabled)
418457
end
419458

420459
return M

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

+30-20
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ local data = {
1313
---@class render.md.Manager
1414
local M = {}
1515

16+
---@private
17+
---@type integer
18+
M.group = vim.api.nvim_create_augroup('RenderMarkdown', { clear = true })
19+
1620
function M.setup()
17-
local group = vim.api.nvim_create_augroup('RenderMarkdown', { clear = true })
1821
-- Attach to buffers based on matching filetype, this will add additional events
1922
vim.api.nvim_create_autocmd({ 'FileType' }, {
20-
group = group,
23+
group = M.group,
2124
pattern = state.config.file_types,
2225
callback = function(event)
23-
if not vim.tbl_contains(state.config.exclude.buftypes, util.get_buftype(event.buf)) then
24-
M.attach(group, event.buf)
25-
end
26+
M.attach(event.buf)
2627
end,
2728
})
2829
-- Window resizing is not buffer specific so is managed more globablly
2930
vim.api.nvim_create_autocmd({ 'WinResized' }, {
30-
group = group,
31+
group = M.group,
3132
callback = function()
3233
for _, win in ipairs(vim.v.event.windows) do
3334
local buf = util.win_to_buf(win)
@@ -39,22 +40,38 @@ function M.setup()
3940
})
4041
end
4142

43+
---@param enabled boolean
44+
M.set_all = function(enabled)
45+
-- Attempt to attach current buffer in case this is from a lazy load
46+
M.attach(vim.api.nvim_get_current_buf())
47+
state.enabled = enabled
48+
for _, buf in ipairs(data.buffers) do
49+
ui.schedule_refresh(buf, true)
50+
end
51+
end
52+
4253
---@private
43-
---@param group integer
4454
---@param buf integer
45-
M.attach = function(group, buf)
46-
if not vim.tbl_contains(data.buffers, buf) then
47-
table.insert(data.buffers, buf)
55+
M.attach = function(buf)
56+
if not vim.tbl_contains(state.config.file_types, util.get_buf(buf, 'filetype')) then
57+
return
4858
end
59+
if vim.tbl_contains(state.config.exclude.buftypes, util.get_buf(buf, 'buftype')) then
60+
return
61+
end
62+
if vim.tbl_contains(data.buffers, buf) then
63+
return
64+
end
65+
table.insert(data.buffers, buf)
4966
vim.api.nvim_create_autocmd({ 'BufWinEnter', 'TextChanged' }, {
50-
group = group,
67+
group = M.group,
5168
buffer = buf,
5269
callback = function()
5370
ui.schedule_refresh(buf, true)
5471
end,
5572
})
5673
vim.api.nvim_create_autocmd({ 'ModeChanged' }, {
57-
group = group,
74+
group = M.group,
5875
buffer = buf,
5976
callback = function()
6077
local render_modes = state.config.render_modes
@@ -69,7 +86,7 @@ M.attach = function(group, buf)
6986
})
7087
if state.config.anti_conceal.enabled then
7188
vim.api.nvim_create_autocmd({ 'CursorMoved' }, {
72-
group = group,
89+
group = M.group,
7390
buffer = buf,
7491
callback = function()
7592
-- Moving cursor should not result in text change, skip parsing
@@ -79,11 +96,4 @@ M.attach = function(group, buf)
7996
end
8097
end
8198

82-
M.toggle = function()
83-
state.enabled = not state.enabled
84-
for _, buf in ipairs(data.buffers) do
85-
ui.schedule_refresh(buf, true)
86-
end
87-
end
88-
8999
return M

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ end
9999
---@param info render.md.NodeInfo
100100
---@return integer
101101
M.concealed = function(buf, info)
102-
if util.get_win_option(util.buf_to_win(buf), 'conceallevel') == 0 then
102+
if util.get_win(util.buf_to_win(buf), 'conceallevel') == 0 then
103103
return 0
104104
end
105105
local result = 0

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ M.refresh = function(buf, mode, parse)
5454
if not M.should_render(buf, win, mode) then
5555
-- Set window options back to default
5656
for name, value in pairs(state.config.win_options) do
57-
util.set_win_option(win, name, value.default)
57+
util.set_win(win, name, value.default)
5858
end
5959
return
6060
end
6161

6262
-- Set window options to rendered & perform render
6363
for name, value in pairs(state.config.win_options) do
64-
util.set_win_option(win, name, value.rendered)
64+
util.set_win(win, name, value.rendered)
6565
end
6666

6767
-- Re-compute marks, needed if missing or between text changes

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,24 @@ end
2727
---@param win integer
2828
---@param name string
2929
---@return number|string
30-
M.get_win_option = function(win, name)
30+
M.get_win = function(win, name)
3131
return vim.api.nvim_get_option_value(name, { scope = 'local', win = win })
3232
end
3333

3434
---@param win integer
3535
---@param name string
3636
---@param value number|string
37-
M.set_win_option = function(win, name, value)
38-
local before = M.get_win_option(win, name)
37+
M.set_win = function(win, name, value)
38+
local before = M.get_win(win, name)
3939
vim.api.nvim_set_option_value(name, value, { scope = 'local', win = win })
4040
logger.debug({ option = name, win = win, before = before, after = value })
4141
end
4242

4343
---@param buf integer
44-
---@return any
45-
M.get_buftype = function(buf)
46-
return vim.api.nvim_get_option_value('buftype', { buf = buf })
44+
---@param name string
45+
---@return number|string
46+
M.get_buf = function(buf, name)
47+
return vim.api.nvim_get_option_value(name, { buf = buf })
4748
end
4849

4950
---@param win integer

0 commit comments

Comments
 (0)