Skip to content

Commit 5c1fc0f

Browse files
feat: add rest.Opts.Env.find option. (fixes #518)
1 parent daecda1 commit 5c1fc0f

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

lua/rest-nvim/config/default.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ local default_config = {
6565
enable = true,
6666
---@type string
6767
pattern = ".*%.env.*",
68+
---@type fun():string[]
69+
find = function()
70+
local config = require("rest-nvim.config")
71+
return vim.fs.find(function(name, _)
72+
return name:match(config.env.pattern)
73+
end, {
74+
path = vim.fn.getcwd(),
75+
type = "file",
76+
limit = math.huge,
77+
})
78+
end,
6879
},
6980
---@class rest.Config.UI
7081
ui = {

lua/rest-nvim/config/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ local config
9292
---@field enable? boolean
9393
--- Environment variables file pattern for telescope.nvim (Default: `"%.env.*"`)
9494
---@field pattern? string
95+
--- Function lists environment files (used for `:Rest env select`)
96+
--- Searches for file matching *rest.Opts.Env.pattern* under same directory by
97+
--- default
98+
---@field find? fun():string[]
9599

96100
---@class rest.Opts.UI
97101
--- Set winbar in result pane (Default: `true`)

lua/rest-nvim/dotenv.lua

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,11 @@ function M.show_registered_file(bufnr)
5353
end
5454
end
5555

56-
---Find a list of environment files starting from the current directory
56+
---Find a list of environment files
57+
---@see rest.Opts.Env.find
5758
---@return string[] files Environment variable files path
5859
function M.find_env_files()
59-
-- We are currently looking for any ".*env*" file, e.g. ".env", ".env.json"
60-
--
61-
-- This algorithm can be improved later on to search from a parent directory if the desired environment file
62-
-- is somewhere else but in the current working directory.
63-
local files = vim.fs.find(function(name, _)
64-
return name:match(config.env.pattern)
65-
end, { limit = math.huge, type = "file", path = "./" })
66-
67-
return files
60+
return config.env.find()
6861
end
6962

7063
---@return string? dotenv file

0 commit comments

Comments
 (0)