Skip to content

Commit accaa60

Browse files
feat: add get, set, and set_buf APIs
## Details Adds 3 additional commands to API: - get: returns whether plugin is enabled or not as a boolean - set: changes whether the plugin is enabled or not, takes a boolean as input which is used directly, if not provided value is toggled - set_buf: same as set but only runs for the current buffer Most of the existing APIs now simply call these under the hood: - enable -> set(true) - disable -> set(false) - toggle -> set() - buf_enable -> set_buf(true) - buf_disable -> set_buf(false) - buf_toggle -> set_buf() Main idea behind this change is to move LazyVim from relying on the `state` module, I'll expose the needed information (get & set) through the public API which will let me refactor as needed. This does make our command line argument parsing substantially more complicated. Previously no additional arguments were passed in to the command being run. Now 2 commands can accept a boolean as input. Sounds simple but required a good amount of reworking, oh well.
1 parent c301209 commit accaa60

File tree

7 files changed

+139
-80
lines changed

7 files changed

+139
-80
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- better support for reloading with lazy.nvim [24aacee](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/24aacee83544ca113055564ed22be7852067c342)
1212
- change concealcursor value to nvic when user disables anti conceal [#463](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/463)
1313
[c809fc1](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/c809fc129f842a7055c672593d24be6346bcc673)
14+
- support quarto executable code block syntax [c301209](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/c3012098bd44381e3b96bbbbbcc21a54d45a286c)
1415

1516
### Bug Fixes
1617

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,23 @@ use({
103103

104104
# Commands
105105

106-
| Command | Lua Function | Description |
107-
| ----------------------------- | ------------------------------------------ | ------------------------------------------------- |
108-
| `:RenderMarkdown` | `require('render-markdown').enable()` | Enable this plugin |
109-
| `:RenderMarkdown enable` | `require('render-markdown').enable()` | Enable this plugin |
110-
| `:RenderMarkdown buf_enable` | `require('render-markdown').buf_enable()` | Enable this plugin for current buffer |
111-
| `:RenderMarkdown disable` | `require('render-markdown').disable()` | Disable this plugin |
112-
| `:RenderMarkdown buf_disable` | `require('render-markdown').buf_disable()` | Disable this plugin for current buffer |
113-
| `:RenderMarkdown toggle` | `require('render-markdown').toggle()` | Toggle state of this plugin |
114-
| `:RenderMarkdown buf_toggle` | `require('render-markdown').buf_toggle()` | Toggle state of this plugin for current buffer |
115-
| `:RenderMarkdown log` | `require('render-markdown').log()` | Opens the log file for this plugin |
116-
| `:RenderMarkdown expand` | `require('render-markdown').expand()` | Increase anti-conceal margin above and below by 1 |
117-
| `:RenderMarkdown contract` | `require('render-markdown').contract()` | Decrease anti-conceal margin above and below by 1 |
118-
| `:RenderMarkdown debug` | `require('render-markdown').debug()` | Prints information about marks on current line |
119-
| `:RenderMarkdown config` | `require('render-markdown').config()` | Prints difference between config and default |
106+
| Command | Lua Function | Description |
107+
| ------------------------------- | ------------------------------------------- | ------------------------------------------------- |
108+
| `:RenderMarkdown` | `require('render-markdown').enable()` | Alias for `enable` |
109+
| `:RenderMarkdown enable` | `require('render-markdown').enable()` | Alias for `set(true)` |
110+
| `:RenderMarkdown buf_enable` | `require('render-markdown').buf_enable()` | Alias for `set_buf(true)` |
111+
| `:RenderMarkdown disable` | `require('render-markdown').disable()` | Alias for `set(false)` |
112+
| `:RenderMarkdown buf_disable` | `require('render-markdown').buf_disable()` | Alias for `set_buf(false)` |
113+
| `:RenderMarkdown toggle` | `require('render-markdown').toggle()` | Alias for `set()` |
114+
| `:RenderMarkdown buf_toggle` | `require('render-markdown').buf_toggle()` | Alias for `set_buf()` |
115+
| `:RenderMarkdown get` | `require('render-markdown').get()` | Return current state |
116+
| `:RenderMarkdown set bool?` | `require('render-markdown').set(bool?)` | Sets state, `nil` to toggle |
117+
| `:RenderMarkdown set_buf bool?` | `require('render-markdown').set_buf(bool?)` | Sets state for current buffer, `nil` to toggle |
118+
| `:RenderMarkdown log` | `require('render-markdown').log()` | Opens the log file for this plugin |
119+
| `:RenderMarkdown expand` | `require('render-markdown').expand()` | Increase anti-conceal margin above and below by 1 |
120+
| `:RenderMarkdown contract` | `require('render-markdown').contract()` | Decrease anti-conceal margin above and below by 1 |
121+
| `:RenderMarkdown debug` | `require('render-markdown').debug()` | Prints information about marks on current line |
122+
| `:RenderMarkdown config` | `require('render-markdown').config()` | Prints difference between config and default |
120123

121124
# Completions
122125

doc/render-markdown.txt

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.2 Last change: 2025 July 08
1+
*render-markdown.txt* For NVIM v0.11.2 Last change: 2025 July 09
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -139,41 +139,45 @@ PACKER.NVIM *render-markdown-install-packer.nvim*
139139
==============================================================================
140140
5. Commands *render-markdown-commands*
141141

142-
-----------------------------------------------------------------------------------------------------
143-
Command Lua Function Description
144-
----------------------------- ------------------------------------------ ----------------------------
145-
:RenderMarkdown require('render-markdown').enable() Enable this plugin
142+
--------------------------------------------------------------------------------------------------------
143+
Command Lua Function Description
144+
------------------------------- ------------------------------------------- ----------------------------
145+
:RenderMarkdown require('render-markdown').enable() Alias for enable
146146

147-
:RenderMarkdown enable require('render-markdown').enable() Enable this plugin
147+
:RenderMarkdown enable require('render-markdown').enable() Alias for set(true)
148148

149-
:RenderMarkdown buf_enable require('render-markdown').buf_enable() Enable this plugin for
150-
current buffer
149+
:RenderMarkdown buf_enable require('render-markdown').buf_enable() Alias for set_buf(true)
151150

152-
:RenderMarkdown disable require('render-markdown').disable() Disable this plugin
151+
:RenderMarkdown disable require('render-markdown').disable() Alias for set(false)
153152

154-
:RenderMarkdown buf_disable require('render-markdown').buf_disable() Disable this plugin for
155-
current buffer
153+
:RenderMarkdown buf_disable require('render-markdown').buf_disable() Alias for set_buf(false)
156154

157-
:RenderMarkdown toggle require('render-markdown').toggle() Toggle state of this plugin
155+
:RenderMarkdown toggle require('render-markdown').toggle() Alias for set()
158156

159-
:RenderMarkdown buf_toggle require('render-markdown').buf_toggle() Toggle state of this plugin
160-
for current buffer
157+
:RenderMarkdown buf_toggle require('render-markdown').buf_toggle() Alias for set_buf()
161158

162-
:RenderMarkdown log require('render-markdown').log() Opens the log file for this
163-
plugin
159+
:RenderMarkdown get require('render-markdown').get() Return current state
164160

165-
:RenderMarkdown expand require('render-markdown').expand() Increase anti-conceal margin
166-
above and below by 1
161+
:RenderMarkdown set bool? require('render-markdown').set(bool?) Sets state, nil to toggle
167162

168-
:RenderMarkdown contract require('render-markdown').contract() Decrease anti-conceal margin
169-
above and below by 1
163+
:RenderMarkdown set_buf bool? require('render-markdown').set_buf(bool?) Sets state for current
164+
buffer, nil to toggle
170165

171-
:RenderMarkdown debug require('render-markdown').debug() Prints information about
172-
marks on current line
166+
:RenderMarkdown log require('render-markdown').log() Opens the log file for this
167+
plugin
173168

174-
:RenderMarkdown config require('render-markdown').config() Prints difference between
175-
config and default
176-
-----------------------------------------------------------------------------------------------------
169+
:RenderMarkdown expand require('render-markdown').expand() Increase anti-conceal margin
170+
above and below by 1
171+
172+
:RenderMarkdown contract require('render-markdown').contract() Decrease anti-conceal margin
173+
above and below by 1
174+
175+
:RenderMarkdown debug require('render-markdown').debug() Prints information about
176+
marks on current line
177+
178+
:RenderMarkdown config require('render-markdown').config() Prints difference between
179+
config and default
180+
--------------------------------------------------------------------------------------------------------
177181

178182
==============================================================================
179183
6. Completions *render-markdown-completions*

lua/render-markdown/api.lua

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
---@class render.md.Api
22
local M = {}
33

4+
---@return boolean
5+
function M.get()
6+
return require('render-markdown.state').enabled
7+
end
8+
9+
---@param enable? boolean
10+
function M.set(enable)
11+
require('render-markdown.core.manager').set(enable)
12+
end
13+
14+
---@param enable? boolean
15+
function M.set_buf(enable)
16+
require('render-markdown.core.manager').set_buf(nil, enable)
17+
end
18+
419
function M.enable()
5-
require('render-markdown.core.manager').set_all(true)
20+
M.set(true)
621
end
722

823
function M.buf_enable()
9-
require('render-markdown.core.manager').set_buf(nil, true)
24+
M.set_buf(true)
1025
end
1126

1227
function M.disable()
13-
require('render-markdown.core.manager').set_all(false)
28+
M.set(false)
1429
end
1530

1631
function M.buf_disable()
17-
require('render-markdown.core.manager').set_buf(nil, false)
32+
M.set_buf(false)
1833
end
1934

2035
function M.toggle()
21-
require('render-markdown.core.manager').set_all(nil)
36+
M.set()
2237
end
2338

2439
function M.buf_toggle()
25-
require('render-markdown.core.manager').set_buf(nil, nil)
40+
M.set_buf()
2641
end
2742

2843
function M.log()

lua/render-markdown/core/command.lua

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,36 @@ M.name = 'RenderMarkdown'
99
---@private
1010
M.plugin = 'render-markdown.nvim'
1111

12+
---@private
13+
---@type table<string, type?>
14+
M.args = {
15+
set = 'boolean',
16+
set_buf = 'boolean',
17+
}
18+
1219
---called from plugin directory
1320
function M.init()
1421
vim.api.nvim_create_user_command(M.name, M.command, {
1522
nargs = '*',
1623
desc = M.plugin .. ' commands',
17-
complete = function(prefix, cmdline)
18-
if cmdline:find(M.name .. '%s+%S+%s+.*') then
19-
return {}
20-
elseif cmdline:find(M.name .. '%s+') then
21-
return M.matches(prefix, vim.tbl_keys(api))
22-
else
23-
return {}
24+
complete = function(_, cmdline, col)
25+
local line = cmdline:sub(1, col):match('^' .. M.name .. '%s+(.*)$')
26+
if line then
27+
local fargs = vim.split(line, '%s+')
28+
if #fargs == 1 then
29+
return M.matches(fargs[1], vim.tbl_keys(api))
30+
elseif #fargs == 2 then
31+
local arg = M.args[fargs[1]]
32+
if arg == 'boolean' then
33+
return M.matches(fargs[2], { 'true', 'false' })
34+
end
35+
end
2436
end
37+
return {}
2538
end,
2639
})
2740
end
2841

29-
---@private
30-
---@param args vim.api.keyset.create_user_command.command_args
31-
function M.command(args)
32-
local fargs, err = args.fargs, nil
33-
if #fargs == 0 or #fargs == 1 then
34-
local command = #fargs == 0 and api.enable or api[fargs[1]]
35-
if command then
36-
command()
37-
else
38-
err = ('unexpected command: %s'):format(fargs[1])
39-
end
40-
else
41-
err = ('unexpected # arguments: %d'):format(#fargs)
42-
end
43-
if err then
44-
vim.notify(('%s: %s'):format(M.plugin, err), vim.log.levels.ERROR)
45-
end
46-
end
47-
4842
---@private
4943
---@param prefix string
5044
---@param values string[]
@@ -59,4 +53,46 @@ function M.matches(prefix, values)
5953
return result
6054
end
6155

56+
---@private
57+
---@param args vim.api.keyset.create_user_command.command_args
58+
function M.command(args)
59+
local err = M.run(args.fargs)
60+
if err then
61+
vim.notify(('%s: %s'):format(M.plugin, err), vim.log.levels.ERROR)
62+
end
63+
end
64+
65+
---@private
66+
---@param fargs string[]
67+
---@return string?
68+
function M.run(fargs)
69+
if #fargs > 2 then
70+
return ('invalid # arguments - %d'):format(#fargs)
71+
end
72+
local name = fargs[1] or 'enable'
73+
local command = api[name]
74+
if not command then
75+
return ('invalid command - %s'):format(name)
76+
end
77+
if #fargs == 2 then
78+
local arg = M.args[name]
79+
local value = fargs[2]
80+
if not arg then
81+
return ('no arguments allowed - %s'):format(name)
82+
elseif arg == 'boolean' then
83+
if value == 'true' then
84+
command(true)
85+
elseif value == 'false' then
86+
command(false)
87+
else
88+
return ('invalid argument - %s(%s)'):format(name, value)
89+
end
90+
else
91+
return ('bug unhandled type - %s'):format(arg)
92+
end
93+
else
94+
command()
95+
end
96+
end
97+
6298
return M

lua/render-markdown/core/manager.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ function M.attached(buf)
6060
return vim.tbl_contains(M.buffers, buf)
6161
end
6262

63-
---@param enabled? boolean
64-
function M.set_all(enabled)
63+
---@param enable? boolean
64+
function M.set(enable)
6565
-- lazy loading: all previously opened buffers have been ignored
6666
if #env.lazy('cmd') > 0 then
6767
M.attach(env.buf.current())
6868
end
69-
if enabled ~= nil then
70-
state.enabled = enabled
69+
if enable ~= nil then
70+
state.enabled = enable
7171
else
7272
state.enabled = not state.enabled
7373
end
@@ -77,13 +77,13 @@ function M.set_all(enabled)
7777
end
7878

7979
---@param buf? integer
80-
---@param enabled? boolean
81-
function M.set_buf(buf, enabled)
80+
---@param enable? boolean
81+
function M.set_buf(buf, enable)
8282
buf = buf or env.buf.current()
8383
if M.attached(buf) then
8484
local config = state.get(buf)
85-
if enabled ~= nil then
86-
config.enabled = enabled
85+
if enable ~= nil then
86+
config.enabled = enable
8787
else
8888
config.enabled = not config.enabled
8989
end

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.5.13'
8+
M.version = '8.5.14'
99

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

0 commit comments

Comments
 (0)