|
29 | 29 | --- env set {path} Register environment file to current `.http` file.
|
30 | 30 | --- `path` should be relative to Neovim's cwd
|
31 | 31 | ---
|
| 32 | +--- curl yank {name?} (experimental) Copy curl command equivelant to HTTP |
| 33 | +--- request with given `name`. If no name is provided, copy |
| 34 | +--- from request under the cursor. |
| 35 | +--- |
| 36 | +--- curl comment {name?} (experimental) Similar to `:Rest curl yank` but instead |
| 37 | +--- copying command to clipboard, directly insert curl |
| 38 | +--- command as a comment. |
| 39 | +--- |
32 | 40 | --- NOTE: All `:Rest` commands opening new window support |command-modifiers|.
|
33 | 41 | --- For example, you can run `:hor Rest open` to open result pane in horizontal
|
34 | 42 | --- split
|
@@ -174,6 +182,76 @@ local rest_command_tbl = {
|
174 | 182 | return match
|
175 | 183 | end,
|
176 | 184 | },
|
| 185 | + -- TODO(boltless): complete curl command |
| 186 | + curl = { |
| 187 | + impl = function (args, _) |
| 188 | + if args[1] == "yank" then |
| 189 | + local req_node |
| 190 | + if not args[2] then |
| 191 | + req_node = parser().get_cursor_request_node() |
| 192 | + if not req_node then |
| 193 | + logger().error("failed to find request at cursor position") |
| 194 | + vim.notify("[rest.nvim] failed to find request at cursor position", vim.log.levels.ERROR) |
| 195 | + return |
| 196 | + end |
| 197 | + else |
| 198 | + req_node = parser().get_request_node_by_name(args[2]) |
| 199 | + if not req_node then |
| 200 | + logger().error("failed to find request with name:" .. args[2]) |
| 201 | + vim.notify("[rest.nvim] failed to find request with name:" .. args[2], vim.log.levels.ERROR) |
| 202 | + return |
| 203 | + end |
| 204 | + end |
| 205 | + local req = parser().parse(req_node, 0) |
| 206 | + if not req then |
| 207 | + logger().error("failed to parse request") |
| 208 | + vim.notify("[rest.nvim] failed to parse request. See `:Rest logs` for more info", vim.log.levels.ERROR) |
| 209 | + return |
| 210 | + end |
| 211 | + local curl_command = require("rest-nvim.client.curl.cli").builder.build_command(req) |
| 212 | + vim.fn.setreg("+", curl_command) |
| 213 | + vim.notify("[rest.nvim] Copied curl command to clipboard", vim.log.levels.INFO) |
| 214 | + elseif args[1] == "comment" then |
| 215 | + local req_node |
| 216 | + if not args[2] then |
| 217 | + req_node = parser().get_cursor_request_node() |
| 218 | + if not req_node then |
| 219 | + logger().error("failed to find request at cursor position") |
| 220 | + vim.notify("[rest.nvim] failed to find request at cursor position", vim.log.levels.ERROR) |
| 221 | + return |
| 222 | + end |
| 223 | + else |
| 224 | + req_node = parser().get_request_node_by_name(args[2]) |
| 225 | + if not req_node then |
| 226 | + logger().error("failed to find request with name:" .. args[2]) |
| 227 | + vim.notify("[rest.nvim] failed to find request with name:" .. args[2], vim.log.levels.ERROR) |
| 228 | + return |
| 229 | + end |
| 230 | + end |
| 231 | + local req = parser().parse(req_node, 0) |
| 232 | + if not req then |
| 233 | + logger().error("failed to parse request") |
| 234 | + vim.notify("[rest.nvim] failed to parse request. See `:Rest logs` for more info", vim.log.levels.ERROR) |
| 235 | + return |
| 236 | + end |
| 237 | + local curl_command = require("rest-nvim.client.curl.cli").builder.build_command(req) |
| 238 | + local start = req_node:range() |
| 239 | + local end_ = start |
| 240 | + vim.api.nvim_buf_set_lines(0, start, end_, false, vim.tbl_map(function (line) return "# " .. line end, vim.split(curl_command, "\n"))) |
| 241 | + -- elseif args[1] == "to-http" then |
| 242 | + -- -- TODO: convert comment with curl to http request and insert it below |
| 243 | + else |
| 244 | + vim.notify("Invalid action '" .. args[1] .. "' provided to 'curl' command", vim.log.levels.ERROR) |
| 245 | + end |
| 246 | + end, |
| 247 | + complete = function (_args) |
| 248 | + return { |
| 249 | + "yank", |
| 250 | + "comment", |
| 251 | + -- "to-http", |
| 252 | + } |
| 253 | + end |
| 254 | + } |
177 | 255 | }
|
178 | 256 |
|
179 | 257 | local function rest(opts)
|
|
0 commit comments