@@ -216,6 +216,15 @@ endfunction "}}}
216216
217217function ! s: Is_opening_folding (lnum) " {{{
218218 " Helper function to see if docstring is opening or closing
219+
220+ " Cache the result so the loop runs only once per change
221+ if get (b: , ' fold_changenr' , -1 ) == changenr ()
222+ return b: fold_cache [a: lnum ] " If odd then it is an opening
223+ else
224+ let b: fold_changenr = changenr ()
225+ let b: fold_cache = []
226+ endif
227+
219228 let number_of_folding = 0 " To be analized if odd/even to inform if it is opening or closing.
220229 let has_open_docstring = 0 " To inform is already has an open docstring.
221230 let extra_docstrings = 0 " To help skipping ''' and " " " which are not docstrings
@@ -224,7 +233,9 @@ function! s:Is_opening_folding(lnum) "{{{
224233 " not just triple quotes (that could be a regular string).
225234 "
226235 " Iterater over all lines from the start until current line (inclusive)
227- for i in range (1 , a: lnum )
236+ for i in range (1 , line (' $' ))
237+ call add (b: fold_cache , number_of_folding % 2 )
238+
228239 let i_line = getline (i )
229240
230241 if i_line = ~ s: doc_line_regex
@@ -255,11 +266,9 @@ function! s:Is_opening_folding(lnum) "{{{
255266 endif
256267 endfor
257268
258- if fmod (number_of_folding, 2 ) == 1 " If odd then it is an opening
259- return 1
260- else
261- return 0
262- endif
269+ call add (b: fold_cache , number_of_folding % 2 )
270+
271+ return b: fold_cache [a: lnum ]
263272endfunction " }}}
264273
265274" vim: fdm = marker:fdl = 0
0 commit comments