Skip to content

Commit

Permalink
v2 - Change to yaml.ansible and *.jinja2 (#55)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
pearofducks committed Mar 29, 2018
1 parent aa34a39 commit 01d0e7d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 169 deletions.
36 changes: 18 additions & 18 deletions README.md
Expand Up @@ -22,10 +22,10 @@ This is a vim syntax plugin for Ansible 2.0, it supports YAML playbooks, Jinja2
- Jinja2 templates are detected if they have a *.j2* suffix
- Files named `hosts` will be treated as Ansible hosts files

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:
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:

```vim
au BufRead,BufNewFile */playbooks/*.yml set filetype=ansible
au BufRead,BufNewFile */playbooks/*.yml set filetype=yaml.ansible
```

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.
Expand Down Expand Up @@ -61,14 +61,11 @@ Use your favorite plugin manager, or try [vim-plug](https://github.com/junegunn/

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.

##### g:ansible_extra_syntaxes
`let g:ansible_extra_syntaxes = "sh.vim ruby.vim"`
##### g:ansible_yamlKeyName

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`.
`let g:ansible_yamlKeyName = 'yamlKey'`

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.

This will *always* load these syntaxes for *all* .j2 files, and should be considered a bit of a (temporary?) hack/workaround.
This option exists to provide additional compatibility with [stephpy/vim-yaml](https://github.com/stephpy/vim-yaml).

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

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

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

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`
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`

##### g:ansible_with_keywords_highlight
`let g:ansible_with_keywords_highlight = 'Constant'`

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

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

This controls the highlight of all `with_.+` keywords in playbooks.
This option changes the highlight of all `with_.+` keywords in playbooks.

##### g:ansible_template_syntaxes
`let g:ansible_template_syntaxes = { '*.rb.j2': 'ruby' }`

Accepts a dictionary in the form of `'regex-for-file': 'filetype'`.
- _regex-for-file_ will receive the full filepath, so directory matching can be done.
- _filetype_ is the root filetype to be applied, `jinja2` will be automatically appended

All files ending in `*.j2` that aren't matched will simply get the `jinja2` filetype.

## bugs, suggestions/requests, & contributions

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

Suggestions for improvements are welcome, pull-requests with completed features even more so. :)

##### contributions

Thanks to:

- The developers of `salt-vim` for parts of the original YAML implementation this is based on
20 changes: 17 additions & 3 deletions ftdetect/ansible.vim
Expand Up @@ -12,6 +12,20 @@ function! s:isAnsible()
return 0
endfunction

:au BufNewFile,BufRead * if s:isAnsible() | set ft=ansible | en
:au BufNewFile,BufRead *.j2 set ft=ansible_template
:au BufNewFile,BufRead hosts set ft=ansible_hosts
function! s:setupTemplate()
if exists("g:ansible_template_syntaxes")
let filepath = expand("%:p")
for syntax_name in items(g:ansible_template_syntaxes)
let s:syntax_string = '\v/'.syntax_name[0]
if filepath =~ s:syntax_string
execute 'set ft='.syntax_name[1].'.jinja2'
return
endif
endfor
endif
set ft=jinja2
endfunction

au BufNewFile,BufRead * if s:isAnsible() | set ft=yaml.ansible | en
au BufNewFile,BufRead *.j2 call s:setupTemplate()
au BufNewFile,BufRead hosts set ft=ansible_hosts
1 change: 1 addition & 0 deletions ftplugin/ansible.vim
Expand Up @@ -2,4 +2,5 @@
if exists('+regexpengine') && ('&regexpengine' == 0)
setlocal regexpengine=1
endif
set isfname+=@-@
set path+=./../templates,./../files,templates,files
98 changes: 25 additions & 73 deletions syntax/ansible.vim
@@ -1,28 +1,23 @@
" Vim syntax file
" Language: Ansible YAML/Jinja templates
" Maintainer: Dave Honneffer <pearofducks@gmail.com>
" Last Change: 2015.09.06

if exists("b:current_syntax")
finish
endif
" Last Change: 2018.02.08

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
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
unlet b:current_syntax
endif

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

if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
endif

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

Expand All @@ -37,65 +32,22 @@ highlight link jinjaVarDelim Delimiter
" YAML
" ================================

if exists("g:ansible_yamlKeyName")
let s:yamlKey = g:ansible_yamlKeyName
else
let s:yamlKey = "yamlBlockMappingKey"
endif

" 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:normal_keywords_highlight(name)
if a:name == 'Comment'
highlight link ansible_normal_keywords Comment
elseif a:name == 'Constant'
highlight link ansible_normal_keywords Constant
elseif a:name == 'Identifier'
highlight link ansible_normal_keywords Identifier
elseif a:name == 'Statement'
highlight link ansible_normal_keywords Statement
elseif a:name == 'PreProc'
highlight link ansible_normal_keywords PreProc
elseif a:name == 'Type'
highlight link ansible_normal_keywords Type
elseif a:name == 'Special'
highlight link ansible_normal_keywords Special
elseif a:name == 'Underlined'
highlight link ansible_normal_keywords Underlined
elseif a:name == 'Ignore'
highlight link ansible_normal_keywords Ignore
elseif a:name == 'Error'
highlight link ansible_normal_keywords Error
elseif a:name == 'Todo'
highlight link ansible_normal_keywords Todo
endif
endfun

fun! s:with_keywords_highlight(name)
if a:name == 'Comment'
highlight link ansible_with_keywords Comment
elseif a:name == 'Constant'
highlight link ansible_with_keywords Constant
elseif a:name == 'Identifier'
highlight link ansible_with_keywords Identifier
elseif a:name == 'Statement'
highlight link ansible_with_keywords Statement
elseif a:name == 'PreProc'
highlight link ansible_with_keywords PreProc
elseif a:name == 'Type'
highlight link ansible_with_keywords Type
elseif a:name == 'Special'
highlight link ansible_with_keywords Special
elseif a:name == 'Underlined'
highlight link ansible_with_keywords Underlined
elseif a:name == 'Ignore'
highlight link ansible_with_keywords Ignore
elseif a:name == 'Error'
highlight link ansible_with_keywords Error
elseif a:name == 'Todo'
highlight link ansible_with_keywords Todo
endif
endfun
" This is only found in stephypy/vim-yaml, since it's one line it isn't worth
" making conditional
highlight link yamlConstant NONE

fun! s:attribute_highlight(attributes)
if a:attributes =~ 'a'
Expand All @@ -119,32 +71,32 @@ else
endif

if exists("g:ansible_name_highlight")
syn keyword ansible_name name containedin=yamlBlockMappingKey contained
execute 'syn keyword ansible_name name containedin='.s:yamlKey.' 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
execute 'syn keyword ansible_debug_keywords debug containedin='.s:yamlKey.' contained'
highlight link ansible_debug_keywords Debug

if exists("g:ansible_extra_keywords_highlight")
syn keyword ansible_extra_special_keywords register always_run changed_when failed_when no_log args vars delegate_to ignore_errors containedin=yamlBlockMappingKey contained
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'
highlight link ansible_extra_special_keywords Statement
endif

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
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'
if exists("g:ansible_normal_keywords_highlight")
call s:normal_keywords_highlight(g:ansible_normal_keywords_highlight)
execute 'highlight link ansible_normal_keywords '.g:ansible_normal_keywords_highlight
else
highlight link ansible_normal_keywords Statement
endif

syn match ansible_with_keywords "\vwith_.+" containedin=yamlBlockMappingKey contained
execute 'syn match ansible_with_keywords "\vwith_.+" containedin='.s:yamlKey.' contained'
if exists("g:ansible_with_keywords_highlight")
call s:with_keywords_highlight(g:ansible_with_keywords_highlight)
execute 'highlight link ansible_with_keywords '.g:ansible_with_keywords_highlight
else
highlight link ansible_with_keywords Statement
endif
Expand Down
27 changes: 0 additions & 27 deletions syntax/ansible_template.vim

This file was deleted.

58 changes: 10 additions & 48 deletions syntax/jinja2.vim
@@ -1,42 +1,15 @@
" Vim syntax file
" Language: Jinja template
" Maintainer: Armin Ronacher <armin.ronacher@active-4.com>
" Last Change: 2008 May 9
" Version: 1.1
"
" Known Bugs:
" because of odd limitations dicts and the modulo operator
" appear wrong in the template.
"
" Changes:
"
" 2008 May 9: Added support for Jinja2 changes (new keyword rules)

" .vimrc variable to disable html highlighting
if !exists('g:jinja_syntax_html')
let g:jinja_syntax_html=1
endif
" Language: Jinja2 - with special modifications for compound-filetype
" compatibility
" Maintainer: Dave Honneffer <pearofducks@gmail.com>
" Last Change: 2018.02.11

" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'jinja'
let main_syntax = 'jinja2'
endif

" Pull in the HTML syntax.
if g:jinja_syntax_html
if version < 600
so <sfile>:p:h/html.vim
else
runtime! syntax/html.vim
unlet b:current_syntax
endif
endif
let b:current_syntax = ''
unlet b:current_syntax

syntax case match

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


" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_jinja_syn_inits")
if version < 508
let did_jinja_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
if !exists("did_jinja_syn_inits")
command -nargs=+ HiLink hi def link <args>

HiLink jinjaPunctuation jinjaOperator
HiLink jinjaAttribute jinjaVariable
Expand All @@ -128,8 +94,4 @@ if version >= 508 || !exists("did_jinja_syn_inits")
delcommand HiLink
endif

let b:current_syntax = "jinja"

if main_syntax == 'jinja'
unlet main_syntax
endif
let b:current_syntax = "jinja2"

0 comments on commit 01d0e7d

Please sign in to comment.