diff --git a/lua/hurl/health.lua b/lua/hurl/health.lua index ba6e176..151f03a 100644 --- a/lua/hurl/health.lua +++ b/lua/hurl/health.lua @@ -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 @@ -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!') diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index 42d2990..82eadba 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -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( @@ -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 @@ -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( diff --git a/lua/hurl/utils.lua b/lua/hurl/utils.lua index bd0a6f4..36b485e 100644 --- a/lua/hurl/utils.lua +++ b/lua/hurl/utils.lua @@ -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 @@ -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