From f9edcfbe80b45866528124c11e5ff0ed8586facc Mon Sep 17 00:00:00 2001 From: Javier Ugarte Date: Thu, 11 Jul 2024 07:06:41 -0300 Subject: [PATCH] feat: add xml type for response formatting and highlight (#181) * feat: add xml type for response formatting and highlight * feat: add xml type for response formatting and highlight * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(doc): necessary installed dependency for example xml command fixed --------- Co-authored-by: Javier Ugarte Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- README.md | 18 ++++++++++++++++++ doc/hurl.nvim.txt | 18 ++++++++++++++++++ lua/hurl/history.lua | 6 +++++- lua/hurl/init.lua | 6 ++++++ lua/hurl/main.lua | 6 +++++- lua/hurl/popup.lua | 2 +- lua/hurl/split.lua | 2 +- lua/hurl/utils.lua | 13 +++++++++++-- 8 files changed, 65 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e18da51..a2cac6f 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,12 @@ Add the following configuration to your Neovim setup with [lazy.nvim](https://gi '--parser', 'html', }, + xml = { + 'tidy', -- Make sure you have installed tidy in your system, e.g: brew install tidy-html5 + '-xml', + '-i', + '-q', + }, }, }, keys = { @@ -251,6 +257,12 @@ local default_config = { '--parser', 'html', }, + xml = { + 'tidy', -- Uses tidy to format XML responses + '-xml', + '-i', + '-q', + }, }, } ``` @@ -269,6 +281,12 @@ require('hurl').setup({ '--parser', 'html', }, + xml = { + 'tidy', -- Customize the XML formatter command + '-xml', + '-i', + '-q', + }, }, }) ``` diff --git a/doc/hurl.nvim.txt b/doc/hurl.nvim.txt index b17fc22..b56973a 100644 --- a/doc/hurl.nvim.txt +++ b/doc/hurl.nvim.txt @@ -65,6 +65,12 @@ Add the following configuration to your Neovim setup with lazy.nvim '--parser', 'html', }, + xml = { + 'tidy', -- Make sure you have installed tidy in your system, e.g: brew install tidy-html5 + '-xml', + '-i', + '-q', + }, }, }, keys = { @@ -326,6 +332,12 @@ CONFIGURATION *hurl.nvim-configuration* '--parser', 'html', }, + xml = { + 'tidy', -- Uses tidy to format XML responses + '-xml', + '-i', + '-q', + }, }, } < @@ -344,6 +356,12 @@ To apply these configurations, include them in your Neovim setup like this: '--parser', 'html', }, + xml = { + 'tidy', -- Customize the XML formatter command + '-xml', + '-i', + '-q', + }, }, }) < diff --git a/lua/hurl/history.lua b/lua/hurl/history.lua index 19dbd67..4da3205 100644 --- a/lua/hurl/history.lua +++ b/lua/hurl/history.lua @@ -26,7 +26,11 @@ M.show = function(response) if utils.is_html_response(content_type) then container.show(response, 'html') else - container.show(response, 'text') + if utils.is_xml_response(content_type) then + container.show(response, 'xml') + else + container.show(response, 'text') + end end end end diff --git a/lua/hurl/init.lua b/lua/hurl/init.lua index 0663d95..695813e 100644 --- a/lua/hurl/init.lua +++ b/lua/hurl/init.lua @@ -23,6 +23,12 @@ local default_config = { '--parser', 'html', }, + xml = { + 'tidy', + '-xml', + '-i', + '-q', + }, }, } --- Global configuration for entire plugin, easy to access from anywhere diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index a59fe61..d7be137 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -183,7 +183,11 @@ local function execute_hurl_cmd(opts, callback) if utils.is_html_response(content_type) then container.show(response, 'html') else - container.show(response, 'text') + if utils.is_xml_response(content_type) then + container.show(response, 'xml') + else + container.show(response, 'text') + end end end end diff --git a/lua/hurl/popup.lua b/lua/hurl/popup.lua index 3c7c7d1..c845a32 100644 --- a/lua/hurl/popup.lua +++ b/lua/hurl/popup.lua @@ -35,7 +35,7 @@ local layout = Layout( ---@param data table --- - body string --- - headers table ----@param type 'json' | 'html' | 'text' +---@param type 'json' | 'html' | 'xml' | 'text' M.show = function(data, type) layout:mount() diff --git a/lua/hurl/split.lua b/lua/hurl/split.lua index 6a4ff19..e612757 100644 --- a/lua/hurl/split.lua +++ b/lua/hurl/split.lua @@ -15,7 +15,7 @@ local M = {} ---@param data table --- - body string --- - headers table ----@param type 'json' | 'html' | 'text' +---@param type 'json' | 'html' | 'xml' | 'text' M.show = function(data, type) -- mount/open the component split:mount() diff --git a/lua/hurl/utils.lua b/lua/hurl/utils.lua index 83ea766..193ab4e 100644 --- a/lua/hurl/utils.lua +++ b/lua/hurl/utils.lua @@ -101,11 +101,15 @@ end --- Format the body of the request ---@param body string ----@param type 'json' | 'html' | 'text' +---@param type 'json' | 'html' | 'xml' | 'text' ---@return string[] | nil util.format = function(body, type) local formatters = _HURL_GLOBAL_CONFIG.formatters - or { json = { 'jq' }, html = { 'prettier', '--parser', 'html' } } + or { + json = { 'jq' }, + html = { 'prettier', '--parser', 'html' }, + xml = { 'tidy', '-xml', '-i', '-q' }, + } -- If no formatter is defined, return the body if not formatters[type] then @@ -170,6 +174,11 @@ util.is_html_response = function(content_type) return string.find(content_type, 'text/html') ~= nil end +util.is_xml_response = function(content_type) + return string.find(content_type, 'text/xml') ~= nil + or string.find(content_type, 'application/xml') ~= nil +end + --- Check if nvim is running in nightly or stable version ---@return boolean util.is_nightly = function()