Gregory Anders | 1cc4cae | 2024-07-15 20:00:48 +0200 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: Typst |
| 3 | " Maintainer: Gregory Anders <greg@gpanders.com> |
Yinzuo Jiang | d181baf | 2024-11-02 16:34:40 +0100 | [diff] [blame] | 4 | " Last Change: 2024 Nov 02 |
Gregory Anders | 1cc4cae | 2024-07-15 20:00:48 +0200 | [diff] [blame] | 5 | " Based on: https://github.com/kaarmu/typst.vim |
| 6 | |
| 7 | if exists('b:current_syntax') |
| 8 | finish |
| 9 | endif |
| 10 | |
| 11 | syntax sync fromstart |
| 12 | syntax spell toplevel |
| 13 | |
| 14 | " Common {{{1 |
| 15 | syntax cluster typstCommon |
| 16 | \ contains=@typstComment |
| 17 | |
| 18 | " Common > Comment {{{2 |
| 19 | syntax cluster typstComment |
| 20 | \ contains=typstCommentBlock,typstCommentLine |
Yinzuo Jiang | d181baf | 2024-11-02 16:34:40 +0100 | [diff] [blame] | 21 | syntax region typstCommentBlock |
| 22 | \ start="/\*" end="\*/" keepend |
Gregory Anders | 1cc4cae | 2024-07-15 20:00:48 +0200 | [diff] [blame] | 23 | \ contains=typstCommentTodo,@Spell |
| 24 | syntax match typstCommentLine |
| 25 | \ #//.*# |
| 26 | \ contains=typstCommentTodo,@Spell |
| 27 | syntax keyword typstCommentTodo |
| 28 | \ contained |
| 29 | \ TODO FIXME XXX TBD |
| 30 | |
| 31 | |
| 32 | " Code {{{1 |
| 33 | syntax cluster typstCode |
| 34 | \ contains=@typstCommon |
| 35 | \ ,@typstCodeKeywords |
| 36 | \ ,@typstCodeConstants |
| 37 | \ ,@typstCodeIdentifiers |
| 38 | \ ,@typstCodeFunctions |
| 39 | \ ,@typstCodeParens |
| 40 | |
| 41 | " Code > Keywords {{{2 |
| 42 | syntax cluster typstCodeKeywords |
| 43 | \ contains=typstCodeConditional |
| 44 | \ ,typstCodeRepeat |
| 45 | \ ,typstCodeKeyword |
| 46 | \ ,typstCodeStatement |
| 47 | syntax keyword typstCodeConditional |
| 48 | \ contained |
| 49 | \ if else |
| 50 | syntax keyword typstCodeRepeat |
| 51 | \ contained |
| 52 | \ while for |
| 53 | syntax keyword typstCodeKeyword |
| 54 | \ contained |
| 55 | \ not in and or return |
| 56 | syntax region typstCodeStatement |
| 57 | \ contained |
| 58 | \ matchgroup=typstCodeStatementWord start=/\v(let|set|import|include)>/ |
| 59 | \ matchgroup=Noise end=/\v%(;|$)/ |
| 60 | \ contains=@typstCode |
| 61 | syntax region typstCodeStatement |
| 62 | \ contained |
| 63 | \ matchgroup=typstCodeStatementWord start=/show/ |
| 64 | \ matchgroup=Noise end=/\v%(:|$)/ keepend |
| 65 | \ contains=@typstCode |
| 66 | \ skipwhite nextgroup=@typstCode,typstCodeShowRocket |
| 67 | syntax match typstCodeShowRocket |
| 68 | \ contained |
| 69 | \ /.*=>/ |
| 70 | \ contains=@typstCode |
| 71 | \ skipwhite nextgroup=@typstCode |
| 72 | |
| 73 | " Code > Identifiers {{{2 |
| 74 | syntax cluster typstCodeIdentifiers |
| 75 | \ contains=typstCodeIdentifier |
| 76 | \ ,typstCodeFieldAccess |
| 77 | syntax match typstCodeIdentifier |
| 78 | \ contained |
| 79 | \ /\v\w\k*>(<%(let|set|show|import|include))@<![\.\[\(]@!/ |
| 80 | syntax match typstCodeFieldAccess |
| 81 | \ contained |
| 82 | \ /\v\w\k*>(<%(let|set|show|import|include))@<!\.[\[\(]@!/ |
| 83 | \ nextgroup=typstCodeFieldAccess,typstCodeFunction |
| 84 | |
| 85 | " Code > Functions {{{2 |
| 86 | syntax cluster typstCodeFunctions |
| 87 | \ contains=typstCodeFunction |
| 88 | syntax match typstCodeFunction |
| 89 | \ contained |
| 90 | \ /\v\w\k*>(<%(let|set|show|import|include))@<![\(\[]@=/ |
| 91 | \ nextgroup=typstCodeFunctionArgument |
| 92 | syntax match typstCodeFunctionArgument |
| 93 | \ contained |
| 94 | \ /\v%(%(\(.{-}\)|\[.{-}\]|\{.{-}\}))*/ transparent |
| 95 | \ contains=@typstCode |
| 96 | |
| 97 | " Code > Constants {{{2 |
| 98 | syntax cluster typstCodeConstants |
| 99 | \ contains=typstCodeConstant |
| 100 | \ ,typstCodeNumberInteger |
| 101 | \ ,typstCodeNumberFloat |
| 102 | \ ,typstCodeNumberLength |
| 103 | \ ,typstCodeNumberAngle |
| 104 | \ ,typstCodeNumberRatio |
| 105 | \ ,typstCodeNumberFraction |
| 106 | \ ,typstCodeString |
| 107 | \ ,typstCodeLabel |
| 108 | syntax match typstCodeConstant |
| 109 | \ contained |
| 110 | \ /\v<%(none|auto|true|false)-@!>/ |
| 111 | syntax match typstCodeNumberInteger |
| 112 | \ contained |
| 113 | \ /\v<\d+>/ |
| 114 | |
| 115 | syntax match typstCodeNumberFloat |
| 116 | \ contained |
| 117 | \ /\v<\d+\.\d*>/ |
| 118 | syntax match typstCodeNumberLength |
| 119 | \ contained |
| 120 | \ /\v<\d+(\.\d*)?(pt|mm|cm|in|em)>/ |
| 121 | syntax match typstCodeNumberAngle |
| 122 | \ contained |
| 123 | \ /\v<\d+(\.\d*)?(deg|rad)>/ |
| 124 | syntax match typstCodeNumberRatio |
| 125 | \ contained |
| 126 | \ /\v<\d+(\.\d*)?\%/ |
| 127 | syntax match typstCodeNumberFraction |
| 128 | \ contained |
| 129 | \ /\v<\d+(\.\d*)?fr>/ |
| 130 | syntax region typstCodeString |
| 131 | \ contained |
| 132 | \ start=/"/ skip=/\v\\\\|\\"/ end=/"/ |
| 133 | \ contains=@Spell |
| 134 | syntax match typstCodeLabel |
| 135 | \ contained |
| 136 | \ /\v\<\K%(\k*-*)*\>/ |
| 137 | |
| 138 | " Code > Parens {{{2 |
| 139 | syntax cluster typstCodeParens |
| 140 | \ contains=typstCodeParen |
| 141 | \ ,typstCodeBrace |
| 142 | \ ,typstCodeBracket |
| 143 | \ ,typstCodeDollar |
| 144 | \ ,typstMarkupRawInline |
| 145 | \ ,typstMarkupRawBlock |
| 146 | syntax region typstCodeParen |
| 147 | \ contained |
| 148 | \ matchgroup=Noise start=/(/ end=/)/ |
| 149 | \ contains=@typstCode |
| 150 | syntax region typstCodeBrace |
| 151 | \ contained |
| 152 | \ matchgroup=Noise start=/{/ end=/}/ |
| 153 | \ contains=@typstCode |
| 154 | syntax region typstCodeBracket |
| 155 | \ contained |
| 156 | \ matchgroup=Noise start=/\[/ end=/\]/ |
| 157 | \ contains=@typstMarkup |
| 158 | syntax region typstCodeDollar |
| 159 | \ contained |
| 160 | \ matchgroup=Number start=/\\\@<!\$/ end=/\\\@<!\$/ |
| 161 | \ contains=@typstMath |
| 162 | |
| 163 | |
| 164 | " Hashtag {{{1 |
| 165 | syntax cluster typstHashtag |
| 166 | \ contains=@typstHashtagKeywords |
| 167 | \ ,@typstHashtagConstants |
| 168 | \ ,@typstHashtagIdentifiers |
| 169 | \ ,@typstHashtagFunctions |
| 170 | \ ,@typstHashtagParens |
| 171 | |
| 172 | " Hashtag > Keywords {{{2 |
| 173 | syntax cluster typstHashtagKeywords |
| 174 | \ contains=typstHashtagConditional |
| 175 | \ ,typstHashtagRepeat |
| 176 | \ ,typstHashtagKeywords |
| 177 | \ ,typstHashtagStatement |
| 178 | |
| 179 | " syntax match typstHashtagControlFlowError |
| 180 | " \ /\v#%(if|while|for)>-@!.{-}$\_.{-}%(\{|\[|\()/ |
| 181 | syntax match typstHashtagControlFlow |
| 182 | \ /\v#%(if|while|for)>.{-}\ze%(\{|\[|\()/ |
| 183 | \ contains=typstHashtagConditional,typstHashtagRepeat |
| 184 | \ nextgroup=@typstCode |
| 185 | syntax region typstHashtagConditional |
| 186 | \ contained |
| 187 | \ start=/\v#if>/ end=/\v\ze(\{|\[)/ |
| 188 | \ contains=@typstCode |
| 189 | syntax region typstHashtagRepeat |
| 190 | \ contained |
| 191 | \ start=/\v#(while|for)>/ end=/\v\ze(\{|\[)/ |
| 192 | \ contains=@typstCode |
| 193 | syntax match typstHashtagKeyword |
| 194 | \ /\v#(return)>/ |
| 195 | \ skipwhite nextgroup=@typstCode |
| 196 | syntax region typstHashtagStatement |
| 197 | \ matchgroup=typstHashtagStatementWord start=/\v#(let|set|import|include)>/ |
| 198 | \ matchgroup=Noise end=/\v%(;|$)/ |
| 199 | \ contains=@typstCode |
| 200 | syntax region typstHashtagStatement |
| 201 | \ matchgroup=typstHashtagStatementWord start=/#show/ |
| 202 | \ matchgroup=Noise end=/\v%(:|$)/ keepend |
| 203 | \ contains=@typstCode |
| 204 | \ skipwhite nextgroup=@typstCode,typstCodeShowRocket |
| 205 | |
| 206 | " Hashtag > Constants {{{2 |
| 207 | syntax cluster typstHashtagConstants |
| 208 | \ contains=typstHashtagConstant |
| 209 | syntax match typstHashtagConstant |
| 210 | \ /\v#(none|auto|true|false)>/ |
| 211 | |
| 212 | " Hashtag > Identifiers {{{2 |
| 213 | syntax cluster typstHashtagIdentifiers |
| 214 | \ contains=typstHashtagIdentifier |
| 215 | \ ,typstHashtagFieldAccess |
| 216 | syntax match typstHashtagIdentifier |
| 217 | \ /\v#\w\k*>(<%(let|set|show|import|include))@<![\.\[\(]@!/ |
| 218 | syntax match typstHashtagFieldAccess |
| 219 | \ /\v#\w\k*>(<%(let|set|show|import|include))@<!\.[\[\(]@!/ |
| 220 | \ nextgroup=typstCodeFieldAccess,typstCodeFunction |
| 221 | |
| 222 | " Hashtag > Functions {{{2 |
| 223 | syntax cluster typstHashtagFunctions |
| 224 | \ contains=typstHashtagFunction |
| 225 | syntax match typstHashtagFunction |
| 226 | \ /\v#\w\k*>(<%(let|set|show|import|include))@<![\(\[]@=/ |
| 227 | \ nextgroup=typstCodeFunctionArgument |
| 228 | |
| 229 | " Hashtag > Parens {{{2 |
| 230 | syntax cluster typstHashtagParens |
| 231 | \ contains=typstHashtagParen |
| 232 | \ ,typstHashtagBrace |
| 233 | \ ,typstHashtagBracket |
| 234 | \ ,typstHashtagDollar |
| 235 | syntax region typstHashtagParen |
| 236 | \ matchgroup=Noise start=/#(/ end=/)/ |
| 237 | \ contains=@typstCode |
| 238 | syntax region typstHashtagBrace |
| 239 | \ matchgroup=Noise start=/#{/ end=/}/ |
| 240 | \ contains=@typstCode |
| 241 | syntax region typstHashtagBracket |
| 242 | \ matchgroup=Noise start=/#\[/ end=/\]/ |
| 243 | \ contains=@typstMarkup |
| 244 | syntax region typstHashtagDollar |
| 245 | \ matchgroup=Noise start=/#\$/ end=/\\\@<!\$/ |
| 246 | \ contains=@typstMath |
| 247 | |
| 248 | |
| 249 | " Markup {{{1 |
| 250 | syntax cluster typstMarkup |
| 251 | \ contains=@typstCommon |
| 252 | \ ,@Spell |
| 253 | \ ,@typstHashtag |
| 254 | \ ,@typstMarkupText |
| 255 | \ ,@typstMarkupParens |
| 256 | |
| 257 | " Markup > Text {{{2 |
| 258 | syntax cluster typstMarkupText |
| 259 | \ contains=typstMarkupRawInline |
| 260 | \ ,typstMarkupRawBlock |
| 261 | \ ,typstMarkupLabel |
| 262 | \ ,typstMarkupReference |
| 263 | \ ,typstMarkupUrl |
| 264 | \ ,typstMarkupHeading |
| 265 | \ ,typstMarkupBulletList |
| 266 | \ ,typstMarkupEnumList |
| 267 | \ ,typstMarkupTermList |
| 268 | \ ,typstMarkupBold |
| 269 | \ ,typstMarkupItalic |
| 270 | \ ,typstMarkupLinebreak |
| 271 | \ ,typstMarkupNonbreakingSpace |
| 272 | \ ,typstMarkupShy |
| 273 | \ ,typstMarkupDash |
| 274 | \ ,typstMarkupEllipsis |
| 275 | |
| 276 | " Raw Text |
| 277 | syntax match typstMarkupRawInline |
| 278 | \ /`.\{-}`/ |
| 279 | syntax region typstMarkupRawBlock |
| 280 | \ matchgroup=Macro start=/```\w*/ |
| 281 | \ matchgroup=Macro end=/```/ keepend |
| 282 | syntax region typstMarkupCodeBlockTypst |
| 283 | \ matchgroup=Macro start=/```typst/ |
| 284 | \ matchgroup=Macro end=/```/ contains=@typstCode keepend |
| 285 | \ concealends |
| 286 | |
| 287 | for s:name in get(g:, 'typst_embedded_languages', []) |
| 288 | let s:include = ['syntax include' |
| 289 | \ ,'@typstEmbedded_'..s:name |
| 290 | \ ,'syntax/'..s:name..'.vim'] |
| 291 | let s:rule = ['syn region' |
| 292 | \,s:name |
| 293 | \,'matchgroup=Macro' |
| 294 | \,'start=/```'..s:name..'\>/ end=/```/' |
| 295 | \,'contains=@typstEmbedded_'..s:name |
| 296 | \,'keepend' |
| 297 | \,'concealends'] |
| 298 | execute 'silent! ' .. join(s:include, ' ') |
| 299 | unlet! b:current_syntax |
| 300 | execute join(s:rule, ' ') |
| 301 | endfor |
| 302 | |
| 303 | " Label & Reference |
| 304 | syntax match typstMarkupLabel |
| 305 | \ /\v\<\K%(\k*-*)*\>/ |
| 306 | syntax match typstMarkupReference |
| 307 | \ /\v\@\K%(\k*-*)*/ |
| 308 | |
| 309 | " URL |
| 310 | syntax match typstMarkupUrl |
| 311 | \ #\v\w+://\S*# |
| 312 | |
| 313 | " Heading |
| 314 | syntax match typstMarkupHeading |
| 315 | \ /^\s*\zs=\{1,6}\s.*$/ |
| 316 | \ contains=typstMarkupLabel,@Spell |
| 317 | |
| 318 | " Lists |
| 319 | syntax match typstMarkupBulletList |
| 320 | \ /\v^\s*-\s+/ |
| 321 | syntax match typstMarkupEnumList |
| 322 | \ /\v^\s*(\+|\d+\.)\s+/ |
| 323 | syntax region typstMarkupTermList |
| 324 | \ oneline start=/\v^\s*\/\s/ end=/:/ |
| 325 | \ contains=@typstMarkup |
| 326 | |
| 327 | " Bold & Italic |
| 328 | syntax match typstMarkupBold |
| 329 | \ /\v(\w|\\)@1<!\*\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!\*/ |
| 330 | \ contains=typstMarkupBoldRegion |
| 331 | syntax match typstMarkupItalic |
| 332 | \ /\v(\w|\\)@1<!_\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!_/ |
| 333 | \ contains=typstMarkupItalicRegion |
| 334 | syntax match typstMarkupBoldItalic |
| 335 | \ contained |
| 336 | \ /\v(\w|\\)@1<![_\*]\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!\2/ |
| 337 | \ contains=typstMarkupBoldRegion,typstMarkupItalicRegion |
| 338 | syntax region typstMarkupBoldRegion |
| 339 | \ contained |
| 340 | \ transparent matchgroup=typstMarkupBold |
| 341 | \ start=/\(^\|[^0-9a-zA-Z]\)\@<=\*/ end=/\*\($\|[^0-9a-zA-Z]\)\@=/ |
| 342 | \ concealends contains=typstMarkupBoldItalic,typstMarkupLabel,@Spell |
| 343 | syntax region typstMarkupItalicRegion |
| 344 | \ contained |
| 345 | \ transparent matchgroup=typstMarkupItalic |
| 346 | \ start=/\(^\|[^0-9a-zA-Z]\)\@<=_/ end=/_\($\|[^0-9a-zA-Z]\)\@=/ |
| 347 | \ concealends contains=typstMarkupBoldItalic,typstMarkupLabel,@Spell |
| 348 | |
| 349 | " Linebreak & Special Whitespace |
| 350 | syntax match typstMarkupLinebreak |
| 351 | \ /\\\\/ |
| 352 | syntax match typstMarkupNonbreakingSpace |
| 353 | \ /\~/ |
| 354 | syntax match typstMarkupShy |
| 355 | \ /-?/ |
| 356 | |
| 357 | " Special Symbols |
| 358 | syntax match typstMarkupDash |
| 359 | \ /-\{2,3}/ |
| 360 | syntax match typstMarkupEllipsis |
| 361 | \ /\.\.\./ |
| 362 | |
| 363 | " Markup > Parens {{{2 |
| 364 | syntax cluster typstMarkupParens |
| 365 | \ contains=typstMarkupBracket |
| 366 | \ ,typstMarkupDollar |
| 367 | syntax region typstMarkupBracket |
| 368 | \ matchgroup=Noise start=/\[/ end=/\]/ |
| 369 | \ contains=@typstMarkup |
| 370 | syntax region typstMarkupDollar |
| 371 | \ matchgroup=Special start=/\\\@<!\$/ end=/\\\@<!\$/ |
| 372 | \ contains=@typstMath |
| 373 | |
| 374 | |
| 375 | " Math {{{1 |
| 376 | syntax cluster typstMath |
| 377 | \ contains=@typstCommon |
| 378 | \ ,@typstHashtag |
| 379 | \ ,typstMathIdentifier |
| 380 | \ ,typstMathFunction |
| 381 | \ ,typstMathNumber |
| 382 | \ ,typstMathSymbol |
| 383 | \ ,typstMathBold |
| 384 | \ ,typstMathScripts |
| 385 | \ ,typstMathQuote |
| 386 | |
| 387 | syntax match typstMathIdentifier |
| 388 | \ /\a\a\+/ |
| 389 | \ contained |
| 390 | syntax match typstMathFunction |
| 391 | \ /\a\a\+\ze(/ |
| 392 | \ contained |
| 393 | syntax match typstMathNumber |
| 394 | \ /\<\d\+\>/ |
| 395 | \ contained |
| 396 | syntax region typstMathQuote |
| 397 | \ matchgroup=String start=/"/ skip=/\\"/ end=/"/ |
| 398 | \ contained |
| 399 | |
| 400 | " Math > Linked groups {{{2 |
| 401 | highlight default link typstMathIdentifier Identifier |
| 402 | highlight default link typstMathFunction Statement |
| 403 | highlight default link typstMathNumber Number |
| 404 | highlight default link typstMathSymbol Statement |
| 405 | |
| 406 | " Highlighting {{{1 |
| 407 | |
| 408 | " Highlighting > Linked groups {{{2 |
| 409 | highlight default link typstCommentBlock Comment |
| 410 | highlight default link typstCommentLine Comment |
| 411 | highlight default link typstCommentTodo Todo |
| 412 | highlight default link typstCodeConditional Conditional |
| 413 | highlight default link typstCodeRepeat Repeat |
| 414 | highlight default link typstCodeKeyword Keyword |
| 415 | highlight default link typstCodeConstant Constant |
| 416 | highlight default link typstCodeNumberInteger Number |
| 417 | highlight default link typstCodeNumberFloat Number |
| 418 | highlight default link typstCodeNumberLength Number |
| 419 | highlight default link typstCodeNumberAngle Number |
| 420 | highlight default link typstCodeNumberRatio Number |
| 421 | highlight default link typstCodeNumberFraction Number |
| 422 | highlight default link typstCodeString String |
| 423 | highlight default link typstCodeLabel Structure |
| 424 | highlight default link typstCodeStatementWord Statement |
| 425 | highlight default link typstCodeIdentifier Identifier |
| 426 | highlight default link typstCodeFieldAccess Identifier |
| 427 | highlight default link typstCodeFunction Function |
| 428 | highlight default link typstCodeParen Noise |
| 429 | highlight default link typstCodeBrace Noise |
| 430 | highlight default link typstCodeBracket Noise |
| 431 | highlight default link typstCodeDollar Noise |
| 432 | " highlight default link typstHashtagControlFlowError Error |
| 433 | highlight default link typstHashtagConditional Conditional |
| 434 | highlight default link typstHashtagRepeat Repeat |
| 435 | highlight default link typstHashtagKeyword Keyword |
| 436 | highlight default link typstHashtagConstant Constant |
| 437 | highlight default link typstHashtagStatementWord Statement |
| 438 | highlight default link typstHashtagIdentifier Identifier |
| 439 | highlight default link typstHashtagFieldAccess Identifier |
| 440 | highlight default link typstHashtagFunction Function |
| 441 | highlight default link typstHashtagParen Noise |
| 442 | highlight default link typstHashtagBrace Noise |
| 443 | highlight default link typstHashtagBracket Noise |
| 444 | highlight default link typstHashtagDollar Noise |
| 445 | highlight default link typstMarkupRawInline Macro |
| 446 | highlight default link typstMarkupRawBlock Macro |
| 447 | highlight default link typstMarkupLabel Structure |
| 448 | highlight default link typstMarkupReference Structure |
| 449 | highlight default link typstMarkupBulletList Structure |
| 450 | " highlight default link typstMarkupItalicError Error |
| 451 | " highlight default link typstMarkupBoldError Error |
| 452 | highlight default link typstMarkupEnumList Structure |
| 453 | highlight default link typstMarkupLinebreak Structure |
| 454 | highlight default link typstMarkupNonbreakingSpace Structure |
| 455 | highlight default link typstMarkupShy Structure |
| 456 | highlight default link typstMarkupDash Structure |
| 457 | highlight default link typstMarkupEllipsis Structure |
| 458 | highlight default link typstMarkupTermList Structure |
| 459 | highlight default link typstMarkupDollar Noise |
| 460 | |
| 461 | " Highlighting > Custom Styling {{{2 |
| 462 | highlight! Conceal ctermfg=NONE ctermbg=NONE guifg=NONE guibg=NONE |
| 463 | |
| 464 | highlight default typstMarkupHeading term=underline,bold cterm=underline,bold gui=underline,bold |
| 465 | highlight default typstMarkupUrl term=underline cterm=underline gui=underline |
| 466 | highlight default typstMarkupBold term=bold cterm=bold gui=bold |
| 467 | highlight default typstMarkupItalic term=italic cterm=italic gui=italic |
| 468 | highlight default typstMarkupBoldItalic term=bold,italic cterm=bold,italic gui=bold,italic |
| 469 | |
| 470 | let b:current_syntax = 'typst' |
| 471 | |
| 472 | " }}}1 |