marko.vim (31731B)
1 " Vim syntax file 2 " Language: Marko 3 " Maintainer: Brian Carbone <brian@briancarbone.com> 4 " URL: https://markojs.com 5 " Filenames: *.marko 6 " Last Change: 2026 Jul 29 7 8 " Marko interleaves HTML-like tags, TypeScript expressions and CSS in two 9 " modes. Mode is tracked structurally: element bodies and "--" text blocks 10 " are html mode (bare text stays plain), everywhere else is concise, where 11 " a line-start name is a tag: 12 " <p>if you want, stop by</p> body text, plain 13 " my-tag size="large" concise tag, highlighted 14 " Same-position priority goes to the item defined last; definition order 15 " is deliberate throughout. 16 17 if !exists("main_syntax") 18 if exists("b:current_syntax") 19 finish 20 endif 21 let main_syntax = "marko" 22 endif 23 24 let s:cpo_save = &cpo 25 set cpo&vim 26 27 " Top level is mostly markup; regions holding prose opt into @Spell. 28 syn spell toplevel 29 30 " Both includes mutate shared state ("syn case", "syn iskeyword", sync, 31 " 'iskeyword'), restored or restated afterwards. 32 let s:iskeyword_save = &l:iskeyword 33 syn include @markoTS syntax/typescript.vim 34 unlet! b:current_syntax 35 syn include @markoCSS syntax/css.vim 36 unlet! b:current_syntax 37 let &l:iskeyword = s:iskeyword_save 38 unlet s:iskeyword_save 39 syn case match 40 41 " A "\%#=1" prefix pins a pattern to the backtracking regexp engine: the 42 " default NFA engine is much slower on this file's characteristic shapes 43 " (a lookbehind or bounded repeat in front of an alternation). Both 44 " engines answer alike; only patterns measured faster carry the prefix. 45 46 " yats attaches type arguments via nextgroup after every identifier, so an 47 " unspaced "<" in any expression would hunt a ">" that may never come: 48 " <div count=i<3> would swallow the rest of the file 49 " That group stays cleared. The comment region replaces yats' block 50 " comment, whose "extend" would escape every containing region below. 51 silent! syn clear typescriptTypeArguments 52 silent! syn clear typescriptComment 53 syn region typescriptComment contained start="/\*" end="\*/" contains=@Spell,typescriptCommentTodo 54 55 " yats claims call arguments, bodies, indexing, arrays, templates and 56 " parenthesized expressions via nextgroup, so the markoTS* pairs below 57 " never see those delimiters. Each row is yats' own definition plus 58 " "contained extend", the one difference from upstream: a multiline group 59 " keeps its containing attribute value open, and a bracketed one shields 60 " the spaces inside, as the parser's group tracking does: 61 " count=Math.max(0, tiers.findIndex((t) => t.featured)) 62 " pair=raw as [string, string] 63 for s:def in [ 64 \ 'typescriptBlock matchgroup=typescriptBraces start=/{/ end=/}/ contains=@typescriptStatement,@typescriptComments fold', 65 \ 'typescriptFuncCallArg matchgroup=typescriptParens start=/(/ end=/)/ contains=@typescriptValue,@typescriptComments,typescriptCastKeyword nextgroup=@typescriptSymbols,typescriptDotNotation skipwhite skipempty skipnl', 66 \ 'typescriptArray matchgroup=typescriptBraces start=/\[/ end=/]/ contains=@typescriptValue,@typescriptComments,typescriptCastKeyword nextgroup=@typescriptSymbols,typescriptDotNotation skipwhite skipempty fold', 67 \ 'typescriptTemplate start=/`/ skip=/\\\\\|\\`\|\n/ end=/`\|$/ contains=typescriptTemplateSubstitution,typescriptSpecial,@Spell nextgroup=@typescriptSymbols skipwhite skipempty', 68 \ 'typescriptObjectLiteral matchgroup=typescriptBraces start=/{/ end=/}/ contains=@typescriptComments,typescriptObjectLabel,typescriptStringProperty,typescriptComputedPropertyName,typescriptObjectAsyncKeyword,typescriptTernary,typescriptCastKeyword fold', 69 \ 'typescriptObjectType matchgroup=typescriptBraces start=/{/ end=/}/ contains=@typescriptTypeMember,typescriptEndColons,@typescriptComments,typescriptAccessibilityModifier,typescriptReadonlyModifier nextgroup=@typescriptTypeOperator skipwhite skipnl fold', 70 \ 'typescriptParenExp matchgroup=typescriptParens start=/(/ end=/)/ contains=@typescriptComments,@typescriptValue,typescriptCastKeyword nextgroup=@typescriptSymbols skipwhite skipempty', 71 \ 'typescriptIndexExpr matchgroup=typescriptProperty start=/\[/ end=/]/ contains=@typescriptValue,typescriptCastKeyword nextgroup=@typescriptSymbols,typescriptDotNotation,typescriptFuncCallArg skipwhite skipempty', 72 \ 'typescriptCall matchgroup=typescriptParens start=/(/ end=/)/ contains=typescriptDecorator,@typescriptParameterList,@typescriptComments nextgroup=typescriptTypeAnnotation,typescriptBlock skipwhite skipnl', 73 \ 'typescriptTupleType matchgroup=typescriptBraces start=/\[/ end=/\]/ contains=@typescriptType,@typescriptComments,typescriptRestOrSpread,typescriptTupleLable skipwhite', 74 \ 'typescriptParenthesizedType matchgroup=typescriptParens start=/(/ end=/)/ contains=@typescriptType nextgroup=@typescriptTypeOperator skipwhite skipempty fold', 75 \ 'typescriptFuncType matchgroup=typescriptParens start=/(\(\k\+:\|)\)\@=/ end=/)\s*=>/me=e-2 contains=@typescriptParameterList nextgroup=typescriptFuncTypeArrow skipwhite skipnl oneline', 76 \ 'typescriptStringLiteralType start=/\z(["'']\)/ skip=/\\\\\|\\\z1\|\\\n/ end=/\z1\|$/ nextgroup=typescriptUnion skipwhite skipempty', 77 \ 'typescriptTemplateLiteralType start=/`/ skip=/\\\\\|\\`\|\n/ end=/`\|$/ contains=typescriptTemplateSubstitutionType nextgroup=typescriptTypeOperator skipwhite skipempty', 78 \ ] 79 silent! exe 'syn clear ' . matchstr(s:def, '^\S\+') 80 exe 'syn region ' . s:def . ' contained extend' 81 endfor 82 unlet s:def 83 " yats' own arrow-function definitions, restated to stay last-defined at 84 " a "(": params eaten as a parenthesized expression turn the arrow's "{" 85 " body into an object literal, whose ternary region has no closing bound: 86 " f((x) => { value = x ?? other; }) 87 syntax match typescriptArrowFuncDef contained /\K\k*\s*=>/ 88 \ contains=typescriptArrowFuncArg,typescriptArrowFunc 89 \ nextgroup=@typescriptExpression,typescriptBlock 90 \ skipwhite skipempty 91 syntax match typescriptArrowFuncDef contained /(\%(\_[^()]\+\|(\_[^()]*)\)*)\_s*=>/ 92 \ contains=typescriptArrowFuncArg,typescriptArrowFunc,@typescriptCallSignature 93 \ nextgroup=@typescriptExpression,typescriptBlock 94 \ skipwhite skipempty 95 syntax region typescriptArrowFuncDef contained start=/(\%(\_[^()]\+\|(\_[^()]*)\)*):/ matchgroup=typescriptArrowFunc end=/=>/ 96 \ contains=typescriptArrowFuncArg,typescriptTypeAnnotation,@typescriptCallSignature 97 \ nextgroup=@typescriptExpression,typescriptBlock 98 \ skipwhite skipempty keepend 99 100 " The bracket pairs keep an outer region open across nested delimiters 101 " and shield the spaces inside groupings from value termination: 102 " a={ b: { c: 1 } } d=2 103 " markoTSDelim is deliberately unlinked: delimiters render plain. 104 syn cluster markoExpr contains=@markoTS,markoTSBraces,markoTSParens,markoTSBrackets 105 for [s:pair, s:open, s:close] in [["Braces", '{', '}'], ["Parens", '(', ')'], ["Brackets", '\[', ']']] 106 exe 'syn region markoTS' . s:pair . ' contained transparent matchgroup=markoTSDelim' 107 \ . ' start="' . s:open . '" end="' . s:close . '"' 108 \ . ' contains=@markoExpr extend' 109 endfor 110 unlet s:pair s:open s:close 111 112 " html-mode "//" and "/* */" comments open wherever whitespace precedes 113 " the slash (a URL's "//" follows ":", so it stays plain); concise context 114 " takes only the line-anchored forms, defined second so they win at a line 115 " start and keep their fold and indent handling: 116 " // note <p>done // for now</p> <a href="https://x.co">plain</a> 117 " No comment item may take "display": a match skipped during offscreen 118 " state computation exposes its text, and a "<tag" inside a comment would 119 " open a body whose highlighting depends on scroll position. 120 syn match markoHtmlComment "\%(^\|\s\)\@1<=//.*$" contains=@Spell 121 syn region markoHtmlComment start="\%(^\|\s\)\@1<=/\*" end="\*/" contains=@Spell fold 122 syn region markoComment start="<!--" end="-->" contains=@Spell fold 123 syn match markoComment "^\s*//.*$" contains=@Spell 124 syn region markoComment start="^\s*/\*" end="\*/" contains=@Spell fold 125 syn region markoTagComment contained start="/\*" end="\*/" extend 126 syn match markoTagComment contained "//.*" 127 128 " Declarations before CDATA: both open at "<!" and the later wins: 129 " <!doctype html> <?xml version="1.0"?> <![CDATA[ raw ]]> 130 syn region markoDeclaration start="<!\(--\)\@!" end=">" 131 syn region markoDeclaration start="<?" end=">" 132 syn region markoCData start="<!\[CDATA\[" end="]]>" 133 134 " Placeholders interpolate into text; backslashes escape pairwise: 135 " ${user.name} $!{rawHtml} \${literal} \\${live} 136 " markoEscape must never take "display" (it exists to suppress 137 " markoPlaceholder); it consumes an escaped "$", or stops before a "${" 138 " an even backslash run leaves live. 139 syn match markoEscape "\%(\\\\\)*\%(\\\$!\={\|\\\\\ze\$!\={\)" 140 syn region markoPlaceholder matchgroup=markoPlaceholderDelim 141 \ start="\$!\={" end="}" 142 \ contains=@markoExpr extend 143 " The no-"extend" variant for keepend parsed-text bodies and the CSS 144 " contexts: an unterminated "\${" must stop at the container's close. 145 syn region markoPlaceholderText contained matchgroup=markoPlaceholderDelim 146 \ start="\$!\={" end="}" 147 \ contains=@markoExpr 148 \ containedin=cssDefinition,cssAttrRegion,cssStringQ,cssStringQQ 149 150 syn match markoEntity "&\%(#\d\+\|#[xX]\x\+\|\w\+\);" display 151 152 " Scriptlets; the whitespace after "$" keeps line-start placeholders out. 153 " No keepend: an open bracket continues, as the parser's group tracking 154 " does. The "$ {" block form is second, so it wins where both start: 155 " $ count += 1 $ { let total = 0; } 156 " $ const rows = [ 157 " "a", 158 " ] 159 syn region markoScriptlet matchgroup=markoScriptletDelim 160 \ start="^\s*\$\s\+" end="$" 161 \ contains=@markoExpr 162 syn region markoScriptlet matchgroup=markoScriptletDelim 163 \ start="^\s*\$\s\+{" end="};\=" 164 \ contains=@markoExpr 165 166 " "--" marks the rest of the line as text; at end of line it opens an 167 " indented block instead: 168 " p -- some text 169 " p -- 170 " A block of text, <b>html mode</b>. 171 " The inline form is a one-line html region, so later dash runs on the 172 " line sit inside it and are never delimiters (the parser's rule), and 173 " keepend bounds what the line opens. Both forms stand down on a line 174 " whose first non-blank is "<" (html content, where dashes are text). 175 " They overlap at a trailing "--": the nextgroup form is second and wins, 176 " and must not take "display". A bare "--" line belongs to the 177 " later-defined markoTextDelimBlock. 178 syn region markoInlineText oneline keepend matchgroup=markoBlockDelim 179 \ start="\%#=1\%(^\s*<.\{-}\)\@200<!\%(^\|\s\)\@1<=--\+\ze\s" end="$" 180 \ contains=@markoHtmlContent 181 syn match markoBlockDelim 182 \ "\%#=1\%(^\s*<.\{-}\)\@200<!\%(^\|\s\)\@1<=--\+\s*$" 183 \ nextgroup=markoTextIndent skipnl 184 " The shared block body: its indent is the first following line's leading 185 " whitespace run, blank lines included (the parser's rule); an empty run 186 " leaves only the column-0 delimiter close, the dedent end being a 187 " negative lookahead on an empty match. 188 let s:indent_block = ' start="^\z(\s*\)\ze\%(\S\|$\)"' 189 \ . ' matchgroup=markoBlockDelim end="^\z1--\+\s*$"' 190 \ . ' matchgroup=NONE end="^\%(\z1\)\@!\ze\s*\S"' 191 exe 'syn region markoTextIndent contained' . s:indent_block 192 \ . ' contains=@markoHtmlContent' 193 194 " A name may hold ":" (not the ":=" of a bound value) and any keyword 195 " character, and must not match a dynamic tag name's "${": 196 " size="large" total:=sum xml:lang="es" <spinner café="ok"/> 197 " An unquoted value ends at whitespace or the mode's terminators, except 198 " that the parser continues across whitespace flanked by an operator: 199 " if=tag === "select" content=a.desc ?? b.desc onClick=(e) => go(e) 200 syn match markoAttrName contained "\%#=1\%(\$!\={\)\@![[:alnum:]_$@]\%(\k\|[$.]\|:=\@!\)*" display 201 syn match markoSpread contained "\.\.\." display 202 syn region markoString contained start=+\z(["']\)+ skip=+\\\z1+ end=+\z1+ contains=@Spell 203 204 " Regions so an interpolation can sit inside one (no keepend: the 205 " placeholder carries the region past its "}"): 206 " <div.card#main> <div#item-${id}> 207 syn region markoShorthandId contained oneline 208 \ start="#[-[:alnum:]_$]\@=" end="[-[:alnum:]_$]\@!" 209 \ contains=markoPlaceholder 210 syn region markoShorthandClass contained oneline 211 \ start="\.[-[:alnum:]_$]\@=" end="[-[:alnum:]_$]\@!" 212 \ contains=markoPlaceholder 213 214 " Tag variables, arguments, |parameters|, <type args> and method bodies: 215 " <let/count=1/> <for|item, i| of=list> <while(cond)> 216 " <generic-list<string>> <button onClick(e) { handle(e) }> 217 " Params and type args hug the preceding name, so a "|" or "<" inside an 218 " expression cannot open them; params and the plain type-args form are 219 " oneline so an unmatched delimiter stops at its line. 220 syn match markoTagVar contained "/\%([[:alnum:]_$]\+\|\ze\s*[{[]\)" 221 \ nextgroup=markoTagVarDestructure skipwhite display 222 syn region markoTagVarDestructure contained matchgroup=markoTagDelim 223 \ start="\[" end="]" 224 \ contains=@markoExpr extend 225 syn region markoArgs contained matchgroup=markoTagDelim 226 \ start="(" end=")" 227 \ contains=@markoExpr extend 228 syn region markoParams contained oneline matchgroup=markoTagDelim 229 \ start="\%(\k\|[$)\]}>]\)\@4<=|" skip="||" end="|" 230 \ contains=@markoExpr 231 syn region markoTypeArgs contained oneline matchgroup=markoTagDelim 232 \ start="\%(\k\|\$\)\@4<=<" end=">" 233 \ contains=@typescriptType,markoTypeArgs 234 " The braced form spans lines: the object type bounds it, and the parser 235 " reads "<{" after a name in tag scope as type args too. 236 syn region markoTypeArgs contained matchgroup=markoTagDelim 237 \ start="\%(\k\|\$\)\@4<=<\ze\s*{" end=">" 238 \ contains=@typescriptType,markoTypeArgs extend 239 syn region markoMethodBody contained matchgroup=markoTagDelim 240 \ start="{" end="}" 241 \ contains=@markoExpr extend 242 243 " The three value variants differ only in terminators: html ends at "," 244 " "/>" or the tag's ">"; concise at "," ";" or whitespace-led "--" (">" 245 " is a comparison there); grouped at "," or "]". Defined after markoArgs 246 " so a "...spread" value wins at "(". keepend bounds foreign TypeScript 247 " at the value's end; the markoTS* and yats groups carry extend and punch 248 " through. A value survives its line end when either side of the break 249 " is an operator in html mode (the lookahead crosses blank lines), only 250 " a trailing one in concise: 251 " <a href=translated 252 " ? localizeUrl($global.url.href, { locale }).href 253 " : baseHref> 254 " s:val_nocont, the trailing set, mirrors the parser: "=>" and ">>" 255 " continue but a bare ">" ends (operator only outside type position, 256 " untrackable, and honoring it swallowed "</div>"-style line ends; 257 " mid-line "x > 1" continues via the break end's own guard); single 258 " "+"/"-" continue, postfix "i++" ends; unary keywords count unless a 259 " word character precedes ("a-new Date()" stays whole), binary and type 260 " keywords only after whitespace ("class=step-in" ends); a trailing "." 261 " continues when a word character follows, across blank lines. A "/" 262 " counts only when spaces flank it (ahead of the break it is ambiguous 263 " with "/>"), and the break demands a non-space before the space, so a 264 " continuation line's indent cannot end the value it continues: 265 " ratio=total / count max=10 266 " total=base + 267 " tax 268 let s:val_nocont = '[&*^:=!<%|?~]\@1<!\%(=>\|>>\)\@2<!\%(+\@1<!+\|-\@1<!-\)\@1<!\%(\s/\)\@2<!' 269 \ . '\%([0-9A-Za-z_$.]\@1<!\%(async\|await\|class\|function\|new\|typeof\|void\)\|\s\%(as\|extends\|instanceof\|in\|satisfies\|asserts\|infer\|is\|keyof\|readonly\|unique\)\)\@12<!' 270 \ . '\%(\.\@1<=\_s*[0-9A-Za-z_$]\)\@!' 271 " Cheap zero-width lead tests keep the wide lookbehinds off most columns. 272 " s:val_last matches after the line's last non-blank, so trailing spaces 273 " and blank lines never host a value end. 274 let s:val_last = '\%#=1\%(\s*$\)\@=' . s:val_nocont . '\S\@1<=' 275 let s:val_break = '\%#=1\s\@=>\@1<!' . s:val_nocont . '\S\@1<=\s' 276 let s:val_op = '[&*^!<%|~+?:={(]\|\.\%(\_s*\w\)\@=\|\<\%(as\|extends\|instanceof\|in\|satisfies\)\%(\_s\+[^:,=/>; \t]\)\@=' 277 let s:val_head = ' contained matchgroup=markoOperator start=":\==>\@!\s*"' 278 \ . ' matchgroup=NONE start="\.\@1<=\%(\.\.\.\)\@3<="' 279 let s:val_tail = ' contains=@markoExpr keepend extend' 280 exe 'syn region markoAttrValue' . s:val_head 281 \ . ' end="\ze," end="\ze/>" end="=\@1<!\ze>"' 282 \ . ' end="' . s:val_last . '\ze\s*\n\%(\_s*\%(' . s:val_op . '\|-\|/[/*>]\@!\)\)\@!"' 283 \ . ' end="' . s:val_break . '\%(\_s*\%(' . s:val_op . '\|-\|/[/*>]\@!\)\)\@!"' . s:val_tail 284 exe 'syn region markoAttrValueConcise' . s:val_head 285 \ . ' end="\ze[,;]" end="' . s:val_last . '"' 286 \ . ' end="' . s:val_break . '\%(\s*\%(' . s:val_op . '\|--\@!\|/[/*]\@!\|>\)\)\@!"' . s:val_tail 287 exe 'syn region markoAttrValueInGroup' . s:val_head 288 \ . ' end="\ze[,\]]" end="' . s:val_last . '"' 289 \ . ' end="' . s:val_break . '\%(\s*\%(' . s:val_op . '\|-\|/[/*]\@!\|>\)\)\@!"' . s:val_tail 290 unlet s:val_nocont s:val_last s:val_break s:val_op s:val_head s:val_tail 291 292 syn cluster markoTagStuff contains=markoTagVar,markoShorthandId,markoShorthandClass, 293 \markoAttrName,markoString,markoArgs,markoParams,markoTypeArgs, 294 \markoMethodBody,markoSpread,markoTagComment,markoPlaceholder 295 " The name groups themselves are defined near the end of the file; a 296 " cluster may name them before they exist. 297 syn cluster markoTagNames contains=markoTagName,markoTagNameTail,markoAttrTagName, 298 \markoTagNameConditional,markoTagNameRepeat,markoTagNameException, 299 \markoTagNameStatement,markoTagNameBuiltin 300 301 " Attribute groups may span lines. The lookbehind keeps a spread's array 302 " literal out; that "[" opens a value, not a group: 303 " define [size = "large"] my-tag ...[1, 2] 304 syn region markoAttrGroup contained matchgroup=markoTagDelim 305 \ start="\%(\.\.\.\)\@3<!\[" end="]" 306 \ contains=@markoTagStuff,markoAttrValueInGroup extend 307 308 " One list per highlight class. The builtin list is factored by first 309 " letter; no name is a prefix of another, so grouping changes nothing. 310 let s:tag_name_classes = [ 311 \ ["Conditional", 'if\|else-if\|else'], 312 \ ["Repeat", 'for\|while'], 313 \ ["Exception", 'try'], 314 \ ["Statement", 'await\|return'], 315 \ ["Builtin", 'l\%(et\|ifecycle\|og\)\|c\%(onst\|ontext\)\|d\%(ebug\|efine\)\|s\%(tyle\|cript\)\|effect\|id\|macro\|textarea\|html-\%(comment\|style\|script\)'], 316 \ ] 317 318 " Concise mode: a line-start name is a tag; so are "${tag}", ".class" and 319 " "#id". The generic rule is first, so class names, "@" tags and the 320 " statement tags win over it. A tag ends at its line unless a comma 321 " trails or leads the continuation; comments and blank lines may sit 322 " between, and a blank line never hosts the end. Brackets, method bodies 323 " and attr groups carry extend, punching through keepend for multiline 324 " constructs: 325 " link 326 " ,rel="canonical" 327 " // a comment between continued lines 328 " ,href=baseHref 329 " input type="text", 330 " value=name 331 " Whitespace-led dashes end the tag and start text. That end begins at 332 " the first dash with the space as lookbehind: the space is also the 333 " value's own break-end, and an end starting inside the value would be 334 " suppressed by its extend: 335 " p ---- some text 336 let s:concise_tag_end = ' matchgroup=markoTagDelim end=";"' 337 \ . ' end="\%#=1\%(,\s*\)\@9<!\%(^\s\{0,60}\)\@61<!\ze\n\%(\%(\s*\%(//.*\|/\*\_.\{-,200}\*/\s*\)\=\n\)\{0,20}\s*,\)\@!"' 338 \ . ' end="\s\@1<=\ze-\{2,}\%(\s\|$\)"' 339 \ . ' contains=@markoTagStuff,markoAttrGroup,markoAttrValueConcise,markoTagNameTail keepend' 340 exe 'syn region markoConciseTag matchgroup=markoTagName' 341 \ . ' start="^\s*\zs\%(-\@!\k\|\$[{ \t]\@!\)\%(\k\|:\|\${\@!\)*"' 342 \ . s:concise_tag_end 343 exe 'syn region markoConciseTag' 344 \ . ' start="^\s*\ze\%(\$!\={\|[.#][-[:alnum:]_$]\)"' 345 \ . s:concise_tag_end 346 for [s:class_suffix, s:name_pattern] in s:tag_name_classes 347 exe 'syn region markoConciseTag matchgroup=markoTagName' . s:class_suffix 348 \ . ' start="^\s*\zs\%(' . s:name_pattern . '\)\%(\k\|\$\|:=\@!\)\@!"' 349 \ . s:concise_tag_end 350 endfor 351 exe 'syn region markoConciseTag matchgroup=markoAttrTagName' 352 \ . ' start="^\s*\zs@[-[:alnum:]_$:]\+"' 353 \ . s:concise_tag_end 354 unlet s:concise_tag_end 355 356 " One TypeScript statement each. static/server/client are Marko keywords; 357 " import/export/class take the include's colors. Defined after the 358 " concise regions so these names win there. 359 syn region markoStatement matchgroup=markoStatementKeyword 360 \ start="^\s*\zs\%(static\|server\|client\)\>" 361 \ matchgroup=NONE start="^\s*\ze\%(import\|export\|class\)\>" 362 \ end="$" contains=@markoExpr 363 364 " A tag name may be empty (<.card>), dynamic (<${tag}>) or an attribute 365 " tag (<@body>); an arrow's ">" does not end the tag. 366 syn region markoTag matchgroup=markoTagDelim 367 \ start="<\ze[^/ \t!?<>=]" end="/>" end="\%#=1=\@1<!>" 368 \ contains=@markoTagStuff,@markoTagNames,markoAttrValue 369 " Recovers a close tag no body end consumed, eg a stray one in concise 370 " context. 371 syn region markoCloseTag matchgroup=markoTagDelim 372 \ start="</" end=">" 373 \ contains=@markoTagNames,markoPlaceholder 374 375 " The name of a close tag whose "</" a body end consumed, plus its ">". 376 " It stops before "${", so a dynamic close name's placeholder colors: 377 " </${tag}> 378 syn match markoCloseName 379 \ "/\@1<=\%(</\)\@2<=\%(\k\|:\|\$\%(!\={\)\@!\)\+\s*" 380 \ nextgroup=markoCloseGt,markoClosePlaceholder display 381 syn match markoCloseGt contained ">" display 382 " A dynamic close name's placeholder, chained so the closing ">" keeps 383 " its color: </${tag}> via the body's nextgroup, </h${size}> via the 384 " close name's. 385 syn region markoClosePlaceholder contained matchgroup=markoPlaceholderDelim 386 \ start="\$!\={" end="}" 387 \ contains=@markoExpr 388 \ nextgroup=markoCloseGt,markoCloseTail 389 syn match markoCloseTail contained "\%(\k\|:\)\+\s*" 390 \ nextgroup=markoCloseGt,markoClosePlaceholder display 391 " The anonymous form (</>): a body end consumed the "</" and there is no 392 " close name to carry the nextgroup, so the ">" takes its color directly. 393 syn match markoCloseAnon "\%(</\)\@2<=>" display 394 395 " Statement and concise tags are absent: bare text in a body is prose. 396 " The close-name refinements keep </if> Conditional at any depth; generic 397 " markoTagName stays out or it would outrank markoCloseName. 398 syn cluster markoHtmlContent contains=markoTag,markoHtmlBody,markoDynamicBody, 399 \markoStyle,markoScript,markoTextareaTag,markoHtmlCommentTag, 400 \markoComment,markoHtmlComment,markoCData,markoDeclaration,markoEscape,markoPlaceholder, 401 \markoEntity,markoScriptlet,markoCloseName,markoCloseAnon,markoAttrTagName, 402 \markoTagNameConditional,markoTagNameRepeat,markoTagNameException, 403 \markoTagNameStatement,markoTagNameBuiltin 404 405 " Whether "<name" begins a body turns on how its open tag ends: an atomic 406 " run over the groupings that may hold a ">" finds the structural one, 407 " and a "/>" there means self-closing, no body: 408 " <my-tag onClick=() => go(a > b) /> 409 " <div onClick() { hidden = a > b }/> 410 " Nothing else hides a ">" (the parser ends an open tag at any ">" not 411 " preceded by "="), and the repeat counts bound the scan so a stray "<" 412 " costs a fixed lookahead; past them no body opens. 413 let s:tag_end_ahead = '\%(\%(\%(<\%([^<>]\|<\%([^<>]\|<[^<>]\{-,40}>\)\{-,60}>\)\{-,100}>' 414 \ . '\|"[^"]\{-,200}"\|' . "'[^']\\{-,200}'" 415 \ . '\|`[^`]\{-,200}`' 416 \ . '\|(\%(\_[^()]\|(\%(\_[^()]\|([^()]\{-,60})\)\{-,100})\)\{-,300})' 417 \ . '\|{\%(\_[^{}]\|{\_[^{}]\{-,100}}\)\{-,300}}' 418 \ . '\|=>\|\_[^>]\)\{0,800}\)\@>/\@1<!=\@1<!>\)\@=' 419 420 " Element bodies: html mode from a non-void, non-self-closing open tag to 421 " its matching close. \z1 pairs the names; the end consumes only "</" so 422 " an inner same-name close cannot end the outer body: 423 " <div><div>nested</div>still the outer body</div> 424 " The excluded names are the parser's void and text lists (<let/x=1> 425 " takes no close tag; <effect> is in neither and opens a body). Defined 426 " after markoTag and the concise regions so a body wins at "<"; a missing 427 " close leaves the body open, as in html.vim. 428 exe 'syn region markoHtmlBody' 429 \ . ' start=+<\ze\%(\%(area\|base\|br\|col\|embed\|hr\|img\|input\|link\|meta\|param' 430 \ . '\|source\|track\|wbr\|style\|script\|textarea\|html-comment\|html-style' 431 \ . '\|html-script\|let\|const\|id\|log\|debug\|lifecycle\|return\)' 432 \ . '\%(\k\|\$\|:=\@!\)\@!\)\@!' 433 \ . '\z(\%(\k\|[$:]\)\+\)\%(\_s\|[/>|(<.#=]\)\@=' . s:tag_end_ahead . '+' 434 \ . ' matchgroup=markoTagDelim' 435 \ . ' end="</\ze\z1\s*>" end="</\ze\s*>"' 436 \ . ' contains=@markoHtmlContent' 437 " A dynamic name -- any mix of literal characters and "${...}" 438 " placeholders -- cannot be paired, so a dynamic body closes at the first 439 " close tag of dynamic shape, regardless of nesting: 440 " <h${size}>heading</h${size}> <${tag}>body</${tag}> 441 exe 'syn region markoDynamicBody' 442 \ . ' start=+<\ze\%(\%(\k\|:\)*\$!\={\)\@=' . s:tag_end_ahead . '+' 443 \ . ' matchgroup=markoTagDelim' 444 \ . ' end="</\ze\%(\%(\k\|:\)*\%(\$!\={\_[^{}]*\%({\_[^{}]*}\_[^{}]*\)\{0,3}}\%(\k\|:\)*\)\+\)\=\s*>"' 445 \ . ' contains=@markoHtmlContent nextgroup=markoClosePlaceholder' 446 unlet s:tag_end_ahead 447 448 " Standalone delimited blocks hold html content, like markoTextIndent. 449 " A block closes at its exact delimiter -- same indent, same dash count, 450 " longer or shorter runs are text -- or on any dedent; a column-0 block 451 " only at its delimiter (the empty \z1 kills the dedent end). Defined 452 " after the markoBlockDelim forms so a bare "--" line opens a block: 453 " -- 454 " A block of text, <b>html mode</b>. 455 " -- 456 syn region markoTextDelimBlock matchgroup=markoBlockDelim 457 \ start="^\z(\s*\)\z(--\+\)\s*$" 458 \ end="^\z1\z2\s*$" end="^\%(\z1\)\@!\ze\s*\S" 459 \ contains=@markoHtmlContent 460 461 " Parsed-text tags: bodies hold only text and placeholders (style/script 462 " embed CSS and TypeScript). Each region consumes only its "<"; the 463 " markoParsedOpen overlay colors the open tag normally: 464 " <style/styles> <script src="x" async> 465 " Defined after the generic tag and body regions to win at "<". The 466 " "/\@1<!" keeps self-closing tags out; keepend stops an unterminated 467 " embedded construct at the close tag. 468 syn region markoParsedOpen contained matchgroup=markoTagDelim 469 \ start="<\ze\%(html-\%(style\|script\|comment\)\|style\|script\|textarea\)\>\%(:=\@!\|\$\)\@!" 470 \ matchgroup=markoTagDelim end="/\@1<!=\@1<!>" 471 \ contains=@markoTagStuff,@markoTagNames,markoAttrValue 472 " The bounded lookahead for the open tag's own ">", shared by all four. 473 let s:open_end_ahead = '\%(\%("[^"]\{-,200}"\|''[^'']\{-,200}''\|`[^`]\{-,200}`\|=>\|\_[^>]\)\{-,300}/\@1<!=\@1<!>\)\@=' 474 for [s:name, s:pat, s:body] in [ 475 \ ['markoStyle', '\%(html-\)\=style', '@markoCSS,markoPlaceholderText,markoParsedOpen keepend fold'], 476 \ ['markoScript', '\%(html-\)\=script', '@markoTS,markoPlaceholderText,markoParsedOpen keepend fold'], 477 \ ['markoTextareaTag', 'textarea', 'markoPlaceholderText,markoEntity,@Spell,markoParsedOpen keepend'], 478 \ ['markoHtmlCommentTag', 'html-comment', 'markoPlaceholderText,@Spell,markoParsedOpen keepend'], 479 \ ] 480 exe 'syn region ' . s:name 481 \ . ' start=+<\ze' . s:pat . '\>\%(:=\@!\|\$\)\@!' . s:open_end_ahead . '+' 482 \ . ' matchgroup=markoTagDelim end="</\ze' . s:pat . '\s*>"' 483 \ . ' contains=' . s:body 484 endfor 485 unlet s:name s:pat s:body 486 syn region markoStyle matchgroup=markoTagNameBuiltin 487 \ start="^\s*style\%([./][^ \t{]*\)\=\s*{" end="}" 488 \ contains=@markoCSS,markoPlaceholderText fold 489 " A trailing "--" on a concise style tag opens a delimited block of CSS; 490 " the lookbehind claims only the dashes, winning over the generic 491 " trailing-"--" form. script works the same way with TypeScript: 492 " style/styles -- 493 " .root { color: red } 494 " -- 495 for [s:kind, s:body] in [["Style", '@markoCSS'], ["Script", '@markoTS']] 496 exe 'syn match marko' . s:kind . 'Delim' 497 \ . ' "\%#=1\%(^\s*' . tolower(s:kind) . '\%(\k\|[$:]\)\@![^;{]*\)\@200<=--\+\s*$"' 498 \ . ' nextgroup=marko' . s:kind . 'Text skipnl' 499 exe 'syn region marko' . s:kind . 'Text contained' . s:indent_block 500 \ . ' contains=' . s:body . ',markoPlaceholderText keepend fold' 501 endfor 502 unlet s:kind s:body s:indent_block 503 unlet s:open_end_ahead 504 505 " Names inside "<...>"/"</...>": a name stops before "${" and 506 " markoTagNameTail resumes after "}"; the tail's lookbehind demands the 507 " placeholder, so a method body's "}" cannot start one: 508 " <h${size}> <div onClick() { go() }disabled> "disabled" is an attr 509 " Generic after attr name, class names after generic: later wins. 510 syn match markoTagName contained "\%(</\=\)\@2<=\%(-\@!\k\|\$[{ \t]\@!\)\%(\k\|:\|\${\@!\)*" display 511 syn match markoTagNameTail contained 512 \ "}\@1<=\%(\$!\={[^{}]*}\)\@120<=\%(\k\|:\|\${\@!\)\+" display 513 syn match markoAttrTagName contained "\%#=1\%(</\=\)\@2<=@[-[:alnum:]_$:]\+" display 514 " The un-contained variants refine markoCloseName after a body's end has 515 " consumed the "</", so builtin and "@" close names keep their classes. 516 syn match markoAttrTagName "\%#=1\%(</\)\@2<=@\%(\k\|[$:]\)\+\s*" nextgroup=markoCloseGt display 517 for [s:class_suffix, s:name_pattern] in s:tag_name_classes 518 exe 'syn match markoTagName' . s:class_suffix 519 \ . ' contained "\%#=1\%(</\=\)\@2<=\%(' . s:name_pattern . '\)\%(\k\|\$\|:=\@!\)\@!" display' 520 exe 'syn match markoTagName' . s:class_suffix 521 \ . ' "\%#=1\%(</\)\@2<=\%(' . s:name_pattern . '\)\%(\k\|[$:]\)\@!\s*"' 522 \ . ' nextgroup=markoCloseGt display' 523 endfor 524 unlet s:tag_name_classes s:class_suffix s:name_pattern 525 526 " The includes install sync rules; clear them and restate the keyword set 527 " ("syn sync clear" clears "syn iskeyword" too; "-" keeps css.vim's 528 " font-family whole). fromstart is the only safe strategy -- bodies and 529 " blocks are unbounded -- and a 5000-line cold parse stays well under a 530 " second. 531 syn sync clear 532 syn sync fromstart 533 syn iskeyword @,48-57,_,192-255,- 534 535 " Scoping regions (tags, bodies, brackets, values) take no link: they 536 " bound contents that carry their own colors. 537 hi def link markoComment Comment 538 hi def link markoTagComment Comment 539 hi def link markoHtmlComment Comment 540 hi def link markoHtmlCommentTag Comment 541 hi def link markoDeclaration PreProc 542 hi def link markoCData String 543 hi def link markoEscape Special 544 hi def link markoEntity Special 545 hi def link markoPlaceholderDelim Special 546 hi def link markoStatementKeyword Keyword 547 hi def link markoScriptletDelim Special 548 hi def link markoBlockDelim Special 549 hi def link markoTagDelim Special 550 hi def link markoTagName Statement 551 hi def link markoTagNameTail Statement 552 hi def link markoCloseName Statement 553 hi def link markoCloseTail Statement 554 hi def link markoCloseGt Special 555 hi def link markoCloseAnon Special 556 hi def link markoAttrTagName Identifier 557 hi def link markoTagNameConditional Conditional 558 hi def link markoTagNameRepeat Repeat 559 hi def link markoTagNameException Exception 560 hi def link markoTagNameStatement Statement 561 hi def link markoTagNameBuiltin Macro 562 hi def link markoShorthandId Constant 563 hi def link markoShorthandClass Identifier 564 hi def link markoStyleDelim Special 565 hi def link markoScriptDelim Special 566 hi def link markoTagVar Identifier 567 hi def link markoAttrName Type 568 hi def link markoString String 569 hi def link markoSpread Operator 570 hi def link markoOperator Operator 571 572 let b:current_syntax = "marko" 573 if main_syntax ==# "marko" 574 unlet main_syntax 575 endif 576 577 let &cpo = s:cpo_save 578 unlet s:cpo_save 579 580 " vim: ts=8