Skip to content

Commit d3d9ce4

Browse files
committed
Fix preview in create_previewable_command
1 parent 51e2f0a commit d3d9ce4

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
local M = {}
22

3-
local user_command = require("live-command.user_command")
4-
53
---@class livecmd.Config.HlGroups
64
---@field insertion string|false
75
---@field deletion string|false
@@ -12,6 +10,7 @@ local user_command = require("live-command.user_command")
1210
---@field enable_highlighting boolean?
1311
---@field inline_highlighting boolean?
1412
---@field hl_groups livecmd.Config.HlGroups?
13+
---@field commands table<string, livecmd.CommandSpec>
1514

1615
---@param config livecmd.Config
1716
M.validate_config = function(config)
@@ -20,10 +19,8 @@ M.validate_config = function(config)
2019
enable_highlighting = { config.enable_highlighting, "boolean" },
2120
inline_highlighting = { config.inline_highlighting, "boolean" },
2221
hl_groups = { config.hl_groups, "table" },
22+
commands = { config.commands, "table" },
2323
}
24-
user_command.register_argument_handler("help", function()
25-
print("Help")
26-
end)
2724
end
2825

2926
return M

lua/live-command/init.lua

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ M.default_config = {
1010
deletion = "DiffDelete",
1111
change = "DiffChange",
1212
},
13+
commands = {},
1314
}
1415

1516
local cmd_executor
@@ -72,6 +73,14 @@ local preview_callback = function(cmd, preview_ns, preview_buf)
7273
return 2
7374
end
7475

76+
local get_range_string = function(cmd)
77+
return (
78+
cmd.range == 2 and ("%s,%s"):format(cmd.line1, cmd.line2)
79+
or cmd.range == 1 and tostring(cmd.line1)
80+
or ""
81+
)
82+
end
83+
7584
M._test_mode = false
7685

7786
---@param preview_cmd_name string
@@ -87,29 +96,31 @@ M.create_preview_command = function(preview_cmd_name)
8796
})
8897
end
8998

90-
---@class livecmd.CommandOpts
99+
---@class livecmd.CommandSpec
91100
---@field cmd string
92101

93102
---@param cmd_name string
94-
---@param cmd_opts livecmd.CommandOpts
95-
M.create_previewable_command = function(cmd_name, cmd_opts)
103+
---@param cmd_specs livecmd.CommandSpec
104+
M.create_previewable_command = function(cmd_name, cmd_specs)
96105
api.nvim_create_user_command(cmd_name, function(cmd)
97-
local range_string = (
98-
cmd.range == 2 and ("%s,%s"):format(cmd.line1, cmd.line2)
99-
or cmd.range == 1 and tostring(cmd.line1)
100-
or ""
101-
)
102-
vim.cmd(range_string .. cmd_opts.cmd .. " " .. cmd.args)
106+
vim.cmd(get_range_string(cmd) .. cmd_specs.cmd .. " " .. cmd.args)
103107
end, {
104108
nargs = "*",
105109
range = true,
106110
preview = function(cmd, preview_ns, preview_buf)
107-
cmd.name = cmd_opts.cmd
108-
return preview_callback(cmd, preview_ns, preview_buf)
111+
local cmd_to_preview = get_range_string(cmd) .. cmd_specs.cmd .. " " .. cmd.args
112+
return preview_callback(cmd_to_preview, preview_ns, preview_buf)
109113
end,
110114
})
111115
end
112116

117+
---@param cmd_specs table<string, livecmd.CommandSpec>
118+
local create_previewable_commands = function(cmd_specs)
119+
for cmd_name, cmd_spec in pairs(cmd_specs) do
120+
M.create_previewable_command(cmd_name, cmd_spec)
121+
end
122+
end
123+
113124
local create_autocmds = function()
114125
local id = api.nvim_create_augroup("command_preview.nvim", { clear = true })
115126
-- We need to be able to tell when the command was cancelled so the buffer lines are refetched next time.
@@ -126,7 +137,7 @@ end
126137
M.setup = function(user_config)
127138
if vim.fn.has("nvim-0.8.0") ~= 1 then
128139
vim.notify(
129-
"[live-command] This plugin requires at least Neovim 0.8. Please upgrade to a more recent vers1ion of Neovim.",
140+
"[live-command] This plugin requires at least Neovim 0.8. Please upgrade to a more recent version of Neovim.",
130141
vim.log.levels.ERROR
131142
)
132143
return
@@ -135,6 +146,7 @@ M.setup = function(user_config)
135146
merged_config = vim.tbl_deep_extend("force", M.default_config, user_config or {})
136147
require("live-command.config_validator").validate_config(merged_config)
137148
M.create_preview_command(merged_config.command_name)
149+
create_previewable_commands(merged_config.commands)
138150
create_autocmds()
139151
end
140152

tests/e2e_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ describe("create_preview_command works for", function()
4343
vim.cmd("G/Second/d")
4444
assert.are_same({ "First line" }, get_lines())
4545
end)
46+
47+
it("command spec in config", function()
48+
live_command.setup {
49+
commands = {
50+
ABC = { cmd = "norm" },
51+
},
52+
}
53+
vim.cmd("ABC daw")
54+
assert.are_same({ "line", "Second line" }, get_lines())
55+
end)
4656
end)
4757

4858
describe(":Preview works for", function()

0 commit comments

Comments
 (0)