forked from vim-scripts/python_ifold
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_ifold.vim
More file actions
250 lines (224 loc) · 6.7 KB
/
python_ifold.vim
File metadata and controls
250 lines (224 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
" Vim folding file
" Language: Python
" Author: Jorrit Wiersma (foldexpr), Max Ischenko (foldtext), Robert,
" Ames (line counts), Jean-Pierre Chauvel (bugfixes and improvements)
" Last Change: 2008 Apr 20
" Version: 2.9.c
" Bugfixes: Jean-Pierre Chauvel
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin = 1
if !exists("g:ifold_mode")
let g:ifold_mode = 1
endif
map <buffer> <leader>f :call ToggleFold()<CR>
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
set foldcolumn=1
let w:nestinglevel = 0
let w:signature = 0
let w:is_folded = 1
function! PythonFoldText()
let line = getline(v:foldstart)
let nnum = nextnonblank(v:foldstart + 1)
let nextline = getline(nnum)
if nextline =~ '^\s\+"""$'
let line = line . getline(nnum + 1)
elseif nextline =~ '^\s\+"""'
let line = line . ' ' . matchstr(nextline, '"""\zs.\{-}\ze\("""\)\?$')
elseif nextline =~ '^\s\+"[^"]\+"$'
let line = line . ' ' . matchstr(nextline, '"\zs.*\ze"')
elseif nextline =~ '^\s\+pass\s*$'
let line = line . ' pass'
endif
let size = 1 + v:foldend - v:foldstart
if size < 10
let size = " " . size
endif
if size < 100
let size = " " . size
endif
if size < 1000
let size = " " . size
endif
return size . " lines: " . line
endfunction
function! GetPythonFold(lnum)
let line = getline(a:lnum - 1)
" Classes and functions get their own folds
if line =~ '^\s*\(class\|def\)\s'
" Verify if the next line is a class or function definition
" as well
let imm_nnum = a:lnum + 1
let nnum = nextnonblank(imm_nnum)
if nnum - imm_nnum < 2
let nind = indent(nnum)
let pind = indent(a:lnum - 1)
if pind >= nind
let nline = getline(nnum)
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
endif
let w:nestinglevel = indent(a:lnum - 1)
return ">" . ((w:nestinglevel + &sw) / &sw)
endif
" If next line has less or equal indentation than the first one,
" we end a fold.
let nind = indent(nextnonblank(a:lnum + 1))
if nind <= w:nestinglevel
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
else
let ind = indent(a:lnum)
if ind == (w:nestinglevel + &sw)
if nind < ind
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
endif
endif
" If none of the above apply, keep the indentation
return "="
endfunction
function! GetPythonFoldBest(lnum)
" Determine folding level in Python source
"
let line = getline(a:lnum - 1)
" Handle Support markers
if line =~ '{{{'
return "a1"
elseif line =~ '}}}'
return "s1"
endif
" Classes and functions get their own folds
if line =~ '^\s*\(class\|def\)\s'
" Verify if the next line is a class or function definition
" as well
let imm_nnum = a:lnum + 1
let nnum = nextnonblank(imm_nnum)
let nind = indent(nnum)
let pind = indent(a:lnum - 1)
if pind >= nind
let nline = getline(nnum)
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
let w:nestinglevel = indent(a:lnum - 1)
return ">" . ((w:nestinglevel + &sw) / &sw)
endif
" If next line has less or equal indentation than the first one,
" we end a fold.
let nnonblank = nextnonblank(a:lnum + 1)
let nextline = getline(nnonblank)
if (nextline !~ '^#\+.*')
let nind = indent(nnonblank)
if nind <= w:nestinglevel
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
else
let ind = indent(a:lnum)
if ind == (w:nestinglevel + &sw)
if nind < ind
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
endif
endif
endif
" If none of the above apply, keep the indentation
return "="
endfunction
function! GetPythonFoldExperimental(lnum)
" Determine folding level in Python source
"
let line = getline(a:lnum - 1)
" Handle suport markers
if line =~ '{{{'
return "a1"
elseif line =~ '}}}'
return "s1"
endif
" Classes and functions get their own folds
if line =~ '^\s*\(class\|def\)\s'
" Verify if the next line is a class or function definition
" as well
let imm_nnum = a:lnum + 1
let nnum = nextnonblank(imm_nnum)
let nind = indent(nnum)
let pind = indent(a:lnum - 1)
if pind >= nind
let nline = getline(nnum)
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
let w:signature = 1
let w:nestinglevel = indent(a:lnum - 1)
endif
if line =~ '^.*:' && w:signature
let w:signature = 0
return ">" . ((w:nestinglevel + &sw) / &sw)
endif
" If next line has less or equal indentation than the first one,
" we end a fold.
let nnonblank = nextnonblank(a:lnum + 1)
let nextline = getline(nnonblank)
if (nextline !~ '^#\+.*')
let nind = indent(nnonblank)
if nind <= w:nestinglevel
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
else
let ind = indent(a:lnum)
if ind == (w:nestinglevel + &sw)
if nind < ind
let w:nestinglevel = nind
return "<" . ((w:nestinglevel + &sw) / &sw)
endif
endif
endif
endif
" If none of the above apply, keep the indentation
return "="
endfunction
function! ToggleFold()
let w:nestinglevel = 0
let w:signature = 0
if w:is_folded
set foldexpr=0
let w:is_folded = 0
else
call ReFold()
" Open the fold we are in
exec 'norm! zO'
let w:is_folded = 1
endif
endfunction
" In case folding breaks down
function! ReFold()
set foldmethod=expr
set foldexpr=0
set foldmethod=expr
if g:ifold_mode == 0
set foldexpr=GetPythonFold(v:lnum)
else
if g:ifold_mode == 1
set foldexpr=GetPythonFoldBest(v:lnum)
else
if g:ifold_mode == 2
set foldexpr=GetPythonFoldExperimental(v:lnum)
endif
endif
endif
if g:ifold_mode
set foldtext=PythonFoldText()
else
set foldtext='Folded\ code'
endif
echo
endfunction
call ReFold()