blob: 3fcc21df493e1a492aeb3174be0eaa05fd52ef82 [file] [log] [blame]
MuntasirSZN14da0fb2025-03-09 08:49:14 +01001" Vim syntax file
2" Language: Tera
3" Maintainer: Muntasir Mahmud <muntasir.joypurhat@gmail.com>
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +01004" Last Change: 2025 Mar 11
MuntasirSZN14da0fb2025-03-09 08:49:14 +01005
6if exists("b:current_syntax")
7 finish
8endif
9
10" Detect the underlying language based on filename pattern
11" For files like file.html.tera, we want to load html syntax
12let s:filename = expand("%:t")
13let s:dotpos = strridx(s:filename, '.', strridx(s:filename, '.tera') - 1)
14let s:underlying_filetype = ""
15
16if 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
21endif
22
23" Load the underlying language syntax if detected
24if s:underlying_filetype != ""
25 execute "runtime! syntax/" . s:underlying_filetype . ".vim"
26 unlet! b:current_syntax
27else
28 " Default to HTML if no specific language detected
29 runtime! syntax/html.vim
30 unlet! b:current_syntax
31endif
32
33" Tera comment blocks: {# comment #}
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +010034syn region teraCommentBlock start="{#" end="#}" contains=@Spell
MuntasirSZN14da0fb2025-03-09 08:49:14 +010035
36" Tera statements: {% if condition %}
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +010037syn region teraStatement start="{%" end="%}" contains=teraKeyword,teraString,teraNumber,teraFunction,teraBoolean,teraFilter,teraOperator
MuntasirSZN14da0fb2025-03-09 08:49:14 +010038
39" Tera expressions: {{ variable }}
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +010040syn region teraExpression start="{{" end="}}" contains=teraString,teraNumber,teraFunction,teraBoolean,teraFilter,teraOperator,teraIdentifier
MuntasirSZN14da0fb2025-03-09 08:49:14 +010041
42" Special handling for raw blocks - content inside shouldn't be processed
43syn region teraRawBlock start="{% raw %}" end="{% endraw %}" contains=TOP,teraCommentBlock,teraStatement,teraExpression
44
45" Control structure keywords
46syn keyword teraKeyword contained if else elif endif for endfor in macro endmacro
47syn keyword teraKeyword contained block endblock extends include import set endset
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +010048syn keyword teraKeyword contained break continue filter endfilter raw endraw
MuntasirSZN14da0fb2025-03-09 08:49:14 +010049
50" Identifiers - define before operators for correct priority
51syn match teraIdentifier contained "\<\w\+\>"
52
53" Operators used in expressions and statements
54syn match teraOperator contained "==\|!=\|>=\|<=\|>\|<\|+\|-\|*\|/"
55syn match teraOperator contained "{\@<!%}\@!" " Match % but not when part of {% or %}
56syn keyword teraOperator contained and or not is as
57
58" Functions and filters
59syn match teraFunction contained "\<\w\+\ze("
60syn match teraFilter contained "|\_s*\w\+"
61
62" String literals - both single and double quoted
63syn region teraString contained start=+"+ skip=+\\"+ end=+"+ contains=@Spell
64syn region teraString contained start=+'+ skip=+\\'+ end=+'+ contains=@Spell
65
66" Numeric literals - both integer and float
67syn match teraNumber contained "\<\d\+\>"
68syn match teraNumber contained "\<\d\+\.\d\+\>"
69
70" Boolean values
71syn keyword teraBoolean contained true false
72
73" Highlighting links
74hi def link teraCommentBlock Comment
75hi def link teraKeyword Statement
76hi def link teraOperator Operator
77hi def link teraFunction Function
78hi def link teraIdentifier Identifier
79hi def link teraString String
80hi def link teraNumber Number
81hi def link teraBoolean Boolean
MuntasirSZNa8aeeeb2025-03-11 21:17:45 +010082hi def link teraFilter Function
83hi def link teraStatement Statement
84hi def link teraExpression Statement
MuntasirSZN14da0fb2025-03-09 08:49:14 +010085
86" Clean up script-local variables
87unlet s:filename
88unlet s:dotpos
89if exists("s:underlying_ext")
90 unlet s:underlying_ext
91endif
92unlet s:underlying_filetype
93
94let b:current_syntax = "tera"