From: Hiroshi Shirosaki Date: Thu, 7 Jan 2016 08:57:00 +0000 (+0900) Subject: Improve code block syntax and fix test failures X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/824e85c3e7b40466c2a375e37c7e81a0584c7a79 Improve code block syntax and fix test failures - Remove syntax match of mkdCodeStart and mkdCodeEnd to work syntax mkdCode properly without a language specifier. - Add highlight setting of mkdCodeStart and mkdCodeEnd. - Change the regexp pattern to use c++ language specifier. - Add viml language. - Add tests for code block syntax. - Fixed `is_mkdCode(lnum)` to work with other than mkdCode, but it doesn't work as expected in some cases. --- diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim index d965a71..3603485 100644 --- a/after/ftplugin/markdown.vim +++ b/after/ftplugin/markdown.vim @@ -7,7 +7,8 @@ " original version from Steve Losh's gist: https://gist.github.com/1038710 function! s:is_mkdCode(lnum) - return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode' + let name = synIDattr(synID(a:lnum, 1, 0), 'name') + return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)') endfunction if get(g:, "vim_markdown_folding_style_pythonic", 0) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 74bbc54..9e4db2d 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -555,12 +555,17 @@ command! -buffer Tocv call s:Toc('vertical') command! -buffer Toct call s:Toc('tab') " Heavily based on vim-notes - http://peterodding.com/code/vim/notes/ +let s:filetype_dict = { + \ 'c++': 'cpp', + \ 'viml': 'vim' +\ } + function! s:Markdown_highlight_sources(force) " Syntax highlight source code embedded in notes. " Look for code blocks in the current file let filetypes = {} for line in getline(1, '$') - let ft = matchstr(line, '```\zs\w*\>') + let ft = matchstr(line, '```\zs[0-9A-Za-z_+-]*') if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif endfor if !exists('b:mkd_known_filetypes') @@ -575,9 +580,13 @@ function! s:Markdown_highlight_sources(force) let endgroup = 'mkdCodeEnd' for ft in keys(filetypes) if a:force || !has_key(b:mkd_known_filetypes, ft) - - let group = 'mkdSnippet' . toupper(ft) - let include = s:syntax_include(ft) + if has_key(s:filetype_dict, ft) + let filetype = s:filetype_dict[ft] + else + let filetype = ft + endif + let group = 'mkdSnippet' . toupper(substitute(filetype, "[+-]", "_", "g")) + let include = s:syntax_include(filetype) let command = 'syntax region %s matchgroup=%s start="^\s*```%s$" matchgroup=%s end="\s*```$" keepend contains=%s%s' execute printf(command, group, startgroup, ft, endgroup, include, has('conceal') ? ' concealends' : '') execute printf('syntax cluster mkdNonListItem add=%s', group) diff --git a/indent/markdown.vim b/indent/markdown.vim index 3f89b5c..e21f287 100755 --- a/indent/markdown.vim +++ b/indent/markdown.vim @@ -19,7 +19,8 @@ setlocal comments+=b:> if exists("*GetMarkdownIndent") | finish | endif function! s:is_mkdCode(lnum) - return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode' + let name = synIDattr(synID(a:lnum, 1, 0), 'name') + return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)') endfunction function! s:is_li_start(line) diff --git a/syntax/markdown.vim b/syntax/markdown.vim index 7244cb5..e28644a 100644 --- a/syntax/markdown.vim +++ b/syntax/markdown.vim @@ -75,8 +75,6 @@ syn region mkdCode start=/\s*``[^`]*/ end=/[^`]*``\s*/ syn region mkdCode start=/^\s*```\s*[0-9A-Za-z_+-]*\s*$/ end=/^\s*```\s*$/ syn region mkdCode start=/\s*\~\~[^\~]*/ end=/[^\~]*\~\~\s*/ syn region mkdCode start=/^\s*\~\~\~\s*[0-9A-Za-z_+-]*\s*$/ end=/^\s*\~\~\~\s*$/ -syn match mkdCodeStart /^```\w*$/ -syn match mkdCodeEnd /^```$/ syn region mkdCode start="]*>" end="" syn region mkdCode start="]*>" end="" syn region mkdFootnote start="\[^" end="\]" @@ -112,11 +110,13 @@ if get(g:, 'vim_markdown_math', 0) syn region mkdMath matchgroup=mkdDelimiter start="\\\@ +code +``` + +```ruby +def a +end +``` + +Execute (fenced code block syntax with a language specifier): + let b:func = Markdown_GetFunc('vim-markdown/ftplugin/markdown.vim', 'Markdown_refresh_syntax') + call b:func(0) + AssertEqual SyntaxOf('include'), 'cInclude' + AssertEqual SyntaxOf('code'), 'mkdSnippetCPP' + AssertEqual SyntaxOf('def'), 'rubyDefine' + +Given markdown; +```a+b- +code +``` + +Execute (fenced code block syntax with an unknown language specifier): + let b:func = Markdown_GetFunc('vim-markdown/ftplugin/markdown.vim', 'Markdown_refresh_syntax') + call b:func(0) + AssertEqual SyntaxOf('code'), 'mkdSnippetA_B_' + # Math Given markdown;