|
10 | 10 | local parser = {} |
11 | 11 |
|
12 | 12 | local Context = require("rest-nvim.context").Context |
13 | | -local script = require("rest-nvim.script") |
14 | 13 | local utils = require("rest-nvim.utils") |
15 | 14 | local logger = require("rest-nvim.logger") |
16 | 15 | local jar = require("rest-nvim.cookie_jar") |
@@ -195,29 +194,49 @@ end |
195 | 194 |
|
196 | 195 | ---@param node TSNode |
197 | 196 | ---@param source Source |
| 197 | +---@return string lang |
198 | 198 | ---@return string str |
199 | 199 | local function parse_script(node, source) |
200 | | - vim.validate({ node = utils.ts_node_spec(node, "script") }) |
201 | | - local str = vim.treesitter.get_node_text(node, source):sub(3,-3) |
202 | | - return str |
| 200 | + local lang = "javascript" |
| 201 | + local prev_node = utils.ts_upper_node(node) |
| 202 | + if prev_node and prev_node:type() == "comment" and get_node_field_text(prev_node, "name", source) == "lang" then |
| 203 | + local value = get_node_field_text(prev_node, "value", source) |
| 204 | + if value then |
| 205 | + lang = value |
| 206 | + end |
| 207 | + end |
| 208 | + local script_node = assert(node:named_child(0)) |
| 209 | + local str = vim.treesitter.get_node_text(script_node, source):sub(3,-3) |
| 210 | + return lang, str |
203 | 211 | end |
204 | 212 |
|
205 | | ----@param script_node TSNode |
| 213 | +---@param node TSNode |
206 | 214 | ---@param source Source |
207 | 215 | ---@param context rest.Context |
208 | | -function parser.parse_pre_request_script(script_node, source, context) |
209 | | - local node = assert(script_node:named_child(0)) |
210 | | - local str = parse_script(node, source) |
211 | | - script.load_prescript(str, context)() |
| 216 | +function parser.parse_pre_request_script(node, source, context) |
| 217 | + local lang, str = parse_script(node, source) |
| 218 | + local ok, script = pcall(require, "rest-nvim.script." .. lang) |
| 219 | + if not ok then |
| 220 | + logger.error(("failed to load script with language '%s'. Can't find script runner client."):format(lang)) |
| 221 | + return |
| 222 | + end |
| 223 | + ---@cast script rest.ScriptClient |
| 224 | + script:load_pre_req_hook(str, context)() |
212 | 225 | end |
213 | 226 |
|
214 | | ----@param handler_node TSNode |
| 227 | +---@param node TSNode |
215 | 228 | ---@param source Source |
216 | 229 | ---@param context rest.Context |
217 | | -function parser.parse_request_handler(handler_node, source, context) |
218 | | - local node = assert(handler_node:named_child(0)) |
219 | | - local str = parse_script(node, source) |
220 | | - return script.load_handler(str, context) |
| 230 | +---@return function? |
| 231 | +function parser.parse_request_handler(node, source, context) |
| 232 | + local lang, str = parse_script(node, source) |
| 233 | + local ok, script = pcall(require, "rest-nvim.script." .. lang) |
| 234 | + if not ok then |
| 235 | + logger.error(("failed to load script with language '%s'. Can't find script runner client."):format(lang)) |
| 236 | + return |
| 237 | + end |
| 238 | + ---@cast script rest.ScriptClient |
| 239 | + return script:load_post_req_hook(str, context) |
221 | 240 | end |
222 | 241 |
|
223 | 242 | ---@param source Source |
@@ -281,7 +300,10 @@ function parser.parse(node, source, ctx) |
281 | 300 | if node_type == "pre_request_script" then |
282 | 301 | parser.parse_pre_request_script(child, source, ctx) |
283 | 302 | elseif node_type == "res_handler_script" then |
284 | | - table.insert(handlers, parser.parse_request_handler(child, source, ctx)) |
| 303 | + local handler = parser.parse_request_handler(child, source, ctx) |
| 304 | + if handler then |
| 305 | + table.insert(handlers, handler) |
| 306 | + end |
285 | 307 | elseif node_type == "request_separator" then |
286 | 308 | name = get_node_field_text(child, "value", source) |
287 | 309 | elseif node_type == "comment" and get_node_field_text(child, "name", source) == "name" then |
|
0 commit comments