|
| 1 | +" Python-mode folding functions |
| 2 | + |
| 3 | + |
| 4 | +let s:defpat = '^\s*\(@\|class\s.*:\|def\s\)' |
| 5 | + |
| 6 | + |
| 7 | +fun! pymode#folding#text() " {{{ |
| 8 | + let fs = v:foldstart |
| 9 | + while getline(fs) =~ '^\s*@' |
| 10 | + let fs = nextnonblank(fs + 1) |
| 11 | + endwhile |
| 12 | + let line = getline(fs) |
| 13 | + |
| 14 | + let nucolwidth = &fdc + &number * &numberwidth |
| 15 | + let windowwidth = winwidth(0) - nucolwidth - 3 |
| 16 | + let foldedlinecount = v:foldend - v:foldstart |
| 17 | + |
| 18 | + " expand tabs into spaces |
| 19 | + let onetab = strpart(' ', 0, &tabstop) |
| 20 | + let line = substitute(line, '\t', onetab, 'g') |
| 21 | + |
| 22 | + let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount)) |
| 23 | + let fillcharcount = windowwidth - len(line) - len(foldedlinecount) |
| 24 | + return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' ' |
| 25 | +endfunction "}}} |
| 26 | + |
| 27 | + |
| 28 | +fun! pymode#folding#indent(lnum) "{{{ |
| 29 | + let indent = indent(pymode#BlockStart(a:lnum)) |
| 30 | + return indent ? indent + &shiftwidth : 0 |
| 31 | +endfunction "}}} |
| 32 | + |
| 33 | + |
| 34 | +fun! pymode#folding#expr(lnum) "{{{ |
| 35 | + let line = getline(a:lnum) |
| 36 | + let indent = indent(a:lnum) |
| 37 | + |
| 38 | + if line == '' | return getline(a:lnum+1) == ''?'=':'-1' | endif |
| 39 | + |
| 40 | + if line =~ s:defpat && getline(prevnonblank(a:lnum-1)) !~ '^\s*@' |
| 41 | + let n = a:lnum |
| 42 | + while getline(n) =~ '^\s*@' | let n = nextnonblank(n + 1) | endwhile |
| 43 | + if getline(n) =~ s:defpat |
| 44 | + return ">".(indent/&shiftwidth+1) |
| 45 | + endif |
| 46 | + endif |
| 47 | + |
| 48 | + let p = prevnonblank(a:lnum-1) |
| 49 | + while p>0 && getline(p) =~ '^\s*#' | let p = prevnonblank(p-1) |
| 50 | + endwhile |
| 51 | + let pind = indent(p) |
| 52 | + if getline(p) =~ s:defpat && getline(prevnonblank(a:lnum - 1)) !~ '^\s*@' |
| 53 | + let pind = pind + &shiftwidth |
| 54 | + elseif p==0 | let pind = 0 |
| 55 | + endif |
| 56 | + |
| 57 | + if indent>0 && indent==pind | return '=' |
| 58 | + elseif indent>pind | return '=' |
| 59 | + elseif indent==0 |
| 60 | + if pind==0 && line =~ '^#' | return 0 |
| 61 | + elseif line !~'^#' |
| 62 | + if 0<pind && line!~'^else\s*:\|^except.*:\|^elif.*:\|^finally\s*:' | return '>1' |
| 63 | + elseif 0==pind && getline(prevnonblank(a:lnum-1)) =~ '^\s*#' | return '>1' |
| 64 | + else | return '=' |
| 65 | + endif |
| 66 | + endif |
| 67 | + let n = nextnonblank(a:lnum+1) |
| 68 | + while n>0 && getline(n) =~'^\s*#' | let n = nextnonblank(n+1) |
| 69 | + endwhile |
| 70 | + if indent(n)==0 | return 0 |
| 71 | + else | return -1 |
| 72 | + end |
| 73 | + endif |
| 74 | + let blockindent = indent(pymode#BlockStart(a:lnum)) + &shiftwidth |
| 75 | + if blockindent==0 | return 1 | endif |
| 76 | + let n = nextnonblank(a:lnum+1) |
| 77 | + while n>0 && getline(n) =~'^\s*#' | let n = nextnonblank(n+1) | endwhile |
| 78 | + let nind = indent(n) |
| 79 | + if line =~ '^\s*#' && indent>=nind | return -1 |
| 80 | + elseif line =~ '^\s*#' | return nind / &shiftwidth |
| 81 | + else | return blockindent / &shiftwidth |
| 82 | + endif |
| 83 | +endfunction "}}} |
| 84 | + |
| 85 | + |
| 86 | +" vim: fdm=marker:fdl=0 |
0 commit comments