Commit 191a941b authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Make parse state variables buffer-scoped

parent dbef0037
Loading
Loading
Loading
Loading
+19 −20
Original line number Diff line number Diff line
let s:root = 0
let s:expect = 0
let s:max_depth = 0

" Python folding plugin

function! folds#python#Enable()
	let b:root = 0
	let b:expect = 0
	let b:max_depth = 0
	setl foldexpr=folds#python#FoldExpr()
	setl foldtext=folds#python#FoldText()
	setl foldmethod=expr
	au InsertLeave <buffer> :if &l:foldenable | normal zxzv
	au BufWinEnter <buffer> :if &l:foldenable | normal zxzRzm
endfunction

function! folds#python#FoldExpr()
	if v:lnum != s:expect | call s:parsebuffer() | endif
	let s:expect = v:lnum + 1
	let fold = s:root.get_at(v:lnum)
	if v:lnum != b:expect | call s:parsebuffer() | endif
	let b:expect = v:lnum + 1
	let fold = b:root.get_at(v:lnum)
	if fold.type == 'doc'
		" multi-line doc-strings go on their own below the deepest level
		return (fold.start != fold.end) ? s:max_depth + 1 : fold.parent.depth
		return (fold.start != fold.end) ? b:max_depth + 1 : fold.parent.depth
	else
		return fold.depth
	endif
endfunction

function! folds#python#FoldText()
	if s:root is 0 | call s:parsebuffer() | endif
	let cur = s:root.get_at(v:foldstart)
	if b:root == 0 | call s:parsebuffer() | endif
	let cur = b:root.get_at(v:foldstart)
	let indent = indent(cur.foldstart)
	let padlen = (&l:textwidth ? (&l:textwidth) : 80)
	let padlen -= (len(cur.msg) + indent)
@@ -61,7 +59,7 @@ function! s:newFold(type, indent, ...)

		let a:parent = get(a:, 1, 0)
		if a:parent is 0
			let a:parent = s:root.get_at(a:start)
			let a:parent = b:root.get_at(a:start)
		endif

		let self.start = a:start
@@ -207,17 +205,17 @@ function! s:newFold(type, indent, ...)
endfunction

function! s:parsebuffer()
	let s:root = s:newFold('file', 0)
	let s:root.has_docstring = 0
	let cur_fold = s:root
	let s:max_depth = 0
	let b:root = s:newFold('file', 0)
	let b:root.has_docstring = 0
	let cur_fold = b:root
	let b:max_depth = 0
	let last_lnum = 0
	for lnum in range(1, line('$'))
		let line = getline(lnum)
		if empty(line) | continue | endif
		let cur_fold = s:parseline(lnum, line, cur_fold, last_lnum)
		let last_lnum = lnum
		let s:max_depth = max([s:max_depth, cur_fold.depth])
		let b:max_depth = max([b:max_depth, cur_fold.depth])
	endfor
endfunction

@@ -322,9 +320,10 @@ function! folds#python#Debug()
	setl foldexpr=folds#python#FoldExpr()
	setl foldenable foldmethod=expr

	exec "buf " . debug_buf
	for lnum in range(1, target_len)
		let cur = s:root.get_at(lnum)
		exec "buf " . target_buf
		let cur = b:root.get_at(lnum)
		exec "buf " . debug_buf
		call append(lnum - 1, "[" . cur.type . "/" . cur.start . "/" . cur.foldstart . "/" . cur.end . "] " . cur.depth . " " . cur.msg)
	endfor
endfunction