|
1 | 1 | local utils = require("rest-nvim.utils") |
2 | 2 | local curl = require("plenary.curl") |
3 | 3 | local config = require("rest-nvim.config") |
| 4 | +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) |
4 | 5 |
|
5 | 6 | local M = {} |
6 | 7 | -- get_or_create_buf checks if there is already a buffer with the rest run results |
@@ -39,75 +40,83 @@ M.get_or_create_buf = function() |
39 | 40 | return new_bufnr |
40 | 41 | end |
41 | 42 |
|
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 |
| 43 | +local function create_callback(method, url) |
| 44 | + return function(res) |
| 45 | + if res.exit ~= 0 then |
| 46 | + log.error("[rest.nvim] " .. utils.curl_error(res.exit)) |
| 47 | + return |
| 48 | + end |
| 49 | + local res_bufnr = M.get_or_create_buf() |
| 50 | + local json_body = false |
51 | 51 |
|
52 | | - if res.exit ~= 0 then |
53 | | - error("[rest.nvim] " .. utils.curl_error(res.exit)) |
54 | | - end |
| 52 | + -- Check if the content-type is "application/json" so we can format the JSON |
| 53 | + -- output later |
| 54 | + for _, header in ipairs(res.headers) do |
| 55 | + if string.find(header, "application/json") then |
| 56 | + json_body = true |
| 57 | + break |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + --- Add metadata into the created buffer (status code, date, etc) |
| 62 | + -- Request statement (METHOD URL) |
| 63 | + vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { method:upper() .. " " .. url }) |
55 | 64 |
|
56 | | - local res_bufnr = M.get_or_create_buf() |
57 | | - local json_body = false |
| 65 | + -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK |
| 66 | + local line_count = vim.api.nvim_buf_line_count(res_bufnr) |
| 67 | + vim.api.nvim_buf_set_lines( |
| 68 | + res_bufnr, |
| 69 | + line_count, |
| 70 | + line_count, |
| 71 | + false, |
| 72 | + { "HTTP/1.1 " .. utils.http_status(res.status) } |
| 73 | + ) |
| 74 | + -- Headers, e.g. Content-Type: application/json |
| 75 | + vim.api.nvim_buf_set_lines( |
| 76 | + res_bufnr, |
| 77 | + line_count + 1, |
| 78 | + line_count + 1 + #res.headers, |
| 79 | + false, |
| 80 | + res.headers |
| 81 | + ) |
58 | 82 |
|
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 |
| 83 | + --- Add the curl command results into the created buffer |
| 84 | + if json_body then |
| 85 | + -- format JSON body |
| 86 | + res.body = vim.fn.system("jq", res.body) |
65 | 87 | end |
66 | | - end |
| 88 | + local lines = utils.split(res.body, "\n") |
| 89 | + line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 |
| 90 | + vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) |
67 | 91 |
|
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 | | - ) |
| 92 | + -- Only open a new split if the buffer is not loaded into the current window |
| 93 | + if vim.fn.bufwinnr(res_bufnr) == -1 then |
| 94 | + local cmd_split = [[vert sb]] |
| 95 | + if config.result_split_horizontal then |
| 96 | + cmd_split = [[sb]] |
| 97 | + end |
| 98 | + vim.cmd(cmd_split .. res_bufnr) |
| 99 | + -- Set unmodifiable state |
| 100 | + vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) |
| 101 | + end |
88 | 102 |
|
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) |
| 103 | + -- Send cursor in response buffer to start |
| 104 | + utils.move_cursor(res_bufnr, 1) |
93 | 105 | 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) |
| 106 | +end |
97 | 107 |
|
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) |
| 108 | +-- curl_cmd runs curl with the passed options, gets or creates a new buffer |
| 109 | +-- and then the results are printed to the recently obtained/created buffer |
| 110 | +-- @param opts curl arguments |
| 111 | +M.curl_cmd = function(opts) |
| 112 | + if opts.dry_run then |
| 113 | + local res = curl[opts.method](opts) |
| 114 | + log.debug("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " ")) |
| 115 | + return |
| 116 | + else |
| 117 | + opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url)) |
| 118 | + curl[opts.method](opts) |
107 | 119 | end |
108 | | - |
109 | | - -- Send cursor in response buffer to start |
110 | | - utils.go_to_line(res_bufnr, 1) |
111 | 120 | end |
112 | 121 |
|
113 | 122 | return M |
0 commit comments