Skip to content

Commit d113fe0

Browse files
committed
Handle folding of nested defs correctly
1 parent 7c8b25a commit d113fe0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

autoload/pymode/folding.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ fun! pymode#folding#expr(lnum) "{{{
5757
return "<".(indent / &shiftwidth + 1)
5858
endif
5959

60+
" Handle nested defs
61+
let last_def = pymode#motion#BlockStart(a:lnum, s:def_regex)
62+
if getline(last_def) =~ s:def_regex
63+
let last_def_end = pymode#motion#BlockEnd(last_def)
64+
if last_def_end < line('$')
65+
let nested = getline(pymode#motion#BlockStart(last_def - 1)) =~ s:def_regex
66+
if nested && getline(nextnonblank(a:lnum)) !~ s:def_regex
67+
let fold_end = min([prevnonblank(last_def_end - 1) + 2, last_def_end])
68+
if a:lnum == fold_end
69+
return 's1'
70+
endif
71+
endif
72+
endif
73+
endif
74+
6075
if line =~ s:blank_regex
6176
if prev_line =~ s:blank_regex
6277
if indent(a:lnum + 1) == 0 && getline(a:lnum + 1) !~ s:blank_regex

autoload/pymode/motion.vim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ endfunction "}}}
3232
fun! pymode#motion#select(pattern, inner) "{{{
3333
let cnt = v:count1 - 1
3434
let orig = getpos('.')[1:2]
35-
let snum = s:BlockStart(orig[0], a:pattern)
35+
let snum = pymode#motion#BlockStart(orig[0], a:pattern)
3636
if getline(snum) !~ a:pattern
3737
return 0
3838
endif
39-
let enum = s:BlockEnd(snum, indent(snum))
39+
let enum = pymode#motion#BlockEnd(snum, indent(snum))
4040
while cnt
4141
let lnum = search(a:pattern, 'nW')
4242
if lnum
43-
let enum = s:BlockEnd(lnum, indent(lnum))
43+
let enum = pymode#motion#BlockEnd(lnum, indent(lnum))
4444
call cursor(enum, 1)
4545
endif
4646
let cnt = cnt - 1
@@ -58,7 +58,7 @@ fun! pymode#motion#select(pattern, inner) "{{{
5858
endfunction "}}}
5959

6060

61-
fun! s:BlockStart(lnum, ...) "{{{
61+
fun! pymode#motion#BlockStart(lnum, ...) "{{{
6262
let pattern = a:0 ? a:1 : '^\s*\(@\|class\s.*:\|def\s\)'
6363
let lnum = a:lnum + 1
6464
let indent = 100
@@ -82,7 +82,7 @@ fun! s:BlockStart(lnum, ...) "{{{
8282
endfunction "}}}
8383

8484

85-
fun! s:BlockEnd(lnum, ...) "{{{
85+
fun! pymode#motion#BlockEnd(lnum, ...) "{{{
8686
let indent = a:0 ? a:1 : indent(a:lnum)
8787
let lnum = a:lnum
8888
while lnum

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy