|
| 1 | +" ___vital___ |
| 2 | +" NOTE: lines between '" ___vital___' is generated by :Vitalize. |
| 3 | +" Do not modify the code nor insert new lines before '" ___vital___' |
| 4 | +function! s:_SID() abort |
| 5 | + return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$') |
| 6 | +endfunction |
| 7 | +execute join(['function! vital#_lsp#VS#Vim#Syntax#Markdown#import() abort', printf("return map({'apply': ''}, \"vital#_lsp#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") |
| 8 | +delfunction s:_SID |
| 9 | +" ___vital___ |
| 10 | +function! s:apply(text, ...) abort |
| 11 | + if !exists('b:___VS_Vim_Syntax_Markdown') |
| 12 | + runtime! syntax/markdown.vim |
| 13 | + let b:___VS_Vim_Syntax_Markdown = {} |
| 14 | + endif |
| 15 | + |
| 16 | + try |
| 17 | + for [l:mark, l:filetype] in items(s:_get_filetype_map(a:text, get(a:000, 0, {}))) |
| 18 | + let l:group = substitute(toupper(l:mark), '\.', '_', 'g') |
| 19 | + if has_key(b:___VS_Vim_Syntax_Markdown, l:group) |
| 20 | + continue |
| 21 | + endif |
| 22 | + let b:___VS_Vim_Syntax_Markdown[l:group] = v:true |
| 23 | + |
| 24 | + try |
| 25 | + if exists('b:current_syntax') |
| 26 | + unlet b:current_syntax |
| 27 | + endif |
| 28 | + execute printf('syntax include @%s syntax/%s.vim', l:group, l:filetype) |
| 29 | + execute printf('syntax region %s matchgroup=Conceal start=/%s/rs=e matchgroup=Conceal end=/%s/re=s contains=@%s containedin=ALL keepend concealends', |
| 30 | + \ l:group, |
| 31 | + \ printf('^\s*```\s*%s\s*', l:mark), |
| 32 | + \ '\s*```\s*$', |
| 33 | + \ l:group |
| 34 | + \ ) |
| 35 | + catch /.*/ |
| 36 | + echomsg printf('[VS.Vim.Syntax.Markdown] The `%s` is not valid filetype! You can add `"let g:markdown_fenced_languages = ["FILETYPE=%s"]`.', l:mark, l:mark) |
| 37 | + endtry |
| 38 | + endfor |
| 39 | + catch /.*/ |
| 40 | + echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint }) |
| 41 | + endtry |
| 42 | +endfunction |
| 43 | + |
| 44 | +" |
| 45 | +" _get_filetype_map |
| 46 | +" |
| 47 | +function! s:_get_filetype_map(text, filetype_map) abort |
| 48 | + let l:filetype_map = {} |
| 49 | + for l:mark in s:_find_marks(a:text) |
| 50 | + let l:filetype_map[l:mark] = s:_get_filetype_from_mark(l:mark, a:filetype_map) |
| 51 | + endfor |
| 52 | + return l:filetype_map |
| 53 | +endfunction |
| 54 | + |
| 55 | +" |
| 56 | +" _find_marks |
| 57 | +" |
| 58 | +function! s:_find_marks(text) abort |
| 59 | + let l:marks = {} |
| 60 | + |
| 61 | + " find from buffer contents. |
| 62 | + let l:pos = 0 |
| 63 | + while 1 |
| 64 | + let l:match = matchlist(a:text, '```\s*\(\w\+\)', l:pos, 1) |
| 65 | + if empty(l:match) |
| 66 | + break |
| 67 | + endif |
| 68 | + let l:marks[l:match[1]] = v:true |
| 69 | + let l:pos = matchend(a:text, '```\s*\(\w\+\)', l:pos, 1) |
| 70 | + endwhile |
| 71 | + |
| 72 | + return keys(l:marks) |
| 73 | +endfunction |
| 74 | + |
| 75 | +" |
| 76 | +" _get_filetype_from_mark |
| 77 | +" |
| 78 | +function! s:_get_filetype_from_mark(mark, filetype_map) abort |
| 79 | + for l:config in get(g:, 'markdown_fenced_languages', []) |
| 80 | + if l:config !~# '=' |
| 81 | + if l:config ==# a:mark |
| 82 | + return a:mark |
| 83 | + endif |
| 84 | + else |
| 85 | + let l:config = split(l:config, '=') |
| 86 | + if l:config[1] ==# a:mark |
| 87 | + return l:config[0] |
| 88 | + endif |
| 89 | + endif |
| 90 | + endfor |
| 91 | + return get(a:filetype_map, a:mark, a:mark) |
| 92 | +endfunction |
| 93 | + |
0 commit comments