Skip to content

Commit b072369

Browse files
committed
improv+docs: better config validation to handle breaking changes
Closes #39.
1 parent 122b03d commit b072369

File tree

4 files changed

+149
-35
lines changed

4 files changed

+149
-35
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# live-command.nvim
2-
![version](https://img.shields.io/badge/version-2.1.0-brightgreen)
2+
![version](https://img.shields.io/badge/version-2.2.0-brightgreen)
33

44
> :exclamation: Version 2.0 has been released with breaking changes! Be sure to check out the [migration guide](./migrate_to_v2.md).
55
@@ -74,20 +74,17 @@ an existing command to run on each keypress, specified via the `cmd` field.
7474

7575
## :gear: Customization
7676

77-
All of the following options can be set globally (affecting all custom commands), or per command.
78-
79-
To change the default options globally, use the `defaults` table. The default settings are:
77+
If you wish to customize the plugin, supply any settings that differ from the defaults
78+
to the `setup` function. The following shows the default options:
8079

8180
```lua
8281
require("live-command").setup {
83-
defaults = {
84-
enable_highlighting = true,
85-
inline_highlighting = true,
86-
hl_groups = {
87-
insertion = "DiffAdd",
88-
deletion = "DiffDelete",
89-
change = "DiffChange",
90-
},
82+
enable_highlighting = true,
83+
inline_highlighting = true,
84+
hl_groups = {
85+
insertion = "DiffAdd",
86+
deletion = "DiffDelete",
87+
change = "DiffChange",
9188
},
9289
}
9390
```

lua/live-command/config_validator.lua

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,103 @@ local user_command = require("live-command.user_command")
1414
---@field hl_groups livecmd.Config.HlGroups?
1515
---@field commands table<string, livecmd.CommandSpec>
1616

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-
See the migration guide for more information: https://github.com/smjonas/live-command.nvim/blob/main/migrate_to_v2.md
22-
]]
17+
local get_affected_cmd_names = function(config, is_affected_cmd_fn)
2318
local affected_cmds = {}
2419
for cmd_name, cmd_spec in pairs(config.commands) do
25-
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then
20+
if is_affected_cmd_fn(cmd_spec) then
2621
table.insert(affected_cmds, '"' .. cmd_name .. '"')
2722
end
2823
end
29-
local cmd_names = table.concat(affected_cmds, ", ")
30-
local formatted_message = string.format(message, cmd_names)
24+
return table.concat(affected_cmds, ", "), #affected_cmds
25+
end
26+
27+
local get_args_range_diagnostics_message = function(config)
28+
local message = [[
29+
- Dropped support for the "args" and "range" keys in the command specification:
30+
The following commands in your configuration are affected: %s. Please remove or modify them.
31+
32+
]]
33+
local cmd_names, cmd_count = get_affected_cmd_names(config, function(cmd_spec)
34+
return cmd_spec.args ~= nil or cmd_spec.range ~= nil
35+
end)
36+
if cmd_count == 0 then
37+
return ""
38+
end
39+
return string.format(message, cmd_names)
40+
end
41+
42+
local get_per_command_diagnostics_message = function(config)
43+
local message = [[
44+
- Dropped support for per-command options:
45+
You can no longer set any of the options "enable_highlighting", "inline_highlighting" or "hl_groups" for individual commands.
46+
The following commands are affected: %s.
47+
Please just set the options for all commands like this:
48+
require("live-command").setup {
49+
enable_highlighting = ...,
50+
inline_highlighting = ...,
51+
hl_groups = {
52+
...
53+
},
54+
}
55+
56+
]]
57+
local cmd_names, cmd_count = get_affected_cmd_names(config, function(cmd_spec)
58+
return cmd_spec.enable_highlighting ~= nil or cmd_spec.inline_highlighting ~= nil or cmd_spec.hl_groups ~= nil
59+
end)
60+
if cmd_count == 0 then
61+
return ""
62+
end
63+
return string.format(message, cmd_names)
64+
end
65+
66+
local get_defaults_diagnostics_message = function(config)
67+
if config.defaults == nil then
68+
return ""
69+
end
70+
return [[
71+
- Inlined the "defaults" option:
72+
To set any of the options "enable_highlighting", "inline_highlighting" or "hl_groups", you should no longer use the "defaults" table.
73+
Instead, just set the options like this:
74+
require("live-command").setup {
75+
enable_highlighting = ...,
76+
inline_highlighting = ...,
77+
hl_groups = {
78+
...
79+
},
80+
}
81+
82+
]]
83+
end
84+
85+
local show_diagnostics_message = function(config)
86+
local message = [[
87+
Version 2.x of live-command.nvim has introduced some changes to the configuration:
88+
%s%s%sSee the migration guide for more information: https://github.com/smjonas/live-command.nvim/blob/main/migrate_to_v2.md
89+
]]
90+
local warning_msg_1 = get_args_range_diagnostics_message(config)
91+
local warning_msg_2 = get_per_command_diagnostics_message(config)
92+
local warning_msg_3 = get_defaults_diagnostics_message(config)
93+
local formatted_message = message:format(warning_msg_1, warning_msg_2, warning_msg_3)
3194
vim.notify(formatted_message, vim.log.levels.INFO)
3295
end
3396

97+
local are_unsupported_features_used = function(config)
98+
if config.defaults ~= nil then
99+
return true
100+
end
101+
for _, cmd_spec in pairs(config.commands) do
102+
if
103+
cmd_spec.args ~= nil
104+
or cmd_spec.range ~= nil
105+
or cmd_spec.enable_highlighting ~= nil
106+
or cmd_spec.inline_highlighting ~= nil
107+
or cmd_spec.hl_groups ~= nil
108+
then
109+
return true
110+
end
111+
end
112+
end
113+
34114
---@param config livecmd.Config
35115
M.validate_config = function(config)
36116
vim.validate {
@@ -40,16 +120,14 @@ M.validate_config = function(config)
40120
hl_groups = { config.hl_groups, "table" },
41121
commands = { config.commands, "table" },
42122
}
43-
for cmd_name, cmd_spec in pairs(config.commands) do
44-
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then
45-
vim.notify(
46-
'[live-command.nvim] Some unsupported features are used in your config. Please run ":LiveCommand diagnose" for details.',
47-
vim.log.levels.WARN
48-
)
49-
user_command.register_argument_handler("diagnose", function()
50-
show_diagnostics_message(config)
51-
end)
52-
end
123+
if are_unsupported_features_used(config) then
124+
vim.notify(
125+
'[live-command.nvim] Some unsupported features are used in your config. Please run ":LiveCommand diagnose" for details.',
126+
vim.log.levels.WARN
127+
)
128+
user_command.register_argument_handler("diagnose", function()
129+
show_diagnostics_message(config)
130+
end)
53131
end
54132
end
55133

lua/live-command/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,6 @@ M.setup = function(user_config)
147147
create_autocmds()
148148
end
149149

150-
M.version = "2.1.0"
150+
M.version = "2.2.0"
151151

152152
return M

migrate_to_v2.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
11
# Migration to v2.x
22
This is a guide for users looking to migrate to version `2.x` of `live-command`.
3-
If you'd prefer to avoid breaking changes, you can pin the plugin to tag [`1.x`](https://github.com/smjonas/live-command.nvim/releases/tag/1.x) tag.
3+
If you'd prefer to avoid 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's new in version 2.0?
66
Version 2.0 is a complete rewrite, aimed at improving maintainability and future extensibility.
77
It introduces a simplified user-facing API, alongside improvements to the architecture of the plugin and the addition of a new `:Preview` command.
88

99
**Breaking changes**:
10+
- The `defaults` table containing the options `enable_highlighting`,
11+
`inline_highlighting` and `hl_groups` has been inlined.
12+
- The options `enable_highlighting`, `inline_highlighting` and `hl_groups` can now no
13+
longer be supplied per command. Instead use the top-level options.
1014
- Custom command specifications now only consist of a `cmd` value (a string). The `args`
11-
and `range` fields have been removed. See [next section](#how-can-i-migrate-from-older-versions) for details.
15+
and `range` fields have been removed.
1216

13-
**New feature**:
17+
See [next section](#how-can-i-migrate-from-older-versions) on how to handle the breaking changes.
18+
19+
**New features**:
1420
- A new generic `:Preview` command allows you to preview any command without needing to
1521
define it in your configuration. This is useful for testing `live-command`'s capabilities
1622
or for previewing commands you use infrequently, where creating a separate user command doesn't
1723
seem necessary. The command accepts a range or count. Example: `:'<,'>Preview norm daw`
1824
previews the deletion of the first word in the selected lines.
25+
- A new user command `:LiveCommand`. Running `:LiveCommand log` will print useful debugging
26+
information while `:LiveCommand diagnose` shows instructions on how to adjust your config if you're
27+
using features that are no longer supported.
1928

2029
## How can I migrate from older versions?
30+
### Adjusting the configuration
31+
In versions `1.x`, the following was a valid configuration:
32+
```lua
33+
require("live-command").setup {
34+
defaults = {
35+
inline_highlighting = false,
36+
},
37+
commands = {
38+
Norm = {
39+
cmd = norm, hl_groups = { deletion = "SomeHlGroup" },
40+
},
41+
},
42+
}
43+
```
44+
In version `2.x`, the `defaults` table has been inlined and you can no longer set some options for individual commands.
45+
If there are any issues with your configuration, a warning message will be displayed. You can then run `:LiveCommand diagnose`
46+
to view more detailed instructions on how to adjust your configuration.
47+
48+
To fix the example configuration given above, change it to this:
49+
```lua
50+
require("live-command").setup {
51+
inline_highlighting = false,
52+
hl_groups = { deletion = "SomeHlGroup" },
53+
commands = {
54+
Norm = { cmd = norm },
55+
},
56+
}
57+
```
58+
59+
### A custom :Reg command
2160
In versions `1.x`, the following example demonstrated how to preview the results of a macro:
2261
```lua
2362
local commands = {

0 commit comments

Comments
 (0)