Skip to content

Commit cc188b6

Browse files
committed
feat: show warning if unsupported features are detected
1 parent dc2850b commit cc188b6

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

README.md

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,7 @@ require("live-command").setup {
7070
```
7171

7272
Each command you want to preview requires a name (must be upper-case) and the name of
73-
an existing command that is run on each keypress.
74-
75-
Here is a list of available settings:
76-
77-
| Key | Type | Description
78-
| ----------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------
79-
| cmd | string | The name of an existing command to preview.
80-
| args | string? \| function(arg: string?, opts: table) -> string | Arguments passed to the command. If a function, takes in the options passed to the command and must return the transformed argument(s) `cmd` will be called with. `opts` has the same structure as the `opts` table passed to the `nvim_create_user_command` callback function. If `nil`, the arguments are supplied from the command-line while the user is typing the command.
81-
| range | string? | The range to prepend to the command. Set this to `""` if you don't want the new command to receive a count, e.g. when turning `:9Reg a` into `:norm 9@a`. If `nil`, the range will be supplied from the command entered.
82-
83-
### Example
84-
The following example creates a `:Reg` command which allows you to preview the effects of macros (e.g. `:5Reg a` to run macro `a` five times).
85-
```lua
86-
local commands = {
87-
Reg = {
88-
cmd = "norm",
89-
-- This will transform ":5Reg a" into ":norm 5@a"
90-
args = function(opts)
91-
return (opts.count == -1 and "" or opts.count) .. "@" .. opts.args
92-
end,
93-
range = "",
94-
},
95-
}
96-
97-
require("live-command").setup {
98-
commands = commands,
99-
}
100-
```
73+
an existing command that is run on each keypress via the `cmd` field.
10174

10275
## :gear: Customization
10376

lua/live-command/config_validator.lua

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

3+
local user_command = require("live-command.user_command")
4+
35
---@class livecmd.Config.HlGroups
46
---@field insertion string|false
57
---@field deletion string|false
@@ -12,6 +14,24 @@ local M = {}
1214
---@field hl_groups livecmd.Config.HlGroups?
1315
---@field commands table<string, livecmd.CommandSpec>
1416

17+
local show_diagnostics_message = function(config)
18+
local message = [[
19+
Version 2.0 of live-command.nvim has dropped support for the "args" and "range" keys in the command specification.
20+
The following commands in your configuration are affected: %s. Please remove or modify them.
21+
The other commands will continue to work as expected.
22+
See the migration guide for more information: https://github.com/smjonas/live-command.nvim/blob/main/migrate_to_v2.md
23+
]]
24+
local affected_cmds = {}
25+
for cmd_name, cmd_spec in pairs(config.commands) do
26+
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then
27+
table.insert(affected_cmds, '"' .. cmd_name .. '"')
28+
end
29+
end
30+
local cmd_names = table.concat(affected_cmds, ", ")
31+
local formatted_message = string.format(message, cmd_names)
32+
vim.notify(formatted_message, vim.log.levels.INFO)
33+
end
34+
1535
---@param config livecmd.Config
1636
M.validate_config = function(config)
1737
vim.validate {
@@ -21,6 +41,17 @@ M.validate_config = function(config)
2141
hl_groups = { config.hl_groups, "table" },
2242
commands = { config.commands, "table" },
2343
}
44+
for cmd_name, cmd_spec in pairs(config.commands) do
45+
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then
46+
vim.notify(
47+
'[live-command.nvim] Some unsupported features are used in your config. Please run ":LiveCommand diagnose" for details.',
48+
vim.log.levels.WARN
49+
)
50+
user_command.register_argument_handler("diagnose", function()
51+
show_diagnostics_message(config)
52+
end)
53+
end
54+
end
2455
end
2556

2657
return M

migrate_to_v2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Migration to v2.0
22
This is a guide for users that want to migrate to version `2.0` of `live-command`.
3-
If you want to stay on the previous major version, you can pin the plugin to the tag [`1.x`](https://github.com/smjonas/live-command.nvim/releases/tag/1.x).
3+
If you want to avoid any breaking changes, you can pin the plugin to the tag [`1.x`](https://github.com/smjonas/live-command.nvim/releases/tag/1.x).
44

55
## What has changed in version 2.0?
66
Version 2.0 is a rewrite of the plugin for better maintainability and future extensibility.

tests/e2e_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ describe("create_preview_command works for", function()
3838
assert.are_same({ "First line", "line" }, get_lines())
3939
end)
4040

41+
it("norm command with range", function()
42+
create_preview_cmd("Norm", { cmd = "norm" })
43+
vim.cmd("1,2Norm daw")
44+
assert.are_same({ "line", "line" }, get_lines())
45+
end)
46+
4147
it("g command", function()
4248
create_preview_cmd("G", { cmd = "g" })
4349
vim.cmd("G/Second/d")

0 commit comments

Comments
 (0)