Skip to content

Commit

Permalink
Setup the foldtext function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nelstrom committed Oct 25, 2012
1 parent dfb0399 commit 617d88c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
8 changes: 6 additions & 2 deletions after/ftplugin/markdown/folding.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ function! HeadingDepth(lnum)
endfunction

function! FoldText()
return 'placeholder'
let level = HeadingDepth(v:foldstart)
let indent = repeat('#', level)
let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
let foldsize = (v:foldend - v:foldstart)
return indent.' '.title.' ['.foldsize.' lines]'
endfunction

" Setup {{{1
setlocal foldmethod=expr
setlocal foldexpr=StackedMarkdownFolds()
let &l:foldtext = s:SID() . 'FoldText()'
setlocal foldtext=FoldText()
command! -buffer FoldToggle
" Teardown {{{1
let b:undo_ftplugin .= '
Expand Down
44 changes: 41 additions & 3 deletions test/folding.vim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe 'setting filetype=markdown'
end

it 'applies foldtext=FoldText()'
TODO
Expect &l:foldtext =~# '<SNR>\d_FoldText()'
end

Expand Down Expand Up @@ -246,7 +247,7 @@ end
describe 'FoldText'

before
silent tabnew test/samples/lorem.md
silent tabnew
setlocal filetype=markdown
end

Expand All @@ -257,11 +258,48 @@ describe 'FoldText'
it 'uses "# level one" headings as is'
call PopulateBuffer([
\ '# Level one heading',
\ 'Lorem ipsum dolor sit amet...',
\ ])
Expect foldtextresult('1') ==# '# Level one heading [1 lines]'
end

it 'uses "## level two" headings as is'
call PopulateBuffer([
\ '## Level two heading',
\ '',
\ 'Lorem ipsum dolor sit amet...',
\ ])
TODO
Expect foldtextresult('1') ==# '# Level one heading [2 lines]'
Expect foldtextresult('1') ==# '## Level two heading [2 lines]'
end

it 'uses "## level three" headings as is'
call PopulateBuffer([
\ '### Level three heading',
\ '',
\ 'Lorem ipsum dolor sit amet,',
\ 'consectetur adipiscing elit.',
\ ])
Expect foldtextresult('1') ==# '### Level three heading [3 lines]'
end

it 'reformats "===" headings to look like "# Level one"'
call PopulateBuffer([
\ 'Level one heading',
\ '=================',
\ '',
\ 'Lorem ipsum dolor sit amet...',
\ ])
Expect foldtextresult('1') ==# '# Level one heading [3 lines]'
end

it 'reformats "---" headings to look like "## Level two"'
call PopulateBuffer([
\ 'Level two heading',
\ '-----------------',
\ '',
\ 'Lorem ipsum dolor sit amet...',
\ ])
Expect foldtextresult('1') ==# '## Level two heading [3 lines]'
end

end

0 comments on commit 617d88c

Please sign in to comment.