@@ -32,15 +32,6 @@ local NAMED_REQUEST_QUERY = vim.treesitter.query.parse(
3232]]
3333)
3434
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-
4435--- @param src string
4536--- @param context rest.Context
4637--- @param encoder ? fun ( s : string ): string
@@ -67,8 +58,8 @@ local function parse_headers(req_node, source, context)
6758 end )
6859 local header_nodes = req_node :field (" header" )
6960 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 )
7263 key = expand_variables (key , context ):lower ()
7364 if value then
7465 value = expand_variables (value , context )
@@ -110,6 +101,7 @@ local function parse_urlencoded_form(str)
110101 logger .error ((" Error while parsing query '%s' from urlencoded form '%s'" ):format (query_pairs , str ))
111102 return nil
112103 end
104+ -- TODO: encode value here
113105 return vim .trim (key ) .. " =" .. vim .trim (value )
114106 end )
115107 :join (" &" )
@@ -126,7 +118,7 @@ function parser.parse_body(content_type, body_node, source, context)
126118 --- @cast body rest.Request.Body
127119 if node_type == " external_body" then
128120 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 ))
130122 if type (source ) ~= " number" then
131123 logger .error (" can't parse external body on non-existing http file" )
132124 return
@@ -137,7 +129,7 @@ function parser.parse_body(content_type, body_node, source, context)
137129 basepath = basepath :gsub (" ^" .. vim .pesc (vim .uv .cwd () .. " /" ), " " )
138130 path = vim .fs .normalize (vim .fs .joinpath (basepath , path ))
139131 body .data = {
140- name = get_node_field_text (body_node , " name" , source ),
132+ name = utils . ts_field_text (body_node , " name" , source ),
141133 path = path ,
142134 }
143135 elseif node_type == " json_body" or content_type == " application/json" then
252244--- @param ctx rest.Context
253245function parser .parse_variable_declaration (vd_node , source , ctx )
254246 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 )))
257249 value = expand_variables (value , ctx )
258250 ctx :set_global (name , value )
259251end
265257local function parse_script (node , source )
266258 local lang = " javascript"
267259 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 )
270262 if value then
271263 lang = value
272264 end
@@ -369,7 +361,7 @@ function parser.parse(node, source, ctx)
369361 local start_row = node :range ()
370362 parser .eval_context (source , ctx , start_row )
371363 end
372- local method = get_node_field_text (req_node , " method" , source )
364+ local method = utils . ts_field_text (req_node , " method" , source )
373365 if not method then
374366 logger .info (" no method provided, falling back to 'GET'" )
375367 method = " GET"
@@ -383,7 +375,7 @@ function parser.parse(node, source, ctx)
383375 for child , _ in node :iter_children () do
384376 local child_type = child :type ()
385377 if child_type == " request" then
386- url = expand_variables (assert (get_node_field_text (req_node , " url" , source )), ctx , utils .escape )
378+ url = expand_variables (assert (utils . ts_field_text (req_node , " url" , source )), ctx , utils .escape )
387379 url = url :gsub (" \n %s+" , " " )
388380 elseif child_type == " pre_request_script" then
389381 parser .parse_pre_request_script (child , source , ctx )
@@ -393,9 +385,9 @@ function parser.parse(node, source, ctx)
393385 table.insert (handlers , handler )
394386 end
395387 elseif child_type == " request_separator" then
396- name = get_node_field_text (child , " value" , source )
397- elseif child_type == " comment" and get_node_field_text (child , " name" , source ) == " name" then
398- name = get_node_field_text (child , " value" , source ) or name
388+ name = utils . ts_field_text (child , " value" , source )
389+ elseif child_type == " comment" and utils . ts_field_text (child , " name" , source ) == " name" then
390+ name = utils . ts_field_text (child , " value" , source ) or name
399391 elseif child_type == " variable_declaration" then
400392 parser .parse_variable_declaration (child , source , ctx )
401393 end
@@ -449,7 +441,7 @@ function parser.parse(node, source, ctx)
449441 name = name ,
450442 method = method ,
451443 url = url ,
452- http_version = get_node_field_text (req_node , " version" , source ),
444+ http_version = utils . ts_field_text (req_node , " version" , source ),
453445 headers = headers ,
454446 cookies = {},
455447 body = body ,
0 commit comments