From 248a07ca3f65c13417b66255c2a3bad1e8341827 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron <886074+teto@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:42:00 +0200 Subject: [PATCH 1/3] feat: make it possible to not inline external file rest.nvim always inlines the payload which generates humongous (superlong) curl commands. If you prefix your external payload with `<@` instead of `<`, rest.nvim will pass the file to curl as-is via --data-binary @filename --- lua/rest-nvim/curl/init.lua | 2 +- lua/rest-nvim/init.lua | 17 +++++++++++++---- lua/rest-nvim/request/init.lua | 16 ++++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 8a1bd2d6..197a51d4 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -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 diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index f5e13896..5d7ca9dc 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -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) @@ -145,13 +146,22 @@ 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(), @@ -159,9 +169,8 @@ rest.run_request = function(req, opts) -- 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, diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 0f364fcc..9f81526e 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -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() @@ -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 @@ -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 @@ -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) From aa8b0b69e7703bec85ccc2d39a482aa94d6ad0d0 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron <886074+teto@users.noreply.github.com> Date: Tue, 11 Jul 2023 19:22:33 +0200 Subject: [PATCH 2/3] doc: mention debug instructions i.e., how to record messages in logfile --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2489c556..88529dd3 100644 --- a/README.md +++ b/README.md @@ -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) From 5834e2fbef8bd506aa25d46c24acf364cade9a7b Mon Sep 17 00:00:00 2001 From: Matthieu Coudron <886074+teto@users.noreply.github.com> Date: Sun, 16 Jul 2023 20:43:32 +0200 Subject: [PATCH 3/3] chore: nvim 0.10 api fixes --- lua/rest-nvim/curl/init.lua | 6 +++--- lua/rest-nvim/request/init.lua | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 197a51d4..e304dcfe 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -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, @@ -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 }) diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 9f81526e..947dc49f 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -397,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()