neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit 9d14b76089dfafbc663b0cfa0b185c484d0f1ed3
parent abf4b65a51b2e96191782adf68bd967f8073f018
Author: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
Date:   Fri, 12 Jul 2024 15:41:19 +0300

refactor(filetype): extract expanding env. vars in separate function

Diffstat:
Mruntime/lua/vim/filetype.lua | 29+++++++++++++++++------------
1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua @@ -2430,6 +2430,19 @@ local function dispatch(ft, path, bufnr, ...) return ft0, on_detect end +--- @param pat string +--- @return boolean +--- @return string +local function expand_envvar_pattern(pat) + local some_env_missing = false + local expanded = pat:gsub('%${(%S-)}', function(env) + local val = vim.env[env] --- @type string? + some_env_missing = some_env_missing or val == nil + return vim.pesc(val or '') + end) + return some_env_missing, expanded +end + --- @param name string --- @param path string --- @param tail string @@ -2440,20 +2453,12 @@ local function match_pattern(name, path, tail, pat) local fullpat, has_slash = pat_cache.fullpat, pat_cache.has_slash if pat_cache.has_env then - local return_early --- @type true? - --- @type string - fullpat = fullpat:gsub('%${(%S-)}', function(env) - -- If an environment variable is present in the pattern but not set, there is no match - if not vim.env[env] then - return_early = true - return nil - end - return vim.pesc(vim.env[env]) - end) - if return_early then + local some_env_missing, expanded = expand_envvar_pattern(fullpat) + -- If any environment variable is present in the pattern but not set, there is no match + if some_env_missing then return false end - has_slash = fullpat:find('/') ~= nil + fullpat, has_slash = expanded, expanded:find('/') ~= nil end -- If the pattern contains a / match against the full path, otherwise just the tail