tmhedberg / SimpylFold

No-BS Python code folding for Vim
BSD 3-Clause "New" or "Revised" License
656 stars 56 forks source link

Fold decorators in with the code they affect #86

Open kylemacfarlane opened 7 years ago

kylemacfarlane commented 7 years ago

With the following block:

@property
def test(self):
    pass

This plugin folds it like:

@property
+---  2 lines def test(self):---------------

But python-mode folds it like:

def test(self): ------------------------- 3-

Would it be possible to fold in the decorators like python-mode?

nfnty commented 7 years ago

It's possible with backtracking, but it would break docstrings as currently implemented. I don't see why you'd want to as it greatly decreases readability and predictability of folds. I vote against including this functionality, even as an option.

kylemacfarlane commented 7 years ago

I don't see how it would affect readability. Maybe in a class it would make it harder to distinguish properties/getters/setters but in things like Django where you use decorators to check permissions, control caching, CSRF, transactions, etc I'm sometimes seeing almost as much code left outside of folds as inside.

tmhedberg commented 7 years ago

Personally, I can see the usefulness of this, but it would require major changes to the code to support it without introducing bugs. For now, it's probably not worth it for the relatively minor improvement.

idahogray commented 6 years ago

I realize this issue is a little dated but I would like to log my support for implementing it. Something like this could be really useful for projects that use flask, click, or a similar API. Once thing to keep in mind is that decorators can be multiple lines.

I would offer my support but I don't know anything about vimscript. I can offer some example code to be folded, if that would be helpful.

disconsis commented 6 years ago

+1 for decorator folding As to the readability issue, adding an option like the ones for docstrings can help. IMO it makes sense for a lot of projects and people, since decorators are essentially part of the function as well. I do agree it reduces the predictability, but the foldstring could possibly be modified to show that decorators have been folded - something like

+-- 2 lines: @ def test(self): ----------------------------

Don't really know how feasible that is though.

TheLocehiliosan commented 6 years ago

I'd like to register another vote for including decorators in folds. I like the idea of including the @ in the folded text to indicate there is a folded decorator present.

lordmauve commented 6 years ago

I found SimpylFold while looking for a fold plugin that folds decorators, so am disappointed it doesn't.

I like the idea of @ def - could be @@ def if there are two decorators?

To add my 2c, I'd like to see built-in decorators property, staticmethod and classmethod displayed in the fold text in place of def:

+--  2 lines: classmethod setUpClass(cls): ----------------------------

This would not be dissimilar to how Sphinx renders these (example).

Of course it's not possible to know whether the decorator is genuinely the known one or a different decorator of the same name, but practically I would think matching by name is reliable enough.

Some well-known decorators in the standard library would also be useful - specifically those that change the type of a function rather than just enhancing its behaviour

For example

@contextlib.contextmanager
def redirect_output(stream):

could fold as

+--  9 lines: contextmanager redirect_output(stream): ----------------------------
fleetingbytes commented 5 years ago

I would also like to seethe decorator folding feature implemented.