Create a "foldable" section within a Python function

1,697 views
Skip to first unread message

Gauvins

unread,
Nov 29, 2018, 9:13:30 AM11/29/18
to BBEdit Talk
I have a long list of constants at the top of a function. Would be great if is could be collapsed, like what happens with def / comment_blocks / """ strings """ and such. 

I cannot find a command that would "create a fold", or a way to define custom regions. I do not wish to spend a significant amount of time to rewrite the code merely to be able to collapse this sub-section. 

Any suggestion?

"Néstor E. Aguilera"

unread,
Nov 29, 2018, 9:40:44 AM11/29/18
to bbe...@googlegroups.com
Hi Gauvins,
Perhaps this is not exactly what you want, but you may select some text and then use "View -> Fold Selection". See the manual (page 90 in version 12.5.1).

Regards,

Nestor

Jim Danner

unread,
Nov 29, 2018, 10:05:53 AM11/29/18
to BBEdit Talk
Another (slightly silly) idea: put those definitions of constants inside a loop that doesn't really do anything, like

def myfunction():
   
for i in [1]:
        constant1
= 10
        constant2
= 'hello'
       
....
    rest of the
function code

This for-lop (which runs just once) will be folded by BBEdit.

Rod Buchanan

unread,
Nov 29, 2018, 11:44:11 AM11/29/18
to 'Jeffrey Jones' via BBEdit Talk

Or Right-click -> Fold Selection
> --
> This is the BBEdit Talk public discussion group. If you have a
> feature request or need technical support, please email
> "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <https://www.twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To post to this group, send email to bbe...@googlegroups.com.
> Visit this group at https://groups.google.com/group/bbedit.

--
Rod


Gauvins

unread,
Nov 29, 2018, 1:18:05 PM11/29/18
to BBEdit Talk
great idea, not silly at all. this creates a "permanent" fold with a clickable toggle. Folding ad-hoc selections do not behave as neatly when you fold-unfold the entire document or something of that nature.

Jim Danner

unread,
Nov 30, 2018, 10:18:49 AM11/30/18
to BBEdit Talk
OK, if this is your preferred solution, you could give some values names that make it easy to understand what this does even when it is folded, like

def myfunction():
   
for putting_values in ['some constants']:

        constant1
= 10
        constant2
= 'hello'
       
....
    rest of the
function code
Reply all
Reply to author
Forward
0 new messages