vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

tera.vim (4491B)


      1 " Vim syntax file
      2 " Language:	Tera
      3 " Maintainer:	Muntasir Mahmud <muntasir.joypurhat@gmail.com>
      4 " Last Change:	2026 Jan 29
      5 " 2026 Mar 31 by Vim project: prevent code execution in filename
      6 
      7 if exists("b:current_syntax")
      8   finish
      9 endif
     10 
     11 " Detect the underlying language based on filename pattern
     12 " For files like file.html.tera, we want to load html syntax
     13 let s:filename = expand("%:t")
     14 let s:dotpos = strridx(s:filename, '.', strridx(s:filename, '.tera') - 1)
     15 let s:underlying_filetype = ""
     16 
     17 if s:dotpos != -1
     18   let s:underlying_ext = s:filename[s:dotpos+1:strridx(s:filename, '.tera')-1]
     19   if s:underlying_ext != "" && s:underlying_ext != "tera"
     20     let s:underlying_filetype = s:underlying_ext
     21   endif
     22 endif
     23 
     24 " Load the underlying language syntax if detected
     25 if s:underlying_filetype != ""
     26   execute "runtime! syntax/" . fnameescape(s:underlying_filetype) . ".vim"
     27   unlet! b:current_syntax
     28 else
     29   " Default to HTML if no specific language detected
     30   runtime! syntax/html.vim
     31   unlet! b:current_syntax
     32 endif
     33 
     34 " Tera comment blocks: {# comment #}
     35 syn region teraCommentBlock start="{#-?" end="-?#}" contains=@Spell
     36 
     37 " Tera statements: {% if condition %}
     38 syn region teraStatement start="{%-?" end="-?%}" contains=teraKeyword,teraString,teraNumber,teraFunction,teraBoolean,teraFilter,teraOperator,teraIdentifier,teraTest,teraNamespace,teraProperty,teraBracket,teraArgument
     39 
     40 " Tera expressions: {{ variable }}
     41 syn region teraExpression start="{{-?" end="-?}}" contains=teraString,teraNumber,teraFunction,teraBoolean,teraFilter,teraOperator,teraIdentifier,teraTest,teraNamespace,teraProperty,teraBracket
     42 
     43 " Special handling for raw blocks - content inside shouldn't be processed
     44 syn region teraRawBlock start="{%-\?\s*raw\s*-%}\?" end="{%-\?\s*endraw\s*-%}\?" contains=TOP,teraCommentBlock,teraStatement,teraExpression
     45 
     46 " Control structure keywords
     47 syn keyword teraKeyword contained if else elif endif for endfor in macro endmacro
     48 syn keyword teraKeyword contained block endblock extends include import set endset set_global
     49 syn keyword teraKeyword contained break continue filter endfilter raw endraw
     50 
     51 " Identifiers - define before operators for correct priority
     52 syn match teraIdentifier contained "\<\w\+\>"
     53 
     54 " Operators used in expressions and statements
     55 syn match teraOperator contained "==\|!=\|>=\|<=\|>\|<\|+\|-\|*\|/"
     56 syn match teraOperator contained "{\@<!%}\@!" " Match % but not when part of {% or %}
     57 syn keyword teraOperator contained and or not is as
     58 
     59 " Functions and filters
     60 syn match teraFunction contained "\<\w\+\ze("
     61 syn match teraFilter contained "|\_s*\w\+"
     62 
     63 " String literals - both single and double quoted
     64 syn region teraString contained start=+"+ skip=+\\"+ end=+"+ contains=@Spell
     65 syn region teraString contained start=+'+ skip=+\\'+ end=+'+ contains=@Spell
     66 
     67 " Numeric literals - both integer and float
     68 syn match teraNumber contained "\<\d\+\>"
     69 syn match teraNumber contained "\<\d\+\.\d\+\>"
     70 
     71 " Boolean values
     72 syn keyword teraBoolean contained true false
     73 
     74 " Special variables (loop, __tera_context)
     75 syn keyword teraSpecialVariable contained loop __tera_context
     76 
     77 " 'is' test patterns: 'is not test_name' or 'is test_name'
     78 syn match teraTest contained "\<is\s\+not\?\s\+\w\+\>"
     79 
     80 " Namespace function calls: namespace::function()
     81 syn match teraNamespace contained "\<\w\+::"
     82 
     83 " Property/member access: .property or ["key"] or [variable]
     84 syn match teraProperty contained "\.\w\+"
     85 syn region teraBracket contained start="\[" end="\]" contains=teraString,teraIdentifier,teraNumber,teraOperator
     86 
     87 " Backtick strings for raw content
     88 syn region teraString contained start="`" skip="\\`" end="`" contains=@Spell
     89 
     90 " String escape sequences
     91 syn match teraStringEscape contained "\\."
     92 
     93 " Highlighting links
     94 hi def link teraCommentBlock Comment
     95 hi def link teraKeyword Statement
     96 hi def link teraOperator Operator
     97 hi def link teraFunction Function
     98 hi def link teraIdentifier Identifier
     99 hi def link teraString String
    100 hi def link teraStringEscape SpecialChar
    101 hi def link teraNumber Number
    102 hi def link teraBoolean Boolean
    103 hi def link teraSpecialVariable Special
    104 hi def link teraTest Keyword
    105 hi def link teraNamespace Function
    106 hi def link teraProperty Identifier
    107 hi def link teraBracket Operator
    108 hi def link teraFilter Function
    109 hi def link teraStatement Statement
    110 hi def link teraExpression Statement
    111 
    112 " Clean up script-local variables
    113 unlet s:filename
    114 unlet s:dotpos
    115 if exists("s:underlying_ext")
    116   unlet s:underlying_ext
    117 endif
    118 unlet s:underlying_filetype
    119 
    120 let b:current_syntax = "tera"