Skip to content

Commit 62ef848

Browse files
authored
Merge pull request #3 from kylelaker/add-content-detect
Allow automatic detecting based on content
2 parents 60aa8ac + 57a808d commit 62ef848

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

ftdetect/cloudformation.vim

+74-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
autocmd BufNewFile,BufRead *.template setfiletype yaml.cloudformation
1+
function! DetectCfn(type)
2+
let likely = 0
3+
let pointsRequired = 10
4+
5+
" A mapping of all the important words in a CloudFormation template to the
6+
" number of points they're worth when detecting a file type. The values
7+
" were chosen fairly arbitrarily, but the section headers are worth 1
8+
" point, intrinsic functions are worth 2, and pseudo parameters are worth
9+
" 4. AWSTemplateFormatVersion is used as a sure sign its a Cfn template,
10+
" and AWS::\w+::\w+ is given 5 points since you're specifying resources
11+
" using the CloudFormation name.
12+
let pointMap = [
13+
\['AWSTemplateFormatVersion', 100],
14+
\['\vAWS::\w+::\w+', 5],
15+
\['AWS::AccountId', 4],
16+
\['AWS::NotificationARNs', 4],
17+
\['AWS::NoValue', 4],
18+
\['AWS::Partition', 4],
19+
\['AWS::Region', 4],
20+
\['AWS::StackId', 4],
21+
\['AWS::StackName', 4],
22+
\['AWS::URLSuffix', 4],
23+
\['Fn::Base64', 2],
24+
\['!Base64', 2],
25+
\['Fn::Cidr', 2],
26+
\['!Cidr', 2],
27+
\['Fn::FindInMap', 2],
28+
\['!FindInMap', 2],
29+
\['Fn::GetAZs', 2],
30+
\['!GetAZs', 2],
31+
\['Fn::ImportValue', 2],
32+
\['!ImportValue', 2],
33+
\['Fn::Join', 2],
34+
\['!Join', 2],
35+
\['Fn::Select', 2],
36+
\['!Select', 2],
37+
\['Fn::Split', 2],
38+
\['!Split', 2],
39+
\['Fn::Sub', 2],
40+
\['!Sub', 2],
41+
\['Fn::Transform', 2],
42+
\['!Transform', 2],
43+
\['!Ref', 2],
44+
\['Description', 1],
45+
\['Metadata', 1],
46+
\['Parameters', 1],
47+
\['Mappings', 1],
48+
\['Conditions', 1],
49+
\['Transform', 1],
50+
\['Resources', 1],
51+
\['Outputs', 1],
52+
\]
53+
for lineContents in getline(1, line('$'))
54+
for strPoints in pointMap
55+
if lineContents =~ strPoints[0]
56+
let likely += strPoints[1]
57+
endif
58+
if likely > pointsRequired
59+
if a:type =~ "yaml"
60+
set filetype=yaml.cloudformation
61+
elseif a:type =~ "json"
62+
set filetype=json.cloudformation
63+
endif
64+
return
65+
endif
66+
endfor
67+
endfor
68+
endfunction
69+
70+
augroup filetypedetect
71+
au BufRead,BufNewFile *.yaml,*.yml call DetectCfn('yaml')
72+
au BufRead,BufNewFile *.json call DetectCfn('json')
73+
au BufNewFile,BufRead *.template setfiletype yaml.cloudformation
74+
augroup END

0 commit comments

Comments
 (0)