18

I have tried a bunch of Python code folding plugins and I have seen this question asked once here, but they all don't seem to be too useful to achieve Python code folding in this manner:

class myClass(models.Model):
    [folded code]

    class Meta:
        [folded code]

    def __unicode__(self):
        [folded code]

    def save(self, *args, **kwargs):
        [folded code]

So my question is, Is there any Python code folding plugin that can do this? I haven't been able to find any so far and I have tried out quite a number of such Vim plugins already.

Community
  • 1
  • 1
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167

3 Answers3

9

description

Because of its reliance on significant whitespace rather than explicit block delimiters, properly folding Python code can be tricky. The Python syntax definition that comes bundled with Vim doesn't contain any fold directives at all, and the simplest workaround is to :set foldmethod=indent, which usually ends up folding a lot more than you really want it to.

There's no shortage of Vim plugins for improved Python folding, but most seem to suffer from cobbled-together algorithms with bizarre, intractable bugs in the corner cases. SimpylFold aims to be exactly what its name suggests: simple, correct folding for Python. It's nothing more than it needs to be: it properly folds class and function/method definitions, and leaves your loops and conditional blocks untouched. There's no BS involved: no screwing around with unrelated options (which several of the other plugins do), no choice of algorithms to scratch your head over (because there's only one that's correct); it just works, simply.

http://www.vim.org/scripts/script.php?script_id=3723

Community
  • 1
  • 1
A T
  • 13,008
  • 21
  • 97
  • 158
  • looks promising. I have tried this plugin yet. Giving it a shot! thanks! – Calvin Cheng Sep 15 '11 at 01:47
  • 1
    That doesn't seem to work. Moved to the line right below class myClass and hit `za` but the entire class folded (and all the class methods are hidden) instead folding as I described above. – Calvin Cheng Sep 16 '11 at 06:47
  • 2
    This may be useful information, but my understanding is that bare links are discouraged as answers. If you fleshed it out a little (inserted explanatory text, for example), you could stand a chance of getting some upvotes. – Keith Pinson Jul 02 '12 at 15:52
0

I have been looking for the same thing, a folding method for python that leaves the entire method signature even when it spans multiple lines. This script worked for me. However, to get multiple line signatures to work, you'll want to add "let g:ifold_mode=2" to your .vimrc file.

I haven't used this script very long, so there may be other issues with it. Good luck!

0

I almost always use set foldmethod=indent and it does almost what you want it to do (except for folding the class global variables).

see the help for how to tweak it.

Sedrik
  • 2,161
  • 17
  • 17