@@ -68,7 +68,7 @@ fun! pymode#folding#expr(lnum) "{{{
6868 " Check if last class/def is not indented and therefore can't be
6969 " nested.
7070 if last_block_indent
71- " Note: This relies on the cursor position being set by s:BlockStart
71+ call cursor ( a: lnum , 0 )
7272 let next_def = searchpos (s: def_regex , ' nW' )[0 ]
7373 let next_def_indent = next_def ? indent (next_def) : -1
7474 let last_block_end = s: BlockEnd (last_block)
@@ -125,7 +125,26 @@ endfunction "}}}
125125fun ! s: BlockStart (lnum) " {{{
126126 " Note: Make sure to reset cursor position after using this function.
127127 call cursor (a: lnum , 0 )
128- let max_indent = max ([indent (prevnonblank (a: lnum )) - &shiftwidth , 0 ])
128+
129+ " In case the end of the block is indented to a higher level than the def
130+ " statement plus one shiftwidth, we need to find the indent level at the
131+ " bottom of that if/for/try/while/etc. block.
132+ let last_def = searchpos (s: def_regex , ' bcnW' )[0 ]
133+ if last_def
134+ let last_def_indent = indent (last_def)
135+ call cursor (last_def, 0 )
136+ let next_stmt_at_def_indent = searchpos (' \v^\s{' .last_def_indent.' }[^[:space:]#]' , ' nW' )[0 ]
137+ else
138+ let next_stmt_at_def_indent = -1
139+ endif
140+
141+ " Now find the class/def one shiftwidth lower than the start of the
142+ " aforementioned indent block.
143+ if next_stmt_at_def_indent && next_stmt_at_def_indent < a: lnum
144+ let max_indent = max ([indent (next_stmt_at_def_indent) - &shiftwidth , 0 ])
145+ else
146+ let max_indent = max ([indent (prevnonblank (a: lnum )) - &shiftwidth , 0 ])
147+ endif
129148 return searchpos (' \v^\s{,' .max_indent.' }(def |class )\w' , ' bcnW' )[0 ]
130149endfunction " }}}
131150
0 commit comments