From 5408d74b351fcb2b26b2fa74c937901e729b152c Mon Sep 17 00:00:00 2001 From: Tinmarino Date: Sat, 1 Aug 2020 22:28:32 -0400 Subject: [PATCH] Syntax: Html support nested, concealable tag and faster Use of syntax region => clearer, faster --- syntax/vimwiki.vim | 129 +++++++++++++++++++++++++++++++++++++-------- test/syntax.vader | 56 ++++++++++++++++++++ test/vimrc | 21 +++++--- 3 files changed, 178 insertions(+), 28 deletions(-) diff --git a/syntax/vimwiki.vim b/syntax/vimwiki.vim index e3e8f76..513ba23 100644 --- a/syntax/vimwiki.vim +++ b/syntax/vimwiki.vim @@ -13,6 +13,10 @@ endif let s:current_syntax = vimwiki#vars#get_wikilocal('syntax') +" Get config: possibly concealed chars +let b:vimwiki_syntax_conceal = exists('+conceallevel') ? ' conceal' : '' +let b:vimwiki_syntax_concealends = has('conceal') ? ' concealends' : '' + call vimwiki#vars#populate_syntax_vars(s:current_syntax) @@ -82,6 +86,9 @@ function! s:highlight_existing_links() abort endfunction +function! s:in_typeface(s_typeface_char) +endfunction + " use max highlighting - could be quite slow if there are too many wikifiles if vimwiki#vars#get_wikilocal('maxhi') " WikiLink @@ -152,34 +159,31 @@ endfor -" possibly concealed chars -let s:conceal = exists('+conceallevel') ? ' conceal' : '' - if vimwiki#vars#get_global('conceal_onechar_markers') execute 'syn match VimwikiEqInChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_eqin').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_eqin').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiBoldChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_bold').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_bold').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiItalicChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_italic').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_italic').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiBoldItalicChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_bolditalic').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_bolditalic').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiItalicBoldChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_italicbold').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_italicbold').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiCodeChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_code').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_code').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiDelTextChar contained /'. - \ vimwiki#vars#get_syntaxlocal('char_deltext').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_deltext').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiSuperScript contained /'. - \ vimwiki#vars#get_syntaxlocal('char_superscript').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_superscript').'/'.b:vimwiki_syntax_conceal execute 'syn match VimwikiSubScript contained /'. - \ vimwiki#vars#get_syntaxlocal('char_subscript').'/'.s:conceal + \ vimwiki#vars#get_syntaxlocal('char_subscript').'/'.b:vimwiki_syntax_conceal endif let s:options = ' contained transparent contains=NONE' if exists('+conceallevel') - let s:options .= s:conceal + let s:options .= b:vimwiki_syntax_conceal endif " A shortener for long URLs: LinkRest (a middle part of the URL) is concealed @@ -270,16 +274,16 @@ elseif vimwiki#vars#get_global('hl_cb_checked') == 2 endif -execute 'syntax match VimwikiEqIn /'.vimwiki#vars#get_syntaxlocal('rxEqIn'). - \ '/ contains=VimwikiEqInChar,@NoSpell' -execute 'syntax match VimwikiEqInT /'.vimwiki#vars#get_syntaxlocal('rxEqIn'). - \ '/ contained contains=VimwikiEqInCharT,@NoSpell' - execute 'syntax match VimwikiBold /'.vimwiki#vars#get_syntaxlocal('rxBold'). \ '/ contains=VimwikiBoldChar,@Spell' execute 'syntax match VimwikiBoldT /'.vimwiki#vars#get_syntaxlocal('rxBold'). \ '/ contained contains=VimwikiBoldCharT,@Spell' +execute 'syntax match VimwikiEqIn /'.vimwiki#vars#get_syntaxlocal('rxEqIn'). + \ '/ contains=VimwikiEqInChar,@NoSpell' +execute 'syntax match VimwikiEqInT /'.vimwiki#vars#get_syntaxlocal('rxEqIn'). + \ '/ contained contains=VimwikiEqInCharT,@NoSpell' + execute 'syntax match VimwikiItalic /'.vimwiki#vars#get_syntaxlocal('rxItalic'). \ '/ contains=VimwikiItalicChar,@Spell' execute 'syntax match VimwikiItalicT /'.vimwiki#vars#get_syntaxlocal('rxItalic'). @@ -339,13 +343,74 @@ syntax match VimwikiPlaceholderParam /.*/ contained " html tags +" Copied from $VIMRUNTIME +" Note: The me=s-1 was omited from the region definition +" See: `syn region VimwikiBoldUnderlineItalic contained start="" end=""me=s-1 contains=VimwikiHTMLTag...` +" Note: Not configurable if vimwiki#vars#get_global('valid_html_tags') !=? '' let s:html_tags = join(split(vimwiki#vars#get_global('valid_html_tags'), '\s*,\s*'), '\|') exe 'syntax match VimwikiHTMLtag #\c#' - execute 'syntax match VimwikiBold #\c.\{-}# contains=VimwikiHTMLTag' - execute 'syntax match VimwikiItalic #\c.\{-}# contains=VimwikiHTMLTag' - execute 'syntax match VimwikiUnderline #\c.\{-}# contains=VimwikiHTMLTag' + " Helper: Create highlight region between html tags + " :param: tag example 'b' + " :param: syntax_group example: VimwikiBold + " :param: contains coma separated and prefixed, default VimwikiHTMLTag + " :param: (1) is contained + function! s:highlight_html(tag, syntax_group, contains, ...) + let opt_is_contained = a:0 > 0 ? 'contained ' : '' + let opt_contains = '' + if a:contains !=# '' + let opt_contains = 'contains=' . a:contains . ' ' + endif + exe 'syn region ' a:syntax_group . ' matchgroup=VimwikiHTMLDelimiter ' . + \ opt_is_contained . + \ 'start="<' . a:tag . '>" end="" '. + \ opt_contains . + \ b:vimwiki_syntax_concealends + endfunction + + " Bold + " -- Bold 1 + call s:highlight_html('b', 'VimwikiBold', 'VimwikiBoldUnderline,VimwikiBoldItalic') + call s:highlight_html('strong', 'VimwikiBold', 'VimwikiBoldUnderline,VimwikiBoldItalic') + " -- Bold 2 + call s:highlight_html('u', 'VimwikiBoldUnderline', 'VimwikiBoldUnderlineItalic', 1) + call s:highlight_html('i', 'VimwikiBoldItalic', 'VimwikiBoldItalicUnderline', 1) + call s:highlight_html('em', 'VimwikiBoldItalic', 'VimwikiBoldItalicUnderline', 1) + " -- Bold 3 + call s:highlight_html('i', 'VimwikiBoldUnderlineItalic', '', 2) + call s:highlight_html('em', 'VimwikiBoldUnderlineItalic', '', 2) + call s:highlight_html('u', 'VimwikiBoldItalicUnderline', '', 2) + + " Italic + " -- Italic 1 + call s:highlight_html('i', 'VimwikiItalic ', 'VimwikiItalicBold,VimwikiItalicUnderline') + call s:highlight_html('em', 'VimwikiItalic ', 'VimwikiItalicBold,VimwikiItalicUnderline') + " -- Italic 2 + call s:highlight_html('b', 'VimwikiItalicBold', 'VimwikiItalicBoldUnderline', 1) + call s:highlight_html('strong', 'VimwikiItalicBold', 'VimwikiItalicBoldUnderline', 1) + call s:highlight_html('u', 'VimwikiItalicUnderline', 'VimwikiItalicUnderlineBold', 1) + " -- Italic 3 + call s:highlight_html('u', 'VimwikiItalicBoldUnderline', '', 2) + call s:highlight_html('b', 'VimwikiItalicUnderlineBold', '', 2) + call s:highlight_html('strong', 'VimwikiItalicUnderlineBold', '', 2) + + " Underline + " -- Underline 1 + call s:highlight_html('u', 'VimwikiUnderline', 'VimwikiUnderlineBold,VimwikiUnderlineItalic') + " -- Underline 2 + call s:highlight_html('b', 'VimwikiUnderlineBold', 'VimwikiUnderlineBoldItalic', 1) + call s:highlight_html('b','VimwikiUnderlineBold', 'VimwikiUnderlineBoldItalic', 1) + call s:highlight_html('strong', 'VimwikiUnderlineBold', 'VimwikiUnderlineBoldItalic', 1) + call s:highlight_html('i', 'VimwikiUnderlineItalic', 'VimwikiUnderlineItalicBold', 1) + call s:highlight_html('em', 'VimwikiUnderlineItalic', 'VimwikiUnderlineItalicBold', 1) + " -- Underline 3 + call s:highlight_html('b', 'VimwikiUnderlineItalicBold', '', 2) + call s:highlight_html('strong', 'VimwikiUnderlineItalicBold', '', 2) + call s:highlight_html('i', 'VimwikiUnderlineBoldItalic', '', 2) + call s:highlight_html('em', 'VimwikiUnderlineBoldItalic', '', 2) + + " Comment: home made execute 'syntax match VimwikiComment /'.vimwiki#vars#get_syntaxlocal('rxComment'). \ '/ contains=@Spell,VimwikiTodo' @@ -387,18 +452,38 @@ hi def link VimwikiMarkers Normal hi def link VimwikiEqIn Number hi def link VimwikiEqInT VimwikiEqIn +" Typeface 1 hi def VimwikiBold term=bold cterm=bold gui=bold hi def link VimwikiBoldT VimwikiBold hi def VimwikiItalic term=italic cterm=italic gui=italic hi def link VimwikiItalicT VimwikiItalic +hi def VimwikiUnderline term=underline cterm=underline gui=underline + +" Typeface 2 +" Bold > Italic > Underline hi def VimwikiBoldItalic term=bold,italic cterm=bold,italic gui=bold,italic hi def link VimwikiItalicBold VimwikiBoldItalic hi def link VimwikiBoldItalicT VimwikiBoldItalic hi def link VimwikiItalicBoldT VimwikiBoldItalic -hi def VimwikiUnderline gui=underline +hi def VimwikiBoldUnderline term=bold,underline cterm=bold,underline gui=bold,underline +hi def link VimwikiUnderlineBold VimwikiBoldUnderline + +hi def VimwikiItalicUnderline term=italic,underline cterm=italic,underline gui=italic,underline +hi def link VimwikiUnderlineItalic VimwikiItalicUnderline + +" Typeface 3 +hi def VimwikiItalicUnderline term=italic,underline cterm=italic,underline gui=italic,underline +hi def link VimwikiBoldUnderlineItalic VimwikiBoldItalicUnderline +hi def link VimwikiItalicBoldUnderline VimwikiBoldItalicUnderline +hi def link VimwikiItalicUnderlineBold VimwikiBoldItalicUnderline +hi def link VimwikiUnderlineBoldItalic VimwikiBoldItalicUnderline +hi def link VimwikiUnderlineItalicBold VimwikiBoldItalicUnderline + +" Typeface 2 +hi def VimwikiBoldUnderlineItalic term=bold,italic,underline cterm=bold,italic,underline gui=bold,italic,underline hi def link VimwikiCode PreProc hi def link VimwikiCodeT VimwikiCode diff --git a/test/syntax.vader b/test/syntax.vader index bbc56cc..01917bc 100644 --- a/test/syntax.vader +++ b/test/syntax.vader @@ -4,6 +4,60 @@ # 1 Typeface {{{1 ################# +# HTML types {{{2 +# Rememner Bold > Italic > Underline (my convention [Tinmarino]) + +Given vimwiki (Typeface for html 1 like italic): + ---- this is bold text 1 ---- + - this is bold 2 - + Italic 1 --cacacacacacacaca-- + Italic 2 -cacacacacacacaca- + Underline -cacacacacc acaca- + +Execute (Set syntax markdown): + call SetSyntax('markdown') + +Execute (Assert Syntax for typeface): + AssertEqual 'VimwikiBold' , SyntaxAt(1, 15) + AssertEqual 'VimwikiBold' , SyntaxAt(2, 15) + AssertEqual 'VimwikiItalic' , SyntaxAt(3, 15) + AssertEqual 'VimwikiItalic' , SyntaxAt(4, 15) + AssertEqual 'VimwikiUnderline' , SyntaxAt(5, 15) + +Given vimwiki (Typeface for html 2 like italicUnderline): + bold this is boldbold italic --------- text 1 ---- + - this is bobold underline ------d 2 - + Italic 1 --cacacabold italic----------acacacaca-- + Italic 2 -cacacaitalic underline-----cacacaca- + Underline -cacacabold underline-------asacc acaca- + Underline -cacacaitalic underline-----asdacacc acaca- + +Execute (Assert Syntax for typeface): + AssertEqual 'VimwikiBoldItalic' , GetSyntaxGroup(1, 30) + AssertEqual 'VimwikiBoldUnderline' , GetSyntaxGroup(2, 30) + AssertEqual 'VimwikiBoldItalic' , GetSyntaxGroup(3, 30) + AssertEqual 'VimwikiItalicUnderline', GetSyntaxGroup(4, 30) + AssertEqual 'VimwikiBoldUnderline' , GetSyntaxGroup(5, 30) + AssertEqual 'VimwikiItalicUnderline', GetSyntaxGroup(6, 30) + +Given vimwiki (Typeface for html 3 like boldItalicUnderline): + bold italic underline + bold italic underline + bold italic underline + bold italic underline + bold italic underline + bold italic underline + +Execute (Assert Syntax for typeface): + AssertEqual 'VimwikiBoldItalicUnderline1', GetSyntaxGroup(1, 22).1 + AssertEqual 'VimwikiBoldItalicUnderline2', GetSyntaxGroup(2, 22).2 + AssertEqual 'VimwikiBoldItalicUnderline3', GetSyntaxGroup(3, 22).3 + AssertEqual 'VimwikiBoldItalicUnderline4', GetSyntaxGroup(4, 22).4 + AssertEqual 'VimwikiBoldItalicUnderline5', GetSyntaxGroup(5, 22).5 + AssertEqual 'VimwikiBoldItalicUnderline6', GetSyntaxGroup(6, 22).6 + +# Keyword uppercase {{{2 + Given vimwiki (TODO, XXX): TODO DONE @@ -20,6 +74,8 @@ Execute (Assert Syntax VimwikiTodo): AssertEqual SyntaxAt(5, 1), 'VimwikiTodo' AssertEqual SyntaxAt(6, 1), 'VimwikiTodo' +# Mardown types {{{2 + Given vimwiki (Typeface for markdown like italic): **bold text 1** __bold text 2__ diff --git a/test/vimrc b/test/vimrc index 873a448..534b0f9 100644 --- a/test/vimrc +++ b/test/vimrc @@ -24,7 +24,7 @@ let vimwiki_default.ext = '.wiki' let vimwiki_default.name = 'DefaultSyntax' let vimwiki_default.base_url = 'https://example.com/' - + " Declare markdown syntax - https://github.github.com/gfm/ let vimwiki_markdown = {} let vimwiki_markdown.path = $HOME . '/testmarkdown' @@ -32,7 +32,7 @@ let vimwiki_markdown.syntax = 'markdown' let vimwiki_markdown.ext = '.md' let vimwiki_markdown.name = 'MarkdownSyntax' - + " Declare mediawiki syntax - https://www.mediawiki.org/wiki/Help:Formatting let vimwiki_mediawiki = {} let vimwiki_mediawiki.path = $HOME . '/testmediawiki' @@ -40,7 +40,7 @@ let vimwiki_mediawiki.syntax = 'media' let vimwiki_mediawiki.ext = '.mw' let vimwiki_mediawiki.name = 'MediaWikiSyntax' - + " Register the 3 wikis let g:vimwiki_list = [vimwiki_default, vimwiki_markdown, vimwiki_mediawiki] @@ -64,10 +64,10 @@ " Map ctrl-p/n for history completion instead of up/down arrows cnoremap cnoremap - + " Map jj to go back to command mode inoremap jj - + " Use to clear the highlighting of :set hlsearch and also preserve the " default behavior of redrawing the screen if maparg('', 'n') ==# '' @@ -204,7 +204,7 @@ 1,$delete " Paste to a named file - " Edit a new file in wiki_root + " Edit a new file in wiki_root edit $HOME/testwiki/test_Convert2Html.wiki " Ensure it is void 1,$delete @@ -231,4 +231,13 @@ call DeleteFile('$HOME/testwiki/test_Convert2Html.wiki') endfunction + + " Get normalized syntax group: usefull for boldItalic Vs italicBold + " -- Here, Vader's SyntaxAt is not enough + " From: https://stackoverflow.com/questions/9464844 + function! GetSyntaxGroup(line, col) + let l:s = synID(a:line, a:col, 1) + return synIDattr(synIDtrans(l:s), 'name') + endfun + " vim: ft=vim:sw=2