diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index 16a9417c..e70e6154 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -1,6 +1,7 @@ local request = require("rest-nvim.request") local config = require("rest-nvim.config") local curl = require("rest-nvim.curl") +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) local rest = {} local Opts = {} @@ -23,8 +24,36 @@ rest.run = function(verbose) return rest.run_request(result, verbose) end -rest.run_request = function(req, verbose) +-- run will retrieve the required request information from the current buffer +-- and then execute curl +-- @param string filename to load +-- @param opts table +-- 1. keep_going boolean keep running even when last request failed +rest.run_file = function(filename, opts) + log.info("Running file :" .. filename) + local new_buf = vim.api.nvim_create_buf(false, false) + + vim.api.nvim_win_set_buf(0, new_buf) + vim.cmd.edit(filename) + local last_line = vim.fn.line("$") + + -- reset cursor position + vim.fn.cursor(1, 1) + local curpos = vim.fn.getcurpos() + while curpos[2] <= last_line do + local ok, req = request.buf_get_request(new_buf, curpos) + if ok then + -- request.print_request(req) + curpos[2] = req.end_line + 1 + rest.run_request(req, opts) + else + return false, req + end + end + return true +end +rest.run_request = function(req, opts) local result = req Opts = { method = result.method:lower(), @@ -35,13 +64,13 @@ rest.run_request = function(req, verbose) raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" }) or result.raw, body = result.body, - dry_run = verbose or false, + dry_run = opts.verbose or false, bufnr = result.bufnr, start_line = result.start_line, end_line = result.end_line, } - if not verbose then + if not opts.verbose then LastOpts = Opts end @@ -56,6 +85,7 @@ rest.run_request = function(req, verbose) "[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.\n\nTraceback: " .. req_err ) + return false, req_err end end diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index e40b8ef7..e2bf03af 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -145,7 +145,7 @@ local function get_curl_args(bufnr, headers_end, end_line) if line_content:find("^ *%-%-?[a-zA-Z%-]+") then local lc = vim.split(line_content, " ") - local x = "" + local x = "" for i, y in ipairs(lc) do x = x .. y @@ -174,17 +174,25 @@ end -- of the current request and returns the linenumber of this request line. -- The current request is defined as the next request line above the cursor -- @param bufnr The buffer nummer of the .http-file -local function start_request() - return vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cbn", 1) +-- @param linenumber (number) From which line to start looking +local function start_request(bufnr, linenumber) + log.debug("Searching pattern starting from " .. linenumber) + + local oldlinenumber = linenumber + utils.move_cursor(bufnr, linenumber) + + local res = vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cn") + -- restore cursor position + utils.move_cursor(bufnr, oldlinenumber) + + return res end -- end_request will find the next request line (e.g. POST http://localhost:8081/foo) -- and returns the linenumber before this request line or the end of the buffer -- @param bufnr The buffer nummer of the .http-file -local function end_request(bufnr) +local function end_request(bufnr, linenumber) -- store old cursor position - local curpos = vim.fn.getcurpos() - local linenumber = curpos[2] local oldlinenumber = linenumber -- start searching for next request from the next line @@ -245,18 +253,19 @@ M.get_current_request = function() end -- buf_get_request returns a table with all the request settings --- @param bufnr the buffer number +-- @param bufnr (number|nil) the buffer number -- @param curpos the cursor position -- @return (boolean, request or string) M.buf_get_request = function(bufnr, curpos) curpos = curpos or vim.fn.getcurpos() bufnr = bufnr or vim.api.nvim_win_get_buf(0) - local start_line = start_request() + local start_line = start_request(bufnr, curpos[2]) + if start_line == 0 then return false, "No request found" end - local end_line = end_request(bufnr) + local end_line = end_request(bufnr, start_line) local parsed_url = parse_url(vim.fn.getline(start_line)) @@ -284,17 +293,28 @@ M.buf_get_request = function(bufnr, curpos) utils.move_cursor(bufnr, curpos[2], curpos[3]) end - return true, { - method = parsed_url.method, - url = parsed_url.url, - http_version = parsed_url.http_version, - headers = headers, - raw = curl_args, - body = body, - bufnr = bufnr, - start_line = start_line, - end_line = end_line, - } + return true, + { + method = parsed_url.method, + url = parsed_url.url, + http_version = parsed_url.http_version, + headers = headers, + raw = curl_args, + body = body, + bufnr = bufnr, + start_line = start_line, + end_line = end_line, + } +end + +M.print_request = function(req) + local str = [[ + version: ]] .. req.url .. [[\n + method: ]] .. req.method .. [[\n + start_line: ]] .. tostring(req.start_line) .. [[\n + end_line: ]] .. tostring(req.end_line) .. [[\n + ]] + print(str) end local select_ns = vim.api.nvim_create_namespace("rest-nvim") diff --git a/lua/rest-nvim/utils/init.lua b/lua/rest-nvim/utils/init.lua index 9e82f78f..79c4f27b 100644 --- a/lua/rest-nvim/utils/init.lua +++ b/lua/rest-nvim/utils/init.lua @@ -50,17 +50,17 @@ M.get_variables = function() local variables = {} -- If there is a line at the beginning with @ first - if vim.fn.search('^@', 'cn') > 0 then + if vim.fn.search("^@", "cn") > 0 then -- Read all lines of the file - local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true); + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) -- For each line for _, line in pairs(lines) do -- Get the name and value form lines that starts with @ - local name, val = line:match("^@([%w!@#$%^&*-_+?~]+)%s*=%s*([^=]+)"); + local name, val = line:match("^@([%w!@#$%^&*-_+?~]+)%s*=%s*([^=]+)") if name then -- Add to variables - variables[name] = val; + variables[name] = val end end end @@ -94,7 +94,7 @@ M.get_variables = function() -- Add that into the variable -- I.E if @url={{path}}:{{port}}/{{source}} -- Substitue in path, port and source - variables[name] = variables[name]:gsub('{{' .. oname .. '}}', ovalue); + variables[name] = variables[name]:gsub("{{" .. oname .. "}}", ovalue) end end end diff --git a/tests/main_spec.lua b/tests/main_spec.lua index bf5c8e01..dc4accfe 100644 --- a/tests/main_spec.lua +++ b/tests/main_spec.lua @@ -1,13 +1,13 @@ -local rest = require('rest-nvim') -local v = vim +local rest = require("rest-nvim") describe("rest testing framework", function() - - it('test create users', function() - - v.api.nvim_cmd({cmd='edit', args = {'tests/post_create_user.http'}}, {}) - - -- first argument is for verbosity - rest.run(false) - end) + it("test create users", function() + local opts = { keep_going = true, verbose = true } + assert(rest.run_file("tests/basic_get.http", opts) == true) + assert(rest.run_file("tests/post_json_form.http", opts) == true) + assert(rest.run_file("tests/post_create_user.http", opts) == true) + assert(rest.run_file("tests/put_update_user.http", opts) == true) + assert(rest.run_file("tests/patch_update_user.http", opts) == true) + assert(rest.run_file("tests/delete.http", opts) == true) + end) end)