sponsor Vim development Vim logo Vim Book Ad

jpythonfold.vim : A better python fold script

 script karma  Rating 313/96, Downloaded by 3404    Comments, bugs, improvements  Vim wiki

created by
Jurjen Bos
 
script type
syntax
 
description
This script allows automatically folding of Python code.
It completely rewritten from Jorrit Wiersma's script (vimscript #515; other competitors are vimscript#781 and vimscript#2002).
The folding and fold text are completely rewritten: the algorithm systematically set up, so it behaves as accurate as possible (at the cost of some speed).
Main feature of this script is that it folds empty space, saving lots of screen space (although you can turn it off, see below).
Classes and defs are folded, of course.
Indent zero comments separating classes and defs are not folded, so you see the global divisions in your code.
Global code is folded in blocks (separated by comments).

There are three options. To use them, check the script:
- Keep empty lines out of a fold (giving more readable classes with folded functions, but using more screen space).
- Ignore non-python files.
- Allow for multiline class definitions (at the cost of folding comments starting with the word "class") or stricter def checking (missing multiline defs).

Known bug:
- multiline strings that look like code are handled as such (basically unfixable, actually)

Example:
------------------------
#unfolded comment
def foo:
#folded comment
    #also folded
    bar = 5

#unfolded, but the empty line above this one is folded with the function
#code below will be folded
a = 5
b = 6
 
install details
Either save it in a file and source that file whenever you want to use the folding, or save it in your ftplugin directory (for example, ~/.vim/ftplugin/python.vim) and it should load every time you edit python code.
If you want it to autodetect python code and ignore other code (which is probably what you want if you installed it in the ftplugin directory), uncomment the three lines denoted as such.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
jpythonfold.vim 3.2 2009-02-25 6.0 Jurjen Bos Improved decorator recognition: now works with doxygen. (Thanks, Grissiom)
Bug removed. (Thanks, Andrew)
jpythonfold.vim 3.1 2009-02-17 6.0 Jurjen Bos Now also folds decorated functions and classes properly. Thanks for the idea, Kai.
jpythonfold.vim 3.0.3 2009-02-15 6.0 Jurjen Bos Anothter bugfix: except/else in global code doesn't introduce extra folds anymore. Thanks, Grissiom
jpythonfold.vim 3.0.2 2009-02-14 6.0 Jurjen Bos Another bugfix: debug code removed. Thanks, Andrew
jpythonfold.vim 3.0.1 2009-02-09 6.0 Jurjen Bos Refactored everything again. Now not only faster, but also systematically set up.
I actually try to explain everything in the script, in case you are interested :-)
Minor bug fix over 3.0; thanks, Grissiom
jpythonfold.vim 2.5 2009-02-07 6.0 Jurjen Bos Found a tiny bug: a function whose first line is a comment got folded wrong. Fixed it by removing (!) some code.
jpythonfold.vim 2.4 2009-02-06 6.0 Jurjen Bos Now expects a colon in a class definition, but not in a function definition, allowing for multiline defs (with explanation if you happen to disagree).
Stamped out a bug where sometimes global code gets folded in a class.
Speeded up a bit.
jpythonfold.vim 2.3 2009-02-04 6.0 Jurjen Bos Thanks to many helpful comment a brand new version, a bit less slow than 2.2.
Folding accuracy is improved thanks to my quality control system (i.e. I made a test Python file handling all cases :-)
jpythonfold.vim 2.2 2009-02-03 6.0 Jurjen Bos Two silly bugs removed in foldtext, making version 2.1 unbearable.
Improved script layout a bit.
jpythonfold.vim 2.1 2009-02-03 6.0 Jurjen Bos (2.0 was too buggy)
Major rewrite; behaves more consistently in many borderline cases. Made more readable using an extra function.
Also handles docstrings with single quotes.
jpythonfold.vim 1.4 2009-02-02 6.0 Jurjen Bos Bugfix release: handles files with tabs, works even when foldcolumn or number is set.
jpythonfold.vim 1.3 2009-02-01 6.0 Jurjen Bos Minor improvement giving dots between text and line count.
jpythonfold.vim 1.1 2009-02-01 6.0 Jurjen Bos Thanks to a few useful reactions, here is a new version.
- It doesn't crash anymore if the script ends in empty lines or comments
- There is (commented out) code for detecting and ignoring non-python scripts

ip used for rating: 54.242.200.172

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo