Skip to content

Commit d08d1fd

Browse files
ref(utils): pass language to utils.ts_get_parser
1 parent 85a22b9 commit d08d1fd

File tree

6 files changed

+34
-34
lines changed

6 files changed

+34
-34
lines changed

lua/rest-nvim/parser/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ end
221221
---@param source Source
222222
---@return TSNode[]
223223
function parser.get_all_request_nodes(source)
224-
local _, tree = utils.ts_parse_source(source)
224+
local _, tree = utils.ts_parse_source(source, "http")
225225
local result = {}
226226
for node, _ in tree:root():iter_children() do
227227
if node:type() == "section" and #node:field("request") > 0 then
@@ -234,7 +234,7 @@ end
234234
---@return TSNode?
235235
function parser.get_request_node_by_name(name)
236236
local source = 0
237-
local _, tree = utils.ts_parse_source(source)
237+
local _, tree = utils.ts_parse_source(source, "http")
238238
local query = NAMED_REQUEST_QUERY
239239
for id, node, _metadata, _match in query:iter_captures(tree:root(), source) do
240240
local capture_name = query.captures[id]
@@ -308,7 +308,7 @@ end
308308
---@param source Source
309309
---@return string[]
310310
function parser.get_request_names(source)
311-
local _, tree = utils.ts_parse_source(source)
311+
local _, tree = utils.ts_parse_source(source, "http")
312312
local query = NAMED_REQUEST_QUERY
313313
local result = {}
314314
for id, node, _metadata, _match in query:iter_captures(tree:root(), source) do

lua/rest-nvim/utils.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
---@brief ]]
88

99
local logger = require("rest-nvim.logger")
10-
-- local config = require("rest-nvim.config")
1110

1211
local utils = {}
1312

@@ -86,11 +85,11 @@ end
8685
function utils.parse_http_time(time_str)
8786
local pattern = "(%a+), (%d+)[%s-](%a+)[%s-](%d+) (%d+):(%d+):(%d+) GMT"
8887
local _, day, month_name, year, hour, min, sec = time_str:match(pattern)
89-
-- stylua: ignore
90-
local months = {
91-
Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6,
92-
Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12,
93-
}
88+
-- stylua: ignore
89+
local months = {
90+
Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6,
91+
Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12,
92+
}
9493
local time_table = {
9594
year = tonumber(year),
9695
month = months[month_name],
@@ -186,20 +185,21 @@ function utils.ts_highlight_node(bufnr, node, ns, timeout)
186185
end
187186

188187
---@param source string|integer
188+
---@param lang string
189189
---@return vim.treesitter.LanguageTree
190-
function utils.ts_get_parser(source)
190+
function utils.ts_get_parser(source, lang)
191191
if type(source) == "string" then
192-
return vim.treesitter.get_string_parser(source, "http")
192+
return vim.treesitter.get_string_parser(source, lang)
193193
else
194-
return vim.treesitter.get_parser(source, "http")
194+
return vim.treesitter.get_parser(source, lang)
195195
end
196196
end
197197

198198
---@param source string|integer
199199
---@return vim.treesitter.LanguageTree
200200
---@return TSTree
201-
function utils.ts_parse_source(source)
202-
local ts_parser = utils.ts_get_parser(source)
201+
function utils.ts_parse_source(source, lang)
202+
local ts_parser = utils.ts_get_parser(source, lang)
203203
return ts_parser, assert(ts_parser:parse(false)[1])
204204
end
205205

spec/examples/examples_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
describe("multi-line-url", function()
1515
it("line breaks should be ignored", function()
1616
local source = open("spec/examples/multi_line_url.http")
17-
local _, tree = utils.ts_parse_source(source)
17+
local _, tree = utils.ts_parse_source(source, "http")
1818
local req_node = assert(tree:root():child(0))
1919
local req = parser.parse(req_node, source)
2020
assert.not_nil(req)
@@ -108,7 +108,7 @@ describe("builtin request hooks", function()
108108
describe("set_content_type", function()
109109
it("with external body", function()
110110
local source = open("spec/examples/post_with_external_body.http")
111-
local _, tree = utils.ts_parse_source(source)
111+
local _, tree = utils.ts_parse_source(source, "http")
112112
local req_node = assert(tree:root():child(0))
113113
local req = assert(parser.parse(req_node, source))
114114
_G.rest_request = req

spec/parser/http_parser_spec.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("http parser", function()
2323
end)
2424
it("parse from http string", function()
2525
local source = "GET https://github.com\n"
26-
local _, tree = utils.ts_parse_source(source)
26+
local _, tree = utils.ts_parse_source(source, "http")
2727
local req_node = assert(tree:root():child(0))
2828
assert.same({
2929
method = "GET",
@@ -35,7 +35,7 @@ describe("http parser", function()
3535
end)
3636
it("parse from http file", function()
3737
local source = open("spec/examples/basic_get.http")
38-
local _, tree = utils.ts_parse_source(source)
38+
local _, tree = utils.ts_parse_source(source, "http")
3939
local req_node = assert(tree:root():child(0))
4040
assert.same({
4141
name = "basic get statement",
@@ -66,7 +66,7 @@ GET http://localhost:80
6666
GET /some/path
6767
HOST: localhost:8000
6868
]]
69-
local _, tree = utils.ts_parse_source(source)
69+
local _, tree = utils.ts_parse_source(source, "http")
7070
local req_node = assert(tree:root():child(0))
7171
local req = assert(parser.parse(req_node, source))
7272
assert.same("http://localhost:8000/some/path", req.url)
@@ -78,7 +78,7 @@ X-Header1: value1
7878
X-Header2:
7979
X-Header1: value2
8080
]]
81-
local _, tree = utils.ts_parse_source(source)
81+
local _, tree = utils.ts_parse_source(source, "http")
8282
local req_node = assert(tree:root():child(0))
8383
assert.same({
8484
url = "http://example.com/api",
@@ -95,7 +95,7 @@ X-Header1: value2
9595
describe("parse body", function()
9696
it("json body", function()
9797
local source = 'POST https://example.com\n\n{\n\t"blah": 1}\n'
98-
local _, tree = utils.ts_parse_source(source)
98+
local _, tree = utils.ts_parse_source(source, "http")
9999
local req_node = assert(tree:root():child(0))
100100
assert.same({
101101
method = "POST",
@@ -111,7 +111,7 @@ X-Header1: value2
111111
end)
112112
it("invalid json body", function()
113113
local source = 'POST https://example.com\n\n{\n\t"blah": 1\n'
114-
local _, tree = utils.ts_parse_source(source)
114+
local _, tree = utils.ts_parse_source(source, "http")
115115
local req_node = assert(tree:root():child(0))
116116
local spy_log_warn = spy.on(logger, "warn")
117117
parser.parse(req_node, source)
@@ -127,7 +127,7 @@ X-Header1: value2
127127
<Password>password</Password>
128128
</Request>
129129
]]
130-
local _, tree = utils.ts_parse_source(source)
130+
local _, tree = utils.ts_parse_source(source, "http")
131131
local req_node = assert(tree:root():child(0))
132132
assert.same({
133133
method = "POST",
@@ -148,7 +148,7 @@ X-Header1: value2
148148
it("parse invalid xml", function()
149149
logger.info("hi")
150150
local source = "POST https://example.com\n\n<?xml\n"
151-
local _, tree = utils.ts_parse_source(source)
151+
local _, tree = utils.ts_parse_source(source, "http")
152152
local req_node = assert(tree:root():child(0))
153153
local spy_log_warn = spy.on(logger, "warn")
154154
parser.parse(req_node, source)
@@ -166,7 +166,7 @@ key3 = value3 &
166166
key4 = value4 &
167167
key5 = value5
168168
]]
169-
local _, tree = utils.ts_parse_source(source)
169+
local _, tree = utils.ts_parse_source(source, "http")
170170
local req_node = assert(tree:root():child(0))
171171
assert.same({
172172
method = "POST",
@@ -185,7 +185,7 @@ key5 = value5
185185
it("parse external body", function()
186186
-- external body can be only sourced when
187187
local source = open("spec/examples/post_with_external_body.http")
188-
local _, tree = utils.ts_parse_source(source)
188+
local _, tree = utils.ts_parse_source(source, "http")
189189
local req_node = assert(tree:root():child(0))
190190
assert.same({
191191
method = "POST",
@@ -210,7 +210,7 @@ key5 = value5
210210
local source = [[POST https://example.com
211211
Authorization: Bearer {{TOKEN}}
212212
]]
213-
local _, tree = utils.ts_parse_source(source)
213+
local _, tree = utils.ts_parse_source(source, "http")
214214
local req_node = assert(tree:root():child(0))
215215
local req = parser.parse(req_node, source)
216216
assert.not_nil(req)
@@ -227,7 +227,7 @@ Authorization: Bearer {{TOKEN}}
227227
"date": "{{DATE}}"
228228
}
229229
]]
230-
local _, tree = utils.ts_parse_source(source)
230+
local _, tree = utils.ts_parse_source(source, "http")
231231
local req_node = assert(tree:root():child(0))
232232
local req = parser.parse(req_node, source)
233233
assert.not_nil(req)
@@ -241,7 +241,7 @@ Authorization: Bearer {{TOKEN}}
241241
end)
242242
it("parse variable declaration", function()
243243
local source = "@foo = bar\n"
244-
local _, tree = utils.ts_parse_source(source)
244+
local _, tree = utils.ts_parse_source(source, "http")
245245
local vd_node = assert(tree:root():child(0):child(0))
246246
assert.same("variable_declaration", vd_node:type())
247247
local c = context:new()
@@ -252,7 +252,7 @@ Authorization: Bearer {{TOKEN}}
252252
end)
253253
it("parse variable declaration with other variable", function()
254254
local source = "@foo = bar\n@baz = {{foo}} {{$date}}\n"
255-
local _, tree = utils.ts_parse_source(source)
255+
local _, tree = utils.ts_parse_source(source, "http")
256256
local c = context:new()
257257
parser.parse_variable_declaration(assert(tree:root():child(0):child(0)), source, c)
258258
parser.parse_variable_declaration(assert(tree:root():child(0):child(1)), source, c)
@@ -265,7 +265,7 @@ Authorization: Bearer {{TOKEN}}
265265

266266
it("parse pre-request script", function()
267267
local source = "# @lang=lua\n< {%\nrequest.variables.set('foo', 'bar')\n%}\n"
268-
local _, tree = utils.ts_parse_source(source)
268+
local _, tree = utils.ts_parse_source(source, "http")
269269
local c = context:new()
270270
local script_node = tree:root():child(0):child(1)
271271
assert(script_node)

spec/script_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ json.data = "overwritten"
3636
response.body = vim.json.encode(json)
3737
%}
3838
]]
39-
local _, tree = utils.ts_parse_source(source)
39+
local _, tree = utils.ts_parse_source(source, "http")
4040
local req_node = assert(tree:root():child(0))
4141
local req = assert(parser.parse(req_node, source))
4242
---fake response
@@ -60,7 +60,7 @@ local json = vim.json.decode(response.body)
6060
client.global.set("MYVAR", json.var)
6161
%}
6262
]]
63-
local _, tree = utils.ts_parse_source(source)
63+
local _, tree = utils.ts_parse_source(source, "http")
6464
local req_node = assert(tree:root():child(0))
6565
local ctx = Context:new()
6666
local req = assert(parser.parse(req_node, source, ctx))

spec/utils_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ response.body = vim.json.encode(json)
1515
]]
1616
local script_node
1717
it("ts_parse_source", function()
18-
local _, tree = utils.ts_parse_source(source)
18+
local _, tree = utils.ts_parse_source(source, "http")
1919
script_node = assert(tree:root():child(0):child(1))
2020
assert.same("res_handler_script", script_node:type())
2121
end)

0 commit comments

Comments
 (0)