pearofducks / ansible-vim

A vim plugin for syntax highlighting Ansible's common filetypes
MIT License
800 stars 98 forks source link

Differences between vendored and upstream jinja.vim #95

Closed dvzrv closed 4 years ago

dvzrv commented 4 years ago

Hi again!

When packaging this plugin, I stumbled across the included jinja2.vim syntax file.

I was wondering, why this is not the upstream jinja.vim and whether I can use upstream's version as a drop-in replacement when packaging.

pearofducks commented 4 years ago

IIRC the included file has some fixes for compound filetypes - so using upstream will probably result in bugs.

dvzrv commented 4 years ago

Hm, from what I can see, this would be worth a shot though:

--- /home/dave/packages/community/vim-ansible/trunk/ansible-vim-2.1/syntax/jinja2.vim   2020-02-09 16:09:52.000000000 +0100
+++ /usr/share/vim/vimfiles/syntax/jinja.vim    2020-01-30 20:40:59.000000000 +0100
@@ -1,15 +1,42 @@
 " Vim syntax file
-" Language: Jinja2 - with special modifications for compound-filetype
-" compatibility
-" Maintainer: Dave Honneffer <pearofducks@gmail.com>
-" Last Change: 2018.02.11
+" 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 Jinja 2 changes (new keyword rules)
+
+" .vimrc variable to disable html highlighting
+if !exists('g:jinja_syntax_html')
+  let g:jinja_syntax_html=1
+endif

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

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

 syntax case match

@@ -48,13 +75,16 @@
 syn region jinjaNested matchgroup=jinjaOperator start="{" end="}" transparent display containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained
 syn region jinjaTagBlock matchgroup=jinjaTagDelim start=/{%-\?/ end=/-\?%}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment

-syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,yamlComment,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment
+syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment

 " Jinja template 'raw' tag
 syn region jinjaRaw matchgroup=jinjaRawDelim start="{%\s*raw\s*%}" end="{%\s*endraw\s*%}" containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaString,jinjaComment

 " Jinja comments
-syn region jinjaComment matchgroup=jinjaCommentDelim start="{#" end="#}" containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaString
+syn region jinjaComment matchgroup=jinjaCommentDelim start="{#" end="#}" containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaString,jinjaComment
+" help support folding for some setups
+setlocal commentstring={#%s#}
+setlocal comments=s:{#,e:#}

 " Block start keywords.  A bit tricker.  We only highlight at the start of a
 " tag block and only if the name is not followed by a comma or equals sign
@@ -66,8 +96,15 @@

 " Define the default highlighting.
-if !exists("did_jinja_syn_inits")
-  command -nargs=+ HiLink hi def link <args>
+" 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 v:version >= 508 || !exists("did_jinja_syn_inits")
+  if v:version < 508
+    let did_jinja_syn_inits = 1
+    command -nargs=+ HiLink hi link <args>
+  else
+    command -nargs=+ HiLink hi def link <args>
+  endif

   HiLink jinjaPunctuation jinjaOperator
   HiLink jinjaAttribute jinjaVariable
@@ -94,4 +131,8 @@
   delcommand HiLink
 endif

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

Upstream sets main_syntax to jinja and not jinja2 though.