Skip to content

Commit 39e245e

Browse files
committed
First
0 parents  commit 39e245e

File tree

8 files changed

+437
-0
lines changed

8 files changed

+437
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Dave Honneffer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## introduction
2+
3+
This is a vim syntax plugin for Ansible, it supports YAML playbooks, Jinja2 templates, and dotini-based 'hosts' files.
4+
5+
- YAML playbooks are detected if:
6+
- they are in the `group_vars` or `host_vars` folder
7+
- they are in the `tasks` or `roles` folder and have either a *.yml* or *.yaml* suffix
8+
- they are named `playbook.y(a)ml` or `site.y(a)ml`
9+
- Jinja2 templates are detected if they have a *.j2* suffix
10+
- Files named `hosts` will be treated as Ansible hosts files
11+
12+
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.
13+
14+
##### examples
15+
16+
Bright (and selective highlight) | Dim
17+
:-----------------------------------:|:-------------------------:
18+
![](http://i.imgur.com/whBOZZK.png) | ![](http://i.imgur.com/XS0T00e.png)
19+
20+
## options
21+
22+
##### g:ansible_extra_syntaxes
23+
`let g:ansible_extra_syntaxes = "sh.vim ruby.vim"`
24+
25+
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`.
26+
27+
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.
28+
29+
This will *always* load these syntaxes for *all* .j2 files, and should be considered a bit of a (temporary?) hack/workaround.
30+
31+
##### g:ansible_attribute_highlight
32+
`let g:ansible_attribute_highlight = "ob"`
33+
34+
Ansible modules use a `key=value` format for specifying module-attributes in playbooks. This highlights those as specified.
35+
36+
Available flags (bold are defaults):
37+
38+
- **a**: highlight *all* instances of `key=`
39+
- o: highlight *only* instances of `key=` found on newlines
40+
- **d**: *dim* the instances of `key=` found
41+
- b: *brighten* the instances of `key=` found
42+
- n: turn this highlight off completely
43+
44+
##### g:ansible_name_highlight
45+
`let g:ansible_name_highlight = 'd'`
46+
47+
Ansible modules commonly start with a `name:` key for self-documentation of playbooks. This option enables/changes highlight of this.
48+
49+
Available flags (this feature is off by default):
50+
51+
- d: *dim* the instances of `name:` found
52+
- b: *brighten* the instances of `name:` found
53+
54+
## installation
55+
56+
Use your favorite plugin manager, or try [vim-plug](https://github.com/junegunn/vim-plug) if you're looking for a really nice one!
57+
58+
##### vim-plug
59+
`Plug 'pearofducks/ansible-vim'`
60+
61+
##### vundle
62+
`Plugin 'pearofducks/ansible-vim'`
63+
64+
## bugs, suggestions/requests, & contributions
65+
66+
##### bugs
67+
68+
It's unlikely that there will be bugs in highlighting that don't exist in the core format. Where appropriate these will be fixed in this plugin, but if the problem is with the original syntax we should probably focus on fixing that instead.
69+
70+
Indenting a full document - e.g with `gg=G` - will not be supported and is not a goal of this plugin (unless someone else develops it!). Please do not file a bug report on this.
71+
72+
##### suggestions/requests
73+
74+
Suggestions for improvements are welcome, pull-requests with completed features even more so. :)
75+
76+
If you make a pull-request please also add your (user)name to the list of contributors!
77+
78+
##### contributions
79+
80+
Thanks to:
81+
82+
- The developers of `salt-vim` for parts of the original YAML implementation this is based on

ftdetect/ansible.vim

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function! DetectAnsible()
2+
let filepath = expand("%:p")
3+
let filename = expand("%:t")
4+
if filepath =~ '\v/(tasks|roles)/.*\.ya?ml$' || filepath =~ '\v/(group|host)_vars/' || filename =~ '\v(playbook|site)\.ya?ml$'
5+
set ft=ansible
6+
endif
7+
unlet filepath
8+
unlet filename
9+
endfunction
10+
11+
:au BufNewFile,BufRead *.yml,*yaml,*/{group,host}_vars/* call DetectAnsible()
12+
:au BufNewFile,BufRead *.j2 set ft=ansible_template " do we have jinja2 available?
13+
:au BufNewFile,BufRead hosts set ft=dosini
14+
15+
" if hosts file, set dosini and patch for :
16+
" if *.j2, set jinja
17+
" if *.yml or *.yaml - look in the directory to detect
18+
" or the filename (site.yaml, playbook.yaml)
19+
" or if the file rests exactly inside of group_vars or host_vars

ftplugin/ansible.vim

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
" Slow yaml highlighting workaround
2+
if exists('+regexpengine') && ('&regexpengine' == 0)
3+
setlocal regexpengine=1
4+
endif
5+
setlocal expandtab
6+
setlocal softtabstop=2
7+
setlocal shiftwidth=2
8+
setlocal commentstring=#%s
9+
setlocal formatoptions=crl
10+
setlocal autoindent
11+
12+
" This function is from https://gist.github.com/871107
13+
" Author: Ian Young
14+
function! GetYamlIndent()
15+
let cnum = v:lnum
16+
let lnum = v:lnum - 1
17+
if lnum == 0
18+
return 0
19+
endif
20+
let line = substitute(getline(lnum),'\s\+$','','')
21+
let cline = substitute(getline(cnum),'\s\+$','','')
22+
let indent = indent(lnum)
23+
let increase = indent + &sw
24+
let decrease = indent - &sw
25+
if line =~ ':$'
26+
return increase
27+
elseif line !~ ':$' && cline =~ ':$'
28+
return decrease
29+
elseif line =~ ':$'
30+
else
31+
return indent
32+
endif
33+
endfunction
34+
setlocal indentexpr=GetYamlIndent()
35+
36+
" folding
37+
setlocal foldmethod=indent
38+
setlocal foldlevel=6 " by default do not fold

syntax/ansible.vim

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
" Vim syntax file
2+
" Language: Ansible YAML/Jinja templates
3+
" Maintainer: Dave Honneffer <[email protected]>
4+
" Last Change: 2015.09.06
5+
6+
if exists("b:current_syntax")
7+
finish
8+
endif
9+
10+
if !exists("main_syntax")
11+
let main_syntax = 'yaml'
12+
endif
13+
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
21+
22+
let b:current_syntax = ''
23+
unlet b:current_syntax
24+
syntax include @Jinja syntax/jinja2.vim
25+
26+
" Jinja
27+
" ================================
28+
29+
syn cluster jinjaSLSBlocks add=jinjaTagBlock,jinjaVarBlock,jinjaComment
30+
" https://github.com/mitsuhiko/jinja2/blob/6b7c0c23/ext/Vim/jinja.vim
31+
syn region jinjaTagBlock matchgroup=jinjaTagDelim start=/{%-\?/ end=/-\?%}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment,@jinjaSLSBlocks
32+
syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment,@jinjaSLSBlocks
33+
syn region jinjaComment matchgroup=jinjaCommentDelim start="{#" end="#}" containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaString,@jinjaSLSBlocks
34+
highlight link jinjaVariable Constant
35+
highlight link jinjaVarDelim Delimiter
36+
37+
" YAML
38+
" ================================
39+
40+
" Reset some YAML to plain styling
41+
" the number 80 in Ansible isn't any more important than the word root
42+
highlight link yamlInteger Ignore
43+
highlight link yamlBool Ignore
44+
highlight link yamlFlowString Ignore
45+
" but it does make sense we visualize quotes easily
46+
highlight link yamlFlowStringDelimiter Delimiter
47+
48+
fun! s:attribute_highlight(attributes)
49+
if a:attributes =~ 'a'
50+
syn match ansible_attributes "\v\w+\=" containedin=yamlPlainScalar
51+
else
52+
syn match ansible_attributes "\v^\s*\w+\=" containedin=yamlPlainScalar
53+
endif
54+
if a:attributes =~ 'n'
55+
highlight link ansible_attributes Ignore
56+
elseif a:attributes =~ 'd'
57+
highlight link ansible_attributes Comment
58+
else
59+
highlight link ansible_attributes Structure
60+
endif
61+
endfun
62+
63+
if exists("g:ansible_attribute_highlight")
64+
call s:attribute_highlight(g:ansible_attribute_highlight)
65+
else
66+
call s:attribute_highlight('ad')
67+
endif
68+
69+
if exists("g:ansible_name_highlight")
70+
syn keyword ansible_name name containedin=yamlBlockMappingKey contained
71+
if g:ansible_name_highlight =~ 'd'
72+
highlight link ansible_name Comment
73+
else
74+
highlight link ansible_name Underlined
75+
endif
76+
endif
77+
78+
syn keyword ansible_debug_keywords debug containedin=yamlBlockMappingKey contained
79+
highlight link ansible_debug_keywords Debug
80+
81+
syn match ansible_with_keywords "\vwith_.+" containedin=yamlBlockMappingKey contained
82+
syn keyword ansible_special_keywords include until retries delay when only_if become become_user block rescue always notify containedin=yamlBlockMappingKey contained
83+
highlight link ansible_with_keywords Statement
84+
highlight link ansible_special_keywords Statement
85+
86+
let b:current_syntax = "ansible"

syntax/ansible_template.vim

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
" Vim syntax file
2+
" Language: Ansible YAML/Jinja templates
3+
" Maintainer: Dave Honneffer <[email protected]>
4+
" Last Change: 2015.09.06
5+
6+
if exists("b:current_syntax")
7+
finish
8+
endif
9+
10+
if !exists("main_syntax")
11+
let main_syntax = 'jinja2'
12+
endif
13+
14+
let b:current_syntax = ''
15+
unlet b:current_syntax
16+
runtime! syntax/jinja2.vim
17+
18+
let s:extra_syntax = split(g:ansible_extra_syntaxes)
19+
for syntax_name in s:extra_syntax
20+
let b:current_syntax = ''
21+
unlet b:current_syntax
22+
execute 'runtime!' "syntax/" . syntax_name
23+
endfor
24+
25+
let b:current_syntax = "ansible_template"

0 commit comments

Comments
 (0)