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

Sweep: The response is not formatted (json/html) (βœ“ Sandbox Passed) #102

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0c55850
feat: Updated lua/hurl/utils.lua
sweep-ai[bot] Mar 7, 2024
c6aaff0
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
7c199bb
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
31f10f4
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
38877df
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
2d714a1
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
9bf1509
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
be987db
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
6a3f063
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
602e30b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 9, 2024
349493a
feat: Updated lua/hurl/utils.lua
sweep-ai[bot] Mar 9, 2024
1432537
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
52c01be
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
fc63bed
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 9, 2024
5bbc6cd
Fix HurlRunnterToEntry using treesitter (#116)
hsanson Mar 11, 2024
26ce505
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 11, 2024
74b20eb
chore(doc): auto generate docs
github-actions[bot] Mar 11, 2024
c171637
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 11, 2024
c01d6f8
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 11, 2024
a577de6
Merge main into sweep/the_response_is_not_formatted_jsonhtml
sweep-ai[bot] Mar 13, 2024
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
114 changes: 33 additions & 81 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_()
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

return {
current = r1 + 1,
current = current_node_idx,
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
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 @@ -369,7 +369,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
33 changes: 28 additions & 5 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,36 @@ util.format = function(body, type)
end

util.log_info('formatting body with ' .. type)
local stdout = vim.fn.systemlist(formatters[type], body)
if vim.v.shell_error ~= 0 then
util.log_error('formatter failed' .. vim.v.shell_error)
util.notify('formatter failed' .. vim.v.shell_error, vim.log.levels.ERROR)
local tempFilePath = vim.fn.tempname()
local ok, err = pcall(vim.fn.writefile, vim.split(body, '\n'), tempFilePath)
if not ok then
util.log_error('Failed to write to temp file: ' .. err)
util.notify('Failed to write to temp file: ' .. err, vim.log.levels.ERROR)
return vim.split(body, '\n')
end

-- Modify the command to use the temp file path
local modifiedCommand = vim.tbl_deep_extend('force', formatters[type], { tempFilePath })
local stdout, readErr = pcall(vim.fn.readfile, tempFilePath)
if not stdout or #stdout == 0 then
util.log_error('Failed to read formatted body from temp file: ' .. (readErr or 'Unknown error'))
util.notify(
'Failed to read formatted body from temp file: ' .. (readErr or 'Unknown error'),
vim.log.levels.ERROR
)
return vim.split(body, '\n')
end
os.remove(tempFilePath)
if stdout == nil or #stdout == 0 then
if not stdout or #stdout == 0 then
util.log_error('Failed to read formatted body from temp file: ' .. (readErr or 'Unknown error'))
util.notify(
'Failed to read formatted body from temp file: ' .. (readErr or 'Unknown error'),
vim.log.levels.ERROR
)
return vim.split(body, '\n')
end
-- Ensure to delete the temporary file
os.remove(tempFilePath)
if stdout == nil or #stdout == 0 then
util.log_info('formatter returned empty body')
return vim.split(body, '\n')
Expand Down
Loading