Skip to content

Commit

Permalink
Re-based the syntax highlighting and html.twig on jinja highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
evidens committed Jul 30, 2011
1 parent b89c864 commit fe00c14
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
3 changes: 3 additions & 0 deletions ftdetect/twig.vim
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
" HTML Twig (must be before Twig)
autocmd BufNewFile,BufRead *.html.twig set filetype=htmltwig

" Twig
autocmd BufNewFile,BufRead *.twig set filetype=twig
28 changes: 28 additions & 0 deletions syntax/htmltwig.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
" Vim syntax file
" Language: Twig HTML template
" Maintainer: Gabriel Gosselin <gabrielNOSPAM@evidens.ca>
" Maintainer: Armin Ronacher <armin.ronacher@active-4.com>
" Last Change: 2011 July 29

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

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

if version < 600
so <sfile>:p:h/twig.vim
so <sfile>:p:h/html.vim
else
runtime! syntax/twig.vim
runtime! syntax/html.vim
unlet b:current_syntax
endif

let b:current_syntax = "htmljinja"
50 changes: 30 additions & 20 deletions syntax/twig.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
" Vim syntax file
" Language: Twig template
" Maintainer: Alex Suraci <i.am@toogeneric.com>
" Last Change: 2008 Sep 28
" Language: Twig template
" Maintainer: Gabriel Gosselin <gabrielNOSPAM@evidens.ca>
" Last Change: 2011 July 27
" Version: 1.0

"
" Based Jinja syntax by: Armin Ronacher <armin.ronacher@active-4.com>
"
" 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)
" 2011 July 27: Changed all references of jinja tp twig

" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
syntax clear
elseif exists("b:current_syntax")
Expand All @@ -12,7 +25,7 @@ endif

syntax case match

" Twig template built-in tags and parameters (without filter, macro, is and raw, they
" Jinja template built-in tags and parameters (without filter, macro, is and raw, they
" have special threatment)
syn keyword twigStatement containedin=twigVarBlock,twigTagBlock,twigNested contained and if else in not or recursive as import

Expand All @@ -30,32 +43,29 @@ syn match twigFilter contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
syn match twigFunction contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
syn match twigBlockName contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/

" Twig template constants
" Jinja template constants
syn region twigString containedin=twigVarBlock,twigTagBlock,twigNested contained start=/"/ skip=/\\"/ end=/"/
syn region twigString containedin=twigVarBlock,twigTagBlock,twigNested contained start=/'/ skip=/\\'/ end=/'/
syn match twigNumber containedin=twigVarBlock,twigTagBlock,twigNested contained /[0-9]\+\(\.[0-9]\+\)\?/

" Operators
"syn match twigOperator containedin=twigVarBlock,twigTagBlock,twigNested contained /[+\-*\/<>=!,:]/
syn match twigOperator containedin=twigVarBlock,twigTagBlock,twigNested contained /[+\-*\/<>=!,:]/
syn match twigPunctuation containedin=twigVarBlock,twigTagBlock,twigNested contained /[()\[\]]/
syn match twigOperator containedin=twigVarBlock,twigTagBlock,twigNested contained /\./ nextgroup=twigAttribute
syn match twigVar /\$/ nextgroup=twigAttribute containedin=ALLBUT,twigTagBlock,twigVarBlock,twigRaw,twigString,twigNested,twigComment
syn match twigAttribute contained /[a-zA-Z_][a-zA-Z0-9_]*/ nextgroup=twigOperator
syn match twigAttribute contained /[a-zA-Z_][a-zA-Z0-9_]*/

" Twig template tag and variable blocks
" Jinja template tag and variable blocks
syn region twigNested matchgroup=twigOperator start="(" end=")" transparent display containedin=twigVarBlock,twigTagBlock,twigNested contained
syn region twigNested matchgroup=twigOperator start="\[" end="\]" transparent display containedin=twigVarBlock,twigTagBlock,twigNested contained
"syn region twigNested matchgroup=twigOperator start="{" end="}" transparent display containedin=twigVarBlock,twigTagBlock,twigNested contained
syn region twigNested matchgroup=twigOperator start="{" end="}" transparent display containedin=twigVarBlock,twigTagBlock,twigNested contained
syn region twigTagBlock matchgroup=twigTagDelim start=/{%-\?/ end=/-\?%}/ skipwhite containedin=ALLBUT,twigTagBlock,twigVarBlock,twigRaw,twigString,twigNested,twigComment

syn region twigVarBlock matchgroup=twigVarDelim start=/\${/ end=/}/ containedin=ALLBUT,twigTagBlock,twigVarBlock,twigRaw,twigString,twigNested,twigComment
"syn region twigVarBlock matchgroup=twigVarDelim start=/$[a-zA-Z_]/ end=/[^a-zA-Z0-9.]/ containedin=ALLBUT,twigTagBlock,twigVarBlock,twigRaw,twigString,twigNested,twigComment

syn region twigVarBlock matchgroup=twigVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,twigTagBlock,twigVarBlock,twigRaw,twigString,twigNested,twigComment

" Twig template 'raw' tag
"syn region twigRaw matchgroup=twigRawDelim start="{%\s*raw\s*%}" end="{%\s*endraw\s*%}" containedin=ALLBUT,twigTagBlock,twigVarBlock,twigString,twigComment
" Jinja template 'raw' tag
syn region twigRaw matchgroup=twigRawDelim start="{%\s*raw\s*%}" end="{%\s*endraw\s*%}" containedin=ALLBUT,twigTagBlock,twigVarBlock,twigString,twigComment

" Twig comments
" Jinja comments
syn region twigComment matchgroup=twigCommentDelim start="{#" end="#}" containedin=ALLBUT,twigTagBlock,twigVarBlock,twigString

" Block start keywords. A bit tricker. We only highlight at the start of a
Expand All @@ -64,7 +74,7 @@ syn region twigComment matchgroup=twigCommentDelim start="{#" end="#}" contained
syn match twigStatement containedin=twigTagBlock contained skipwhite /\({%-\?\s*\)\@<=\<[a-zA-Z_][a-zA-Z0-9_]*\>\(\s*[,=]\)\@!/

" and context modifiers
"syn match twigStatement containedin=twigTagBlock contained /\<with\(out\)\?\s\+context\>/ skipwhite
syn match twigStatement containedin=twigTagBlock contained /\<with\(out\)\?\s\+context\>/ skipwhite


" Define the default highlighting.
Expand All @@ -89,14 +99,13 @@ if version >= 508 || !exists("did_twig_syn_inits")

HiLink twigSpecial Special
HiLink twigOperator Normal
"HiLink twigRaw Normal
HiLink twigRaw Normal
HiLink twigTagBlock PreProc
HiLink twigVarBlock PreProc
HiLink twigStatement Statement
HiLink twigFilter Function
HiLink twigBlockName Function
HiLink twigVariable Identifier
HiLink twigVar Identifier
HiLink twigString Constant
HiLink twigNumber Constant
HiLink twigComment Comment
Expand All @@ -105,3 +114,4 @@ if version >= 508 || !exists("did_twig_syn_inits")
endif

let b:current_syntax = "twig"

0 comments on commit fe00c14

Please sign in to comment.