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"
```
This commit is contained in:
Alex Vear
2018-11-07 11:14:12 +00:00
committed by Tim Pope
parent c043a93c84
commit a2e123a288

View File

@@ -23,23 +23,27 @@ function! MarkdownFold()
let line = getline(v:lnum) let line = getline(v:lnum)
" Regular headers " Regular headers
if line =~# '^#\+ ' if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
return ">" . match(line, ' ') return ">" . match(line, ' ')
endif endif
" Setext style headings " Setext style headings
let nextline = getline(v:lnum + 1) let nextline = getline(v:lnum + 1)
if (line =~ '^.\+$') && (nextline =~ '^=\+$') if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
return ">1" return ">1"
endif endif
if (line =~ '^.\+$') && (nextline =~ '^-\+$') if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
return ">2" return ">2"
endif endif
return "=" return "="
endfunction endfunction
function! s:NotCodeBlock(lnum)
return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
endfunction
function! MarkdownFoldText() function! MarkdownFoldText()
let hash_indent = s:HashIndent(v:foldstart) let hash_indent = s:HashIndent(v:foldstart)
let title = substitute(getline(v:foldstart), '^#\+\s*', '', '') let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')