Skip to content

Commit

Permalink
Next Release: Version 2 (#207)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent b1ba767 commit 1137aec
Show file tree
Hide file tree
Showing 17 changed files with 1,548 additions and 596 deletions.
4 changes: 2 additions & 2 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"diagnostics.globals": ["describe", "it"],
"diagnostics.disable": ["undefined-field"]
"diagnostics.globals": ["describe", "it", "vim"],
"diagnostics.disable": []
}
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

> [!IMPORTANT]
> Version 2 is in development! We're working on making hurl.nvim even better. Try out the [canary branch](https://github.com/jellydn/hurl.nvim/pull/207) and share your feedback:
>
> ```bash
> {
> "jellydn/hurl.nvim",
Expand All @@ -13,7 +14,6 @@
> }
> ```
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-)
Expand Down Expand Up @@ -46,8 +46,16 @@ Add the following configuration to your Neovim setup with [lazy.nvim](https://gi
dependencies = {
"MunifTanjim/nui.nvim",
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter"
},
"nvim-treesitter/nvim-treesitter",
-- Optional, for markdown rendering with render-markdown.nvim
{
'MeanderingProgrammer/render-markdown.nvim',
opts = {
file_types = { "markdown" },
},
ft = { "markdown" },
},
}
ft = "hurl",
opts = {
-- Show debugging info
Expand Down Expand Up @@ -86,6 +94,7 @@ Add the following configuration to your Neovim setup with [lazy.nvim](https://gi
{ "<leader>tE", "<cmd>HurlRunnerToEnd<CR>", desc = "Run Api request from current entry to end" },
{ "<leader>tm", "<cmd>HurlToggleMode<CR>", desc = "Hurl Toggle Mode" },
{ "<leader>tv", "<cmd>HurlVerbose<CR>", desc = "Run Api in verbose mode" },
{ "<leader>tV", "<cmd>HurlVeryVerbose<CR>", desc = "Run Api in very verbose mode" },
-- Run Hurl request in visual mode
{ "<leader>h", ":HurlRunner<CR>", desc = "Hurl Runner", mode = "v" },
},
Expand Down Expand Up @@ -211,9 +220,9 @@ Place your cursor on a HURL entry and press `<leader>a` or run `HurlRunnerAt` co
#### Verbose mode
Run `HurlVerbose` command to execute the request in verbose mode. The response will be displayed in QuickFix window. This is useful for debugging purposes or getting the curl command from hurl file.
Run `HurlVerbose` command to execute the request in verbose mode.
[![Run at current line in verbose mode](https://i.gyazo.com/7d0f709e2db53f8c9e05655347f11bc9.gif)](https://gyazo.com/7d0f709e2db53f8c9e05655347f11bc9)
[![Run in verbose mode](https://i.gyazo.com/6136ea63c0a3d0e1293e1fd2c724973a.gif)](https://gyazo.com/6136ea63c0a3d0e1293e1fd2c724973a)
### Run to entry
Expand Down Expand Up @@ -373,17 +382,6 @@ Adjust the settings as per your needs to enhance your development experience wit
- Logs are saved at `~/.local/state/nvim/hurl.nvim.log` on macOS.
> [!TIP]
> Split mode with Edgy
- `hurl.nvim` can be used with [edgy.nvim](https://github.com/folke/edgy.nvim) to manage layout when using the split mode.
```lua
right = {
{ title = "Hurl Nvim", size = { width = 0.5 }, ft = "hurl-nvim" },
}
```
> [!TIP]
> Syntax Highlighting in Stable Neovim
Expand Down
1 change: 1 addition & 0 deletions example/dogs.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ HTTP 200

[Captures]
id: jsonpath "$.data[0].id"
name: jsonpath "$.data[0].attributes.name"

GET https://dogapi.dog/api/v2/breeds/{{id}}

Expand Down
4 changes: 2 additions & 2 deletions example/example.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ header "server" contains "MangaDex"

GET https://google.com

HTTP 301
HTTP 302
[Asserts]
xpath "string(//title)" == "301 Moved"
xpath "string(//title)" == "302 Moved"

GET https://www.google.com

Expand Down
25 changes: 25 additions & 0 deletions example/todo.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Get all todos
GET https://wattpm-demo.fly.dev/express/api/todo

HTTP 200
[Asserts]
jsonpath "$.status" == "success"
header "server" contains "Fly"


# Throw error if text is not provided
POST https://wattpm-demo.fly.dev/express/api/todo
Content-Type: application/json
{}
HTTP 400
[Asserts]
jsonpath "$.status" == "error"
jsonpath "$.message" == "Text is required"

# Create a new todo
POST https://wattpm-demo.fly.dev/express/api/todo
Content-Type: application/json
{
"text": "Call Express API from hurl.nvim at {{now}}"
}
HTTP 201
56 changes: 56 additions & 0 deletions lua/hurl/codelens.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local M = {}

-- Add virtual text for Hurl entries by finding HTTP verbs
function M.add_virtual_text_for_hurl_entries()
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

-- Create a dedicated namespace for Hurl entry markers
local ns_id = vim.api.nvim_create_namespace('hurl_entries')

-- Clear existing virtual text before adding new ones
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)

-- Define all supported HTTP methods
local http_methods = {
'GET',
'POST',
'PUT',
'DELETE',
'PATCH',
'HEAD',
'OPTIONS',
}

local entry_number = 1
for i, line in ipairs(lines) do
-- Match any HTTP method, ignoring preceding whitespace and comments
local method = line:match('^%s*#?%s*([A-Z]+)')
if method and vim.tbl_contains(http_methods, method) then
vim.api.nvim_buf_set_virtual_text(bufnr, ns_id, i - 1, {
{ 'Entry #' .. entry_number, 'Comment' },
}, {})
entry_number = entry_number + 1
end
end
end

-- Setup function to attach buffer autocmd
function M.setup()
local group = vim.api.nvim_create_augroup('HurlEntryMarkers', { clear = true })

-- Handle buffer events
vim.api.nvim_create_autocmd({
'BufEnter',
'TextChanged',
'InsertLeave',
}, {
group = group,
pattern = '*.hurl',
callback = function()
M.add_virtual_text_for_hurl_entries()
end,
})
end

return M
17 changes: 16 additions & 1 deletion lua/hurl/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ M.check = function()
)
end

-- TODO: Add check for hurl version, e.g: > 4.3.0 to use new features
-- Check for hurl version > 4.3.0
local hurl_version_output = vim.fn.system('hurl --version')
local hurl_version = hurl_version_output:match('%d+%.%d+%.%d+')

if hurl_version then
local major, minor, patch = hurl_version:match('(%d+)%.(%d+)%.(%d+)')
major, minor, patch = tonumber(major), tonumber(minor), tonumber(patch)

if major > 4 or (major == 4 and (minor > 3 or (minor == 3 and patch > 0))) then
ok('hurl version > 4.3.0 found')
else
error('hurl version <= 4.3.0 found')
end
else
error('Unable to determine hurl version')
end

ok('hurl.nvim: All good!')
end
Expand Down
74 changes: 48 additions & 26 deletions lua/hurl/history.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
local utils = require('hurl.utils')
local M = {}

--- Show the last request in the history
---@param response table
M.show = function(response)
local container = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode)
if not response.headers then
-- Do not show anything if there is no response
return
-- Store the last 10 responses
local response_history = {}
local max_history_size = 10

-- Add a response to the history
local function add_to_history(response)
table.insert(response_history, 1, response)
if #response_history > max_history_size then
table.remove(response_history)
end
end

local content_type = response.headers['content-type']
or response.headers['Content-Type']
or response.headers['Content-type']
or 'unknown'
-- Show the last response
function M.show_last_response()
if #response_history == 0 then
utils.notify('No response history available', vim.log.levels.INFO)
return
end

utils.log_info('Detected content type: ' .. content_type)
if response.headers['content-length'] == '0' then
utils.log_info('hurl: empty response')
utils.notify('hurl: empty response', vim.log.levels.INFO)
local last_response = response_history[1]
local ok, display = pcall(require, 'hurl.' .. (_HURL_GLOBAL_CONFIG.mode or 'split'))
if not ok then
utils.notify('Failed to load display module: ' .. display, vim.log.levels.ERROR)
return
end
if utils.is_json_response(content_type) then
container.show(response, 'json')

display.show(last_response, last_response.display_type or 'text')
end

-- Function to be called after each successful request
function M.update_history(response)
-- Ensure response_time is a number
response.response_time = tonumber(response.response_time) or '-'

-- Determine the content type and set display_type
local content_type = response.headers['Content-Type']
or response.headers['content-type']
or 'text/plain'

if content_type:find('json') then
response.display_type = 'json'
elseif content_type:find('html') then
response.display_type = 'html'
elseif content_type:find('xml') then
response.display_type = 'xml'
else
if utils.is_html_response(content_type) then
container.show(response, 'html')
else
if utils.is_xml_response(content_type) then
container.show(response, 'xml')
else
container.show(response, 'text')
end
end
response.display_type = 'text'
end

add_to_history(response)
end

function M.get_last_response()
return response_history[1]
end

return M
2 changes: 2 additions & 0 deletions lua/hurl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ local default_config = {
},
-- File root directory for uploading files
-- file_root = vim.fn.getcwd(),
-- Save capture as global variable
save_captures_as_globals = true,
}
--- Global configuration for entire plugin, easy to access from anywhere
_HURL_GLOBAL_CONFIG = default_config
Expand Down
Loading

0 comments on commit 1137aec

Please sign in to comment.