Skip to content

Commit

Permalink
Add ansible support, closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Dec 17, 2015
1 parent 7cbd509 commit de3110b
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Optionally download one of the [releases](https://github.com/sheerun/vim-polyglo

## Language packs

- [ansible](https://github.com/pearofducks/ansible-vim) (syntax, indent, ftplugin, ftdetect)
- [arduino](https://github.com/sudar/vim-arduino-syntax) (syntax, indent, ftdetect)
- [blade](https://github.com/jwalton512/vim-blade) (syntax, indent, ftdetect)
- [c++11](https://github.com/octol/vim-cpp-enhanced-highlight) (syntax)
Expand Down
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ EOF
}

PACKS="
ansible:pearofducks/ansible-vim
arduino:sudar/vim-arduino-syntax
blade:jwalton512/vim-blade
c++11:octol/vim-cpp-enhanced-highlight
Expand Down
15 changes: 15 additions & 0 deletions ftdetect/polyglot.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

function! DetectAnsible()
let filepath = expand("%:p")
let filename = expand("%:t")
if filepath =~ '\v/(tasks|roles)/.*\.ya?ml$' || filepath =~ '\v/(group|host)_vars/' || filename =~ '\v(playbook|site)\.ya?ml$'
set ft=ansible
endif
unlet filepath
unlet filename
endfunction
:au BufNewFile,BufRead *.yml,*yaml,*/{group,host}_vars/* call DetectAnsible()
:au BufNewFile,BufRead *.j2 set ft=ansible_template
:au BufNewFile,BufRead hosts set ft=ansible_hosts
endif
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'arduino') == -1

au BufRead,BufNewFile *.ino,*.pde set filetype=arduino
Expand Down
8 changes: 8 additions & 0 deletions ftplugin/ansible.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

" Slow yaml highlighting workaround
if exists('+regexpengine') && ('&regexpengine' == 0)
setlocal regexpengine=1
endif

endif
58 changes: 58 additions & 0 deletions indent/ansible.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

let s:save_cpo = &cpo
set cpo&vim

setlocal indentexpr=GetAnsibleIndent(v:lnum)
setlocal indentkeys=!^F,o,O,0#,0},0],<:>,-,*<Return>
setlocal nosmartindent
setlocal expandtab
setlocal softtabstop=2
setlocal shiftwidth=2
setlocal commentstring=#%s
setlocal formatoptions=cl
" c -> wrap long comments, including #
" l -> do not wrap long lines

let s:comment = '\v^\s*#' " # comment
let s:array_entry = '\v^\s*-\s' " - foo
let s:named_module_entry = '\v^\s*-\s*(name|hosts):\s*\S' " - name: 'do stuff'
let s:dictionary_entry = '\v^\s*[^:-]+:\s*$' " with_items:
let s:key_value = '\v^\s*[^:-]+:\s*\S' " apt: name=package
let s:scalar_value = '\v:\s*[>|\|]\s*$' " shell: >

if exists('*GetAnsibleIndent')
finish
endif

function GetAnsibleIndent(lnum)
if a:lnum == 1 || !prevnonblank(a:lnum-1)
return 0
endif
let prevlnum = prevnonblank(a:lnum - 1)
let maintain = indent(prevlnum)
let increase = maintain + &sw

let line = getline(prevlnum)
if line =~ s:array_entry
if line =~ s:named_module_entry
return increase
else
return maintain
endif
elseif line =~ s:dictionary_entry
return increase
elseif line =~ s:key_value
if line =~ s:scalar_value
return increase
else
return maintain
endif
else
return maintain
endif
endfunction

let &cpo = s:save_cpo

endif
90 changes: 90 additions & 0 deletions syntax/ansible.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

" Vim syntax file
" Language: Ansible YAML/Jinja templates
" Maintainer: Dave Honneffer <[email protected]>
" Last Change: 2015.09.06

if exists("b:current_syntax")
finish
endif

if !exists("main_syntax")
let main_syntax = 'yaml'
endif

let b:current_syntax = ''
unlet b:current_syntax
runtime! syntax/yaml.vim

let b:current_syntax = ''
unlet b:current_syntax
syntax include @Yaml syntax/yaml.vim

let b:current_syntax = ''
unlet b:current_syntax
syntax include @Jinja syntax/jinja2.vim

" Jinja
" ================================

syn cluster jinjaSLSBlocks add=jinjaTagBlock,jinjaVarBlock,jinjaComment
" https://github.com/mitsuhiko/jinja2/blob/6b7c0c23/ext/Vim/jinja.vim
syn region jinjaTagBlock matchgroup=jinjaTagDelim start=/{%-\?/ end=/-\?%}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment,@jinjaSLSBlocks
syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment,@jinjaSLSBlocks
syn region jinjaComment matchgroup=jinjaCommentDelim start="{#" end="#}" containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaString,@jinjaSLSBlocks
highlight link jinjaVariable Constant
highlight link jinjaVarDelim Delimiter

" YAML
" ================================

" Reset some YAML to plain styling
" the number 80 in Ansible isn't any more important than the word root
highlight link yamlInteger NONE
highlight link yamlBool NONE
highlight link yamlFlowString NONE
" but it does make sense we visualize quotes easily
highlight link yamlFlowStringDelimiter Delimiter

fun! s:attribute_highlight(attributes)
if a:attributes =~ 'a'
syn match ansible_attributes "\v\w+\=" containedin=yamlPlainScalar
else
syn match ansible_attributes "\v^\s*\w+\=" containedin=yamlPlainScalar
endif
if a:attributes =~ 'n'
highlight link ansible_attributes NONE
elseif a:attributes =~ 'd'
highlight link ansible_attributes Comment
else
highlight link ansible_attributes Structure
endif
endfun

if exists("g:ansible_attribute_highlight")
call s:attribute_highlight(g:ansible_attribute_highlight)
else
call s:attribute_highlight('ad')
endif

if exists("g:ansible_name_highlight")
syn keyword ansible_name name containedin=yamlBlockMappingKey contained
if g:ansible_name_highlight =~ 'd'
highlight link ansible_name Comment
else
highlight link ansible_name Underlined
endif
endif

syn keyword ansible_debug_keywords debug containedin=yamlBlockMappingKey contained
highlight link ansible_debug_keywords Debug

syn match ansible_with_keywords "\vwith_.+" containedin=yamlBlockMappingKey contained
syn keyword ansible_special_keywords include until retries delay when only_if become become_user block rescue always notify containedin=yamlBlockMappingKey contained
highlight link ansible_with_keywords Statement
highlight link ansible_special_keywords Statement

let b:current_syntax = "ansible"

endif
35 changes: 35 additions & 0 deletions syntax/ansible_hosts.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

" Vim syntax file
" Language: Ansible hosts files
" Maintainer: Dave Honneffer <[email protected]>
" Last Change: 2015.09.23

if exists("b:current_syntax")
finish
endif

syn case ignore
syn match hostsFirstWord "\v^\S+"
syn match hostsAttributes "\v\S*\="
syn region hostsHeader start="\v^\s*\[" end="\v\]"
syn keyword hostsHeaderSpecials children vars containedin=hostsHeader contained
syn match hostsComment "\v^[#;].*$"

highlight link hostsFirstWord Label
highlight link hostsHeader Define
highlight link hostsComment Comment
highlight link hostsHeaderSpecials Identifier
highlight link hostsAttributes Structure

if exists("g:ansible_attribute_highlight")
if g:ansible_attribute_highlight =~ 'n'
highlight link hostsAttributes NONE
elseif g:ansible_attribute_highlight =~ 'd'
highlight link hostsAttributes Comment
endif
endif

let b:current_syntax = "ansible_hosts"

endif
31 changes: 31 additions & 0 deletions syntax/ansible_template.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ansible') == -1

" Vim syntax file
" Language: Ansible YAML/Jinja templates
" Maintainer: Dave Honneffer <[email protected]>
" Last Change: 2015.09.06

if exists("b:current_syntax")
finish
endif

if !exists("main_syntax")
let main_syntax = 'jinja2'
endif

let b:current_syntax = ''
unlet b:current_syntax
runtime! syntax/jinja2.vim

if exists("g:ansible_extra_syntaxes")
let s:extra_syntax = split(g:ansible_extra_syntaxes)
for syntax_name in s:extra_syntax
let b:current_syntax = ''
unlet b:current_syntax
execute 'runtime!' "syntax/" . syntax_name
endfor
endif

let b:current_syntax = "ansible_template"

endif
Loading

0 comments on commit de3110b

Please sign in to comment.