-
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathinit.lua
More file actions
464 lines (436 loc) · 15.2 KB
/
init.lua
File metadata and controls
464 lines (436 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
---@mod rest-nvim.parser rest.nvim http syntax parsing module
---
---@brief [[
---
--- rest.nvim `.http` syntax parsing module.
--- rest.nvim uses `tree-sitter-http` as a core parser for `.http` syntax
---
---@brief ]]
local parser = {}
local Context = require("rest-nvim.context").Context
local utils = require("rest-nvim.utils")
local logger = require("rest-nvim.logger")
local jar = require("rest-nvim.cookie_jar")
---@alias Source integer|string Buffer or string which the `node` is extracted
local NAMED_REQUEST_QUERY = vim.treesitter.query.parse(
"http",
[[
(section
(request_separator
value: (_) @name)
request: (_)) @request
(section
(comment
name: (_) @_keyword
value: (_) @name
(#eq? @_keyword "name"))
request: (_)) @request
]]
)
---@param node TSNode
---@param field string
---@param source Source
---@return string|nil
local function get_node_field_text(node, field, source)
local n = node:field(field)[1]
return n and vim.treesitter.get_node_text(n, source) or nil
end
---@param src string
---@param context rest.Context
---@param encoder? fun(s:string):string
---@return string
---@return integer
local function expand_variables(src, context, encoder)
return src:gsub("{{(.-)}}", function(name)
name = vim.trim(name)
local res = context:resolve(name)
if encoder then
res = encoder(res)
end
return res
end)
end
---@param req_node TSNode Tree-sitter request node
---@param source Source
---@param context rest.Context
---@return table<string,string[]> headers
local function parse_headers(req_node, source, context)
local headers = vim.defaulttable(function()
return {}
end)
local header_nodes = req_node:field("header")
for _, node in ipairs(header_nodes) do
local key = assert(get_node_field_text(node, "name", source))
local value = get_node_field_text(node, "value", source)
key = expand_variables(key, context):lower()
if value then
value = expand_variables(value, context)
table.insert(headers[key], value)
else
headers[key] = {}
end
end
return setmetatable(headers, nil)
end
---@param str string
---@return boolean
local function validate_json(str)
local ok, _ = pcall(vim.json.decode, str)
return ok
end
---@param str string
---@return boolean
local function validate_xml(str)
local xml2lua = require("xml2lua")
local handler = require("xmlhandler.tree"):new()
local xml_parser = xml2lua.parser(handler)
local ok = pcall(function(t)
return xml_parser:parse(t)
end, str)
return ok
end
---@param str string
---@return string?
local function parse_urlencoded_form(str)
local query_pairs = vim.split(str, "&")
return vim.iter(query_pairs)
:map(function(query)
local key, value = query:match("([^=]+)=?(.*)")
if not key then
logger.error(("Error while parsing query '%s' from urlencoded form '%s'"):format(query_pairs, str))
return nil
end
return vim.trim(key) .. "=" .. vim.trim(value)
end)
:join("&")
end
---@param content_type string?
---@param body_node TSNode
---@param source Source
---@param context rest.Context
---@return rest.Request.Body|nil
function parser.parse_body(content_type, body_node, source, context)
local body = {}
local node_type = body_node:type()
---@cast body rest.Request.Body
if node_type == "external_body" then
body.__TYPE = "external"
local path = assert(get_node_field_text(body_node, "path", source))
if type(source) ~= "number" then
logger.error("can't parse external body on non-existing http file")
return
end
---@cast source integer
local basepath = vim.fs.dirname(vim.api.nvim_buf_get_name(source))
---@diagnostic disable-next-line: undefined-field
basepath = basepath:gsub("^" .. vim.pesc(vim.uv.cwd() .. "/"), "")
path = vim.fs.normalize(vim.fs.joinpath(basepath, path))
body.data = {
name = get_node_field_text(body_node, "name", source),
path = path,
}
elseif node_type == "json_body" or content_type == "application/json" then
body.__TYPE = "json"
body.data = vim.trim(vim.treesitter.get_node_text(body_node, source))
body.data = expand_variables(body.data, context)
local ok = validate_json(body.data)
if not ok then
logger.warn("invalid json: '" .. body.data .. "'")
return nil
end
elseif node_type == "xml_body" or content_type == "application/xml" then
body.__TYPE = "xml"
body.data = vim.trim(vim.treesitter.get_node_text(body_node, source))
body.data = expand_variables(body.data, context)
local ok = validate_xml(body.data)
if not ok then
logger.warn("invalid xml: '" .. body.data .. "'")
return nil
end
elseif node_type == "raw_body" then
local text = vim.treesitter.get_node_text(body_node, source)
if content_type and vim.startswith(content_type, "application/x-www-form-urlencoded") then
body.__TYPE = "raw"
body.data = parse_urlencoded_form(text)
if not body.data then
logger.error("Error while parsing urlencoded form")
return nil
end
else
body.__TYPE = "raw"
body.data = text
end
elseif node_type == "multipart_form_data" then
body.__TYPE = "multipart_form_data"
-- TODO:
logger.error("multipart form data is not supported yet")
elseif node_type == "graphql_body" then
logger.error("graphql body is not supported yet")
end
return body
end
---In-place variables can be evaluated in loaded buffers due to treesitter limitations
---@param source integer
---@param ctx rest.Context
---@param endline number zero-based line number
function parser.eval_context(source, ctx, endline)
vim.validate("source", source, "number")
local startline = ctx.linenr
for ln = startline, endline do
local start_node = vim.treesitter.get_node({ pos = { ln, 0 } })
if start_node then
local node = utils.ts_find(start_node, "variable_declaration", true)
if node then
parser.parse_variable_declaration(node, source, ctx)
end
end
end
end
---@return TSNode? node TSNode with type `section`
function parser.get_request_node_by_cursor()
local node = vim.treesitter.get_node()
if node then
node = utils.ts_find(node, "section")
if not node then
logger.error("can't find request section node")
return
elseif node:has_error() then
logger.error(utils.ts_node_error_log(node))
return
elseif #node:field("request") < 1 then
logger.error("request section doesn't have request node")
return
end
end
return node
end
---@param source Source
---@return TSNode[]
function parser.get_all_request_nodes(source)
local _, tree = utils.ts_parse_source(source)
local result = {}
for node, _ in tree:root():iter_children() do
if node:type() == "section" and #node:field("request") > 0 then
table.insert(result, node)
end
end
return result
end
---@return TSNode?
function parser.get_request_node_by_name(name)
local source = 0
local _, tree = utils.ts_parse_source(source)
local query = NAMED_REQUEST_QUERY
for id, node, _metadata, _match in query:iter_captures(tree:root(), source) do
local capture_name = query.captures[id]
if capture_name == "name" and vim.treesitter.get_node_text(node, source) == name then
local find = utils.ts_find(node, "section")
if find then
return find
end
end
end
end
---@param vd_node TSNode
---@param source Source
---@param ctx rest.Context
function parser.parse_variable_declaration(vd_node, source, ctx)
vim.validate({ node = utils.ts_node_spec(vd_node, "variable_declaration") })
local name = assert(get_node_field_text(vd_node, "name", source))
local value = vim.trim(assert(get_node_field_text(vd_node, "value", source)))
value = expand_variables(value, ctx)
ctx:set_global(name, value)
end
---@param node TSNode
---@param source Source
---@return string lang
---@return string str
local function parse_script(node, source)
local lang = "javascript"
local prev_node = utils.ts_upper_node(node)
if prev_node and prev_node:type() == "comment" and get_node_field_text(prev_node, "name", source) == "lang" then
local value = get_node_field_text(prev_node, "value", source)
if value then
lang = value
end
end
local script_node = assert(node:named_child(0))
local str = vim.treesitter.get_node_text(script_node, source):sub(3, -3)
return lang, str
end
---@param node TSNode
---@param source Source
---@param context rest.Context
function parser.parse_pre_request_script(node, source, context)
local lang, str = parse_script(node, source)
local ok, script = pcall(require, "rest-nvim.script." .. lang)
if not ok then
logger.error(("failed to load script with language '%s'. Can't find script runner client."):format(lang))
return
end
---@cast script rest.ScriptClient
script.load_pre_req_hook(str, context)()
end
---@param node TSNode
---@param source Source
---@param context rest.Context
---@return function?
function parser.parse_request_handler(node, source, context)
local lang, str = parse_script(node, source)
local ok, script = pcall(require, "rest-nvim.script." .. lang)
if not ok then
logger.error(("failed to load script with language '%s'. Can't find script runner client."):format(lang))
return
end
---@cast script rest.ScriptClient
return script.load_post_req_hook(str, context)
end
---@param source Source
---@return string[]
function parser.get_request_names(source)
local _, tree = utils.ts_parse_source(source)
local query = NAMED_REQUEST_QUERY
local result = {}
for id, node, _metadata, _match in query:iter_captures(tree:root(), source) do
local capture_name = query.captures[id]
if capture_name == "name" then
table.insert(result, vim.treesitter.get_node_text(node, source))
end
end
return result
end
---@param name string|nil
---@return TSNode?
function parser.get_request_node(name)
local req_node
if not name then
req_node = parser.get_request_node_by_cursor()
if not req_node then
logger.error("Failed to find request at cursor position")
vim.notify(
"Failed to find request at cursor position. See `:Rest logs` for more info.",
vim.log.levels.ERROR,
{ title = "rest.nvim" }
)
return
end
else
req_node = parser.get_request_node_by_name(name)
if not req_node then
logger.error("Failed to find request by name: " .. name)
vim.notify(
"Failed to find request by name: " .. name .. ". See `:Rest logs` for more info.",
vim.log.levels.ERROR,
{ title = "rest.nvim" }
)
return
end
end
return req_node
end
---Parse the request node and create Request object. Returns `nil` if parsing
---failed.
---@param node TSNode Tree-sitter request node
---@param source Source
---@param ctx? rest.Context
---@return rest.Request|nil
function parser.parse(node, source, ctx)
assert(node:type() == "section")
assert(not node:has_error())
local req_node = node:field("request")[1]
assert(req_node)
ctx = ctx or Context:new()
-- TODO: note that in-place variables won't be evaluated due to treesitter limitations
-- when source is given as raw string
if type(source) == "number" then
local start_row = node:range()
parser.eval_context(source, ctx, start_row)
end
local method = get_node_field_text(req_node, "method", source)
if not method then
logger.info("no method provided, falling back to 'GET'")
method = "GET"
end
-- NOTE: url will be parsed after because in-place variables should be parsed
-- first
local url
---@type string|nil
local name
local handlers = {}
for child, _ in node:iter_children() do
local child_type = child:type()
if child_type == "request" then
url = expand_variables(assert(get_node_field_text(req_node, "url", source)), ctx, utils.escape)
url = url:gsub("\n%s+", "")
elseif child_type == "pre_request_script" then
parser.parse_pre_request_script(child, source, ctx)
elseif child_type == "res_handler_script" then
local handler = parser.parse_request_handler(child, source, ctx)
if handler then
table.insert(handlers, handler)
end
elseif child_type == "request_separator" then
name = get_node_field_text(child, "value", source)
elseif child_type == "comment" and get_node_field_text(child, "name", source) == "name" then
name = get_node_field_text(child, "value", source) or name
elseif child_type == "variable_declaration" then
parser.parse_variable_declaration(child, source, ctx)
end
end
if not name then
if type(source) == "number" then
local filename = vim.api.nvim_buf_get_name(source)
name = filename:match(".*/%.?(.*).http$") or filename
name = name .. "#" .. vim.b[source]._rest_nvim_count
vim.b[source]._rest_nvim_count = vim.b[source]._rest_nvim_count + 1
end
end
local headers = parse_headers(req_node, source, ctx)
if headers["host"] and vim.startswith(url, "/") then
local host = headers["host"][1]
if not host:match("^https?://") then
local port = host:match(":(%d%d+)$")
local protocol = "http://"
if not port or port == "443" then
protocol = "https://"
end
host = protocol .. host
end
url = host .. url
table.remove(headers["host"], 1)
end
---@type string?
local content_type
if headers["content-type"] and #headers["content-type"] > 0 then
content_type = headers["content-type"][1]:match("([^;]+)")
end
local body
local body_node = req_node:field("body")[1]
if body_node then
body = parser.parse_body(content_type, body_node, source, ctx)
if not body then
logger.error("parsing body failed")
vim.notify(
"parsing request body failed. See `:Rest logs` for more info.",
vim.log.levels.ERROR,
{ title = "rest.nvim" }
)
return nil
end
end
---@type rest.Request
local req = {
name = name,
method = method,
url = url,
http_version = get_node_field_text(req_node, "version", source),
headers = headers,
cookies = {},
body = body,
handlers = handlers,
}
ctx:clear_local()
jar.load_cookies(req)
return req
end
return parser