Skip to content

Commit 01d0e7d

Browse files
authored
v2 - Change to yaml.ansible and *.jinja2 (#55)
- The filetype for playbooks is now set to `yaml.ansible`. - Using a compound filetype here improves compatibility with some other plugins, and is a bit more honest about the filetypes being used. We _could_ set it to `yaml.jinja2.ansible`, if there are strong opinions on this please open an issue. - This _only_ breaks setups using vim plugin on-demand loading features — e.g. `{ 'for': 'ansible' }` in vim-plug. Otherwise this change should not break anything. - `g:ansible_extra_syntaxes` is now deprecated in favor of `g:ansible_template_syntaxes` — which will use conditional compound filetypes, instead of sourcing all filetypes listed and hiding them under `ansible_template`. - While this is a complete deprecation of one setting, the new functionality is significantly better all around and should support the same use-cases. - Example: a ruby+ansible-template will have a filetype of `ruby.jinja2` instead of `ansible_template` One non-breaking change is also added, this plugin gains additional compatibility with _stephpy/vim-yaml_ — syntax highlights will be improved when using this plugin.
1 parent aa34a39 commit 01d0e7d

File tree

6 files changed

+71
-169
lines changed

6 files changed

+71
-169
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ This is a vim syntax plugin for Ansible 2.0, it supports YAML playbooks, Jinja2
2222
- Jinja2 templates are detected if they have a *.j2* suffix
2323
- Files named `hosts` will be treated as Ansible hosts files
2424

25-
You can also set the filetype to `ansible`, `ansible_template`, or `ansible_hosts` if auto-detection does not work (e.g. `:set ft=ansible`). **Note**: If you want to detect a custom pattern of your own, you can easily add this in your `.vimrc` using something like this:
25+
You can also set the filetype to `yaml.ansible`, `*.jinja2`, or `ansible_hosts` if auto-detection does not work (e.g. `:set ft=yaml.ansible` or `:set ft=ruby.jinja2`). **Note**: If you want to detect a custom pattern of your own, you can easily add this in your `.vimrc` using something like this:
2626

2727
```vim
28-
au BufRead,BufNewFile */playbooks/*.yml set filetype=ansible
28+
au BufRead,BufNewFile */playbooks/*.yml set filetype=yaml.ansible
2929
```
3030

3131
This plugin should be quite reliable, as it sources the original formats and simply modifies the highlights as appropriate. This also enables a focus on simplicity and configurability instead of patching bad syntax detection.
@@ -61,14 +61,11 @@ Use your favorite plugin manager, or try [vim-plug](https://github.com/junegunn/
6161

6262
When this variable is set, indentation will completely reset (unindent to column 0) after two newlines in insert-mode. The normal behavior of YAML is to always keep the previous indentation, even across multiple newlines with no content.
6363

64-
##### g:ansible_extra_syntaxes
65-
`let g:ansible_extra_syntaxes = "sh.vim ruby.vim"`
64+
##### g:ansible_yamlKeyName
6665

67-
The space-separated options specified must be the actual syntax files, not the filetype - typically these are in something like `/usr/share/vim/syntax`. For example Bash is not `bash.vim` but seems to live in `sh.vim`.
66+
`let g:ansible_yamlKeyName = 'yamlKey'`
6867

69-
This flag enables extra syntaxes to be loaded for Jinja2 templates. If you frequently work with specific filetypes in Ansible, this can help get highlighting in those files.
70-
71-
This will *always* load these syntaxes for *all* .j2 files, and should be considered a bit of a (temporary?) hack/workaround.
68+
This option exists to provide additional compatibility with [stephpy/vim-yaml](https://github.com/stephpy/vim-yaml).
7269

7370
##### g:ansible_attribute_highlight
7471
`let g:ansible_attribute_highlight = "ob"`
@@ -105,20 +102,29 @@ By default we only highlight: `include until retries delay when only_if become b
105102
##### g:ansible_normal_keywords_highlight
106103
`let g:ansible_normal_keywords_highlight = 'Constant'`
107104

108-
This option accepts the first line of each option in `:help E669` - thus the first 3 options are _Comment_, _Constant_, and _Identifier_
105+
Accepts any syntax group name from `:help E669` - e.g. _Comment_, _Constant_, and _Identifier_
109106

110107
*Note:* Defaults to 'Statement' when not set.
111108

112-
This controls the highlight of the following common keywords in playbooks: `include until retries delay when only_if become become_user block rescue always notify`
109+
This option change the highlight of the following common keywords in playbooks: `include until retries delay when only_if become become_user block rescue always notify`
113110

114111
##### g:ansible_with_keywords_highlight
115112
`let g:ansible_with_keywords_highlight = 'Constant'`
116113

117-
This option accepts the first line of each group in `:help E669` - thus the first 3 are _Comment_, _Constant_, and _Identifier_
114+
Accepts any syntax group-name from `:help E669` - e.g. _Comment_, _Constant_, and _Identifier_
118115

119116
*Note:* Defaults to 'Statement' when not set.
120117

121-
This controls the highlight of all `with_.+` keywords in playbooks.
118+
This option changes the highlight of all `with_.+` keywords in playbooks.
119+
120+
##### g:ansible_template_syntaxes
121+
`let g:ansible_template_syntaxes = { '*.rb.j2': 'ruby' }`
122+
123+
Accepts a dictionary in the form of `'regex-for-file': 'filetype'`.
124+
- _regex-for-file_ will receive the full filepath, so directory matching can be done.
125+
- _filetype_ is the root filetype to be applied, `jinja2` will be automatically appended
126+
127+
All files ending in `*.j2` that aren't matched will simply get the `jinja2` filetype.
122128

123129
## bugs, suggestions/requests, & contributions
124130

@@ -131,9 +137,3 @@ Indenting a full document - e.g with `gg=G` - will not be supported and is not a
131137
##### suggestions/requests
132138

133139
Suggestions for improvements are welcome, pull-requests with completed features even more so. :)
134-
135-
##### contributions
136-
137-
Thanks to:
138-
139-
- The developers of `salt-vim` for parts of the original YAML implementation this is based on

ftdetect/ansible.vim

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ function! s:isAnsible()
1212
return 0
1313
endfunction
1414

15-
:au BufNewFile,BufRead * if s:isAnsible() | set ft=ansible | en
16-
:au BufNewFile,BufRead *.j2 set ft=ansible_template
17-
:au BufNewFile,BufRead hosts set ft=ansible_hosts
15+
function! s:setupTemplate()
16+
if exists("g:ansible_template_syntaxes")
17+
let filepath = expand("%:p")
18+
for syntax_name in items(g:ansible_template_syntaxes)
19+
let s:syntax_string = '\v/'.syntax_name[0]
20+
if filepath =~ s:syntax_string
21+
execute 'set ft='.syntax_name[1].'.jinja2'
22+
return
23+
endif
24+
endfor
25+
endif
26+
set ft=jinja2
27+
endfunction
28+
29+
au BufNewFile,BufRead * if s:isAnsible() | set ft=yaml.ansible | en
30+
au BufNewFile,BufRead *.j2 call s:setupTemplate()
31+
au BufNewFile,BufRead hosts set ft=ansible_hosts

ftplugin/ansible.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
if exists('+regexpengine') && ('&regexpengine' == 0)
33
setlocal regexpengine=1
44
endif
5+
set isfname+=@-@
56
set path+=./../templates,./../files,templates,files

syntax/ansible.vim

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
" Vim syntax file
22
" Language: Ansible YAML/Jinja templates
33
" Maintainer: Dave Honneffer <[email protected]>
4-
" Last Change: 2015.09.06
5-
6-
if exists("b:current_syntax")
7-
finish
8-
endif
4+
" Last Change: 2018.02.08
95

106
if !exists("main_syntax")
117
let main_syntax = 'yaml'
128
endif
139

14-
let b:current_syntax = ''
15-
unlet b:current_syntax
16-
runtime! syntax/yaml.vim
17-
18-
let b:current_syntax = ''
19-
unlet b:current_syntax
20-
syntax include @Yaml syntax/yaml.vim
10+
if exists('b:current_syntax')
11+
let s:current_syntax=b:current_syntax
12+
unlet b:current_syntax
13+
endif
2114

22-
let b:current_syntax = ''
23-
unlet b:current_syntax
2415
syntax include @Jinja syntax/jinja2.vim
2516

17+
if exists('s:current_syntax')
18+
let b:current_syntax=s:current_syntax
19+
endif
20+
2621
" Jinja
2722
" ================================
2823

@@ -37,65 +32,22 @@ highlight link jinjaVarDelim Delimiter
3732
" YAML
3833
" ================================
3934

35+
if exists("g:ansible_yamlKeyName")
36+
let s:yamlKey = g:ansible_yamlKeyName
37+
else
38+
let s:yamlKey = "yamlBlockMappingKey"
39+
endif
40+
4041
" Reset some YAML to plain styling
4142
" the number 80 in Ansible isn't any more important than the word root
4243
highlight link yamlInteger NONE
4344
highlight link yamlBool NONE
4445
highlight link yamlFlowString NONE
4546
" but it does make sense we visualize quotes easily
4647
highlight link yamlFlowStringDelimiter Delimiter
47-
48-
fun! s:normal_keywords_highlight(name)
49-
if a:name == 'Comment'
50-
highlight link ansible_normal_keywords Comment
51-
elseif a:name == 'Constant'
52-
highlight link ansible_normal_keywords Constant
53-
elseif a:name == 'Identifier'
54-
highlight link ansible_normal_keywords Identifier
55-
elseif a:name == 'Statement'
56-
highlight link ansible_normal_keywords Statement
57-
elseif a:name == 'PreProc'
58-
highlight link ansible_normal_keywords PreProc
59-
elseif a:name == 'Type'
60-
highlight link ansible_normal_keywords Type
61-
elseif a:name == 'Special'
62-
highlight link ansible_normal_keywords Special
63-
elseif a:name == 'Underlined'
64-
highlight link ansible_normal_keywords Underlined
65-
elseif a:name == 'Ignore'
66-
highlight link ansible_normal_keywords Ignore
67-
elseif a:name == 'Error'
68-
highlight link ansible_normal_keywords Error
69-
elseif a:name == 'Todo'
70-
highlight link ansible_normal_keywords Todo
71-
endif
72-
endfun
73-
74-
fun! s:with_keywords_highlight(name)
75-
if a:name == 'Comment'
76-
highlight link ansible_with_keywords Comment
77-
elseif a:name == 'Constant'
78-
highlight link ansible_with_keywords Constant
79-
elseif a:name == 'Identifier'
80-
highlight link ansible_with_keywords Identifier
81-
elseif a:name == 'Statement'
82-
highlight link ansible_with_keywords Statement
83-
elseif a:name == 'PreProc'
84-
highlight link ansible_with_keywords PreProc
85-
elseif a:name == 'Type'
86-
highlight link ansible_with_keywords Type
87-
elseif a:name == 'Special'
88-
highlight link ansible_with_keywords Special
89-
elseif a:name == 'Underlined'
90-
highlight link ansible_with_keywords Underlined
91-
elseif a:name == 'Ignore'
92-
highlight link ansible_with_keywords Ignore
93-
elseif a:name == 'Error'
94-
highlight link ansible_with_keywords Error
95-
elseif a:name == 'Todo'
96-
highlight link ansible_with_keywords Todo
97-
endif
98-
endfun
48+
" This is only found in stephypy/vim-yaml, since it's one line it isn't worth
49+
" making conditional
50+
highlight link yamlConstant NONE
9951

10052
fun! s:attribute_highlight(attributes)
10153
if a:attributes =~ 'a'
@@ -119,32 +71,32 @@ else
11971
endif
12072

12173
if exists("g:ansible_name_highlight")
122-
syn keyword ansible_name name containedin=yamlBlockMappingKey contained
74+
execute 'syn keyword ansible_name name containedin='.s:yamlKey.' contained'
12375
if g:ansible_name_highlight =~ 'd'
12476
highlight link ansible_name Comment
12577
else
12678
highlight link ansible_name Underlined
12779
endif
12880
endif
12981

130-
syn keyword ansible_debug_keywords debug containedin=yamlBlockMappingKey contained
82+
execute 'syn keyword ansible_debug_keywords debug containedin='.s:yamlKey.' contained'
13183
highlight link ansible_debug_keywords Debug
13284

13385
if exists("g:ansible_extra_keywords_highlight")
134-
syn keyword ansible_extra_special_keywords register always_run changed_when failed_when no_log args vars delegate_to ignore_errors containedin=yamlBlockMappingKey contained
86+
execute 'syn keyword ansible_extra_special_keywords register always_run changed_when failed_when no_log args vars delegate_to ignore_errors containedin='.s:yamlKey.' contained'
13587
highlight link ansible_extra_special_keywords Statement
13688
endif
13789

138-
syn keyword ansible_normal_keywords include include_tasks import_tasks until retries delay when only_if become become_user block rescue always notify containedin=yamlBlockMappingKey contained
90+
execute 'syn keyword ansible_normal_keywords include include_tasks import_tasks until retries delay when only_if become become_user block rescue always notify containedin='.s:yamlKey.' contained'
13991
if exists("g:ansible_normal_keywords_highlight")
140-
call s:normal_keywords_highlight(g:ansible_normal_keywords_highlight)
92+
execute 'highlight link ansible_normal_keywords '.g:ansible_normal_keywords_highlight
14193
else
14294
highlight link ansible_normal_keywords Statement
14395
endif
14496

145-
syn match ansible_with_keywords "\vwith_.+" containedin=yamlBlockMappingKey contained
97+
execute 'syn match ansible_with_keywords "\vwith_.+" containedin='.s:yamlKey.' contained'
14698
if exists("g:ansible_with_keywords_highlight")
147-
call s:with_keywords_highlight(g:ansible_with_keywords_highlight)
99+
execute 'highlight link ansible_with_keywords '.g:ansible_with_keywords_highlight
148100
else
149101
highlight link ansible_with_keywords Statement
150102
endif

syntax/ansible_template.vim

Lines changed: 0 additions & 27 deletions
This file was deleted.

syntax/jinja2.vim

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
" Vim syntax file
2-
" Language: Jinja template
3-
" Maintainer: Armin Ronacher <[email protected]>
4-
" Last Change: 2008 May 9
5-
" Version: 1.1
6-
"
7-
" Known Bugs:
8-
" because of odd limitations dicts and the modulo operator
9-
" appear wrong in the template.
10-
"
11-
" Changes:
12-
"
13-
" 2008 May 9: Added support for Jinja2 changes (new keyword rules)
14-
15-
" .vimrc variable to disable html highlighting
16-
if !exists('g:jinja_syntax_html')
17-
let g:jinja_syntax_html=1
18-
endif
2+
" Language: Jinja2 - with special modifications for compound-filetype
3+
" compatibility
4+
" Maintainer: Dave Honneffer <[email protected]>
5+
" Last Change: 2018.02.11
196

20-
" For version 5.x: Clear all syntax items
21-
" For version 6.x: Quit when a syntax file was already loaded
227
if !exists("main_syntax")
23-
if version < 600
24-
syntax clear
25-
elseif exists("b:current_syntax")
26-
finish
27-
endif
28-
let main_syntax = 'jinja'
8+
let main_syntax = 'jinja2'
299
endif
3010

31-
" Pull in the HTML syntax.
32-
if g:jinja_syntax_html
33-
if version < 600
34-
so <sfile>:p:h/html.vim
35-
else
36-
runtime! syntax/html.vim
37-
unlet b:current_syntax
38-
endif
39-
endif
11+
let b:current_syntax = ''
12+
unlet b:current_syntax
4013

4114
syntax case match
4215

@@ -93,15 +66,8 @@ syn match jinjaStatement containedin=jinjaTagBlock contained /\<with\(out\)\?\s\
9366

9467

9568
" Define the default highlighting.
96-
" For version 5.7 and earlier: only when not done already
97-
" For version 5.8 and later: only when an item doesn't have highlighting yet
98-
if version >= 508 || !exists("did_jinja_syn_inits")
99-
if version < 508
100-
let did_jinja_syn_inits = 1
101-
command -nargs=+ HiLink hi link <args>
102-
else
103-
command -nargs=+ HiLink hi def link <args>
104-
endif
69+
if !exists("did_jinja_syn_inits")
70+
command -nargs=+ HiLink hi def link <args>
10571

10672
HiLink jinjaPunctuation jinjaOperator
10773
HiLink jinjaAttribute jinjaVariable
@@ -128,8 +94,4 @@ if version >= 508 || !exists("did_jinja_syn_inits")
12894
delcommand HiLink
12995
endif
13096

131-
let b:current_syntax = "jinja"
132-
133-
if main_syntax == 'jinja'
134-
unlet main_syntax
135-
endif
97+
let b:current_syntax = "jinja2"

0 commit comments

Comments
 (0)