Skip to content

Commit 86c7598

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
Refactor http result syntax highlights
1 parent b98ee9e commit 86c7598

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

lua/rest-nvim/curl/init.lua

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ local function create_callback(method, url)
9595
res.body = vim.fn.system(formatter, res.body):gsub("\n$", "")
9696
end
9797

98+
-- append response container
99+
res.body = "#+RESPONSE\n" .. res.body .. "\n#+END"
100+
98101
local lines = utils.split(res.body, "\n")
99102
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
100103
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)
@@ -117,22 +120,16 @@ local function create_callback(method, url)
117120
utils.move_cursor(res_bufnr, 1)
118121

119122
-- add syntax highlights for response
120-
if content_type == "json" then
121-
vim.cmd([[
122-
unlet b:current_syntax
123-
syn include @json syntax/json.vim
124-
syn region jsonBody start="\v\{" end="\v\}$" contains=@json
123+
local syntax_file = vim.fn.expand(string.format("$VIMRUNTIME/syntax/%s.vim", content_type))
125124

126-
let b:current_syntax = "httpResult"
127-
]])
128-
elseif content_type == "html" then
129-
vim.cmd([[
125+
if vim.fn.filereadable(syntax_file) == 1 then
126+
vim.cmd(string.gsub([[
130127
unlet b:current_syntax
131-
syn include @html syntax/html.vim
132-
syn region htmlBody start=+<html.*>+ end=+<\/ *html>+ contains=@html
128+
syn include @%s syntax/%s.vim
129+
syn region %sBody matchgroup=Comment start=+\v^#\+RESPONSE$+ end=+\v^#\+END$+ contains=@%s
133130
134131
let b:current_syntax = "httpResult"
135-
]])
132+
]], "%%s",content_type))
136133
end
137134
end
138135
end
@@ -165,7 +162,7 @@ M.curl_cmd = function(opts)
165162
vim.cmd("let @+=" .. string.format("%q", curl_cmd))
166163
end
167164

168-
log.debug("[rest.nvim] Request preview:\n" .. curl_cmd)
165+
vim.api.nvim_echo({{"[rest.nvim] Request preview:\n", "Comment"}, {curl_cmd}}, false, {})
169166
return
170167
else
171168
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url))

syntax/httpResult.vim

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ syn match httpResultComment "\v^#.*$"
44
syn keyword httpResultTitle GET POST PATCH PUT HEAD DELETE nextgroup=httpResultPath
55
syn match httpResultPat /.*$/ contained
66

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
7+
syn match httpResultField /^\(\w\)[^:]\+:/he=e-1
8+
syn match httpResultDateField /^[Dd]ate:/he=e-1 nextgroup=httpResultDate
9+
syn match httpResultDateField /^[Ee]xpires:/he=e-1 nextgroup=httpResultDate
10+
syn match httpResultDate /.*$/ contained
1111

1212
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
13+
syn match httpResult200 /2\d\d.*$/ contained
14+
syn match httpResult300 /3\d\d.*$/ contained
15+
syn match httpResult400 /4\d\d.*$/ contained
16+
syn match httpResult500 /5\d\d.*$/ contained
1717

1818
syn region httpResultString start=/\vr?"/ end=/\v"/
19-
syn match httpResultNumber /\v[ =]@1<=\d+[ \n]/
19+
syn match httpResultNumber /\v[ =]@1<=[0-9]*.?[0-9]+[ ,;&\n]/he=e-1
2020

2121
hi link httpResultComment Comment
2222
hi link httpResultTitle Type

0 commit comments

Comments
 (0)