Skip to content

Commit

Permalink
feat(http_utils): add support for finding HTTP verbs in buffer for st…
Browse files Browse the repository at this point in the history
…able neovim

This commit introduces a new feature in the `http_utils` module that allows finding HTTP verbs (GET, POST, PUT, DELETE, PATCH) in the current buffer. This feature is used to determine the start and end lines of an HTTP request in the buffer, which is useful for running the request. The commit also includes changes in `main.lua` to use this new feature when running HTTP requests. The feature is only enabled for Neovim nightly builds.
  • Loading branch information
jellydn committed Mar 15, 2024
1 parent 470b59f commit c10a905
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
71 changes: 71 additions & 0 deletions lua/hurl/http_utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,76 @@
local M = {}

--- Find the HTTP verb in the given line
---@param line string
---@param current_line_number number
local function find_http_verb(line, current_line_number)
if not line then
return nil
end

local verbs = { 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' }
local verb_start, verb_end, verb

for _, v in ipairs(verbs) do
verb_start, verb_end = line:find(v)
if verb_start then
verb = v
break
end
end

if verb_start then
return {
line_number = current_line_number,
start_pos = verb_start,
end_pos = verb_end,
method = verb,
}
else
return nil
end
end

--- Find the HTTP verbs in the current buffer
---@return table
M.find_http_verb_positions_in_buffer = function()
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

--- Find the closest HURL entry at the current cursor position.
M.find_hurl_entry_positions_in_buffer = function()
local ts = vim.treesitter
Expand Down
14 changes: 10 additions & 4 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ function M.setup()

-- Run request at current line if there is a HTTP method
utils.create_cmd('HurlRunnerAt', function(opts)
local result = http.find_hurl_entry_positions_in_buffer()
local is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 and result.start_line and result.end_line then
utils.log_info(
'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line
Expand All @@ -372,7 +374,9 @@ 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 is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
utils.log_info('hurl: running request to entry #' .. vim.inspect(result))
if result.current > 0 then
run_at_lines(1, result.end_line, opts.fargs)
Expand Down Expand Up @@ -400,7 +404,9 @@ function M.setup()
utils.create_cmd('HurlVerbose', function(opts)
-- It should be the same logic with run at current line but with verbose flag
-- The response will be sent to quickfix
local result = http.find_hurl_entry_positions_in_buffer()
local is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 and result.start_line and result.end_line then
utils.log_info(
'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line
Expand All @@ -413,7 +419,7 @@ function M.setup()
title = 'hurl',
lines = {},
})
run_at_lines(result.start_line, result.end_line, opts.fargs, function(code, data, event)
run_at_lines(1, result.end_line, opts.fargs, function(code, data, event)
utils.log_info('hurl: verbose callback ' .. vim.inspect(code) .. vim.inspect(data))
vim.fn.setqflist({}, 'a', {
title = 'hurl - data',
Expand Down
9 changes: 9 additions & 0 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ util.is_html_response = function(content_type)
return string.find(content_type, 'text/html') ~= nil
end

util.is_nightly = function()
local is_stable_version = false
if vim.fn.has('nvim-0.10.0') == 1 then
is_stable_version = true
end

return is_stable_version
end

return util

0 comments on commit c10a905

Please sign in to comment.