Skip to content

Commit

Permalink
Merge pull request #216 from teto/dont-inline-file
Browse files Browse the repository at this point in the history
  • Loading branch information
teto authored Jul 16, 2023
2 parents 8f7d45a + 5834e2f commit 22673c8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ request method (e.g. `GET`) and run `rest.nvim`.
---

### Debug


Run `export DEBUG_PLENARY="debug"` before starting nvim. Logs will appear most
likely in ~/.cache/nvim/rest.nvim.log


## Contribute

1. Fork it (https://github.com/rest-nvim/rest.nvim/fork)
Expand Down
8 changes: 4 additions & 4 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ M.get_or_create_buf = function()
local existing_bufnr = vim.fn.bufnr(tmp_name)
if existing_bufnr ~= -1 then
-- Set modifiable
vim.api.nvim_set_option_value(existing_bufnr, "modifiable", true)
vim.api.nvim_set_option_value("modifiable", true, { buf = existing_bufnr})
-- Prevent modified flag
vim.api.nvim_set_option_value(existing_bufnr, "buftype", "nofile")
vim.api.nvim_set_option_value("buftype", "nofile", { buf = existing_bufnr})
-- Delete buffer content
vim.api.nvim_buf_set_lines(
existing_bufnr,
Expand All @@ -59,7 +59,7 @@ M.get_or_create_buf = function()
end

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

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

if config.get("result").show_url then
Expand Down
17 changes: 13 additions & 4 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rest.setup = function(user_configs)
config.set(user_configs or {})
end


-- run will retrieve the required request information from the current buffer
-- and then execute curl
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
Expand Down Expand Up @@ -145,23 +146,31 @@ end
rest.run_request = function(req, opts)
-- TODO rename result to request
local result = req
local curl_raw_args = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
or result.raw
opts = vim.tbl_deep_extend(
"force", -- use value from rightmost map
defaultRequestOpts,
opts or {}
)

-- body =
-- if we want to pass as a file, we pass nothing to plenary
local spliced_body = nil
if not req.body.inline and req.body.filename_tpl then
curl_raw_args = vim.tbl_extend("force", curl_raw_args, {
'--data-binary', '@'..load_external_payload(req.body.filename_tpl)})
else
spliced_body = splice_body(result.headers, result.body)
end

Opts = {
method = result.method:lower(),
url = result.url,
-- plenary.curl can't set http protocol version
-- http_version = result.http_version,
headers = splice_headers(result.headers),
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
or result.raw,
body = splice_body(result.headers, result.body),
raw = curl_raw_args,
body = spliced_body,
dry_run = opts.verbose,
bufnr = result.bufnr,
start_line = result.start_line,
Expand Down
19 changes: 11 additions & 8 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local config = require("rest-nvim.config")
-- get_importfile returns in case of an imported file the absolute filename
-- @param bufnr Buffer number, a.k.a id
-- @param stop_line Line to stop searching
-- @return tuple filename and whether we should inline it when invoking curl
local function get_importfile_name(bufnr, start_line, stop_line)
-- store old cursor position
local oldpos = vim.fn.getcurpos()
Expand All @@ -17,10 +18,13 @@ local function get_importfile_name(bufnr, start_line, stop_line)
if import_line > 0 then
local fileimport_string
local fileimport_line
local fileimport_inlined
fileimport_line = vim.api.nvim_buf_get_lines(bufnr, import_line - 1, import_line, false)
fileimport_string =
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
return fileimport_string
-- check second char against '@' (meaning "dont inline")
fileimport_inlined = string.sub(fileimport_line[1], 2, 2) ~= '@'
fileimport_string = string.gsub(fileimport_line[1], "<@?", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
return fileimport_inlined, fileimport_string

end
return nil
end
Expand All @@ -35,10 +39,10 @@ end
-- @return table { external = bool; filename_tpl or body_tpl; }
local function get_body(bufnr, start_line, stop_line)
-- first check if the body should be imported from an external file
local importfile = get_importfile_name(bufnr, start_line, stop_line)
local inline, importfile = get_importfile_name(bufnr, start_line, stop_line)
local lines -- an array of strings
if importfile ~= nil then
return { external = true, filename_tpl = importfile }
return { external = true; inline = inline; filename_tpl = importfile }
else
lines = vim.api.nvim_buf_get_lines(bufnr, start_line, stop_line, false)
end
Expand All @@ -59,7 +63,7 @@ local function get_body(bufnr, start_line, stop_line)
end
end

return { external = false, body_tpl = lines2 }
return { external = false; inline = false; body_tpl = lines2 }
end

local function get_response_script(bufnr, start_line, stop_line)
Expand Down Expand Up @@ -393,8 +397,7 @@ M.highlight = function(bufnr, start_line, end_line)
higroup,
{ start_line - 1, 0 },
{ end_line - 1, end_column },
"c",
false
{ regtype = "c"; inclusive = false }
)

vim.defer_fn(function()
Expand Down

0 comments on commit 22673c8

Please sign in to comment.