Skip to content

Commit

Permalink
feat: better syntax highlights in http result
Browse files Browse the repository at this point in the history
  • Loading branch information
udayvir-singh authored and NTBBloodbath committed Aug 9, 2022
1 parent 3c46649 commit 5b21f91
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
30 changes: 24 additions & 6 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ local function create_callback(method, url)
return
end
local res_bufnr = M.get_or_create_buf()
local json_body = false
local content_type = nil

-- Check if the content-type is "application/json" so we can format the JSON
-- output later
-- get content type
for _, header in ipairs(res.headers) do
if header:find("application/json") then
json_body = true
if string.lower(header):find("^content%-type") then
content_type = header:match("application/(%l+)") or header:match("text/(%l+)")
break
end
end
Expand Down Expand Up @@ -90,7 +89,7 @@ local function create_callback(method, url)
end

--- Add the curl command results into the created buffer
if json_body then
if content_type == "json" then
-- format JSON body
res.body = vim.fn.system("jq", res.body):gsub("\n$", "")
end
Expand All @@ -114,6 +113,25 @@ local function create_callback(method, url)

-- Send cursor in response buffer to start
utils.move_cursor(res_bufnr, 1)

-- add syntax highlights for response
if content_type == "json" then
vim.cmd([[
unlet b:current_syntax
syn include @json syntax/json.vim
syn region jsonBody start="\v\{" end="\v\}$" contains=@json
let b:current_syntax = "httpResult"
]])
elseif content_type == "html" then
vim.cmd([[
unlet b:current_syntax
syn include @html syntax/html.vim
syn region htmlBody start=+<html.*>+ end=+<\/ *html>+ contains=@html
let b:current_syntax = "httpResult"
]])
end
end
end

Expand Down
42 changes: 28 additions & 14 deletions syntax/httpResult.vim
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
if exists("b:current_syntax") | finish | endif

syn match httpResultComment "\v^#.*$"
syn keyword httpResultTitle GET POST PATCH PUT HEAD DELETE nextgroup=httpResultPath
syn match httpResultPath ".*$" contained
syn match httpResultField /^\(\w\)[^:]\+:/he=e-1
syn match httpResultComment "\v^#.*$"
syn keyword httpResultTitle GET POST PATCH PUT HEAD DELETE nextgroup=httpResultPath
syn match httpResultPat /.*$/ contained

syn region httpResultString start=/\vr?"/ end=/\v"/
syn match httpResultNumber /\v([a-zA-Z_:]\d*)@<!\d+(\.(\d+)?)?/
syn match httpResultField /^\(\w\)[^:]\+:/he=e-1
syn region httpResultDateField start=+^[Dd]ate:+he=e-1 end=+ + nextgroup=httpResultDate
syn region httpResultDateField start=+^[Ee]xpires:+he=e-1 end=+ + nextgroup=httpResultDate
syn match httpResultDate /.*$/ contained

syn region httpResultHeader start=+^HTTP/+ end=+ + nextgroup=httpResult200,httpResult300,httpResult400,httpResult500
syn match httpResult200 /2.*$/ contained
syn match httpResult300 /3.*$/ contained
syn match httpResult400 /4.*$/ contained
syn match httpResult500 /5.*$/ contained

syn include @json syntax/json.vim
syn region jsonBody start="\v\{" end="\v\}$" contains=@json keepend
syn region httpResultString start=/\vr?"/ end=/\v"/
syn match httpResultNumber /\v[ =]@1<=\d+[ \n]/

hi link httpResultComment Comment
hi link httpResultTitle Type
hi link httpResultPath Title
hi link httpResultField Variable
hi link httpResultString String
hi link httpResultNumber Number
hi link httpResultComment Comment
hi link httpResultTitle Type
hi link httpResultPath httpTSURI
hi link httpResultField Identifier
hi link httpResultDateField Identifier
hi link httpResultDate String
hi link httpResultString String
hi link httpResultNumber Number
hi link httpResultHeader Type
hi link httpResult200 String
hi link httpResult300 Function
hi link httpResult400 Number
hi link httpResult500 Number

let b:current_syntax = "httpResult"

0 comments on commit 5b21f91

Please sign in to comment.