Skip to content

Commit 025c9d4

Browse files
feat(dotenv): auto-register matching dotenv file
1 parent 3142c62 commit 025c9d4

File tree

9 files changed

+66
-0
lines changed

9 files changed

+66
-0
lines changed

ftplugin/http.lua

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
vim.bo.commentstring = "# %s"
22
vim.wo.conceallevel = 0
33

4+
local dotenv = require("rest-nvim.dotenv")
5+
46
vim.b._rest_nvim_count = 1
7+
vim.b._rest_nvim_env_file = dotenv.find_relevent_env_file()

lua/rest-nvim/dotenv.lua

+29
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ function M.find_env_files()
7171
return files
7272
end
7373

74+
---@return string? dotenv file
75+
function M.find_relevent_env_file()
76+
local filename = vim.fn.expand("%:t:r")
77+
if filename == "" then
78+
return nil
79+
end
80+
filename = filename:gsub("%.http$", "")
81+
---@type string?
82+
local env_file
83+
-- search for `/same/path/filename.env`
84+
env_file = vim.fs.find(filename .. ".env", { type = "file" })[1]
85+
if env_file then
86+
return env_file
87+
end
88+
-- search upward for `.env` file
89+
env_file = vim.fs.find(function (name, _)
90+
return name == ".env"
91+
end, {
92+
path = vim.fn.expand("%:h"),
93+
upward = true,
94+
stop = vim.fn.getcwd(),
95+
type = "file",
96+
limit = math.huge,
97+
})[1]
98+
if env_file then
99+
return env_file
100+
end
101+
end
102+
74103
---@param bufnr number? buffer identifier, default to current buffer
75104
---@return string? path
76105
function M.select_file(bufnr)

spec/dotenv_spec.lua

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---@module 'luassert'
2+
3+
require("spec.minimum_init")
4+
5+
local dotenv = require("rest-nvim.dotenv")
6+
7+
local function open(path)
8+
vim.cmd.edit(path)
9+
vim.cmd.source("ftplugin/http.lua")
10+
return 0
11+
end
12+
13+
local function remove_cwd(path)
14+
path = path:gsub(vim.pesc(vim.fn.getcwd()) .. "/", "")
15+
return path
16+
end
17+
18+
describe("dotenv", function ()
19+
it("find dotenv file with same name to http file", function ()
20+
open("spec/examples/dotenv/with_dotenv.http")
21+
assert.same(
22+
"spec/examples/dotenv/with_dotenv.env",
23+
remove_cwd(vim.b._rest_nvim_env_file)
24+
)
25+
end)
26+
it("find dotenv file in parent dir", function ()
27+
open("spec/examples/dotenv/without_dotenv.http")
28+
assert.same(
29+
"spec/examples/.env",
30+
remove_cwd(vim.b._rest_nvim_env_file)
31+
)
32+
end)
33+
end)

spec/examples/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# dotenv file for `example/dotenv/without_dotenv.http` example

spec/examples/dotenv/deeper_path/.env

Whitespace-only changes.

spec/examples/dotenv/deeper_path/with_dotenv.env

Whitespace-only changes.

spec/examples/dotenv/with_dotenv.env

Whitespace-only changes.

spec/examples/dotenv/with_dotenv.http

Whitespace-only changes.

spec/examples/dotenv/without_dotenv.http

Whitespace-only changes.

0 commit comments

Comments
 (0)