Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find entry via treesitter #124

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/hurl.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*hurl.nvim.txt* For NVIM v0.8.0 Last change: 2024 March 09
*hurl.nvim.txt* For NVIM v0.8.0 Last change: 2024 March 11

==============================================================================
Table of Contents *hurl.nvim-table-of-contents*
Expand Down
116 changes: 34 additions & 82 deletions lua/hurl/http_utils.lua
Original file line number Diff line number Diff line change
@@ -1,103 +1,55 @@
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 closest HURL entry at the current cursor position.
local function find_hurl_entry_positions_in_buffer()
local ts = vim.treesitter
local node = ts.get_node()

while node and node:type() ~= 'entry' do
node = node:parent()
-- Look for closest `entry` node to cursor position.
local current_node = ts.get_node()
while current_node and current_node:type() ~= 'entry' do
current_node = current_node:parent()
end

if not node then
if not current_node then
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
return {
current = 0,
start_line = nil,
end_line = nil,
start_line = cursor_line,
end_line = cursor_line,
}
else
local r1, _, _ = node:start()
local r2, _, _ = node:end_()
return {
current = r1 + 1,
start_line = r1 + 1,
end_line = r2 + 1,
}
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
local r1, _, _ = current_node:start()
local r2, _, _ = current_node:end_()

local hurl_file = current_node:parent()

local current_node_idx = 1
if hurl_file and hurl_file:type() == 'hurl_file' then
-- Find the current node index
for node in hurl_file:iter_children() do
if node:id() == current_node:id() then
break
end
current_node_idx = current_node_idx + 1
end
else
-- Parent node is not a hurl_file, HURL file must have errors.
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
return {
current = 0,
start_line = cursor_line,
end_line = cursor_line,
}
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
return {
current = current_node_idx,
start_line = r1 + 1,
end_line = r2,
}
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_http_verb_positions_in_buffer()
local result = http.find_hurl_entry_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
Loading