@@ -23,12 +23,18 @@ local NAMED_REQUEST_QUERY = vim.treesitter.query.parse("http", [[
2323 request: (_)) @request
2424(section
2525 (comment
26- name: (_) @keyword
26+ name: (_) @_keyword
2727 value: (_) @name
28- (#eq? @keyword "name"))
28+ (#eq? @_keyword "name"))
2929 request: (_)) @request
3030]] )
3131
32+ local IN_PLACE_VARIABLE_QUERY = vim .treesitter .query .parse (" http" , [[
33+ (section
34+ !request
35+ (variable_declaration)+ @inplace_variable)
36+ ]] )
37+
3238--- @param node TSNode
3339--- @param field string
3440--- @param source Source
@@ -96,7 +102,7 @@ local function parse_urlencoded_form(str)
96102 return vim .iter (query_pairs ):map (function (query )
97103 local key , value = query :match (" ([^=]+)=?(.*)" )
98104 if not key then
99- -- TODO: error
105+ logger . error (( " Error while parsing query '%s' from urlencoded form '%s' " ): format ( query_pairs , str ))
100106 return nil
101107 end
102108 return vim .trim (key ) .. " =" .. vim .trim (value )
@@ -147,13 +153,12 @@ function parser.parse_body(content_type, body_node, source, context)
147153 return nil
148154 end
149155 elseif node_type == " raw_body" then
150- -- TODO: exclude comments from text
151156 local text = vim .treesitter .get_node_text (body_node , source )
152157 if content_type and vim .startswith (content_type , " application/x-www-form-urlencoded" ) then
153158 body .__TYPE = " raw"
154159 body .data = parse_urlencoded_form (text )
155160 if not body .data then
156- -- TODO: parsing urlencoded form failed
161+ logger . error ( " Error while parsing urlencoded form" )
157162 return nil
158163 end
159164 else
@@ -170,16 +175,13 @@ function parser.parse_body(content_type, body_node, source, context)
170175 return body
171176end
172177
173- local IN_PLACE_VARIABLE_QUERY = " (variable_declaration) @inplace_variable"
174-
175178--- parse all in-place variables from source
176179--- @param source Source
177180--- @return rest.Context ctx
178181function parser .create_context (source )
179- local query = vim . treesitter . query . parse ( " http " , IN_PLACE_VARIABLE_QUERY )
182+ local query = IN_PLACE_VARIABLE_QUERY
180183 local ctx = Context :new ()
181184 local _ , tree = utils .ts_parse_source (source )
182- -- TODO: capture variable_decalarations in section without request
183185 for _ , node in query :iter_captures (tree :root (), source ) do
184186 if node :type () == " variable_declaration" then
185187 parser .parse_variable_declaration (node , source , ctx )
@@ -213,7 +215,7 @@ function parser.get_all_request_nodes(source)
213215 local _ , tree = utils .ts_parse_source (source )
214216 local result = {}
215217 for node , _ in tree :root ():iter_children () do
216- if node :type () == " section" then
218+ if node :type () == " section" and # node : field ( " request " ) > 0 then
217219 table.insert (result , node )
218220 end
219221 end
@@ -327,28 +329,34 @@ function parser.parse(node, source, ctx)
327329 logger .info (" no method provided, falling back to 'GET'" )
328330 method = " GET"
329331 end
330- local url = expand_variables (
331- assert (get_node_field_text (req_node , " url" , source )),
332- ctx ,
333- utils .escape
334- )
335- url = url :gsub (" \n %s+" , " " )
332+ -- NOTE: url will be parsed after because in-place variables should be parsed
333+ -- first
334+ local url
336335
337336 local name
338337 local handlers = {}
339338 for child , _ in node :iter_children () do
340- local node_type = child :type ()
341- if node_type == " pre_request_script" then
339+ local child_type = child :type ()
340+ if child_type == " request" then
341+ url = expand_variables (
342+ assert (get_node_field_text (req_node , " url" , source )),
343+ ctx ,
344+ utils .escape
345+ )
346+ url = url :gsub (" \n %s+" , " " )
347+ elseif child_type == " pre_request_script" then
342348 parser .parse_pre_request_script (child , source , ctx )
343- elseif node_type == " res_handler_script" then
349+ elseif child_type == " res_handler_script" then
344350 local handler = parser .parse_request_handler (child , source , ctx )
345351 if handler then
346352 table.insert (handlers , handler )
347353 end
348- elseif node_type == " request_separator" then
354+ elseif child_type == " request_separator" then
349355 name = get_node_field_text (child , " value" , source )
350- elseif node_type == " comment" and get_node_field_text (child , " name" , source ) == " name" then
356+ elseif child_type == " comment" and get_node_field_text (child , " name" , source ) == " name" then
351357 name = get_node_field_text (child , " value" , source ) or name
358+ elseif child_type == " variable_declaration" then
359+ parser .parse_variable_declaration (child , source , ctx )
352360 end
353361 end
354362 if not name then
0 commit comments