Skip to content

Commit 5b21f91

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
feat: better syntax highlights in http result
1 parent 3c46649 commit 5b21f91

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

lua/rest-nvim/curl/init.lua

+24-6
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ local function create_callback(method, url)
4747
return
4848
end
4949
local res_bufnr = M.get_or_create_buf()
50-
local json_body = false
50+
local content_type = nil
5151

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

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

115114
-- Send cursor in response buffer to start
116115
utils.move_cursor(res_bufnr, 1)
116+
117+
-- add syntax highlights for response
118+
if content_type == "json" then
119+
vim.cmd([[
120+
unlet b:current_syntax
121+
syn include @json syntax/json.vim
122+
syn region jsonBody start="\v\{" end="\v\}$" contains=@json
123+
124+
let b:current_syntax = "httpResult"
125+
]])
126+
elseif content_type == "html" then
127+
vim.cmd([[
128+
unlet b:current_syntax
129+
syn include @html syntax/html.vim
130+
syn region htmlBody start=+<html.*>+ end=+<\/ *html>+ contains=@html
131+
132+
let b:current_syntax = "httpResult"
133+
]])
134+
end
117135
end
118136
end
119137

syntax/httpResult.vim

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
if exists("b:current_syntax") | finish | endif
22

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

8-
syn region httpResultString start=/\vr?"/ end=/\v"/
9-
syn match httpResultNumber /\v([a-zA-Z_:]\d*)@<!\d+(\.(\d+)?)?/
7+
syn match httpResultField /^\(\w\)[^:]\+:/he=e-1
8+
syn region httpResultDateField start=+^[Dd]ate:+he=e-1 end=+ + nextgroup=httpResultDate
9+
syn region httpResultDateField start=+^[Ee]xpires:+he=e-1 end=+ + nextgroup=httpResultDate
10+
syn match httpResultDate /.*$/ contained
11+
12+
syn region httpResultHeader start=+^HTTP/+ end=+ + nextgroup=httpResult200,httpResult300,httpResult400,httpResult500
13+
syn match httpResult200 /2.*$/ contained
14+
syn match httpResult300 /3.*$/ contained
15+
syn match httpResult400 /4.*$/ contained
16+
syn match httpResult500 /5.*$/ contained
1017

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

14-
hi link httpResultComment Comment
15-
hi link httpResultTitle Type
16-
hi link httpResultPath Title
17-
hi link httpResultField Variable
18-
hi link httpResultString String
19-
hi link httpResultNumber Number
21+
hi link httpResultComment Comment
22+
hi link httpResultTitle Type
23+
hi link httpResultPath httpTSURI
24+
hi link httpResultField Identifier
25+
hi link httpResultDateField Identifier
26+
hi link httpResultDate String
27+
hi link httpResultString String
28+
hi link httpResultNumber Number
29+
hi link httpResultHeader Type
30+
hi link httpResult200 String
31+
hi link httpResult300 Function
32+
hi link httpResult400 Number
33+
hi link httpResult500 Number
2034

2135
let b:current_syntax = "httpResult"

0 commit comments

Comments
 (0)