|
| 1 | +local utils = require("rest-nvim.utils") |
| 2 | +local curl = require("plenary.curl") |
| 3 | +local config = require("rest-nvim.config") |
| 4 | + |
| 5 | +local M = {} |
| 6 | +-- get_or_create_buf checks if there is already a buffer with the rest run results |
| 7 | +-- and if the buffer does not exists, then create a new one |
| 8 | +M.get_or_create_buf = function() |
| 9 | + local tmp_name = "rest_nvim_results" |
| 10 | + |
| 11 | + -- Check if the file is already loaded in the buffer |
| 12 | + local existing_bufnr = vim.fn.bufnr(tmp_name) |
| 13 | + if existing_bufnr ~= -1 then |
| 14 | + -- Set modifiable |
| 15 | + vim.api.nvim_buf_set_option(existing_bufnr, "modifiable", true) |
| 16 | + -- Prevent modified flag |
| 17 | + vim.api.nvim_buf_set_option(existing_bufnr, "buftype", "nofile") |
| 18 | + -- Delete buffer content |
| 19 | + vim.api.nvim_buf_set_lines( |
| 20 | + existing_bufnr, |
| 21 | + 0, |
| 22 | + vim.api.nvim_buf_line_count(existing_bufnr) - 1, |
| 23 | + false, |
| 24 | + {} |
| 25 | + ) |
| 26 | + |
| 27 | + -- Make sure the filetype of the buffer is httpResult so it will be highlighted |
| 28 | + vim.api.nvim_buf_set_option(existing_bufnr, "ft", "httpResult") |
| 29 | + |
| 30 | + return existing_bufnr |
| 31 | + end |
| 32 | + |
| 33 | + -- Create new buffer |
| 34 | + local new_bufnr = vim.api.nvim_create_buf(false, "nomodeline") |
| 35 | + vim.api.nvim_buf_set_name(new_bufnr, tmp_name) |
| 36 | + vim.api.nvim_buf_set_option(new_bufnr, "ft", "httpResult") |
| 37 | + vim.api.nvim_buf_set_option(new_bufnr, "buftype", "nofile") |
| 38 | + |
| 39 | + return new_bufnr |
| 40 | +end |
| 41 | + |
| 42 | +-- curl_cmd runs curl with the passed options, gets or creates a new buffer |
| 43 | +-- and then the results are printed to the recently obtained/created buffer |
| 44 | +-- @param opts curl arguments |
| 45 | +M.curl_cmd = function(opts) |
| 46 | + local res = curl[opts.method](opts) |
| 47 | + if opts.dry_run then |
| 48 | + print("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " ")) |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + if res.exit ~= 0 then |
| 53 | + error("[rest.nvim] " .. utils.curl_error(res.exit)) |
| 54 | + end |
| 55 | + |
| 56 | + local res_bufnr = M.get_or_create_buf() |
| 57 | + local json_body = false |
| 58 | + |
| 59 | + -- Check if the content-type is "application/json" so we can format the JSON |
| 60 | + -- output later |
| 61 | + for _, header in ipairs(res.headers) do |
| 62 | + if string.find(header, "application/json") then |
| 63 | + json_body = true |
| 64 | + break |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + --- Add metadata into the created buffer (status code, date, etc) |
| 69 | + -- Request statement (METHOD URL) |
| 70 | + vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { opts.method:upper() .. " " .. opts.url }) |
| 71 | + -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK |
| 72 | + local line_count = vim.api.nvim_buf_line_count(res_bufnr) |
| 73 | + vim.api.nvim_buf_set_lines( |
| 74 | + res_bufnr, |
| 75 | + line_count, |
| 76 | + line_count, |
| 77 | + false, |
| 78 | + { "HTTP/1.1 " .. utils.http_status(res.status) } |
| 79 | + ) |
| 80 | + -- Headers, e.g. Content-Type: application/json |
| 81 | + vim.api.nvim_buf_set_lines( |
| 82 | + res_bufnr, |
| 83 | + line_count + 1, |
| 84 | + line_count + 1 + #res.headers, |
| 85 | + false, |
| 86 | + res.headers |
| 87 | + ) |
| 88 | + |
| 89 | + --- Add the curl command results into the created buffer |
| 90 | + if json_body then |
| 91 | + -- format JSON body |
| 92 | + res.body = vim.fn.system("jq", res.body) |
| 93 | + end |
| 94 | + local lines = utils.split(res.body, "\n") |
| 95 | + line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 |
| 96 | + vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) |
| 97 | + |
| 98 | + -- Only open a new split if the buffer is not loaded into the current window |
| 99 | + if vim.fn.bufwinnr(res_bufnr) == -1 then |
| 100 | + local cmd_split = [[vert sb]] |
| 101 | + if config.result_split_horizontal == true then |
| 102 | + cmd_split = [[sb]] |
| 103 | + end |
| 104 | + vim.cmd(cmd_split .. res_bufnr) |
| 105 | + -- Set unmodifiable state |
| 106 | + vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) |
| 107 | + end |
| 108 | + |
| 109 | + -- Send cursor in response buffer to start |
| 110 | + utils.go_to_line(res_bufnr, 1) |
| 111 | +end |
| 112 | + |
| 113 | +return M |
0 commit comments