Skip to content

Commit 20c8a53

Browse files
committed
Add folding.
1 parent 357ec1c commit 20c8a53

File tree

3 files changed

+103
-7
lines changed

3 files changed

+103
-7
lines changed

autoload/pymode/folding.vim

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

ftplugin/python/pymode.vim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ if !pymode#Default('g:pymode_options_indent', 1) || g:pymode_options_indent
2424
setlocal autoindent
2525
endif
2626

27-
" Python fold options
28-
if !pymode#Default('g:pymode_options_fold', 1) || g:pymode_options_fold
29-
setlocal foldlevelstart=99
30-
setlocal foldlevel=99
31-
setlocal foldmethod=indent
32-
endif
33-
3427
" Python other options
3528
if !pymode#Default('g:pymode_options_other', 1) || g:pymode_options_other
3629
setlocal complete+=t
@@ -154,4 +147,18 @@ endif
154147

155148
" }}}
156149

150+
151+
" Folding {{{
152+
153+
if g:pymode_folding
154+
155+
setlocal foldmethod=expr
156+
setlocal foldexpr=pymode#folding#expr(v:lnum)
157+
setlocal foldtext=pymode#folding#text()
158+
159+
endif
160+
161+
" }}}
162+
163+
157164
" vim: fdm=marker:fdl=0

plugin/pymode.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ endif
258258
" }}}
259259

260260

261+
" OPTION: g:pymode_folding -- bool. Enable python-mode folding for pyfiles.
262+
call pymode#Default("g:pymode_folding", 1)
263+
261264
" OPTION: g:pymode_utils_whitespaces -- bool. Remove unused whitespaces on save
262265
call pymode#Default("g:pymode_utils_whitespaces", 1)
263266

0 commit comments

Comments
 (0)