df2e92b1e5f0

vim: custom foldtext for python files
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 08 Jun 2010 02:03:01 -0400
parents e261bb2a24e2
children 5a0b9ffa6b91
branches/tags (none)
files vim/ftplugin/python.vim

Changes

--- a/vim/ftplugin/python.vim	Tue Jun 08 00:47:33 2010 -0400
+++ b/vim/ftplugin/python.vim	Tue Jun 08 02:03:01 2010 -0400
@@ -49,32 +49,23 @@
 setlocal foldtext=PythonFoldText()
 
 function! PythonFoldText()
-  let fs = v:foldstart
-  while getline(fs) =~ '^\s*@' | let fs = nextnonblank(fs + 1)
-  endwhile
-  let line = getline(fs)
-  let nnum = nextnonblank(fs + 1)
-  let nextline = getline(nnum)
-  "get the document string: next line is ''' or """
-  if nextline =~ "^\\s\\+[\"']\\{3}\\s*$"
-      let line = line . " " . matchstr(getline(nextnonblank(nnum + 1)), '^\s*\zs.*\ze$')
-  "next line starts with qoutes, and has text
-  elseif nextline =~ "^\\s\\+[\"']\\{1,3}"
-      let line = line." ".matchstr(nextline, "^\\s\\+[\"']\\{1,3}\\zs.\\{-}\\ze['\"]\\{0,3}$")
-  elseif nextline =~ '^\s\+pass\s*$'
-    let line = line . ' pass'
-  endif
-  "compute the width of the visible part of the window (see Note above)
-  let w = winwidth(0) - &foldcolumn - (&number ? 8 : 0)
-  let size = 1 + v:foldend - v:foldstart
-  "compute expansion string
-  let spcs = '................'
-  while strlen(spcs) < w | let spcs = spcs . spcs
-  endwhile
-  "expand tabs (mail me if you have tabstop>10)
-  let onetab = strpart('          ', 0, &tabstop)
-  let line = substitute(line, '\t', onetab, 'g')
-  return strpart(line.spcs, 0, w-strlen(size)-7).'.'.size.' lines'
+    " ignore decorators
+    let fs = v:foldstart
+    while getline(fs) =~ '^\s*@' | let fs = nextnonblank(fs + 1)
+    endwhile
+    let line = getline(fs)
+
+    let nucolwidth = &fdc + &number * &numberwidth
+    let windowwidth = winwidth(0) - nucolwidth - 3
+    let foldedlinecount = v:foldend - v:foldstart
+
+    " expand tabs into spaces
+    let onetab = strpart('          ', 0, &tabstop)
+    let line = substitute(line, '\t', onetab, 'g')
+
+    let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
+    let fillcharcount = windowwidth - len(line) - len(foldedlinecount) - 1
+    return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' '
 endfunction
 
 function! GetBlockIndent(lnum)