neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

tera.vim (4413B)


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