Skip to content

Commit

Permalink
revert: add find http verb position util
Browse files Browse the repository at this point in the history
This works for the case to run to hurl entry
  • Loading branch information
jellydn committed Mar 9, 2024
1 parent efd77bd commit f77a52f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Run `HurlVerbose` command to execute the request in verbose mode. The response w

### Run to entry

Place your cursor on the line you want to run to that entry and press `<leader>te` or run `HurlRunnerToEntry` command to execute the request.
Place your cursor on the line you want to run to that entry and press `<leader>te` or run `HurlRunnerToEntry` command to execute the request. It need be one of the HTTP methods listed: GET, POST, PUT, DELETE, PATCH.

[![Run to entry in split mode](https://i.gyazo.com/14d47adbfcab9e945f89e020b83328a9.gif)](https://gyazo.com/14d47adbfcab9e945f89e020b83328a9)

Expand Down
41 changes: 41 additions & 0 deletions lua/hurl/http_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ local function find_hurl_entry_positions_in_buffer()
end
end

--- Find the HTTP verbs in the current buffer
---@return table
local function find_http_verb_positions_in_buffer()
local buf = vim.api.nvim_get_current_buf()
local total_lines = vim.api.nvim_buf_line_count(buf)
local cursor = vim.api.nvim_win_get_cursor(0)
local current_line_number = cursor[1]

local next_entry = 0
local current_index = 0
local current_verb = nil
local end_line = total_lines -- Default to the last line of the buffer

for i = 1, total_lines do
local line = vim.api.nvim_buf_get_lines(buf, i - 1, i, false)[1]
local result = find_http_verb(line, i)
if result then
next_entry = next_entry + 1
if i == current_line_number then
current_index = next_entry
current_verb = result
elseif current_verb and i > current_verb.line_number then
end_line = i - 1 -- The end line of the current verb is the line before the next verb starts
break -- No need to continue once the end line of the current verb is found
end
end
end

if current_verb and current_index == next_entry then
-- If the current verb is the last one, the end line is the last line of the buffer
end_line = total_lines
end

return {
current = current_index,
start_line = current_verb and current_verb.line_number or nil,
end_line = end_line,
}
end

M.find_http_verb_positions_in_buffer = find_http_verb_positions_in_buffer
M.find_hurl_entry_positions_in_buffer = find_hurl_entry_positions_in_buffer

return M
2 changes: 1 addition & 1 deletion lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function M.setup()

-- Run request to current entry if there is a HTTP method
utils.create_cmd('HurlRunnerToEntry', function(opts)
local result = http.find_hurl_entry_positions_in_buffer()
local result = http.find_http_verb_positions_in_buffer()
if result.current > 0 then
opts.fargs = opts.fargs or {}
opts.fargs = vim.list_extend(opts.fargs, { '--to-entry', result.current })
Expand Down

0 comments on commit f77a52f

Please sign in to comment.