@@ -10,6 +10,7 @@ M.default_config = {
10
10
deletion = " DiffDelete" ,
11
11
change = " DiffChange" ,
12
12
},
13
+ commands = {},
13
14
}
14
15
15
16
local cmd_executor
@@ -72,6 +73,10 @@ local preview_callback = function(cmd, preview_ns, preview_buf)
72
73
return 2
73
74
end
74
75
76
+ local get_range_string = function (cmd )
77
+ return (cmd .range == 2 and (" %s,%s" ):format (cmd .line1 , cmd .line2 ) or cmd .range == 1 and tostring (cmd .line1 ) or " " )
78
+ end
79
+
75
80
M ._test_mode = false
76
81
77
82
--- @param preview_cmd_name string
@@ -87,29 +92,31 @@ M.create_preview_command = function(preview_cmd_name)
87
92
})
88
93
end
89
94
90
- --- @class livecmd.CommandOpts
95
+ --- @class livecmd.CommandSpec
91
96
--- @field cmd string
92
97
93
98
--- @param cmd_name string
94
- --- @param cmd_opts livecmd.CommandOpts
95
- M .create_previewable_command = function (cmd_name , cmd_opts )
99
+ --- @param cmd_specs livecmd.CommandSpec
100
+ M .create_previewable_command = function (cmd_name , cmd_specs )
96
101
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 )
102
+ vim .cmd (get_range_string (cmd ) .. cmd_specs .cmd .. " " .. cmd .args )
103
103
end , {
104
104
nargs = " *" ,
105
105
range = true ,
106
106
preview = function (cmd , preview_ns , preview_buf )
107
- cmd . name = cmd_opts . cmd
108
- return preview_callback (cmd , preview_ns , preview_buf )
107
+ local cmd_to_preview = get_range_string ( cmd ) .. cmd_specs . cmd .. " " .. cmd . args
108
+ return preview_callback (cmd_to_preview , preview_ns , preview_buf )
109
109
end ,
110
110
})
111
111
end
112
112
113
+ --- @param cmd_specs table<string , livecmd.CommandSpec>
114
+ local create_previewable_commands = function (cmd_specs )
115
+ for cmd_name , cmd_spec in pairs (cmd_specs ) do
116
+ M .create_previewable_command (cmd_name , cmd_spec )
117
+ end
118
+ end
119
+
113
120
local create_autocmds = function ()
114
121
local id = api .nvim_create_augroup (" command_preview.nvim" , { clear = true })
115
122
-- We need to be able to tell when the command was cancelled so the buffer lines are refetched next time.
126
133
M .setup = function (user_config )
127
134
if vim .fn .has (" nvim-0.8.0" ) ~= 1 then
128
135
vim .notify (
129
- " [live-command] This plugin requires at least Neovim 0.8. Please upgrade to a more recent vers1ion of Neovim." ,
136
+ " [live-command] This plugin requires at least Neovim 0.8. Please upgrade to a more recent version of Neovim." ,
130
137
vim .log .levels .ERROR
131
138
)
132
139
return
@@ -135,6 +142,7 @@ M.setup = function(user_config)
135
142
merged_config = vim .tbl_deep_extend (" force" , M .default_config , user_config or {})
136
143
require (" live-command.config_validator" ).validate_config (merged_config )
137
144
M .create_preview_command (merged_config .command_name )
145
+ create_previewable_commands (merged_config .commands )
138
146
create_autocmds ()
139
147
end
140
148
0 commit comments