@@ -32,15 +32,6 @@ local NAMED_REQUEST_QUERY = vim.treesitter.query.parse(
32
32
]]
33
33
)
34
34
35
- --- @param node TSNode
36
- --- @param field string
37
- --- @param source Source
38
- --- @return string | nil
39
- local function get_node_field_text (node , field , source )
40
- local n = node :field (field )[1 ]
41
- return n and vim .treesitter .get_node_text (n , source ) or nil
42
- end
43
-
44
35
--- @param src string
45
36
--- @param context rest.Context
46
37
--- @param encoder ? fun ( s : string ): string
@@ -67,8 +58,8 @@ local function parse_headers(req_node, source, context)
67
58
end )
68
59
local header_nodes = req_node :field (" header" )
69
60
for _ , node in ipairs (header_nodes ) do
70
- local key = assert (get_node_field_text (node , " name" , source ))
71
- local value = get_node_field_text (node , " value" , source )
61
+ local key = assert (utils . ts_field_text (node , " name" , source ))
62
+ local value = utils . ts_field_text (node , " value" , source )
72
63
key = expand_variables (key , context ):lower ()
73
64
if value then
74
65
value = expand_variables (value , context )
@@ -110,6 +101,7 @@ local function parse_urlencoded_form(str)
110
101
logger .error ((" Error while parsing query '%s' from urlencoded form '%s'" ):format (query_pairs , str ))
111
102
return nil
112
103
end
104
+ -- TODO: encode value here
113
105
return vim .trim (key ) .. " =" .. vim .trim (value )
114
106
end )
115
107
:join (" &" )
@@ -126,7 +118,7 @@ function parser.parse_body(content_type, body_node, source, context)
126
118
--- @cast body rest.Request.Body
127
119
if node_type == " external_body" then
128
120
body .__TYPE = " external"
129
- local path = assert (get_node_field_text (body_node , " path" , source ))
121
+ local path = assert (utils . ts_field_text (body_node , " path" , source ))
130
122
if type (source ) ~= " number" then
131
123
logger .error (" can't parse external body on non-existing http file" )
132
124
return
@@ -137,7 +129,7 @@ function parser.parse_body(content_type, body_node, source, context)
137
129
basepath = basepath :gsub (" ^" .. vim .pesc (vim .uv .cwd () .. " /" ), " " )
138
130
path = vim .fs .normalize (vim .fs .joinpath (basepath , path ))
139
131
body .data = {
140
- name = get_node_field_text (body_node , " name" , source ),
132
+ name = utils . ts_field_text (body_node , " name" , source ),
141
133
path = path ,
142
134
}
143
135
elseif node_type == " json_body" or content_type == " application/json" then
252
244
--- @param ctx rest.Context
253
245
function parser .parse_variable_declaration (vd_node , source , ctx )
254
246
vim .validate ({ node = utils .ts_node_spec (vd_node , " variable_declaration" ) })
255
- local name = assert (get_node_field_text (vd_node , " name" , source ))
256
- local value = vim .trim (assert (get_node_field_text (vd_node , " value" , source )))
247
+ local name = assert (utils . ts_field_text (vd_node , " name" , source ))
248
+ local value = vim .trim (assert (utils . ts_field_text (vd_node , " value" , source )))
257
249
value = expand_variables (value , ctx )
258
250
ctx :set_global (name , value )
259
251
end
265
257
local function parse_script (node , source )
266
258
local lang = " javascript"
267
259
local prev_node = utils .ts_upper_node (node )
268
- if prev_node and prev_node :type () == " comment" and get_node_field_text (prev_node , " name" , source ) == " lang" then
269
- local value = get_node_field_text (prev_node , " value" , source )
260
+ if prev_node and prev_node :type () == " comment" and utils . ts_field_text (prev_node , " name" , source ) == " lang" then
261
+ local value = utils . ts_field_text (prev_node , " value" , source )
270
262
if value then
271
263
lang = value
272
264
end
@@ -369,7 +361,7 @@ function parser.parse(node, source, ctx)
369
361
local start_row = node :range ()
370
362
parser .eval_context (source , ctx , start_row )
371
363
end
372
- local method = get_node_field_text (req_node , " method" , source )
364
+ local method = utils . ts_field_text (req_node , " method" , source )
373
365
if not method then
374
366
logger .info (" no method provided, falling back to 'GET'" )
375
367
method = " GET"
@@ -384,7 +376,7 @@ function parser.parse(node, source, ctx)
384
376
for child , _ in node :iter_children () do
385
377
local child_type = child :type ()
386
378
if child_type == " request" then
387
- url = expand_variables (assert (get_node_field_text (req_node , " url" , source )), ctx , utils .escape )
379
+ url = expand_variables (assert (utils . ts_field_text (req_node , " url" , source )), ctx , utils .escape )
388
380
url = url :gsub (" \n %s+" , " " )
389
381
elseif child_type == " pre_request_script" then
390
382
parser .parse_pre_request_script (child , source , ctx )
@@ -394,9 +386,9 @@ function parser.parse(node, source, ctx)
394
386
table.insert (handlers , handler )
395
387
end
396
388
elseif child_type == " request_separator" then
397
- name = get_node_field_text (child , " value" , source )
398
- elseif child_type == " comment" and get_node_field_text (child , " name" , source ) == " name" then
399
- name = get_node_field_text (child , " value" , source ) or name
389
+ name = utils . ts_field_text (child , " value" , source )
390
+ elseif child_type == " comment" and utils . ts_field_text (child , " name" , source ) == " name" then
391
+ name = utils . ts_field_text (child , " value" , source ) or name
400
392
elseif child_type == " variable_declaration" then
401
393
parser .parse_variable_declaration (child , source , ctx )
402
394
end
@@ -450,7 +442,7 @@ function parser.parse(node, source, ctx)
450
442
name = name ,
451
443
method = method ,
452
444
url = url ,
453
- http_version = get_node_field_text (req_node , " version" , source ),
445
+ http_version = utils . ts_field_text (req_node , " version" , source ),
454
446
headers = headers ,
455
447
cookies = {},
456
448
body = body ,
0 commit comments