From a2e123a288a72efe3f800c3a90736f9d2e48ebea Mon Sep 17 00:00:00 2001 From: Alex Vear Date: Wed, 7 Nov 2018 11:14:12 +0000 Subject: [PATCH] Prevent the folding of heading syntax in code fences Previously in Markdown files, Vim would treat heading syntax in code fences as Markdown headers. This commit ensures that only headers will be folded by checking the 'synIDattr' of the item. E.g. The comment in this code snippet would have been treated as a Markdown header. ```sh # This is a comment echo "Hello world" ``` --- ftplugin/markdown.vim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 39c0f8e..7cda6b1 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -23,23 +23,27 @@ function! MarkdownFold() let line = getline(v:lnum) " Regular headers - if line =~# '^#\+ ' + if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum) return ">" . match(line, ' ') endif " Setext style headings let nextline = getline(v:lnum + 1) - if (line =~ '^.\+$') && (nextline =~ '^=\+$') + if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1) return ">1" endif - if (line =~ '^.\+$') && (nextline =~ '^-\+$') + if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1) return ">2" endif return "=" endfunction +function! s:NotCodeBlock(lnum) + return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode' +endfunction + function! MarkdownFoldText() let hash_indent = s:HashIndent(v:foldstart) let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')