From 8d5ffd9a1cdf254683a22f5ac0f78f42b36c1767 Mon Sep 17 00:00:00 2001 From: jens1205 Date: Sat, 28 Aug 2021 22:38:55 +0200 Subject: [PATCH 01/10] first draft of highlighting. curl disabled as this interfers with highlight -> has to be fixed --- lua/rest-nvim/config/init.lua | 4 +++ lua/rest-nvim/init.lua | 23 +++++++++---- lua/rest-nvim/request/init.lua | 40 +++++++++++++++++++++- tests/external_file/multiple_requests.http | 2 +- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lua/rest-nvim/config/init.lua b/lua/rest-nvim/config/init.lua index ee762ed1..5ee2ed1a 100644 --- a/lua/rest-nvim/config/init.lua +++ b/lua/rest-nvim/config/init.lua @@ -3,6 +3,10 @@ local M = {} local config = { result_split_horizontal = false, skip_ssl_verification = false, + highlight = { + enabled = true, + timeout = 150, + } } --- Get a configuration value diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index 350999f2..08048bc3 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -6,6 +6,7 @@ local LastOpts = {} rest.setup = function(user_configs) config.set(user_configs or {}) + vim.api.nvim_command("hi def link RestNvimSelect Search") end -- run will retrieve the required request information from the current buffer @@ -25,16 +26,24 @@ rest.run = function(verbose) raw = config.skip_ssl_verification and { "-k" } or nil, body = result.body, dry_run = verbose or false, + bufnr = result.bufnr, + start_line = result.start_line, + end_line = result.end_line, } - local success_req, req_err = pcall(curl.curl_cmd, LastOpts) - - if not success_req then - vim.api.nvim_err_writeln( - "[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 - ) + if config.get("highlight").enabled == true then + print("highlighting request") + request.highlight(result.bufnr, result.start_line, result.end_line) end + + -- local success_req, req_err = pcall(curl.curl_cmd, LastOpts) + + -- if not success_req then + -- vim.api.nvim_err_writeln( + -- "[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 + -- ) + -- end end -- last will run the last curl request, if available diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index f84297d2..5bb696c8 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -1,6 +1,7 @@ local utils = require("rest-nvim.utils") local path = require("plenary.path") local log = require("plenary.log").new({ plugin = "rest.nvim", level = "warn" }) +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 @@ -144,7 +145,13 @@ local function end_request(bufnr) -- restore cursor position utils.go_to_line(bufnr, oldlinenumber) - return next > 1 and next - 1 or vim.fn.line("$") + local last_line = vim.fn.line("$") + + if next == 0 or (next == last_line and linenumber == last_line) then + return last_line + else + return next - 1 + end end -- parse_url returns a table with the method of the request and the URL @@ -180,7 +187,38 @@ M.get_current_request = function() url = parsed_url.url, headers = headers, body = body, + bufnr = bufnr, + start_line = start_line, + end_line = end_line, } end +local select_ns = vim.api.nvim_create_namespace('rest-nvim') +M.highlight = function(bufnr, start_line, end_line) + local opts = config.get("highlight") or {} + -- local higroup = "RestNvimSelect" + local higroup = "IncSearch" + local timeout = opts.timeout or 150 + + vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) + + local end_column = string.len(vim.fn.getline(end_line)) + + vim.highlight.range(bufnr, select_ns, higroup, {start_line-1,0}, {end_line-1,end_column}, "c", false) + + vim.defer_fn( + function() + print("in defer") + if vim.api.nvim_buf_is_valid(bufnr) then + print("is valid") + vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) + else + print("not valid") + end + end, + timeout + ) +end + + return M diff --git a/tests/external_file/multiple_requests.http b/tests/external_file/multiple_requests.http index b87ed581..b57ca44a 100644 --- a/tests/external_file/multiple_requests.http +++ b/tests/external_file/multiple_requests.http @@ -16,5 +16,5 @@ Content-Type: application/json < ./user.json #### - +### GET https://reqres.in/api/users?page=5 From 2244b256182df87d170556bace75f9fcdce7296b Mon Sep 17 00:00:00 2001 From: NTBBloodbath Date: Thu, 26 Aug 2021 10:30:07 -0400 Subject: [PATCH 02/10] feat: add tree-sitter parser instructions, remove syntax highlighting lazy-loading. Closes #30 --- README.md | 18 ++++++++++++++++++ ftdetect/http.vim | 1 + lua/rest-nvim/init.lua | 4 ---- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 ftdetect/http.vim diff --git a/README.md b/README.md index 1e139f35..ed3294d4 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,24 @@ use { } ``` +### Tree-Sitter parser + +We are using a Tree-Sitter parser for our HTTP files, in order to get the correct syntax highlighting +you should add this before your tree-sitter setup. + +```lua +local parser_configs = require("nvim-treesitter.parsers").get_parser_configs() +parser_configs.http = { + install_info = { + url = "https://github.com/NTBBloodbath/tree-sitter-http", + files = { "src/parser.c" }, + branch = "main", + }, +} +``` + +And then add `"http"` in your `ensure_installed` table or manually run `:TSInstall http`. + ## Keybindings By default `rest.nvim` does not have any key mappings so you will not have diff --git a/ftdetect/http.vim b/ftdetect/http.vim new file mode 100644 index 00000000..02b25691 --- /dev/null +++ b/ftdetect/http.vim @@ -0,0 +1 @@ +au BufRead,BufNewFile *.http set ft=http diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index ea8b00d9..350999f2 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -4,12 +4,8 @@ local config = require("rest-nvim.config") local curl = require("rest-nvim.curl") local LastOpts = {} --- setup is needed for enabling syntax highlighting for http files rest.setup = function(user_configs) config.set(user_configs or {}) - if vim.fn.expand("%:e") == "http" then - vim.api.nvim_buf_set_option("%", "filetype", "http") - end end -- run will retrieve the required request information from the current buffer From ce9506d05931120d1bec035a51ce2057e3fdd846 Mon Sep 17 00:00:00 2001 From: NTBBloodbath Date: Thu, 26 Aug 2021 11:09:33 -0400 Subject: [PATCH 03/10] chore: remove not needed syntax file for `*.http` --- syntax/http.vim | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 syntax/http.vim diff --git a/syntax/http.vim b/syntax/http.vim deleted file mode 100644 index 838462df..00000000 --- a/syntax/http.vim +++ /dev/null @@ -1,26 +0,0 @@ -if exists('b:current_syntax') | finish | endif - -syn match httpUrl "\(https\?:\/\{2}\)\?\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*" - -syn keyword httpCommentKeyword TODO NOTE FIXME BUG contained -syn match httpComment "\v^#.*$" contains=httpCommentKeyword - -syn keyword httpMethod GET POST PATCH PUT HEAD DELETE nextgroup=httpPath -syn match httpPath ".*$" contained - -syn match httpHeaderKey "^\(\w\)[^:]\+" nextgroup=httpHeaderSeparator skipwhite -syn match httpHeaderSeparator "[=:]" contained - -syn include @json syntax/json.vim -syn region jsonBody start="\v^\{" end="\v^\}$" contains=@json keepend - - -hi link httpComment Comment -hi link httpCommentKeyword Todo -hi link httpUrl Title -hi link httpMethod Type -hi link httpPath Title -hi link httpHeaderKey Identifier -hi link httpHeaderSeparator Delimiter - -let b:current_syntax = 'http' From 24c980477888c46c30bb2266eb719460c19b839f Mon Sep 17 00:00:00 2001 From: NTBBloodbath Date: Thu, 26 Aug 2021 11:11:18 -0400 Subject: [PATCH 04/10] docs(README): add new notice about syntax highlighting --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ed3294d4..bc9ef4d1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ have to leave Neovim! ## Notices +- **2021-08-26**: We have deleted the syntax file for HTTP files to start using the tree-sitter parser instead, + please see [Tree-Sitter parser](#tree-sitter-parser) section for more information. - **2021-07-01**: Now for getting syntax highlighting in http files you should add a `require('rest-nvim').setup()` to your `rest.nvim` setup, refer to [packer.nvim](#packernvim). This breaking change should allow lazy-loading of `rest.nvim`. From 95042cc2836fb308d6029680adc5cf23c3f0c828 Mon Sep 17 00:00:00 2001 From: jens1205 Date: Sun, 29 Aug 2021 11:37:08 +0200 Subject: [PATCH 05/10] add debug --- lua/rest-nvim/curl/init.lua | 8 +++++++- lua/rest-nvim/init.lua | 3 ++- lua/rest-nvim/request/init.lua | 31 ++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 12d33b25..4d5c52ad 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -1,11 +1,13 @@ local utils = require("rest-nvim.utils") local curl = require("plenary.curl") local config = require("rest-nvim.config") +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) local M = {} -- get_or_create_buf checks if there is already a buffer with the rest run results -- and if the buffer does not exists, then create a new one M.get_or_create_buf = function() + log.debug("curl.get_or_create_buf") local tmp_name = "rest_nvim_results" -- Check if the file is already loaded in the buffer @@ -30,12 +32,14 @@ M.get_or_create_buf = function() return existing_bufnr end + log.debug("create new buffer") -- Create new buffer local new_bufnr = vim.api.nvim_create_buf(false, "nomodeline") vim.api.nvim_buf_set_name(new_bufnr, tmp_name) vim.api.nvim_buf_set_option(new_bufnr, "ft", "httpResult") vim.api.nvim_buf_set_option(new_bufnr, "buftype", "nofile") + log.debug("exit get_or_create_buf") return new_bufnr end @@ -45,7 +49,7 @@ end M.curl_cmd = function(opts) local res = curl[opts.method](opts) if opts.dry_run then - print("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " ")) + log.debug("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " ")) return end @@ -65,6 +69,7 @@ M.curl_cmd = function(opts) end end + log.debug("writing to result buffer...") --- Add metadata into the created buffer (status code, date, etc) -- Request statement (METHOD URL) vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { opts.method:upper() .. " " .. opts.url }) @@ -86,6 +91,7 @@ M.curl_cmd = function(opts) res.headers ) + log.debug("calling jq...") --- Add the curl command results into the created buffer if json_body then -- format JSON body diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index 08048bc3..04b993fb 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -1,4 +1,5 @@ local rest = {} +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) local request = require("rest-nvim.request") local config = require("rest-nvim.config") local curl = require("rest-nvim.curl") @@ -32,7 +33,7 @@ rest.run = function(verbose) } if config.get("highlight").enabled == true then - print("highlighting request") + log.debug("highlighting request") request.highlight(result.bufnr, result.start_line, result.end_line) end diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 5bb696c8..634c4dd5 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -1,6 +1,6 @@ local utils = require("rest-nvim.utils") local path = require("plenary.path") -local log = require("plenary.log").new({ plugin = "rest.nvim", level = "warn" }) +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) local config = require("rest-nvim.config") -- get_importfile returns in case of an imported file the absolute filename @@ -147,7 +147,7 @@ local function end_request(bufnr) utils.go_to_line(bufnr, oldlinenumber) local last_line = vim.fn.line("$") - if next == 0 or (next == last_line and linenumber == last_line) then + if next == 0 or (oldlinenumber == last_line) then return last_line else return next - 1 @@ -193,8 +193,16 @@ M.get_current_request = function() } end +local function time_with_ms() + local ms = require'socket'.gettime()*1000 + local tf=os.date('%H:%M:%S.',os.time()) + return tf..ms +end + local select_ns = vim.api.nvim_create_namespace('rest-nvim') +local clearfunc M.highlight = function(bufnr, start_line, end_line) + log.debug(time_with_ms() .. ": highlighting request") local opts = config.get("highlight") or {} -- local higroup = "RestNvimSelect" local higroup = "IncSearch" @@ -208,17 +216,30 @@ M.highlight = function(bufnr, start_line, end_line) vim.defer_fn( function() - print("in defer") + log.debug(time_with_ms() .. ": in defer") if vim.api.nvim_buf_is_valid(bufnr) then - print("is valid") + log.debug(time_with_ms() .. ": is valid") vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) else - print("not valid") + log.debug(time_with_ms() .. ": not valid") end end, timeout ) + clearfunc = function() + if vim.api.nvim_buf_is_valid(bufnr) then + -- log.debug("is valid") + vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) + else + -- log.debug("not valid") + end + end end +M.clear = function() + clearfunc() +end + + return M From a5dbf855fb7946b4ba0a38875886f8821eb52809 Mon Sep 17 00:00:00 2001 From: jens1205 Date: Sun, 29 Aug 2021 22:44:40 +0200 Subject: [PATCH 06/10] highlighting working --- lua/rest-nvim/curl/init.lua | 137 +++++++++++++++++---------------- lua/rest-nvim/init.lua | 15 ++-- lua/rest-nvim/request/init.lua | 37 ++------- lua/rest-nvim/utils/init.lua | 11 ++- 4 files changed, 93 insertions(+), 107 deletions(-) diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 4d5c52ad..6d8455f2 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -7,7 +7,6 @@ local M = {} -- get_or_create_buf checks if there is already a buffer with the rest run results -- and if the buffer does not exists, then create a new one M.get_or_create_buf = function() - log.debug("curl.get_or_create_buf") local tmp_name = "rest_nvim_results" -- Check if the file is already loaded in the buffer @@ -32,88 +31,94 @@ M.get_or_create_buf = function() return existing_bufnr end - log.debug("create new buffer") -- Create new buffer local new_bufnr = vim.api.nvim_create_buf(false, "nomodeline") vim.api.nvim_buf_set_name(new_bufnr, tmp_name) vim.api.nvim_buf_set_option(new_bufnr, "ft", "httpResult") vim.api.nvim_buf_set_option(new_bufnr, "buftype", "nofile") - log.debug("exit get_or_create_buf") return new_bufnr end +local function create_callback(method, url) + return function(res) + if res.exit ~= 0 then + log.error("[rest.nvim] " .. utils.curl_error(res.exit)) + return + end + local res_bufnr = M.get_or_create_buf() + local json_body = false + + -- Check if the content-type is "application/json" so we can format the JSON + -- output later + for _, header in ipairs(res.headers) do + if string.find(header, "application/json") then + json_body = true + break + end + end + + --- Add metadata into the created buffer (status code, date, etc) + -- Request statement (METHOD URL) + vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { method:upper() .. " " .. url }) + + -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK + local line_count = vim.api.nvim_buf_line_count(res_bufnr) + vim.api.nvim_buf_set_lines( + res_bufnr, + line_count, + line_count, + false, + { "HTTP/1.1 " .. utils.http_status(res.status) } + ) + -- Headers, e.g. Content-Type: application/json + vim.api.nvim_buf_set_lines( + res_bufnr, + line_count + 1, + line_count + 1 + #res.headers, + false, + res.headers + ) + + --- Add the curl command results into the created buffer + if json_body then + -- format JSON body + res.body = vim.fn.system("jq", res.body) + end + local lines = utils.split(res.body, "\n") + line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 + vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) + + -- Only open a new split if the buffer is not loaded into the current window + if vim.fn.bufwinnr(res_bufnr) == -1 then + local cmd_split = [[vert sb]] + if config.result_split_horizontal == true then + cmd_split = [[sb]] + end + vim.cmd(cmd_split .. res_bufnr) + -- Set unmodifiable state + vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) + end + + -- Send cursor in response buffer to start + utils.move_cursor(res_bufnr, 1) + + end +end + -- curl_cmd runs curl with the passed options, gets or creates a new buffer -- and then the results are printed to the recently obtained/created buffer -- @param opts curl arguments M.curl_cmd = function(opts) - local res = curl[opts.method](opts) if opts.dry_run then + local res = curl[opts.method](opts) log.debug("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " ")) return + else + opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url)) + curl[opts.method](opts) end - - if res.exit ~= 0 then - error("[rest.nvim] " .. utils.curl_error(res.exit)) - end - - local res_bufnr = M.get_or_create_buf() - local json_body = false - - -- Check if the content-type is "application/json" so we can format the JSON - -- output later - for _, header in ipairs(res.headers) do - if string.find(header, "application/json") then - json_body = true - break - end - end - - log.debug("writing to result buffer...") - --- Add metadata into the created buffer (status code, date, etc) - -- Request statement (METHOD URL) - vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { opts.method:upper() .. " " .. opts.url }) - -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK - local line_count = vim.api.nvim_buf_line_count(res_bufnr) - vim.api.nvim_buf_set_lines( - res_bufnr, - line_count, - line_count, - false, - { "HTTP/1.1 " .. utils.http_status(res.status) } - ) - -- Headers, e.g. Content-Type: application/json - vim.api.nvim_buf_set_lines( - res_bufnr, - line_count + 1, - line_count + 1 + #res.headers, - false, - res.headers - ) - - log.debug("calling jq...") - --- Add the curl command results into the created buffer - if json_body then - -- format JSON body - res.body = vim.fn.system("jq", res.body) - end - local lines = utils.split(res.body, "\n") - line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 - vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) - - -- Only open a new split if the buffer is not loaded into the current window - if vim.fn.bufwinnr(res_bufnr) == -1 then - local cmd_split = [[vert sb]] - if config.result_split_horizontal == true then - cmd_split = [[sb]] - end - vim.cmd(cmd_split .. res_bufnr) - -- Set unmodifiable state - vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) - end - - -- Send cursor in response buffer to start - utils.go_to_line(res_bufnr, 1) end + return M diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index 04b993fb..dcfef1aa 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -33,18 +33,17 @@ rest.run = function(verbose) } if config.get("highlight").enabled == true then - log.debug("highlighting request") request.highlight(result.bufnr, result.start_line, result.end_line) end - -- local success_req, req_err = pcall(curl.curl_cmd, LastOpts) + local success_req, req_err = pcall(curl.curl_cmd, LastOpts) - -- if not success_req then - -- vim.api.nvim_err_writeln( - -- "[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 - -- ) - -- end + if not success_req then + vim.api.nvim_err_writeln( + "[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 + ) + end end -- last will run the last curl request, if available diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 634c4dd5..310a7b7e 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -9,11 +9,11 @@ local config = require("rest-nvim.config") local function get_importfile_name(bufnr, start_line, stop_line) -- store old cursor position local oldpos = vim.fn.getcurpos() - utils.go_to_line(bufnr, start_line) + utils.move_cursor(bufnr, start_line) local import_line = vim.fn.search("^<", "n", stop_line) -- restore old cursor position - utils.go_to_line(bufnr, oldpos[2]) + utils.move_cursor(bufnr, oldpos[2]) if import_line > 0 then local fileimport_string @@ -139,12 +139,12 @@ local function end_request(bufnr) if linenumber < vim.fn.line("$") then linenumber = linenumber + 1 end - utils.go_to_line(bufnr, linenumber) + utils.move_cursor(bufnr, linenumber) local next = vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cn", vim.fn.line("$")) -- restore cursor position - utils.go_to_line(bufnr, oldlinenumber) + utils.move_cursor(bufnr, oldlinenumber) local last_line = vim.fn.line("$") if next == 0 or (oldlinenumber == last_line) then @@ -167,6 +167,7 @@ end local M = {} M.get_current_request = function() + local curpos = vim.fn.getcurpos() local bufnr = vim.api.nvim_win_get_buf(0) local start_line = start_request() @@ -174,7 +175,7 @@ M.get_current_request = function() error("No request found") end local end_line = end_request() - utils.go_to_line(bufnr, start_line) + -- utils.move_cursor(bufnr, start_line) local parsed_url = parse_url(vim.fn.getline(start_line)) @@ -182,6 +183,8 @@ M.get_current_request = function() local body = get_body(bufnr, body_start, end_line) + utils.move_cursor(bufnr,curpos[2], curpos[3]) + return { method = parsed_url.method, url = parsed_url.url, @@ -193,16 +196,9 @@ M.get_current_request = function() } end -local function time_with_ms() - local ms = require'socket'.gettime()*1000 - local tf=os.date('%H:%M:%S.',os.time()) - return tf..ms -end local select_ns = vim.api.nvim_create_namespace('rest-nvim') -local clearfunc M.highlight = function(bufnr, start_line, end_line) - log.debug(time_with_ms() .. ": highlighting request") local opts = config.get("highlight") or {} -- local higroup = "RestNvimSelect" local higroup = "IncSearch" @@ -216,30 +212,13 @@ M.highlight = function(bufnr, start_line, end_line) vim.defer_fn( function() - log.debug(time_with_ms() .. ": in defer") if vim.api.nvim_buf_is_valid(bufnr) then - log.debug(time_with_ms() .. ": is valid") vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) else - log.debug(time_with_ms() .. ": not valid") end end, timeout ) - clearfunc = function() - if vim.api.nvim_buf_is_valid(bufnr) then - -- log.debug("is valid") - vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) - else - -- log.debug("not valid") - end - end -end - -M.clear = function() - clearfunc() end - - return M diff --git a/lua/rest-nvim/utils/init.lua b/lua/rest-nvim/utils/init.lua index 7ea8491c..ba2d7b4d 100644 --- a/lua/rest-nvim/utils/init.lua +++ b/lua/rest-nvim/utils/init.lua @@ -1,14 +1,17 @@ local random = math.random +local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) math.randomseed(os.time()) local M = {} --- go_to_line moves the cursor to the desired line in the provided buffer +-- move_cursor moves the cursor to the desired position in the provided buffer -- @param bufnr Buffer number, a.k.a id --- @param line the desired cursor position -M.go_to_line = function(bufnr, line) +-- @param line the desired line +-- @param column the desired column, defaults to 1 +M.move_cursor = function(bufnr, line, column) + column = column or 1 vim.api.nvim_buf_call(bufnr, function() - vim.fn.cursor(line, 1) + vim.fn.cursor(line, column) end) end From fc551091ff7dfff5e2bd5810afa9036d93f8afba Mon Sep 17 00:00:00 2001 From: jens1205 Date: Mon, 30 Aug 2021 20:16:24 +0200 Subject: [PATCH 07/10] documentation. highlight for last(). jump_to_request option --- README.md | 11 ++++++++++- doc/rest-nvim.txt | 7 +++++++ lua/rest-nvim/config/init.lua | 3 ++- lua/rest-nvim/init.lua | 6 +++++- lua/rest-nvim/request/init.lua | 8 +++++--- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bc9ef4d1..a4c45128 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,14 @@ use { result_split_horizontal = false, -- Skip SSL verification, useful for unknown certificates skip_ssl_verification = false, - }) + -- Highlight request on run + highlight = { + enabled = true, + timeout = 150, + }, + -- Jump to request line on run + jump_to_request = false, + }) end } ``` @@ -100,6 +107,8 @@ To run `rest.nvim` you should map the following commands: on vertical) - `skip_ssl_verification` passes the `-k` flag to cURL in order to skip SSL verification, useful when using unknown certificates +- `highlight` allows to enable and configure the highlighting of the selected request when send, +- `jump_to_request` moves the cursor to the selected request line when send, ## Usage diff --git a/doc/rest-nvim.txt b/doc/rest-nvim.txt index 52451670..c017f279 100644 --- a/doc/rest-nvim.txt +++ b/doc/rest-nvim.txt @@ -56,6 +56,13 @@ function, it looks like this by default: ` result_split_horizontal = false,` ` -- Skip SSL verification, useful for unknown certificates` ` skip_ssl_verification = false,` +` -- Highlight request on run` +` highlight = {` +` enabled = true,` +` timeout = 150,` +` },` +` -- Jump to request line on run` +` jump_to_request = false,` `})` In this section we will be using `https://reqres.in/` for requests. diff --git a/lua/rest-nvim/config/init.lua b/lua/rest-nvim/config/init.lua index 5ee2ed1a..6d3dd380 100644 --- a/lua/rest-nvim/config/init.lua +++ b/lua/rest-nvim/config/init.lua @@ -6,7 +6,8 @@ local config = { highlight = { enabled = true, timeout = 150, - } + }, + jump_to_request = false, } --- Get a configuration value diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index dcfef1aa..fb0637e8 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -7,7 +7,6 @@ local LastOpts = {} rest.setup = function(user_configs) config.set(user_configs or {}) - vim.api.nvim_command("hi def link RestNvimSelect Search") end -- run will retrieve the required request information from the current buffer @@ -52,6 +51,11 @@ rest.last = function() vim.api.nvim_err_writeln("[rest.nvim]: Last request not found") return end + + if config.get("highlight").enabled == true then + request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line) + end + local success_req, req_err = pcall(curl.curl_cmd, LastOpts) if not success_req then diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 310a7b7e..5c8c9323 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -175,7 +175,6 @@ M.get_current_request = function() error("No request found") end local end_line = end_request() - -- utils.move_cursor(bufnr, start_line) local parsed_url = parse_url(vim.fn.getline(start_line)) @@ -183,7 +182,11 @@ M.get_current_request = function() local body = get_body(bufnr, body_start, end_line) - utils.move_cursor(bufnr,curpos[2], curpos[3]) + if config.get("jump_to_request") == true then + utils.move_cursor(bufnr, start_line) + else + utils.move_cursor(bufnr,curpos[2], curpos[3]) + end return { method = parsed_url.method, @@ -200,7 +203,6 @@ end local select_ns = vim.api.nvim_create_namespace('rest-nvim') M.highlight = function(bufnr, start_line, end_line) local opts = config.get("highlight") or {} - -- local higroup = "RestNvimSelect" local higroup = "IncSearch" local timeout = opts.timeout or 150 From 5a7f8991fc3c5de0f39d616c422fc11f4a702018 Mon Sep 17 00:00:00 2001 From: jens1205 Date: Mon, 30 Aug 2021 20:24:25 +0200 Subject: [PATCH 08/10] fix luacheck --- lua/rest-nvim/init.lua | 1 - lua/rest-nvim/request/init.lua | 1 - lua/rest-nvim/utils/init.lua | 1 - 3 files changed, 3 deletions(-) diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index fb0637e8..ec59f4ec 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -1,5 +1,4 @@ local rest = {} -local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) local request = require("rest-nvim.request") local config = require("rest-nvim.config") local curl = require("rest-nvim.curl") diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 5c8c9323..4043af33 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -216,7 +216,6 @@ M.highlight = function(bufnr, start_line, end_line) function() if vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) - else end end, timeout diff --git a/lua/rest-nvim/utils/init.lua b/lua/rest-nvim/utils/init.lua index ba2d7b4d..b4594fd1 100644 --- a/lua/rest-nvim/utils/init.lua +++ b/lua/rest-nvim/utils/init.lua @@ -1,5 +1,4 @@ local random = math.random -local log = require("plenary.log").new({ plugin = "rest.nvim", level = "debug" }) math.randomseed(os.time()) local M = {} From f114ea1638f1f44d5d108cd85747b77a173b3be8 Mon Sep 17 00:00:00 2001 From: jens1205 Date: Mon, 30 Aug 2021 20:24:54 +0200 Subject: [PATCH 09/10] stylua --- lua/rest-nvim/config/init.lua | 4 +- lua/rest-nvim/curl/init.lua | 110 ++++++++++++++++----------------- lua/rest-nvim/init.lua | 4 +- lua/rest-nvim/request/init.lua | 34 +++++----- 4 files changed, 77 insertions(+), 75 deletions(-) diff --git a/lua/rest-nvim/config/init.lua b/lua/rest-nvim/config/init.lua index 6d3dd380..ffdfa77d 100644 --- a/lua/rest-nvim/config/init.lua +++ b/lua/rest-nvim/config/init.lua @@ -4,8 +4,8 @@ local config = { result_split_horizontal = false, skip_ssl_verification = false, highlight = { - enabled = true, - timeout = 150, + enabled = true, + timeout = 150, }, jump_to_request = false, } diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 6d8455f2..740e409d 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -41,69 +41,68 @@ M.get_or_create_buf = function() end local function create_callback(method, url) - return function(res) - if res.exit ~= 0 then - log.error("[rest.nvim] " .. utils.curl_error(res.exit)) - return - end - local res_bufnr = M.get_or_create_buf() - local json_body = false - - -- Check if the content-type is "application/json" so we can format the JSON - -- output later - for _, header in ipairs(res.headers) do - if string.find(header, "application/json") then - json_body = true - break - end + return function(res) + if res.exit ~= 0 then + log.error("[rest.nvim] " .. utils.curl_error(res.exit)) + return + end + local res_bufnr = M.get_or_create_buf() + local json_body = false + + -- Check if the content-type is "application/json" so we can format the JSON + -- output later + for _, header in ipairs(res.headers) do + if string.find(header, "application/json") then + json_body = true + break end + end --- Add metadata into the created buffer (status code, date, etc) -- Request statement (METHOD URL) - vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { method:upper() .. " " .. url }) - - -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK - local line_count = vim.api.nvim_buf_line_count(res_bufnr) - vim.api.nvim_buf_set_lines( - res_bufnr, - line_count, - line_count, - false, - { "HTTP/1.1 " .. utils.http_status(res.status) } - ) - -- Headers, e.g. Content-Type: application/json - vim.api.nvim_buf_set_lines( - res_bufnr, - line_count + 1, - line_count + 1 + #res.headers, - false, - res.headers - ) + vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { method:upper() .. " " .. url }) - --- Add the curl command results into the created buffer - if json_body then - -- format JSON body - res.body = vim.fn.system("jq", res.body) - end - local lines = utils.split(res.body, "\n") - line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 - vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) + -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK + local line_count = vim.api.nvim_buf_line_count(res_bufnr) + vim.api.nvim_buf_set_lines( + res_bufnr, + line_count, + line_count, + false, + { "HTTP/1.1 " .. utils.http_status(res.status) } + ) + -- Headers, e.g. Content-Type: application/json + vim.api.nvim_buf_set_lines( + res_bufnr, + line_count + 1, + line_count + 1 + #res.headers, + false, + res.headers + ) - -- Only open a new split if the buffer is not loaded into the current window - if vim.fn.bufwinnr(res_bufnr) == -1 then - local cmd_split = [[vert sb]] - if config.result_split_horizontal == true then - cmd_split = [[sb]] - end - vim.cmd(cmd_split .. res_bufnr) - -- Set unmodifiable state - vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) + --- Add the curl command results into the created buffer + if json_body then + -- format JSON body + res.body = vim.fn.system("jq", res.body) + end + local lines = utils.split(res.body, "\n") + line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1 + vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines) + + -- Only open a new split if the buffer is not loaded into the current window + if vim.fn.bufwinnr(res_bufnr) == -1 then + local cmd_split = [[vert sb]] + if config.result_split_horizontal == true then + cmd_split = [[sb]] end - - -- Send cursor in response buffer to start - utils.move_cursor(res_bufnr, 1) - + vim.cmd(cmd_split .. res_bufnr) + -- Set unmodifiable state + vim.api.nvim_buf_set_option(res_bufnr, "modifiable", false) end + + -- Send cursor in response buffer to start + utils.move_cursor(res_bufnr, 1) + end end -- curl_cmd runs curl with the passed options, gets or creates a new buffer @@ -120,5 +119,4 @@ M.curl_cmd = function(opts) end end - return M diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index ec59f4ec..c9f960de 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -31,7 +31,7 @@ rest.run = function(verbose) } if config.get("highlight").enabled == true then - request.highlight(result.bufnr, result.start_line, result.end_line) + request.highlight(result.bufnr, result.start_line, result.end_line) end local success_req, req_err = pcall(curl.curl_cmd, LastOpts) @@ -52,7 +52,7 @@ rest.last = function() end if config.get("highlight").enabled == true then - request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line) + request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line) end local success_req, req_err = pcall(curl.curl_cmd, LastOpts) diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 4043af33..d19ad930 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -148,9 +148,9 @@ local function end_request(bufnr) local last_line = vim.fn.line("$") if next == 0 or (oldlinenumber == last_line) then - return last_line + return last_line else - return next - 1 + return next - 1 end end @@ -185,7 +185,7 @@ M.get_current_request = function() if config.get("jump_to_request") == true then utils.move_cursor(bufnr, start_line) else - utils.move_cursor(bufnr,curpos[2], curpos[3]) + utils.move_cursor(bufnr, curpos[2], curpos[3]) end return { @@ -199,27 +199,31 @@ M.get_current_request = function() } end - -local select_ns = vim.api.nvim_create_namespace('rest-nvim') +local select_ns = vim.api.nvim_create_namespace("rest-nvim") M.highlight = function(bufnr, start_line, end_line) local opts = config.get("highlight") or {} local higroup = "IncSearch" - local timeout = opts.timeout or 150 + local timeout = opts.timeout or 150 vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) local end_column = string.len(vim.fn.getline(end_line)) - vim.highlight.range(bufnr, select_ns, higroup, {start_line-1,0}, {end_line-1,end_column}, "c", false) - - vim.defer_fn( - function() - if vim.api.nvim_buf_is_valid(bufnr) then - vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) - end - end, - timeout + vim.highlight.range( + bufnr, + select_ns, + higroup, + { start_line - 1, 0 }, + { end_line - 1, end_column }, + "c", + false ) + + vim.defer_fn(function() + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_clear_namespace(bufnr, select_ns, 0, -1) + end + end, timeout) end return M From 07289d0389a0739c20b6f7c42e9780c45745126e Mon Sep 17 00:00:00 2001 From: jens1205 Date: Tue, 31 Aug 2021 07:07:51 +0200 Subject: [PATCH 10/10] removed == true in if statements --- lua/rest-nvim/curl/init.lua | 2 +- lua/rest-nvim/init.lua | 4 ++-- lua/rest-nvim/request/init.lua | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 740e409d..37fa3976 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -92,7 +92,7 @@ local function create_callback(method, url) -- Only open a new split if the buffer is not loaded into the current window if vim.fn.bufwinnr(res_bufnr) == -1 then local cmd_split = [[vert sb]] - if config.result_split_horizontal == true then + if config.result_split_horizontal then cmd_split = [[sb]] end vim.cmd(cmd_split .. res_bufnr) diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index c9f960de..f4f02b59 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -30,7 +30,7 @@ rest.run = function(verbose) end_line = result.end_line, } - if config.get("highlight").enabled == true then + if config.get("highlight").enabled then request.highlight(result.bufnr, result.start_line, result.end_line) end @@ -51,7 +51,7 @@ rest.last = function() return end - if config.get("highlight").enabled == true then + if config.get("highlight").enabled then request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line) end diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index d19ad930..1ebffdc5 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -182,7 +182,7 @@ M.get_current_request = function() local body = get_body(bufnr, body_start, end_line) - if config.get("jump_to_request") == true then + if config.get("jump_to_request") then utils.move_cursor(bufnr, start_line) else utils.move_cursor(bufnr, curpos[2], curpos[3])