3 files changed,
19 insertions(+),
39 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2026-04-28 18:35:32 +0300
Authored at:
2026-04-28 18:21:23 +0300
Change ID:
sxmouzuulxymqpuzqpzqklottkxlonpr
Parent:
53cfede
jump to
| M | lua/curl.lua |
| M | readme |
| M | stylua.toml |
M
lua/curl.lua
··· 2 2 cache_dir = vim.fs.joinpath(vim.fn.stdpath "data", "curl_cache"), 3 3 query_buffers = {}, 4 4 query_autocmd_set = false, 5 - output_buf = nil, 5 + output_buf = -1, 6 6 command_created = false, 7 7 running_request = nil, 8 8 } ··· 22 22 vim.filetype.add { extension = { curl = "curl" } } 23 23 24 24 if not H.command_created then 25 - vim.api.nvim_create_user_command("Curl", function() 26 - curl.open() 27 - end, { desc = "Open curl.nvim query buffer" }) 25 + vim.api.nvim_create_user_command("Curl", function() curl.open() end, { desc = "Open curl.nvim query buffer" }) 28 26 H.command_created = true 29 27 end 30 28 ··· 32 30 vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile", "BufEnter", "BufWinEnter", "BufFilePost" }, { 33 31 group = vim.api.nvim_create_augroup("curl_nvim_query_files", { clear = true }), 34 32 pattern = "*.curl", 35 - callback = function(args) 36 - H.attach_curl_buffer(args.buf) 37 - end, 33 + callback = function(args) H.attach_curl_buffer(args.buf) end, 38 34 }) 39 35 for _, buf in ipairs(vim.api.nvim_list_bufs()) do 40 36 H.attach_curl_buffer(buf) ··· 77 73 end 78 74 79 75 H.save_query_buffer(query_buf, query_file) 80 - H.open_output_window(query_buf) 81 76 82 77 if H.running_request then 83 - pcall(function() 84 - H.running_request:kill(15) 85 - end) 78 + pcall(function() H.running_request:kill(15) end) 86 79 H.running_request = nil 87 80 end 88 81 ··· 93 86 output = output == "" and result.stderr or (output .. "\n\n" .. result.stderr) 94 87 end 95 88 if output == "" then output = "curl exited with code " .. result.code end 96 - H.write_formatted_output(output) 89 + H.write_formatted_output(query_buf, output) 97 90 end) 98 91 end 99 92 100 -function H.is_valid_buf(buf) 101 - return type(buf) == "number" and buf > 0 and vim.api.nvim_buf_is_valid(buf) 102 -end 93 +function H.is_valid_buf(buf) return type(buf) == "number" and buf > 0 and vim.api.nvim_buf_is_valid(buf) end 103 94 104 -function H.is_curl_file(path) 105 - return type(path) == "string" and path ~= "" and path:sub(-5) == ".curl" 106 -end 95 +function H.is_curl_file(path) return type(path) == "string" and path ~= "" and path:sub(-5) == ".curl" end 107 96 108 97 function H.is_query_start(line) 109 98 if not line then return false end ··· 169 158 vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "BufLeave", "VimLeavePre" }, { 170 159 group = vim.api.nvim_create_augroup("curl_nvim_cache_" .. buf, { clear = true }), 171 160 buffer = buf, 172 - callback = function() 173 - H.save_query_buffer(buf, file) 174 - end, 161 + callback = function() H.save_query_buffer(buf, file) end, 175 162 }) 176 163 end 177 164 ··· 291 278 local body_parts = {} 292 279 for _, line in ipairs(query_lines) do 293 280 local s = vim.trim(line) 294 - if s ~= "" and not s:match "^#" then table.insert(body_parts, s:gsub("\\%s*$", "")) end 281 + if s ~= "" and not s:match "^#" then table.insert(body_parts, (s:gsub("\\%s*$", ""))) end 295 282 end 296 283 297 284 local body = vim.trim(table.concat(body_parts, " ")) ··· 307 294 return cmd 308 295 end 309 296 310 -function H.write_output(text, is_json) 297 +function H.write_output(query_buf, text) 298 + H.open_output_window(query_buf) 311 299 local buf = get_output_buffer() 312 300 local lines = vim.split(text or "", "\n", { plain = true }) 313 301 while #lines > 1 and lines[#lines] == "" do ··· 318 306 vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) 319 307 vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) 320 308 vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) 321 - vim.api.nvim_set_option_value("filetype", is_json and "json" or "text", { buf = buf }) 309 + vim.api.nvim_set_option_value("filetype", "json", { buf = buf }) 322 310 end 323 311 324 -function H.write_formatted_output(output) 312 +function H.write_formatted_output(query_buf, output) 325 313 if output == "" or vim.fn.executable "jq" ~= 1 then 326 - vim.schedule(function() 327 - H.write_output(output, false) 328 - end) 314 + vim.schedule(function() H.write_output(query_buf, output) end) 329 315 return 330 316 end 331 317 ··· 338 324 end 339 325 end 340 326 if not json_index then 341 - vim.schedule(function() 342 - H.write_output(output, false) 343 - end) 327 + vim.schedule(function() H.write_output(query_buf, output) end) 344 328 return 345 329 end 346 330 ··· 351 335 local json_body = table.concat(vim.list_slice(lines, json_index, #lines), "\n") 352 336 353 337 vim.system({ "jq", "." }, { text = true, stdin = json_body }, function(result) 354 - local text, is_json = output, false 355 338 if result.code == 0 and result.stdout and result.stdout ~= "" then 356 - is_json = true 357 - text = #headers > 0 and (table.concat(headers, "\n") .. "\n" .. result.stdout) or result.stdout 339 + output = #headers > 0 and (table.concat(headers, "\n") .. "\n" .. result.stdout) or result.stdout 358 340 end 359 - vim.schedule(function() 360 - H.write_output(text, is_json) 361 - end) 341 + vim.schedule(function() H.write_output(query_buf, output) end) 362 342 end) 363 343 end 364 344