|
| 1 | +# Migration to v2.0 |
| 2 | +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). |
| 4 | + |
| 5 | +## What has changed in version 2.0? |
| 6 | +Version 2.0 is a rewrite of the plugin for better maintainability and future extensibility. |
| 7 | +It simplifies the user-facing API while improving the architecture of the plugin and adding a new `:Preview` command. |
| 8 | + |
| 9 | +**Breaking change**: |
| 10 | +- Custom command specifications now only consist of a `cmd` value (a string); `args` |
| 11 | + and `range` have been removed. See [next section](#how-can-i-migrate-from-older-versions). |
| 12 | + |
| 13 | +**New feature**: |
| 14 | +- New generic `:Preview` command that allows to preview any command without having to |
| 15 | + define it in the configuration. This is useful to test out the capabilities of |
| 16 | + `live-command` or if you don't use a command as often to warrant a separate user command. |
| 17 | + The command itself does not take a range or count. Example: `:Preview '<,'>norm daw` |
| 18 | + previews deletion of the first word of the selected lines. |
| 19 | + |
| 20 | +## How can I migrate from older versions? |
| 21 | +In versions `1.x`, the following example was provided showing how to preview the results of a macro: |
| 22 | +```lua |
| 23 | +local commands = { |
| 24 | + Reg = { |
| 25 | + cmd = "norm", |
| 26 | + -- This will transform ":5Reg a" into ":norm 5@a" |
| 27 | + args = function(opts) |
| 28 | + return (opts.count == -1 and "" or opts.count) .. "@" .. opts.args |
| 29 | + end, |
| 30 | + range = "", |
| 31 | + }, |
| 32 | +} |
| 33 | +``` |
| 34 | +In version `2.0`, you have two options: |
| 35 | +1. Define a command `Norm = { cmd = "norm" }` and use it as `:Norm <count>@<register>` (e.g., `:Norm 5@a` to apply macro stored in register `a` five times). |
| 36 | +2. Define a custom `:Reg` user command like this that works just like the old version: |
| 37 | + |
| 38 | +<details> |
| 39 | + <summary>View code</summary> |
| 40 | + |
| 41 | +```lua |
| 42 | +-- Turns ":5Reg a" into ":norm 5@a" |
| 43 | +local function get_command_string(cmd) |
| 44 | + local get_range_string = require("live-command").get_range_string |
| 45 | + local args = (cmd.count == -1 and "" or cmd.count) .. "@" .. cmd.args |
| 46 | + return get_range_string(cmd) .. "norm " .. args |
| 47 | +end |
| 48 | + |
| 49 | +vim.api.nvim_create_user_command("Reg", function(cmd) |
| 50 | + vim.cmd(get_command_string(cmd)) |
| 51 | +end, { |
| 52 | + nargs = "?", |
| 53 | + range = true, |
| 54 | + preview = function(cmd, preview_ns, preview_buf) |
| 55 | + local cmd_to_preview = get_command_string(cmd) |
| 56 | + return require("live-command").preview_callback(cmd_to_preview, preview_ns, preview_buf) |
| 57 | + end |
| 58 | +}) |
| 59 | +``` |
| 60 | +</details> |
0 commit comments