I run into situations a lot where I have code that looks like

new_stuff = ( 
    blah.blah ( stuff,
    ).blah_two( [morestuff,
    stuffffff ] )
    .blah_three( ....

I'd like to be able to collapse the stuff inside the parenthesis.

This can be achieved using vimish-fold (with the default keybindings) by defining a fold with z f (selecting the target with visual mode, so e.g. the entire sequence could be v % v f), then z o / z c.

The problem with this solution is that :

  • Ideally I'd be able to have the fold regions identified automatically based on paranthesis syntax. This could probably be done with a script.

  • I'd like to be able to collapse and open them recursively of them. This seems like a bigger issue, because vimish folds doesn't support recursive folds: https://github.com/matsievskiysv/vimish-fold/issues/64 ( further discussion here, including a pointer to a fork where this was implemented: https://github.com/matsievskiysv/vimish-fold/issues/17#issuecomment-589936173 )

    In particular, I can't have a custom fold around the definition of new_stuff and custom_folds around each of the arguments being passed to the blah methods.

To clarify, the ideal behavior here is that, for instance, I can be on the outer parens and collapse the entire definition of new stuff, or open it and leave the inner parens collapsed, and then uncollapse a specific one to look at it.


new_stuff = (n lines collapsed ... )


new_stuff = ( 
    blah.blah ( stuff,
    ).blah_two( m lines collapsed ...
    ).blah_three( y lines collapsed ...)

Relatedly, it seems like a solution would also be able to collapse lists or dictionaries:

my_list = ([1,

my_list = ([...])

The issues here are the same, I'd like to be able to collapse lists inside the list, and identify the overlapping regions syntactically. I think the blockers are the same.

Question: Is there a solution to parenthesis collapsing compatible with doom emacs that handles the above features?

A very similar question was asked here, but the answer is not specific to doom Fold code between parentheses and brackets in python mode + hideshow

Another similar question, but this is about class folding: https://stackoverflow.com/questions/5244485/python-code-folding-in-emacs?noredirect=1&lq=1

  • @Ian I'm not trying to change the contents of the file (which in the blah.blah case is how black formats it), just to hide them in the way that vimish fold does.
    – Elle Najt
    Commented Feb 9 at 17:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.