@@ -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,14 @@ 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 (
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
+
75
84
M ._test_mode = false
76
85
77
86
--- @param preview_cmd_name string
@@ -87,29 +96,31 @@ M.create_preview_command = function(preview_cmd_name)
87
96
})
88
97
end
89
98
90
- --- @class livecmd.CommandOpts
99
+ --- @class livecmd.CommandSpec
91
100
--- @field cmd string
92
101
93
102
--- @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 )
96
105
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 )
103
107
end , {
104
108
nargs = " *" ,
105
109
range = true ,
106
110
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 )
109
113
end ,
110
114
})
111
115
end
112
116
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
+
113
124
local create_autocmds = function ()
114
125
local id = api .nvim_create_augroup (" command_preview.nvim" , { clear = true })
115
126
-- We need to be able to tell when the command was cancelled so the buffer lines are refetched next time.
126
137
M .setup = function (user_config )
127
138
if vim .fn .has (" nvim-0.8.0" ) ~= 1 then
128
139
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." ,
130
141
vim .log .levels .ERROR
131
142
)
132
143
return
@@ -135,6 +146,7 @@ M.setup = function(user_config)
135
146
merged_config = vim .tbl_deep_extend (" force" , M .default_config , user_config or {})
136
147
require (" live-command.config_validator" ).validate_config (merged_config )
137
148
M .create_preview_command (merged_config .command_name )
149
+ create_previewable_commands (merged_config .commands )
138
150
create_autocmds ()
139
151
end
140
152
0 commit comments