1- --- @mod rest-nvim.autocmds rest.nvim autocommands
2- ---
3- --- @mod rest-nvim.autocmds rest.nvim autocommands
1+ --- @mod rest-nvim.events rest.nvim user events
42---
53--- @brief [[
64---
@@ -48,51 +46,51 @@ local autocmds = {}
4846--- Set up Rest autocommands group
4947--- @package
5048function autocmds .setup ()
51- vim .api .nvim_create_augroup (" Rest" , { clear = true })
49+ vim .api .nvim_create_augroup (" Rest" , { clear = true })
5250
53- vim .api .nvim_create_autocmd (" User" , {
54- pattern = " RestRequestPre" ,
55- callback = function (_ev )
56- local config = require (" rest-nvim.config" )
57- local utils = require (" rest-nvim.utils" )
58- local req = _G .rest_request
59- local hooks = config .request .hooks
60- if hooks .encode_url then
61- req .url = utils .escape (req .url , true )
62- end
63- if hooks .user_agent ~= " " then
64- local header_empty = not req .headers [" user-agent" ] or # req .headers [" user-agent" ] < 1
65- if header_empty then
66- local user_agent = type (hooks .user_agent ) == " function" and hooks .user_agent () or hooks .user_agent
67- --- @cast user_agent string
68- req .headers [" user-agent" ] = { user_agent }
69- end
70- end
71- if hooks .set_content_type then
72- local header_empty = not req .headers [" content-type" ] or # req .headers [" content-type" ] < 1
73- if header_empty and req .body then
74- if req .body .__TYPE == " json" then
75- req .headers [" content-type" ] = { " application/json" }
76- elseif req .body .__TYPE == " xml" then
77- req .headers [" content-type" ] = { " application/xml" }
78- -- TODO: auto-set content-type header for external body
79- end
80- end
81- end
82- end
83- })
84- vim .api .nvim_create_autocmd (" User" , {
85- pattern = " RestResponsePre" ,
86- callback = function (_ev )
87- local config = require (" rest-nvim.config" )
88- local utils = require (" rest-nvim.utils" )
89- local req = _G .rest_request
90- local _res = _G .rest_response
91- if config .response .hooks .decode_url then
92- req .url = utils .url_decode (req .url )
93- end
94- end
95- })
51+ vim .api .nvim_create_autocmd (" User" , {
52+ pattern = " RestRequestPre" ,
53+ callback = function (_ev )
54+ local config = require (" rest-nvim.config" )
55+ local utils = require (" rest-nvim.utils" )
56+ local req = _G .rest_request
57+ local hooks = config .request .hooks
58+ if hooks .encode_url then
59+ req .url = utils .escape (req .url , true )
60+ end
61+ if hooks .user_agent ~= " " then
62+ local header_empty = not req .headers [" user-agent" ] or # req .headers [" user-agent" ] < 1
63+ if header_empty then
64+ local user_agent = type (hooks .user_agent ) == " function" and hooks .user_agent () or hooks .user_agent
65+ --- @cast user_agent string
66+ req .headers [" user-agent" ] = { user_agent }
67+ end
68+ end
69+ if hooks .set_content_type then
70+ local header_empty = not req .headers [" content-type" ] or # req .headers [" content-type" ] < 1
71+ if header_empty and req .body then
72+ if req .body .__TYPE == " json" then
73+ req .headers [" content-type" ] = { " application/json" }
74+ elseif req .body .__TYPE == " xml" then
75+ req .headers [" content-type" ] = { " application/xml" }
76+ -- TODO: auto-set content-type header for external body
77+ end
78+ end
79+ end
80+ end ,
81+ })
82+ vim .api .nvim_create_autocmd (" User" , {
83+ pattern = " RestResponsePre" ,
84+ callback = function (_ev )
85+ local config = require (" rest-nvim.config" )
86+ local utils = require (" rest-nvim.utils" )
87+ local req = _G .rest_request
88+ local _res = _G .rest_response
89+ if config .response .hooks .decode_url then
90+ req .url = utils .url_decode (req .url )
91+ end
92+ end ,
93+ })
9694end
9795
9896--- Register a new autocommand in the `Rest` augroup
@@ -104,28 +102,28 @@ end
104102--- @param description string Autocommand description
105103--- @package
106104function autocmds .register_autocmd (events , cb , description )
107- vim .validate ({
108- events = { events , " table" },
109- cb = { cb , { " function" , " string" } },
110- description = { description , " string" },
111- })
105+ vim .validate ({
106+ events = { events , " table" },
107+ cb = { cb , { " function" , " string" } },
108+ description = { description , " string" },
109+ })
112110
113- local autocmd_opts = {
114- group = vim .api .nvim_create_augroup (" Rest" , { clear = false }),
115- desc = description ,
116- }
111+ local autocmd_opts = {
112+ group = vim .api .nvim_create_augroup (" Rest" , { clear = false }),
113+ desc = description ,
114+ }
117115
118- if type (cb ) == " function" then
119- autocmd_opts = vim .tbl_deep_extend (" force" , autocmd_opts , {
120- callback = cb ,
121- })
122- elseif type (cb ) == " string" then
123- autocmd_opts = vim .tbl_deep_extend (" force" , autocmd_opts , {
124- command = cb ,
125- })
126- end
116+ if type (cb ) == " function" then
117+ autocmd_opts = vim .tbl_deep_extend (" force" , autocmd_opts , {
118+ callback = cb ,
119+ })
120+ elseif type (cb ) == " string" then
121+ autocmd_opts = vim .tbl_deep_extend (" force" , autocmd_opts , {
122+ command = cb ,
123+ })
124+ end
127125
128- vim .api .nvim_create_autocmd (events , autocmd_opts )
126+ vim .api .nvim_create_autocmd (events , autocmd_opts )
129127end
130128
131129return autocmds
0 commit comments