1- " Fold routines for python code, version 2.1
2- " Last Change: 2009 Feb 2, when I had better go to bed
1+ " Fold routines for python code, version 2.2
2+ " Last Change: 2009 Feb 3
33" Author: Jurjen Bos (foldexpr, and most of foldtext), Max Ischenko (foldtext)
44" Bug fixes: Grissiom, David Froger
55
@@ -28,9 +28,11 @@ function! PythonFoldText()
2828 let line = getline (v: foldstart )
2929 let nnum = nextnonblank (v: foldstart + 1 )
3030 let nextline = getline (nnum)
31- " get the document string. This is ugly, but I can't fix it
32- if nextline = ~ " ^\\ s\\ +[\" ']\\ {1,3}"
33- let line = matchstr (nextline, " ^\\ s\\ +[\" ']\\ {1,3}\\ zs.\\ {-}\\ ze['\" ]\\ {0,3}$" )
31+ " get the document string.
32+ if nextline = ~ " ^\\ s\\ +[\" ']\\ {3}\\ s*$"
33+ let line = line . " " . matchstr (getline (nextnonblank (nnum + 1 )), ' ^\s*\zs.*\ze$' )
34+ elseif nextline = ~ " ^\\ s\\ +[\" ']\\ {1,3}"
35+ let line = line ." " .matchstr (nextline, " ^\\ s\\ +[\" ']\\ {1,3}\\ zs.\\ {-}\\ ze['\" ]\\ {0,3}$" )
3436 elseif nextline = ~ ' ^\s\+pass\s*$'
3537 let line = line . ' pass'
3638 endif
@@ -52,13 +54,13 @@ function! GetBlockIndent(lnum)
5254 " Auxiliary function; determines the indent level of the def/class
5355 " "global" lines are level 0, first def &sw, and so on
5456 " scan backwards for class/def that is shallower or equal
55- let p = a: lnum
57+ let p = nextnonblank ( a: lnum)
5658 " skip comments and empty lines, since we don't trust their indent
57- while indent ( p ) >= 0 && getline (p ) = ~ ' ^\s*#\|^$'
58- let p = p + 1
59+ while p > 0 && getline (p ) = ~ ' ^\s*#\|^$'
60+ let p = nextnonblank ( p + 1 )
5961 endwhile
6062 let ind = indent (p )
61- while indent (p )>= 0
63+ while indent (p ) >= 0
6264 let p = p - 1
6365 " skip comments and empty lines
6466 if getline (p ) = ~ ' ^$\|^\s*#'
@@ -69,7 +71,7 @@ function! GetBlockIndent(lnum)
6971 " indent is strictly less at this point: check for def/class
7072 elseif getline (p ) = ~ ' ^\s*\(def\|class\)\s'
7173 " this is the level!
72- return indent (p )+ &sw
74+ return indent (p ) + &sw
7375 " zero-level regular line
7476 elseif indent (p ) == 0
7577 return 0
@@ -100,13 +102,13 @@ function! GetPythonFold(lnum)
100102 return 1
101103 " regular line: deep line or non-comment line
102104 elseif ind>= blockindent || line !~ ' ^\s*#'
103- return blockindent/ &sw
105+ return blockindent / &sw
104106 endif
105107 " shallow comment: level is determined by next line
106108 " search for next non-comment nonblank line
107- let n = a: lnum + 1
108- while indent ( n ) >= 0 && getline (n ) = ~ ' ^\s*#\|^$'
109- let n = n + 1
109+ let n = nextnonblank ( a: lnum + 1 )
110+ while n > 0 && getline (n ) = ~ ' ^\s*#\|^$'
111+ let n = nextnonblank ( n + 1 )
110112 endwhile
111- return GetBlockIndent (n )/ &sw
113+ return GetBlockIndent (n ) / &sw
112114endfunction
0 commit comments