1

When programming in PyCharm, I scroll a source file up and down, and some piece of code is long enough and it disturbs my attention. Example of code:

some code
some code
    ...
    some upper code at same level
    
    if somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
   
    some downer code at same level
    ...
some code
some code

That part with if ... elif ... elif is what I want to fold. I can fold individually each piece (if, elif, and another elif), but I want to be able to fold them at onece. The problem is that this is arbitrary block of code, there is a code at up and down at the same indent level.
I can write the vim fold markers:

some code
some code
    ...
    some upper code at same level
    # {{{ The conditions for dothis
    if somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    # }}}
    some downer code at same level
    ...
some code
some code

And when working with Kate editor, I can fold them. It looks like this:

some code
some code
    ...
    some upper code at same level
    # {{{ The conditions for dothis ... }}}
    some downer code at same level
    ...
some code
some code

But in PyCharm, at such markers there is no folding button.

Is it possible to fold vim fold markers in PyCharm? I seached in plugins store, and at the moment (may 2023) did not found such plugin.

Ashark
  • 643
  • 7
  • 16

1 Answers1

0

I do not know exactly for the vim/kate "{{{" "}}}" markers, but there is an alternative. PyCharm has its own type of comments for the same purpose: "editor-fold" and "region". The idea is taken from this answer.

Wrap the code with this

some code
some code
    ...
    some upper code at same level
    # region The conditions for dothis
    if somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    # endregion 
    some downer code at same level
    ...
some code
some code

or this:

some code
some code
    ...
    some upper code at same level
    # <editor-fold desc="The conditions for dothis">
    if somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    elif somecond:
        dothis
        dothis
    # </editor-fold>
    some downer code at same level
    ...
some code
some code

Also instead of typing all these # region Desription you can just press CRTL+ALT+T (but not on ubuntu, where this combination is for opening terminal) or going to Code -> Surround With and selecting your surrounding type. Here you can see the documentation.

Using this type of surrounding you can fold any arbitrary parts of the code. But you can also do this with selecting the code and pressing CTRL + ..

Ashark
  • 643
  • 7
  • 16
  • 1
    Vim is pretty liberal in accepting fold markers. So comments like `# desc="The condition for doThis"> {{{` and `# }}} ` should work both with PyCharm and Vim. – Friedrich May 11 '23 at 05:40