Skip to content

Commit

Permalink
Refactor http result syntax highlights
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 b98ee9e commit 86c7598
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
23 changes: 10 additions & 13 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ local function create_callback(method, url)
res.body = vim.fn.system(formatter, res.body):gsub("\n$", "")
end

-- append response container
res.body = "#+RESPONSE\n" .. res.body .. "\n#+END"

local lines = utils.split(res.body, "\n")
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)
Expand All @@ -117,22 +120,16 @@ local function create_callback(method, url)
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
local syntax_file = vim.fn.expand(string.format("$VIMRUNTIME/syntax/%s.vim", content_type))

let b:current_syntax = "httpResult"
]])
elseif content_type == "html" then
vim.cmd([[
if vim.fn.filereadable(syntax_file) == 1 then
vim.cmd(string.gsub([[
unlet b:current_syntax
syn include @html syntax/html.vim
syn region htmlBody start=+<html.*>+ end=+<\/ *html>+ contains=@html
syn include @%s syntax/%s.vim
syn region %sBody matchgroup=Comment start=+\v^#\+RESPONSE$+ end=+\v^#\+END$+ contains=@%s
let b:current_syntax = "httpResult"
]])
]], "%%s",content_type))
end
end
end
Expand Down Expand Up @@ -165,7 +162,7 @@ M.curl_cmd = function(opts)
vim.cmd("let @+=" .. string.format("%q", curl_cmd))
end

log.debug("[rest.nvim] Request preview:\n" .. curl_cmd)
vim.api.nvim_echo({{"[rest.nvim] Request preview:\n", "Comment"}, {curl_cmd}}, false, {})
return
else
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url))
Expand Down
18 changes: 9 additions & 9 deletions syntax/httpResult.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ syn match httpResultComment "\v^#.*$"
syn keyword httpResultTitle GET POST PATCH PUT HEAD DELETE nextgroup=httpResultPath
syn match httpResultPat /.*$/ contained

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 match httpResultField /^\(\w\)[^:]\+:/he=e-1
syn match httpResultDateField /^[Dd]ate:/he=e-1 nextgroup=httpResultDate
syn match httpResultDateField /^[Ee]xpires:/he=e-1 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 match httpResult200 /2\d\d.*$/ contained
syn match httpResult300 /3\d\d.*$/ contained
syn match httpResult400 /4\d\d.*$/ contained
syn match httpResult500 /5\d\d.*$/ contained

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

hi link httpResultComment Comment
hi link httpResultTitle Type
Expand Down

0 comments on commit 86c7598

Please sign in to comment.