Skip to content

Commit 48b15cb

Browse files
hellhoundvim-scripts
authored andcommitted
Version 2.8.3.3
Only in the b version or if the g:ifold_accuracy global variable is set Fix: Now the script omits any comment mark that lays between the beginning of the line and the first sentence's character, that is, it wont unfold your code even if you put comment marks without proper indentation.
1 parent 04cc7fb commit 48b15cb

2 files changed

Lines changed: 215 additions & 14 deletions

File tree

plugin/python_ifold.a.vim

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
" Vim folding file
2+
" Language: Python
3+
" Author: Jorrit Wiersma (foldexpr), Max Ischenko (foldtext), Robert,
4+
" Jean-Pierre Chauvel (bugfixes)
5+
" Ames (line counts)
6+
" Last Change: 2008 Feb 29
7+
" Version: 2.8.3.3.a
8+
" Bugfix: Jean-Pierre Chauvel
9+
10+
11+
12+
if exists("b:did_ftplugin")
13+
finish
14+
endif
15+
16+
let b:did_ftplugin = 1
17+
18+
if !exists("g:ifold_support_markers")
19+
let g:ifold_support_markers = 0
20+
endif
21+
22+
if !exists("g:ifold_show_text")
23+
let g:ifold_show_text = 0
24+
endif
25+
26+
if !exists("g:ifold_accuracy")
27+
let g:ifold_accuracy = 0
28+
endif
29+
30+
map <buffer> f :call ToggleFold()<CR>
31+
32+
function! PythonFoldText()
33+
let line = getline(v:foldstart)
34+
let nnum = nextnonblank(v:foldstart + 1)
35+
let nextline = getline(nnum)
36+
if nextline =~ '^\s\+"""$'
37+
let line = line . getline(nnum + 1)
38+
elseif nextline =~ '^\s\+"""'
39+
let line = line . ' ' . matchstr(nextline, '"""\zs.\{-}\ze\("""\)\?$')
40+
elseif nextline =~ '^\s\+"[^"]\+"$'
41+
let line = line . ' ' . matchstr(nextline, '"\zs.*\ze"')
42+
elseif nextline =~ '^\s\+pass\s*$'
43+
let line = line . ' pass'
44+
endif
45+
let size = 1 + v:foldend - v:foldstart
46+
if size < 10
47+
let size = " " . size
48+
endif
49+
if size < 100
50+
let size = " " . size
51+
endif
52+
if size < 1000
53+
let size = " " . size
54+
endif
55+
return size . " lines: " . line
56+
endfunction
57+
58+
59+
let b:nestinglevel = 0
60+
61+
function! GetPythonFold(lnum)
62+
" Determine folding level in Python source
63+
"
64+
let line = getline(a:lnum - 1)
65+
66+
" Support markers
67+
if g:ifold_support_markers
68+
if line =~ '{{{'
69+
return "a1"
70+
elseif line =~ '}}}'
71+
return "s1"
72+
endif
73+
endif
74+
75+
" Classes and functions get their own folds
76+
if line =~ '^\s*\(class\|def\)\s'
77+
" Verify if the next line is a class or function definition
78+
" as well
79+
let imm_nnum = a:lnum + 1
80+
let nnum = nextnonblank(imm_nnum)
81+
if nnum - imm_nnum < 2
82+
let nind = indent(nnum)
83+
let pind = indent(a:lnum - 1)
84+
if pind >= nind
85+
let nline = getline(nnum)
86+
let b:nestinglevel = nind
87+
return "<" . ((b:nestinglevel + &sw) / &sw)
88+
endif
89+
endif
90+
let b:nestinglevel = indent(a:lnum - 1)
91+
return ">" . ((b:nestinglevel + &sw) / &sw)
92+
endif
93+
94+
" If next line has less or equal indentation than the first one,
95+
" we end a fold.
96+
let nind = indent(nextnonblank(a:lnum + 1))
97+
if nind <= b:nestinglevel
98+
let b:nestinglevel = nind
99+
return "<" . ((b:nestinglevel + &sw) / &sw)
100+
else
101+
let ind = indent(a:lnum)
102+
if ind == (b:nestinglevel + &sw)
103+
if nind < ind
104+
let b:nestinglevel = nind
105+
return "<" . ((b:nestinglevel + &sw) / &sw)
106+
endif
107+
endif
108+
endif
109+
110+
" If none of the above apply, keep the indentation
111+
return "="
112+
endfunction
113+
114+
function! GetPythonFoldAccurately(lnum)
115+
" Determine folding level in Python source
116+
"
117+
let line = getline(a:lnum - 1)
118+
119+
" Support markers
120+
if g:ifold_support_markers
121+
if line =~ '{{{'
122+
return "a1"
123+
elseif line =~ '}}}'
124+
return "s1"
125+
endif
126+
endif
127+
128+
" Classes and functions get their own folds
129+
if line =~ '^\s*\(class\|def\)\s'
130+
" Verify if the next line is a class or function definition
131+
" as well
132+
let imm_nnum = a:lnum + 1
133+
let nnum = nextnonblank(imm_nnum)
134+
let nind = indent(nnum)
135+
let pind = indent(a:lnum - 1)
136+
if pind >= nind
137+
let nline = getline(nnum)
138+
let b:nestinglevel = nind
139+
return "<" . ((b:nestinglevel + &sw) / &sw)
140+
endif
141+
let b:nestinglevel = indent(a:lnum - 1)
142+
return ">" . ((b:nestinglevel + &sw) / &sw)
143+
endif
144+
145+
" If next line has less or equal indentation than the first one,
146+
" we end a fold.
147+
let nnonblank = nextnonblank(a:lnum + 1)
148+
if getline(nnonblank) !~ '^\s*#.*'
149+
let nind = indent(nnonblank)
150+
if nind <= b:nestinglevel
151+
let b:nestinglevel = nind
152+
return "<" . ((b:nestinglevel + &sw) / &sw)
153+
else
154+
let ind = indent(a:lnum)
155+
if ind == (b:nestinglevel + &sw)
156+
if nind < ind
157+
let b:nestinglevel = nind
158+
return "<" . ((b:nestinglevel + &sw) / &sw)
159+
endif
160+
endif
161+
endif
162+
endif
163+
164+
" If none of the above apply, keep the indentation
165+
return "="
166+
endfunction
167+
168+
let b:is_folding = 1
169+
170+
function! ToggleFold()
171+
if b:is_folding
172+
set foldexpr=0
173+
let b:is_folding = 0
174+
else
175+
call ReFold()
176+
" Open the fold we are in
177+
exec 'norm! zO'
178+
let b:is_folding = 1
179+
endif
180+
endfunction
181+
182+
" In case folding breaks down
183+
function! ReFold()
184+
set foldmethod=expr
185+
set foldexpr=0
186+
set foldmethod=expr
187+
if g:ifold_accuracy
188+
set foldexpr=GetPythonFoldAccurately(v:lnum)
189+
else
190+
set foldexpr=GetPythonFold(v:lnum)
191+
endif
192+
if g:ifold_show_text
193+
set foldtext=PythonFoldText()
194+
else
195+
set foldtext='Folded\ code'
196+
endif
197+
echo
198+
endfunction
199+
200+
call ReFold()
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
" Author: Jorrit Wiersma (foldexpr), Max Ischenko (foldtext), Robert,
44
" Jean-Pierre Chauvel (bugfixes)
55
" Ames (line counts)
6-
" Last Change: 2007 Nov 25
7-
" Version: 2.8.3.2.b
6+
" Last Change: 2008 Feb 29
7+
" Version: 2.8.3.3.b
88
" Bugfix: Jean-Pierre Chauvel
99

1010

@@ -72,7 +72,6 @@ function! GetPythonFold(lnum)
7272
endif
7373
endif
7474

75-
7675
" Classes and functions get their own folds
7776
if line =~ '^\s*\(class\|def\)\s'
7877
" Verify if the next line is a class or function definition
@@ -126,7 +125,6 @@ function! GetPythonFoldAccurately(lnum)
126125
endif
127126
endif
128127

129-
130128
" Classes and functions get their own folds
131129
if line =~ '^\s*\(class\|def\)\s'
132130
" Verify if the next line is a class or function definition
@@ -146,16 +144,19 @@ function! GetPythonFoldAccurately(lnum)
146144

147145
" If next line has less or equal indentation than the first one,
148146
" we end a fold.
149-
let nind = indent(nextnonblank(a:lnum + 1))
150-
if nind <= b:nestinglevel
151-
let b:nestinglevel = nind
152-
return "<" . ((b:nestinglevel + &sw) / &sw)
153-
else
154-
let ind = indent(a:lnum)
155-
if ind == (b:nestinglevel + &sw)
156-
if nind < ind
157-
let b:nestinglevel = nind
158-
return "<" . ((b:nestinglevel + &sw) / &sw)
147+
let nnonblank = nextnonblank(a:lnum + 1)
148+
if getline(nnonblank) !~ '^\s*#.*'
149+
let nind = indent(nnonblank)
150+
if nind <= b:nestinglevel
151+
let b:nestinglevel = nind
152+
return "<" . ((b:nestinglevel + &sw) / &sw)
153+
else
154+
let ind = indent(a:lnum)
155+
if ind == (b:nestinglevel + &sw)
156+
if nind < ind
157+
let b:nestinglevel = nind
158+
return "<" . ((b:nestinglevel + &sw) / &sw)
159+
endif
159160
endif
160161
endif
161162
endif

0 commit comments

Comments
 (0)