Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit189ff56

Browse files
committed
feat(filetype): expand environment variables in filetype patterns
1 parent91a2e7a commit189ff56

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

‎runtime/doc/lua.txt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,10 @@ add({filetypes}) *vim.filetype.add()*
20822082

20832083
Filename patterns can specify an optional priority to resolve cases when a
20842084
file path matches multiple patterns. Higher priorities are matched first.
2085-
When omitted, the priority defaults to 0.
2085+
When omitted, the priority defaults to 0. A pattern can contain
2086+
environment variables of the form "${ENV}" that will be automatically
2087+
expanded. If the environment variable is not set, the pattern won't be
2088+
matched.
20862089

20872090
See $VIMRUNTIME/lua/vim/filetype.lua for more examples.
20882091

@@ -2112,6 +2115,8 @@ add({filetypes}) *vim.filetype.add()*
21122115
['.*/etc/foo/.*'] = 'fooscript',
21132116
-- Using an optional priority
21142117
['.*/etc/foo/.*%.conf'] = { 'dosini', { priority = 10 } },
2118+
-- A pattern containing an environment variable
2119+
['${XDG_CONFIG_HOME}/foo/git'] = 'git',
21152120
['README.(a+)$'] = function(path, bufnr, ext)
21162121
if ext == 'md' then
21172122
return 'markdown'

‎runtime/lua/vim/filetype.lua‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,6 @@ local filename = {
12911291
WORKSPACE='bzl',
12921292
BUILD='bzl',
12931293
['cabal.project']='cabalproject',
1294-
[vim.env.HOME..'/cabal.config']='cabalconfig',
12951294
['cabal.config']='cabalconfig',
12961295
calendar='calendar',
12971296
catalog='catalog',
@@ -1699,6 +1698,7 @@ local pattern = {
16991698
['.*/meta%-.*/conf/.*%.conf']='bitbake',
17001699
['bzr_log%..*']='bzr',
17011700
['.*enlightenment/.*%.cfg']='c',
1701+
['${HOME}/cabal%.config']='cabalconfig',
17021702
['cabal%.project%..*']=starsetf('cabalproject'),
17031703
['.*/%.calendar/.*']=starsetf('calendar'),
17041704
['.*/share/calendar/.*/calendar%..*']=starsetf('calendar'),
@@ -1823,41 +1823,21 @@ local pattern = {
18231823
['.*/%.config/git/config']='gitconfig',
18241824
['.*%.git/config%.worktree']='gitconfig',
18251825
['.*%.git/worktrees/.*/config%.worktree']='gitconfig',
1826-
['.*/git/config']=function(path,bufnr)
1827-
ifvim.env.XDG_CONFIG_HOMEandpath:find(vim.env.XDG_CONFIG_HOME..'/git/config')then
1828-
return'gitconfig'
1829-
end
1830-
end,
1826+
['${XDG_CONFIG_HOME}/git/config']='gitconfig',
18311827
['.*%.git/info/attributes']='gitattributes',
18321828
['.*/etc/gitattributes']='gitattributes',
18331829
['.*/%.config/git/attributes']='gitattributes',
1834-
['.*/git/attributes']=function(path,bufnr)
1835-
ifvim.env.XDG_CONFIG_HOMEandpath:find(vim.env.XDG_CONFIG_HOME..'/git/attributes')then
1836-
return'gitattributes'
1837-
end
1838-
end,
1830+
['${XDG_CONFIG_HOME}/git/attributes']='gitattributes',
18391831
['.*%.git/info/exclude']='gitignore',
18401832
['.*/%.config/git/ignore']='gitignore',
1841-
['.*/git/ignore']=function(path,bufnr)
1842-
ifvim.env.XDG_CONFIG_HOMEandpath:find(vim.env.XDG_CONFIG_HOME..'/git/ignore')then
1843-
return'gitignore'
1844-
end
1845-
end,
1833+
['${XDG_CONFIG_HOME}/git/ignore']='gitignore',
18461834
['%.gitsendemail%.msg%.......']='gitsendemail',
18471835
['gkrellmrc_.']='gkrellmrc',
18481836
['.*/usr/.*/gnupg/options%.skel']='gpg',
18491837
['.*/%.gnupg/options']='gpg',
18501838
['.*/%.gnupg/gpg%.conf']='gpg',
1851-
['.*/options']=function(path,bufnr)
1852-
ifvim.env.GNUPGHOMEandpath:find(vim.env.GNUPGHOME..'/options')then
1853-
return'gpg'
1854-
end
1855-
end,
1856-
['.*/gpg%.conf']=function(path,bufnr)
1857-
ifvim.env.GNUPGHOMEandpath:find(vim.env.GNUPGHOME..'/gpg%.conf')then
1858-
return'gpg'
1859-
end
1860-
end,
1839+
['${GNUPGHOME}/options']='gpg',
1840+
['${GNUPGHOME}/gpg%.conf']='gpg',
18611841
['.*/etc/group']='group',
18621842
['.*/etc/gshadow']='group',
18631843
['.*/etc/group%.edit']='group',
@@ -1871,7 +1851,7 @@ local pattern = {
18711851
['.*/etc/grub%.conf']='grub',
18721852
-- gtkrc* and .gtkrc*
18731853
['%.?gtkrc.*']=starsetf('gtkrc'),
1874-
[vim.env.VIMRUNTIME..'/doc/.*%.txt']='help',
1854+
['${VIMRUNTIME}/doc/.*%.txt']='help',
18751855
['hg%-editor%-.*%.txt']='hgcommit',
18761856
['.*/etc/host%.conf']='hostconf',
18771857
['.*/etc/hosts%.deny']='hostsaccess',
@@ -2275,6 +2255,9 @@ end
22752255
--- Filename patterns can specify an optional priority to resolve cases when a
22762256
--- file path matches multiple patterns. Higher priorities are matched first.
22772257
--- When omitted, the priority defaults to 0.
2258+
--- A pattern can contain environment variables of the form "${ENV}" that will be
2259+
--- automatically expanded. If the environment variable is not set, the pattern
2260+
--- won't be matched.
22782261
---
22792262
--- See $VIMRUNTIME/lua/vim/filetype.lua for more examples.
22802263
---
@@ -2303,6 +2286,8 @@ end
23032286
--- ['.*/etc/foo/.*'] = 'fooscript',
23042287
--- -- Using an optional priority
23052288
--- ['.*/etc/foo/.*%.conf'] = { 'dosini', { priority = 10 } },
2289+
--- -- A pattern containing an environment variable
2290+
--- ['${XDG_CONFIG_HOME}/foo/git'] = 'git',
23062291
--- ['README.(%a+)$'] = function(path, bufnr, ext)
23072292
--- if ext == 'md' then
23082293
--- return 'markdown'
@@ -2376,8 +2361,28 @@ local function dispatch(ft, path, bufnr, ...)
23762361
end
23772362
end
23782363

2364+
-- Caches calls to string.find
2365+
localexpand_env_lookup= {}
2366+
23792367
---@private
23802368
localfunctionmatch_pattern(name,path,tail,pat)
2369+
ifexpand_env_lookup[pat]==nilthen
2370+
expand_env_lookup[pat]=pat:find('%$')~=nil
2371+
end
2372+
ifexpand_env_lookup[pat]then
2373+
localreturn_early
2374+
pat=pat:gsub('%${(%S-)}',function(env)
2375+
-- If an environment variable is present in the pattern but not set, there is no match
2376+
ifnotvim.env[env]then
2377+
return_early=true
2378+
returnnil
2379+
end
2380+
returnvim.env[env]
2381+
end)
2382+
ifreturn_earlythen
2383+
returnfalse
2384+
end
2385+
end
23812386
-- If the pattern contains a / match against the full path, otherwise just the tail
23822387
localfullpat='^'..pat..'$'
23832388
localmatches

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp