@@ -23,12 +23,18 @@ local NAMED_REQUEST_QUERY = vim.treesitter.query.parse("http", [[
23
23
request: (_)) @request
24
24
(section
25
25
(comment
26
- name: (_) @keyword
26
+ name: (_) @_keyword
27
27
value: (_) @name
28
- (#eq? @keyword "name"))
28
+ (#eq? @_keyword "name"))
29
29
request: (_)) @request
30
30
]] )
31
31
32
+ local IN_PLACE_VARIABLE_QUERY = vim .treesitter .query .parse (" http" , [[
33
+ (section
34
+ !request
35
+ (variable_declaration)+ @inplace_variable)
36
+ ]] )
37
+
32
38
--- @param node TSNode
33
39
--- @param field string
34
40
--- @param source Source
@@ -96,7 +102,7 @@ local function parse_urlencoded_form(str)
96
102
return vim .iter (query_pairs ):map (function (query )
97
103
local key , value = query :match (" ([^=]+)=?(.*)" )
98
104
if not key then
99
- -- TODO: error
105
+ logger . error (( " Error while parsing query '%s' from urlencoded form '%s' " ): format ( query_pairs , str ))
100
106
return nil
101
107
end
102
108
return vim .trim (key ) .. " =" .. vim .trim (value )
@@ -147,13 +153,12 @@ function parser.parse_body(content_type, body_node, source, context)
147
153
return nil
148
154
end
149
155
elseif node_type == " raw_body" then
150
- -- TODO: exclude comments from text
151
156
local text = vim .treesitter .get_node_text (body_node , source )
152
157
if content_type and vim .startswith (content_type , " application/x-www-form-urlencoded" ) then
153
158
body .__TYPE = " raw"
154
159
body .data = parse_urlencoded_form (text )
155
160
if not body .data then
156
- -- TODO: parsing urlencoded form failed
161
+ logger . error ( " Error while parsing urlencoded form" )
157
162
return nil
158
163
end
159
164
else
@@ -170,16 +175,13 @@ function parser.parse_body(content_type, body_node, source, context)
170
175
return body
171
176
end
172
177
173
- local IN_PLACE_VARIABLE_QUERY = " (variable_declaration) @inplace_variable"
174
-
175
178
--- parse all in-place variables from source
176
179
--- @param source Source
177
180
--- @return rest.Context ctx
178
181
function parser .create_context (source )
179
- local query = vim . treesitter . query . parse ( " http " , IN_PLACE_VARIABLE_QUERY )
182
+ local query = IN_PLACE_VARIABLE_QUERY
180
183
local ctx = Context :new ()
181
184
local _ , tree = utils .ts_parse_source (source )
182
- -- TODO: capture variable_decalarations in section without request
183
185
for _ , node in query :iter_captures (tree :root (), source ) do
184
186
if node :type () == " variable_declaration" then
185
187
parser .parse_variable_declaration (node , source , ctx )
@@ -213,7 +215,7 @@ function parser.get_all_request_nodes(source)
213
215
local _ , tree = utils .ts_parse_source (source )
214
216
local result = {}
215
217
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
217
219
table.insert (result , node )
218
220
end
219
221
end
@@ -327,28 +329,34 @@ function parser.parse(node, source, ctx)
327
329
logger .info (" no method provided, falling back to 'GET'" )
328
330
method = " GET"
329
331
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
336
335
337
336
local name
338
337
local handlers = {}
339
338
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
342
348
parser .parse_pre_request_script (child , source , ctx )
343
- elseif node_type == " res_handler_script" then
349
+ elseif child_type == " res_handler_script" then
344
350
local handler = parser .parse_request_handler (child , source , ctx )
345
351
if handler then
346
352
table.insert (handlers , handler )
347
353
end
348
- elseif node_type == " request_separator" then
354
+ elseif child_type == " request_separator" then
349
355
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
351
357
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 )
352
360
end
353
361
end
354
362
if not name then
0 commit comments