@@ -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--- @return string
@@ -63,8 +54,8 @@ local function parse_headers(req_node, source, context)
6354 end )
6455 local header_nodes = req_node :field (" header" )
6556 for _ , node in ipairs (header_nodes ) do
66- local key = assert (get_node_field_text (node , " name" , source ))
67- local value = get_node_field_text (node , " value" , source )
57+ local key = assert (utils . ts_field_text (node , " name" , source ))
58+ local value = utils . ts_field_text (node , " value" , source )
6859 key = expand_variables (key , context ):lower ()
6960 if value then
7061 value = expand_variables (value , context )
@@ -106,6 +97,7 @@ local function parse_urlencoded_form(str)
10697 logger .error ((" Error while parsing query '%s' from urlencoded form '%s'" ):format (query_pairs , str ))
10798 return nil
10899 end
100+ -- TODO: encode value here
109101 return vim .trim (key ) .. " =" .. vim .trim (value )
110102 end )
111103 :join (" &" )
@@ -122,7 +114,7 @@ function parser.parse_body(content_type, body_node, source, context)
122114 --- @cast body rest.Request.Body
123115 if node_type == " external_body" then
124116 body .__TYPE = " external"
125- local path = assert (get_node_field_text (body_node , " path" , source ))
117+ local path = assert (utils . ts_field_text (body_node , " path" , source ))
126118 if type (source ) ~= " number" then
127119 logger .error (" can't parse external body on non-existing http file" )
128120 return
@@ -133,7 +125,7 @@ function parser.parse_body(content_type, body_node, source, context)
133125 basepath = basepath :gsub (" ^" .. vim .pesc (vim .uv .cwd () .. " /" ), " " )
134126 path = vim .fs .normalize (vim .fs .joinpath (basepath , path ))
135127 body .data = {
136- name = get_node_field_text (body_node , " name" , source ),
128+ name = utils . ts_field_text (body_node , " name" , source ),
137129 path = path ,
138130 }
139131 elseif node_type == " json_body" or content_type == " application/json" then
248240--- @param ctx rest.Context
249241function parser .parse_variable_declaration (vd_node , source , ctx )
250242 vim .validate ({ node = utils .ts_node_spec (vd_node , " variable_declaration" ) })
251- local name = assert (get_node_field_text (vd_node , " name" , source ))
252- local value = vim .trim (assert (get_node_field_text (vd_node , " value" , source )))
243+ local name = assert (utils . ts_field_text (vd_node , " name" , source ))
244+ local value = vim .trim (assert (utils . ts_field_text (vd_node , " value" , source )))
253245 value = expand_variables (value , ctx )
254246 ctx :set_global (name , value )
255247end
261253local function parse_script (node , source )
262254 local lang = " javascript"
263255 local prev_node = utils .ts_upper_node (node )
264- if prev_node and prev_node :type () == " comment" and get_node_field_text (prev_node , " name" , source ) == " lang" then
265- local value = get_node_field_text (prev_node , " value" , source )
256+ if prev_node and prev_node :type () == " comment" and utils . ts_field_text (prev_node , " name" , source ) == " lang" then
257+ local value = utils . ts_field_text (prev_node , " value" , source )
266258 if value then
267259 lang = value
268260 end
@@ -365,7 +357,7 @@ function parser.parse(node, source, ctx)
365357 local start_row = node :range ()
366358 parser .eval_context (source , ctx , start_row )
367359 end
368- local method = get_node_field_text (req_node , " method" , source )
360+ local method = utils . ts_field_text (req_node , " method" , source )
369361 if not method then
370362 logger .info (" no method provided, falling back to 'GET'" )
371363 method = " GET"
@@ -379,7 +371,7 @@ function parser.parse(node, source, ctx)
379371 for child , _ in node :iter_children () do
380372 local child_type = child :type ()
381373 if child_type == " request" then
382- url = expand_variables (assert (get_node_field_text (req_node , " url" , source )), ctx )
374+ url = expand_variables (assert (utils . ts_field_text (req_node , " url" , source )), ctx )
383375 url = url :gsub (" \n %s+" , " " )
384376 elseif child_type == " pre_request_script" then
385377 parser .parse_pre_request_script (child , source , ctx )
@@ -390,9 +382,9 @@ function parser.parse(node, source, ctx)
390382 table.insert (handlers , handler )
391383 end
392384 elseif child_type == " request_separator" then
393- name = get_node_field_text (child , " value" , source )
394- elseif child_type == " comment" and get_node_field_text (child , " name" , source ) == " name" then
395- name = get_node_field_text (child , " value" , source ) or name
385+ name = utils . ts_field_text (child , " value" , source )
386+ elseif child_type == " comment" and utils . ts_field_text (child , " name" , source ) == " name" then
387+ name = utils . ts_field_text (child , " value" , source ) or name
396388 elseif child_type == " variable_declaration" then
397389 parser .parse_variable_declaration (child , source , ctx )
398390 end
@@ -455,7 +447,7 @@ function parser.parse(node, source, ctx)
455447 name = name ,
456448 method = method ,
457449 url = url ,
458- http_version = get_node_field_text (req_node , " version" , source ),
450+ http_version = utils . ts_field_text (req_node , " version" , source ),
459451 headers = headers ,
460452 cookies = {},
461453 body = body ,
0 commit comments