Skip to content

Commit 49c02a2

Browse files
chore: prepare for breaking tree-sitter-http release
1 parent f18149a commit 49c02a2

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

lua/rest-nvim/parser/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ function parser.parse(node, source, ctx)
383383
url = url:gsub("\n%s+", "")
384384
elseif child_type == "pre_request_script" then
385385
parser.parse_pre_request_script(child, source, ctx)
386+
-- won't be a case anymore with latest tree-sitter-http parser. just for backward compatibility
386387
elseif child_type == "res_handler_script" then
387388
local handler = parser.parse_request_handler(child, source, ctx)
388389
if handler then
@@ -396,6 +397,15 @@ function parser.parse(node, source, ctx)
396397
parser.parse_variable_declaration(child, source, ctx)
397398
end
398399
end
400+
for child, _ in req_node:iter_children() do
401+
local child_type = child:type()
402+
if child_type == "res_handler_script" then
403+
local handler = parser.parse_request_handler(child, source, ctx)
404+
if handler then
405+
table.insert(handlers, handler)
406+
end
407+
end
408+
end
399409
if not name then
400410
if type(source) == "number" then
401411
local filename = vim.api.nvim_buf_get_name(source)

queries/http/injections.scm

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,12 @@
1313
(#set! injection.language "graphql"))
1414

1515
; Script (default to javascript)
16-
((script) @injection.content
17-
(#offset! @injection.content 0 2 0 -2)
18-
(#set! injection.language "javascript"))
19-
20-
; Script with other languages
21-
((comment
16+
((#set! injection.language "javascript")
17+
(comment
2218
name: (_) @_name
2319
(#eq? @_name "lang")
24-
value: (_) @injection.language)
25-
.
26-
(_
27-
(script) @injection.content
28-
(#offset! @injection.content 0 2 0 -2)))
29-
30-
; post-request scripts for requests without body
31-
((request
32-
!body
33-
(comment
34-
name: (_) @_name
35-
(#eq? @_name "lang")
36-
value: (_) @injection.language) .)
37-
.
38-
(res_handler_script
39-
(script) @injection.content
40-
(#offset! @injection.content 0 2 0 -2)))
20+
value: (_) @injection.language)?
21+
.
22+
(_
23+
(script) @injection.content
24+
(#offset! @injection.content 0 2 0 -2)))

spec/utils_spec.lua

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@
22

33
local utils = require("rest-nvim.utils")
44

5-
describe("tree-sitter utils", function()
6-
local source = [[
7-
http://localhost:8000
5+
local function open(path)
6+
vim.cmd.edit(path)
7+
return 0
8+
end
89

9-
# @lang=lua
10-
> {%
11-
local json = vim.json.decode(response.body)
12-
json.data = "overwritten"
13-
response.body = vim.json.encode(json)
14-
%}
15-
]]
16-
local script_node
10+
describe("tree-sitter utils", function()
11+
local source = open("spec/examples/script/post_request_script.http")
1712
it("ts_parse_source", function()
1813
local _, tree = utils.ts_parse_source(source)
19-
script_node = assert(tree:root():child(0):child(1))
20-
assert.same("res_handler_script", script_node:type())
14+
local url_node = assert(tree:root():child(0):field("request")[1]:field("url")[1])
15+
assert.same("target_url", url_node:type())
16+
assert.is_false(tree:root():has_error())
2117
end)
2218
it("ts_find", function()
23-
local section_node = assert(utils.ts_find(script_node, "section"))
24-
assert.same("section", section_node:type())
25-
local sr, sc, er, ec = section_node:range()
26-
assert.same({ 0, 0, 8, 0 }, { sr, sc, er, ec })
19+
local start_node = assert(vim.treesitter.get_node({pos={4, 3}, lang="http"}))
20+
local script_node = assert(utils.ts_find(start_node, "script"))
21+
assert.same("script", script_node:type())
22+
local sr, sc, er, ec = script_node:range()
23+
assert.same({ 4, 2, 7, 2 }, { sr, sc, er, ec })
2724
end)
2825
it("ts_upper_node", function()
29-
local comment_node = assert(utils.ts_upper_node(script_node))
26+
local start_node = assert(vim.treesitter.get_node({pos={4, 3}, lang="http"}))
27+
local comment_node = assert(utils.ts_upper_node(start_node))
3028
assert.same("comment", comment_node:type())
3129
end)
3230
end)

0 commit comments

Comments
 (0)