better python fold function

This commit is contained in:
Felix Van der Jeugt 2016-04-04 16:43:15 +02:00
parent accb2ea687
commit 74f2632b0b
2 changed files with 37 additions and 13 deletions

View File

@ -13,25 +13,49 @@ endfunction
nnoremap <buffer> <Leader>x :call Pylint()<CR> nnoremap <buffer> <Leader>x :call Pylint()<CR>
function! IndentLevel(lnum)
return indent(a:lnum) / &shiftwidth
endfunction
function! IsBlankLine(lnum)
return getline(a:lnum) =~? '\v^\s*$'
endfunction
function! IsDefLine(lnum)
return getline(a:lnum) =~ '^\s*def\s' || getline(a:lnum) =~ '^\s*class\s'
endfunction
" Folding " Folding
function! PythonFold(lnum) function! PythonFold(lnum)
if getline(a:lnum-1) =~ '^\s*def\s' || getline(a:lnum-1) =~ '^\s*class\s' if IsBlankLine(a:lnum)
return indent(a:lnum-1) / 4 + 1 return '-1'
endif endif
if getline(a:lnum) =~ '^\s*def\s' || getline(a:lnum) =~ '^\s*class\s'
return indent(a:lnum) / 4 let this_indent = IndentLevel(a:lnum)
if IsDefLine(a:lnum)
return '>' . (this_indent + 1)
endif endif
if getline(a:lnum+1) =~ '^\s*def\s' || getline(a:lnum+1) =~ '^\s*class\s'
return indent(a:lnum + 1) / 4 let minimum = this_indent
endif let current = a:lnum
if getline(a:lnum+1) =~ '^\S.*$' while current > 1 && minimum > 0
return '0' let current -= 1
endif if IsBlankLine(current)
return '=' continue
endif
let current_indent = IndentLevel(current)
if current_indent < minimum
if IsDefLine(current)
return current_indent + 1
endif
let minimum = current_indent
endif
endwhile
return minimum
endfunction endfunction
function! PythonFoldText() function! PythonFoldText()
return repeat(' ', indent(v:foldstart - 1) + 4) . '+' return getline(v:foldstart) . ' (' . (v:foldend - v:foldstart) . ' lines) '
endfunction endfunction
setlocal foldmethod=expr setlocal foldmethod=expr

View File

@ -40,7 +40,7 @@ nnoremap <Left> <NOP>
nnoremap <Right> <NOP> nnoremap <Right> <NOP>
" Not too much folding " Not too much folding
set foldlevelstart=2 "set foldlevelstart=2
" Hmmmm neovim terminals " Hmmmm neovim terminals
nnoremap <Leader>s :vsplit +:terminal<CR> nnoremap <Leader>s :vsplit +:terminal<CR>