Skip to content

Commit ff9cf3d

Browse files
feat: automatically name request by file name
1 parent f01fb8e commit ff9cf3d

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

ftplugin/http.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
vim.bo.commentstring = "# %s"
22

3+
vim.b._rest_nvim_count = 1
4+
35
local commands = require("rest-nvim.commands")
46
---@diagnostic disable-next-line: invisible
57
commands.init(0)

lua/rest-nvim/parser/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ function M.parse(node, source, ctx)
285285
name = get_node_field_text(child, "value", source) or name
286286
end
287287
end
288+
if not name then
289+
if type(source) == "number" then
290+
local filename = vim.api.nvim_buf_get_name(source)
291+
name = filename:match(".*/%.?(.*).http$") or filename
292+
name = name .. "#" .. vim.b[source]._rest_nvim_count
293+
vim.b[source]._rest_nvim_count = vim.b[source]._rest_nvim_count + 1
294+
end
295+
end
288296

289297
local headers = parse_headers(req_node, source, ctx)
290298
-- HACK: check if url doesn't have host

spec/api_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local spy = require("luassert.spy")
1111

1212
local function open(path)
1313
vim.cmd.edit(path)
14+
vim.cmd.source("ftplugin/http.lua")
1415
return 0
1516
end
1617

@@ -36,6 +37,7 @@ describe("parser", function()
3637
local _, tree = utils.ts_parse_source(source)
3738
local req_node = assert(tree:root():child(0))
3839
assert.same({
40+
name = "api#1",
3941
context = context:new(),
4042
method = "GET",
4143
url = "https://api.github.com/users/boltlessengineer",

0 commit comments

Comments
 (0)