diff options
author | Evgeni Chasnovski <evgeni.chasnovski@gmail.com> | 2024-07-12 15:41:19 +0300 |
---|---|---|
committer | Evgeni Chasnovski <evgeni.chasnovski@gmail.com> | 2024-07-12 21:16:09 +0300 |
commit | 9d14b76089dfafbc663b0cfa0b185c484d0f1ed3 (patch) | |
tree | 14ac682c658e7eae3f09dd881b3c3f8c38711df6 /runtime/lua/vim | |
parent | abf4b65a51b2e96191782adf68bd967f8073f018 (diff) | |
download | rneovim-9d14b76089dfafbc663b0cfa0b185c484d0f1ed3.tar.gz rneovim-9d14b76089dfafbc663b0cfa0b185c484d0f1ed3.tar.bz2 rneovim-9d14b76089dfafbc663b0cfa0b185c484d0f1ed3.zip |
refactor(filetype): extract expanding env. vars in separate function
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r-- | runtime/lua/vim/filetype.lua | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 236c67790f..5e34bac728 100644 --- 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 |