diff --git a/ftplugin/http.lua b/ftplugin/http.lua index 49da4da..23b5c02 100644 --- a/ftplugin/http.lua +++ b/ftplugin/http.lua @@ -1,4 +1,7 @@ vim.bo.commentstring = "# %s" vim.wo.conceallevel = 0 +local dotenv = require("rest-nvim.dotenv") + vim.b._rest_nvim_count = 1 +vim.b._rest_nvim_env_file = dotenv.find_relevent_env_file() diff --git a/lua/rest-nvim/dotenv.lua b/lua/rest-nvim/dotenv.lua index 8e58678..81113a4 100644 --- a/lua/rest-nvim/dotenv.lua +++ b/lua/rest-nvim/dotenv.lua @@ -71,6 +71,35 @@ function M.find_env_files() return files end +---@return string? dotenv file +function M.find_relevent_env_file() + local filename = vim.fn.expand("%:t:r") + if filename == "" then + return nil + end + filename = filename:gsub("%.http$", "") + ---@type string? + local env_file + -- search for `/same/path/filename.env` + env_file = vim.fs.find(filename .. ".env", { type = "file" })[1] + if env_file then + return env_file + end + -- search upward for `.env` file + env_file = vim.fs.find(function (name, _) + return name == ".env" + end, { + path = vim.fn.expand("%:h"), + upward = true, + stop = vim.fn.getcwd(), + type = "file", + limit = math.huge, + })[1] + if env_file then + return env_file + end +end + ---@param bufnr number? buffer identifier, default to current buffer ---@return string? path function M.select_file(bufnr) diff --git a/spec/dotenv_spec.lua b/spec/dotenv_spec.lua new file mode 100644 index 0000000..d1588e0 --- /dev/null +++ b/spec/dotenv_spec.lua @@ -0,0 +1,33 @@ +---@module 'luassert' + +require("spec.minimum_init") + +local dotenv = require("rest-nvim.dotenv") + +local function open(path) + vim.cmd.edit(path) + vim.cmd.source("ftplugin/http.lua") + return 0 +end + +local function remove_cwd(path) + path = path:gsub(vim.pesc(vim.fn.getcwd()) .. "/", "") + return path +end + +describe("dotenv", function () + it("find dotenv file with same name to http file", function () + open("spec/examples/dotenv/with_dotenv.http") + assert.same( + "spec/examples/dotenv/with_dotenv.env", + remove_cwd(vim.b._rest_nvim_env_file) + ) + end) + it("find dotenv file in parent dir", function () + open("spec/examples/dotenv/without_dotenv.http") + assert.same( + "spec/examples/.env", + remove_cwd(vim.b._rest_nvim_env_file) + ) + end) +end) diff --git a/spec/examples/.env b/spec/examples/.env new file mode 100644 index 0000000..657866e --- /dev/null +++ b/spec/examples/.env @@ -0,0 +1 @@ +# dotenv file for `example/dotenv/without_dotenv.http` example diff --git a/spec/examples/dotenv/deeper_path/.env b/spec/examples/dotenv/deeper_path/.env new file mode 100644 index 0000000..e69de29 diff --git a/spec/examples/dotenv/deeper_path/with_dotenv.env b/spec/examples/dotenv/deeper_path/with_dotenv.env new file mode 100644 index 0000000..e69de29 diff --git a/spec/examples/dotenv/with_dotenv.env b/spec/examples/dotenv/with_dotenv.env new file mode 100644 index 0000000..e69de29 diff --git a/spec/examples/dotenv/with_dotenv.http b/spec/examples/dotenv/with_dotenv.http new file mode 100644 index 0000000..e69de29 diff --git a/spec/examples/dotenv/without_dotenv.http b/spec/examples/dotenv/without_dotenv.http new file mode 100644 index 0000000..e69de29