diff --git a/lua/rest-nvim/parser/init.lua b/lua/rest-nvim/parser/init.lua index 57621c7..5550c9b 100644 --- a/lua/rest-nvim/parser/init.lua +++ b/lua/rest-nvim/parser/init.lua @@ -383,6 +383,7 @@ function parser.parse(node, source, ctx) url = url:gsub("\n%s+", "") elseif child_type == "pre_request_script" then parser.parse_pre_request_script(child, source, ctx) + -- won't be a case anymore with latest tree-sitter-http parser. just for backward compatibility elseif child_type == "res_handler_script" then local handler = parser.parse_request_handler(child, source, ctx) if handler then @@ -396,6 +397,15 @@ function parser.parse(node, source, ctx) parser.parse_variable_declaration(child, source, ctx) end end + for child, _ in req_node:iter_children() do + local child_type = child:type() + if child_type == "res_handler_script" then + local handler = parser.parse_request_handler(child, source, ctx) + if handler then + table.insert(handlers, handler) + end + end + end if not name then if type(source) == "number" then local filename = vim.api.nvim_buf_get_name(source) diff --git a/queries/http/injections.scm b/queries/http/injections.scm index f693936..c05d539 100644 --- a/queries/http/injections.scm +++ b/queries/http/injections.scm @@ -13,28 +13,12 @@ (#set! injection.language "graphql")) ; Script (default to javascript) -((script) @injection.content - (#offset! @injection.content 0 2 0 -2) - (#set! injection.language "javascript")) - -; Script with other languages -((comment +((#set! injection.language "javascript") +(comment name: (_) @_name (#eq? @_name "lang") - value: (_) @injection.language) - . - (_ - (script) @injection.content - (#offset! @injection.content 0 2 0 -2))) - -; post-request scripts for requests without body -((request - !body - (comment - name: (_) @_name - (#eq? @_name "lang") - value: (_) @injection.language) .) - . - (res_handler_script - (script) @injection.content - (#offset! @injection.content 0 2 0 -2))) + value: (_) @injection.language)? +. +(_ + (script) @injection.content + (#offset! @injection.content 0 2 0 -2))) diff --git a/spec/utils_spec.lua b/spec/utils_spec.lua index 42015fa..e309f03 100644 --- a/spec/utils_spec.lua +++ b/spec/utils_spec.lua @@ -2,31 +2,29 @@ local utils = require("rest-nvim.utils") -describe("tree-sitter utils", function() - local source = [[ -http://localhost:8000 +local function open(path) + vim.cmd.edit(path) + return 0 +end -# @lang=lua -> {% -local json = vim.json.decode(response.body) -json.data = "overwritten" -response.body = vim.json.encode(json) -%} -]] - local script_node +describe("tree-sitter utils", function() + local source = open("spec/examples/script/post_request_script.http") it("ts_parse_source", function() local _, tree = utils.ts_parse_source(source) - script_node = assert(tree:root():child(0):child(1)) - assert.same("res_handler_script", script_node:type()) + local url_node = assert(tree:root():child(0):field("request")[1]:field("url")[1]) + assert.same("target_url", url_node:type()) + assert.is_false(tree:root():has_error()) end) it("ts_find", function() - local section_node = assert(utils.ts_find(script_node, "section")) - assert.same("section", section_node:type()) - local sr, sc, er, ec = section_node:range() - assert.same({ 0, 0, 8, 0 }, { sr, sc, er, ec }) + local start_node = assert(vim.treesitter.get_node({pos={4, 3}, lang="http"})) + local script_node = assert(utils.ts_find(start_node, "script")) + assert.same("script", script_node:type()) + local sr, sc, er, ec = script_node:range() + assert.same({ 4, 2, 7, 2 }, { sr, sc, er, ec }) end) it("ts_upper_node", function() - local comment_node = assert(utils.ts_upper_node(script_node)) + local start_node = assert(vim.treesitter.get_node({pos={4, 3}, lang="http"})) + local comment_node = assert(utils.ts_upper_node(start_node)) assert.same("comment", comment_node:type()) end) end)