Skip to content

Commit 22673c8

Browse files
authored
Merge pull request #216 from teto/dont-inline-file
2 parents 8f7d45a + 5834e2f commit 22673c8

4 files changed

Lines changed: 35 additions & 16 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ request method (e.g. `GET`) and run `rest.nvim`.
153153
154154
---
155155

156+
### Debug
157+
158+
159+
Run `export DEBUG_PLENARY="debug"` before starting nvim. Logs will appear most
160+
likely in ~/.cache/nvim/rest.nvim.log
161+
162+
156163
## Contribute
157164

158165
1. Fork it (https://github.com/rest-nvim/rest.nvim/fork)

lua/rest-nvim/curl/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ M.get_or_create_buf = function()
4040
local existing_bufnr = vim.fn.bufnr(tmp_name)
4141
if existing_bufnr ~= -1 then
4242
-- Set modifiable
43-
vim.api.nvim_set_option_value(existing_bufnr, "modifiable", true)
43+
vim.api.nvim_set_option_value("modifiable", true, { buf = existing_bufnr})
4444
-- Prevent modified flag
45-
vim.api.nvim_set_option_value(existing_bufnr, "buftype", "nofile")
45+
vim.api.nvim_set_option_value("buftype", "nofile", { buf = existing_bufnr})
4646
-- Delete buffer content
4747
vim.api.nvim_buf_set_lines(
4848
existing_bufnr,
@@ -59,7 +59,7 @@ M.get_or_create_buf = function()
5959
end
6060

6161
-- Create new buffer
62-
local new_bufnr = vim.api.nvim_create_buf(false, "nomodeline")
62+
local new_bufnr = vim.api.nvim_create_buf(false, true)
6363
vim.api.nvim_buf_set_name(new_bufnr, tmp_name)
6464
vim.api.nvim_set_option_value("ft", "httpResult", { buf = new_bufnr })
6565
vim.api.nvim_set_option_value("buftype", "nofile", { buf = new_bufnr })
@@ -102,7 +102,7 @@ local function create_callback(curl_cmd, method, url, script_str)
102102

103103
-- This can be quite verbose so let user control it
104104
if config.get("result").show_curl_command then
105-
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command :" .. curl_cmd })
105+
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command: " .. curl_cmd })
106106
end
107107

108108
if config.get("result").show_url then

lua/rest-nvim/init.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rest.setup = function(user_configs)
1818
config.set(user_configs or {})
1919
end
2020

21+
2122
-- run will retrieve the required request information from the current buffer
2223
-- and then execute curl
2324
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
@@ -145,23 +146,31 @@ end
145146
rest.run_request = function(req, opts)
146147
-- TODO rename result to request
147148
local result = req
149+
local curl_raw_args = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
150+
or result.raw
148151
opts = vim.tbl_deep_extend(
149152
"force", -- use value from rightmost map
150153
defaultRequestOpts,
151154
opts or {}
152155
)
153156

154-
-- body =
157+
-- if we want to pass as a file, we pass nothing to plenary
158+
local spliced_body = nil
159+
if not req.body.inline and req.body.filename_tpl then
160+
curl_raw_args = vim.tbl_extend("force", curl_raw_args, {
161+
'--data-binary', '@'..load_external_payload(req.body.filename_tpl)})
162+
else
163+
spliced_body = splice_body(result.headers, result.body)
164+
end
155165

156166
Opts = {
157167
method = result.method:lower(),
158168
url = result.url,
159169
-- plenary.curl can't set http protocol version
160170
-- http_version = result.http_version,
161171
headers = splice_headers(result.headers),
162-
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
163-
or result.raw,
164-
body = splice_body(result.headers, result.body),
172+
raw = curl_raw_args,
173+
body = spliced_body,
165174
dry_run = opts.verbose,
166175
bufnr = result.bufnr,
167176
start_line = result.start_line,

lua/rest-nvim/request/init.lua

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local config = require("rest-nvim.config")
55
-- get_importfile returns in case of an imported file the absolute filename
66
-- @param bufnr Buffer number, a.k.a id
77
-- @param stop_line Line to stop searching
8+
-- @return tuple filename and whether we should inline it when invoking curl
89
local function get_importfile_name(bufnr, start_line, stop_line)
910
-- store old cursor position
1011
local oldpos = vim.fn.getcurpos()
@@ -17,10 +18,13 @@ local function get_importfile_name(bufnr, start_line, stop_line)
1718
if import_line > 0 then
1819
local fileimport_string
1920
local fileimport_line
21+
local fileimport_inlined
2022
fileimport_line = vim.api.nvim_buf_get_lines(bufnr, import_line - 1, import_line, false)
21-
fileimport_string =
22-
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
23-
return fileimport_string
23+
-- check second char against '@' (meaning "dont inline")
24+
fileimport_inlined = string.sub(fileimport_line[1], 2, 2) ~= '@'
25+
fileimport_string = string.gsub(fileimport_line[1], "<@?", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
26+
return fileimport_inlined, fileimport_string
27+
2428
end
2529
return nil
2630
end
@@ -35,10 +39,10 @@ end
3539
-- @return table { external = bool; filename_tpl or body_tpl; }
3640
local function get_body(bufnr, start_line, stop_line)
3741
-- first check if the body should be imported from an external file
38-
local importfile = get_importfile_name(bufnr, start_line, stop_line)
42+
local inline, importfile = get_importfile_name(bufnr, start_line, stop_line)
3943
local lines -- an array of strings
4044
if importfile ~= nil then
41-
return { external = true, filename_tpl = importfile }
45+
return { external = true; inline = inline; filename_tpl = importfile }
4246
else
4347
lines = vim.api.nvim_buf_get_lines(bufnr, start_line, stop_line, false)
4448
end
@@ -59,7 +63,7 @@ local function get_body(bufnr, start_line, stop_line)
5963
end
6064
end
6165

62-
return { external = false, body_tpl = lines2 }
66+
return { external = false; inline = false; body_tpl = lines2 }
6367
end
6468

6569
local function get_response_script(bufnr, start_line, stop_line)
@@ -393,8 +397,7 @@ M.highlight = function(bufnr, start_line, end_line)
393397
higroup,
394398
{ start_line - 1, 0 },
395399
{ end_line - 1, end_column },
396-
"c",
397-
false
400+
{ regtype = "c"; inclusive = false }
398401
)
399402

400403
vim.defer_fn(function()

0 commit comments

Comments
 (0)