folding.vim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. " Python-mode folding functions
  2. let s:blank_regex = '^\s*$'
  3. let s:def_regex = '^\s*\(class\|def\) \w\+'
  4. fun! pymode#folding#text() " {{{
  5. let fs = v:foldstart
  6. while getline(fs) =~ '^\s*@'
  7. let fs = nextnonblank(fs + 1)
  8. endwhile
  9. let line = getline(fs)
  10. let nucolwidth = &fdc + &number * &numberwidth
  11. let windowwidth = winwidth(0) - nucolwidth - 3
  12. let foldedlinecount = v:foldend - v:foldstart
  13. " expand tabs into spaces
  14. let onetab = strpart(' ', 0, &tabstop)
  15. let line = substitute(line, '\t', onetab, 'g')
  16. let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
  17. let fillcharcount = windowwidth - len(line) - len(foldedlinecount)
  18. return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' '
  19. endfunction "}}}
  20. fun! pymode#folding#expr(lnum) "{{{
  21. let line = getline(a:lnum)
  22. let indent = indent(a:lnum)
  23. if line =~ s:def_regex
  24. return ">".(indent / &shiftwidth + 1)
  25. endif
  26. if line =~ '^\s*@'
  27. return -1
  28. endif
  29. if line =~ s:blank_regex
  30. let prev_line = getline(a:lnum - 1)
  31. if prev_line =~ s:blank_regex
  32. return -1
  33. else
  34. return foldlevel(prevnonblank(a:lnum))
  35. endif
  36. endif
  37. if indent == 0
  38. return 0
  39. endif
  40. return '='
  41. endfunction "}}}
  42. " vim: fdm=marker:fdl=0