Skip to content

Commit

Permalink
feat(hurl.nvim): improve treesitter parser detection and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Mar 18, 2024
1 parent 42b0a18 commit 00d76db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
21 changes: 6 additions & 15 deletions lua/hurl/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local M = {}

local start = vim.health.start or vim.health.report_start
local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error

-- Add health check for default formatter: jq and prettier
Expand All @@ -22,22 +23,12 @@ M.check = function()
ok('prettier found')
end

local ts
if
xpcall(function()
ts = require('nvim-treesitter-language')
end, function(e)
ts = e
end)
then
ok('nvim-treesitter found')
if ts.language.get_lang('hurl') == 'hurl' then
ok(' hurl parser installed')
else
error(' hurl parser not found')
end
if require('hurl.utils').is_hurl_parser_available then
ok('treesitter[hurl]: installed')
else
error('nvim-treesitter not found')
warn(
'treesitter[hurl]: missing parser for syntax highlighting. Install "nvim-treesitter/nvim-treesitter" plugin and run ":TSInstall hurl".'
)
end

ok('hurl.nvim: All good!')
Expand Down
12 changes: 6 additions & 6 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ function M.setup()

-- Run request at current line if there is a HTTP method
utils.create_cmd('HurlRunnerAt', function(opts)
local is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl 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(
Expand All @@ -382,8 +382,8 @@ function M.setup()

-- Run request to current entry if there is a HTTP method
utils.create_cmd('HurlRunnerToEntry', function(opts)
local is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl 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
Expand Down Expand Up @@ -412,8 +412,8 @@ 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 is_nightly_build = utils.is_nightly()
local result = is_nightly_build and http.find_hurl_entry_positions_in_buffer()
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl 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(
Expand Down
12 changes: 12 additions & 0 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ util.is_html_response = function(content_type)
return string.find(content_type, 'text/html') ~= nil
end

--- Check if nvim is running in nightly or stable version
---@return boolean
util.is_nightly = function()
local is_stable_version = false
if vim.fn.has('nvim-0.10.0') == 1 then
Expand All @@ -178,4 +180,14 @@ util.is_nightly = function()
return is_stable_version
end

--- Check if a treesitter parser is available
---@param ft string
---@return boolean
local function treesitter_parser_available(ft)
local res, parser = pcall(vim.treesitter.get_parser, 0, ft)
return res and parser ~= nil
end

util.is_hurl_parser_available = treesitter_parser_available('hurl')

return util

0 comments on commit 00d76db

Please sign in to comment.