sponsor Vim development Vim logo Vim Book Ad

python_ifold : Improved version of python_fold. Uses folding expression for python code.

 script karma  Rating 370/303, Downloaded by 7373  Comments, bugs, improvements  Vim wiki

created by
Jean-Pierre Chauvel
 
script type
utility
 
description
Folds are created for:

- 'class' and 'def' definitions.
- Blocks between {{{, }}} markers (but not yet {{{n, etc.)


Flavors:

- A (python_ifold.a.vim) - ideal for legacy systems.
- B (python_ifold.b.vim) -  with all options set by default.
- C (python_ifold.c.vim) -  same as flavor B, but uses extra Vim settings for user comfort.


Improvements over python_fold:

- Fixes the wild (i.e. bogus) folding bug
- Shows class and function signatures
- Now, as in the original, flavor B will omit any misplaced comment mark
- Flavor A boosts performance on outdated systems, something of great importance when there are lots of nested folds over large code pages


Caveats:

- The fold level for each fold is equal to the indentation divided by the value of the 'shiftwidth' option, so make sure that 'shiftwidth' is set to the right value for your code. For best results set 'shiftwidth' , 'softtabstop'  and 'tabstop' to the same value, and use 'expandtab' and 'autoindent' (all included in flavor C).

Example:
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
set foldcolumn=1  " Optional

- Class and function folds show a counter of number of lines (and the first line below the signature) that are within the fold when it's closed. Read the change history to learn more about this.

- Press F key whenever you want to disable temporarily the folding, press F again to re-enable it (won't fold the block of code where the cursor is, though).

- If g:ifold_mode is set to a value greater than 0, will make extra parsing to your code to avoid including blank lines at the end of each fold and wont unfold your code if any comment marks are misplaced. This behavior is set by default in flavor B. But be warned , though. This option requires more power from your system (hence the two versions: 'A' for lower-end systems and 'B' for powerful-enough systems), so if you lack of requirements you shouldn't use it.

- The docstring is not shown at all.

- If you choose flavor A or if you set the global g:ifold_mode to 0, bare in mind that any comment mark between the beginning of a line and the first expression's character (of a sentence) WILL interfere with the whole folding process, that is, only if these marks do not respect the indentation sequence.
 
install details
Download the archive and extract the flavor that suits your needs. Then, either source that file whenever you want to use the folding, or save it in your ftplugin directory (for example, ~/.vim/ftplugin) and it should load every time you edit python code.
 

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
python_ifold-2.9.zip 2.9 2008-04-20 6.0 Jean-Pierre Chauvel - Code refactoring and small fixes.
- There's a new experimental engine for testing purposes. I did a regression from version 2.8.4 to 2.8.3.6 to the code, moving the enhancement you usually found in 2.8.4 to the experimental engine. It'll stay in this state until I fix some minor bugs. If you wish to continue using the "signature" enhancement, you need to put this in your vimrc:
let g:ifold_mode=2
- The globals g:ifold_support_markers, g:ifold_show_text, and g:ifold_accuracy were deleted and their respective functionality was replaced with the global g:ifold_mode when its value is 1 ore greater.
python_ifold-2.8.4.zip 2.8.4 2008-04-18 6.0 Jean-Pierre Chauvel Now it will show the entire signatures of class and function definitions, even if spanned for several lines.
python_ifold-2.8.3.6.zip 2.8.3.6 2008-04-18 6.0 Jean-Pierre Chauvel minor bugfix: Fixed a small bug introduced in the previous release.
python_ifold-2.8.3.5.zip 2.8.3.5 2008-04-01 6.0 Jean-Pierre Chauvel New python_ifold flavor: After receiving many requests via email, I decided to put all the vimrc recommended settings inside the plugin. In a nutshell, it loads these settings whenever the user opens a python file:

set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
set foldcolumn=1
python_ifold-2.8.3.4.zip 2.8.3.4 2008-03-08 6.0 Jean-Pierre Chauvel Fix (applicable only for the 'b' flavor): small fix for bug introduced in version 2.8.3.3.b
python_ifold-2.8.3.3.zip 2.8.3.3 2008-02-29 6.0 Jean-Pierre Chauvel Only in the b version or if the g:ifold_accuracy global variable is set
Fix: Now the script omits any comment mark that lays between the beginning of the line and the first sentence's character, that is, it wont unfold your code even if you put comment marks without proper indentation.
python_ifold.vim 2.8.3.2.b 2007-11-25 6.0 Jean-Pierre Chauvel python_ifold with all options selected by default
python_ifold.vim 2.8.3.2.a 2007-11-25 6.0 Jean-Pierre Chauvel python_ifold for legacy systems
python_ifold.vim 2.8.3.2 2007-10-18 6.0 Jean-Pierre Chauvel Enhancement: Now the script prevents "folding preemption", that is, when you try to expand the distance between two fold groups--be them class or function definitions--the engine will prevent from blank lines being eaten up at the bottom of the first fold group; nevertheless, I created a new global (g:ifold_accuracy) so that you can compare the former behavior with the new one.  Also, I decided to make the older engine the default method because it's quicker.

New global:
g:ifold_accuracy (0 by default, set it to 1 for experimentation with the new, someway better, behavior)

Put this in your vimrc to try it:
let g:fold_accuracy = 1
python_ifold.vim 2.8.3.1 2007-08-31 6.0 Jean-Pierre Chauvel bugfix: the ugly bug introduced in 2.8.3 is no more :D
python_ifold.vim 2.8.3 2007-08-31 6.0 Jean-Pierre Chauvel code refactoring and bugfixing:
before this upgrade the script tend to include commented lines within folded block of lines when they where uncommented, pretty annoying.
python_ifold.vim 2.8.2 2007-08-31 6.0 Jean-Pierre Chauvel bugfix: a stupid bug didn't allow the script to segregate line comments from the class or function definition even if this token was found two lines ahead. I really don't know if this was the expected behavior.

Example:

class A:
    pass

    #this line was included inside the fold
python_ifold.vim 2.8.1 2007-08-31 6.0 Jean-Pierre Chauvel Bugfixes:
- the script now avoids folding a block of code when there is a class or function definition 2 lines ahead.
- It is now possible to have one line of separation between classes of the same level of indentation without worrying the script eating that line--although happening in extremely rare cases:

class A():
    # will start folding here
    ...
    class SubSomething():
        somethinghere = ''
        # the script ate the next
        # blank line before the bugfix

class B():
    ...
python_ifold.vim 2.8 2007-08-27 6.0 Jean-Pierre Chauvel Bugfix: this time, the script won't fold when the immediate line is a class or function definition, like the example below

class MyClass():
    def __init__():
        return something     #no fold will be here
    class MyInnerClass():
        #...
python_ifold.vim 2.7 2007-08-26 6.0 Jean-Pierre Chauvel Feature: Now you can toggle on/off the script with 'f' key (it won't fold the block where the cursor is tough, but can be folded with zC later).
python_ifold.vim 2.6 2007-08-26 6.0 Jean-Pierre Chauvel Feature: The support markers and the text displayed in the folds are now optional (unset by default), in a desperate attempt to achieve better performance on legacy systems.
If you want the opposite behavior then you have to set the globals g:ifold_support_markers and g:ifold_show_text to 1.
python_ifold.vim 2.5 2007-08-25 6.0 Jean-Pierre Chauvel Feature: Added ReFold() function, pretty useful when some or all folds disappear.
python_ifold.vim 2.4 2007-08-25 6.0 Jean-Pierre Chauvel Initial upload
ip used for rating: 34.237.245.80

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.
   
Vim at Github