@@ -16,6 +16,19 @@ local config = require("rest-nvim.config")
1616--- @field __TYPE BodyType
1717--- @field data any
1818
19+ local NAMED_REQUEST_QUERY = vim .treesitter .query .parse (" http" , [[
20+ (section
21+ (request_separator
22+ value: (_) @name)
23+ request: (_)) @request
24+ (section
25+ (comment
26+ name: (_) @keyword
27+ value: (_) @name
28+ (#eq? @keyword "name"))
29+ request: (_)) @request
30+ ]] )
31+
1932--- @param node TSNode
2033--- @param field string
2134--- @param source Source
@@ -105,7 +118,6 @@ function M.parse_body(body_node, source, context)
105118 name = get_node_field_text (body_node , " name" , source ),
106119 path = path ,
107120 }
108- vim .print (body .data .path )
109121 elseif body .__TYPE == " graphql" then
110122 logger .error (" graphql body is not supported yet" )
111123 end
@@ -129,10 +141,16 @@ function M.create_context(source)
129141 return ctx
130142end
131143
132- --- @return TSNode ? node TSNode with type ` request_section `
144+ --- @return TSNode ? node TSNode with type ` section `
133145function M .get_cursor_request_node ()
134146 local node = vim .treesitter .get_node ()
135- return node and utils .ts_find (node , " request_section" )
147+ if node then
148+ node = utils .ts_find (node , " section" )
149+ if not node or # node :field (" request" ) < 1 then
150+ return
151+ end
152+ end
153+ return node
136154end
137155
138156--- @return TSNode[]
@@ -148,6 +166,22 @@ function M.get_all_request_node()
148166 return reqs
149167end
150168
169+ --- @return TSNode ?
170+ function M .get_request_node_by_name (name )
171+ local source = 0
172+ local _ , tree = utils .ts_parse_source (source )
173+ local query = NAMED_REQUEST_QUERY
174+ for id , node , _metadata , _match in query :iter_captures (tree :root (), source ) do
175+ local capture_name = query .captures [id ]
176+ if capture_name == " name" and vim .treesitter .get_node_text (node , source ) == name then
177+ local find = utils .ts_find (node , " section" )
178+ if find then
179+ return find
180+ end
181+ end
182+ end
183+ end
184+
151185--- @param vd_node TSNode
152186--- @param source Source
153187--- @param ctx Context
@@ -186,34 +220,19 @@ function M.parse_request_handler(handler_node, source, context)
186220 return script .load_handler (str , context )
187221end
188222
189- --- @param node TSNode
190- --- @param kind ? string[]
191- --- @return TSNode[] siblings
192- local function collect_prev_siblings (node , kind )
193- local siblings = {}
194- local n = node :prev_named_sibling ()
195- while n and n :type () ~= " request_separator" do
196- if not kind or vim .tbl_contains (kind , n :type ()) then
197- table.insert (siblings , 0 , n )
198- end
199- n = n :prev_named_sibling ()
200- end
201- return siblings
202- end
203-
204- --- @param node TSNode
205- --- @param kind ? string[]
206- --- @return TSNode[] siblings
207- local function collect_next_siblings (node , kind )
208- local siblings = {}
209- local n = node :next_named_sibling ()
210- while n and n :type () ~= " request_separator" do
211- if not kind or vim .tbl_contains (kind , n :type ()) then
212- table.insert (siblings , n )
223+ --- @param source Source
224+ --- @return string[]
225+ function M .get_request_names (source )
226+ local _ , tree = utils .ts_parse_source (source )
227+ local query = NAMED_REQUEST_QUERY
228+ local result = {}
229+ for id , node , _metadata , _match in query :iter_captures (tree :root (), source ) do
230+ local capture_name = query .captures [id ]
231+ if capture_name == " name" then
232+ table.insert (result , vim .treesitter .get_node_text (node , source ))
213233 end
214- n = n :next_named_sibling ()
215234 end
216- return siblings
235+ return result
217236end
218237
219238--- Parse the request node and create Request object. Returns `nil` if parsing
223242--- @param context ? Context
224243--- @return Request | nil
225244function M .parse (node , source , context )
226- assert (node :type () == " request_section " )
245+ assert (node :type () == " section " )
227246 context = context or Context :new ()
228247 -- request should not include error
229248 if node :has_error () then
@@ -235,11 +254,14 @@ function M.parse(node, source, context)
235254 logger .error (" request section doesn't have request node" )
236255 return nil
237256 end
257+ local body
238258 local body_node = req_node :field (" body" )[1 ]
239- local body = body_node and M .parse_body (body_node , source , context )
240- if body_node and not body then
241- logger .error (" parsing body failed" )
242- return nil
259+ if body_node then
260+ body = M .parse_body (body_node , source , context )
261+ if not body then
262+ logger .error (" parsing body failed" )
263+ return nil
264+ end
243265 end
244266 local method = get_node_field_text (req_node , " method" , source )
245267 if not method then
@@ -252,15 +274,19 @@ function M.parse(node, source, context)
252274 config .encode_url and utils .escape or nil
253275 )
254276
255- -- FIXME: use query instead
256- local pre_script_nodes = collect_prev_siblings (req_node , {" pre_request_script" })
257- for _ , script_node in ipairs (pre_script_nodes ) do
258- M .parse_pre_request_script (script_node , source , context )
277+ local name
278+ local handlers = {}
279+ for child , _ in node :iter_children () do
280+ local node_type = child :type ()
281+ if node_type == " pre_request_script" then
282+ M .parse_pre_request_script (child , source , context )
283+ elseif node_type == " res_handler_script" then
284+ table.insert (handlers , M .parse_request_handler (child , source , context ))
285+ elseif node_type == " request_separator" then
286+ name = get_node_field_text (child , " value" , source )
287+ end
259288 end
260- local handler_nodes = collect_next_siblings (req_node , {" res_handler_script" })
261- local handlers = vim .iter (handler_nodes ):map (function (n )
262- return M .parse_request_handler (n , source , context )
263- end ):totable ()
289+
264290 local headers = parse_headers (req_node , source , context )
265291 -- HACK: check if url doesn't have host
266292 if headers [" host" ] and url [1 ] == " /" then
@@ -269,6 +295,7 @@ function M.parse(node, source, context)
269295 end
270296 --- @type Request
271297 return {
298+ name = name ,
272299 context = context ,
273300 method = method ,
274301 url = url ,
0 commit comments