|
| 1 | +" Vim syntax file |
| 2 | +" Language: svelte.js |
| 3 | +" Author: Robert burner Schadek |
| 4 | +" Maintainer: Robert burner Schadek |
| 5 | + |
| 6 | +if exists("b:current_syntax") |
| 7 | + finish |
| 8 | +endif |
| 9 | + |
| 10 | +runtime! syntax/html.vim |
| 11 | +syntax clear htmlTagName |
| 12 | +syntax match htmlTagName contained "\<[a-zA-Z0-9:-]*\>" |
| 13 | +unlet! b:current_syntax |
| 14 | + |
| 15 | +"" |
| 16 | +" Get the pattern for a HTML {name} attribute with {value}. |
| 17 | +function! s:attr(name, value) |
| 18 | + return a:name . '=\("\|''\)[^\1]*' . a:value . '[^\1]*\1' |
| 19 | +endfunction |
| 20 | + |
| 21 | +"" |
| 22 | +" Check whether a syntax file for a given {language} exists. |
| 23 | +function! s:syntax_available(language) |
| 24 | + return !empty(globpath(&runtimepath, 'syntax/' . a:language . '.vim')) |
| 25 | +endfunction |
| 26 | + |
| 27 | +"" |
| 28 | +" Register {language} for a given {tag}. If [attr_override] is given and not |
| 29 | +" empty, it will be used for the attribute pattern. |
| 30 | +function! s:register_language(language, tag, ...) |
| 31 | + let attr_override = a:0 ? a:1 : '' |
| 32 | + let attr = !empty(attr_override) ? attr_override : s:attr('lang', a:language) |
| 33 | + |
| 34 | + if s:syntax_available(a:language) |
| 35 | + execute 'syntax include @' . a:language . ' syntax/' . a:language . '.vim' |
| 36 | + unlet! b:current_syntax |
| 37 | + execute 'syntax region svelte_' . a:language |
| 38 | + \ 'keepend' |
| 39 | + \ 'start=/<' . a:tag . '\>\_[^>]*' . attr . '\_[^>]*>/' |
| 40 | + \ 'end="</' . a:tag . '>"me=s-1' |
| 41 | + \ 'contains=@' . a:language . ',svelteSurroundingTag' |
| 42 | + \ 'fold' |
| 43 | + endif |
| 44 | +endfunction |
| 45 | + |
| 46 | +if !exists("g:svelte_disable_pre_processors") || !g:svelte_disable_pre_processors |
| 47 | + call s:register_language('less', 'style') |
| 48 | + call s:register_language('pug', 'template', s:attr('lang', '\%(pug\|jade\)')) |
| 49 | + call s:register_language('slm', 'template') |
| 50 | + call s:register_language('handlebars', 'template') |
| 51 | + call s:register_language('haml', 'template') |
| 52 | + call s:register_language('typescript', 'script', '\%(lang=\("\|''\)[^\1]*\(ts\|typescript\)[^\1]*\1\|ts\)') |
| 53 | + call s:register_language('coffee', 'script') |
| 54 | + call s:register_language('stylus', 'style') |
| 55 | + call s:register_language('sass', 'style') |
| 56 | + call s:register_language('scss', 'style') |
| 57 | +endif |
| 58 | + |
| 59 | +syn region svelteSurroundingTag contained start=+<\(script\|style\)+ end=+>+ fold contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent |
| 60 | +syn keyword htmlSpecialTagName contained template |
| 61 | +syn keyword htmlArg contained scoped ts |
| 62 | +syn match htmlArg "[@v:][-:.0-9_a-z]*\>" contained |
| 63 | + |
| 64 | +let b:current_syntax = "svelte" |
0 commit comments