pycharmfolding

Mark specific code block to fold/collapse by default in PyCharm


I want to mark some particular functions to fold by default. Something like:

# PyFoldDefault
def my_func()  # PyFoldDefault - will hide a function.
    ...

Similar behavior there in PyCharm, for warnings suppression. PyCharm has a global logic like "fold all comments", imports, etc", But I need more precise control during development.

P.S. This isn't duplicate.


Solution

  • The only way to do what you want is using a code region and configuring it at Settings > Editor > General > Code Folding > Collapse by default and check Costum folding regions.

    (You could also use the Ctrl+- shortcut on a code selection but the folding won't persist after you restart the IDE and it can't turn on/off folding at multiple places with a single action.)

    # region your_region
    def my_func()
        ...
    # endregion your_region
    

    screenshot of IDE settings dialogue


    This can't be done using single line comments, what your example in the question reminds of are the inspection warning suppressing comments but no such single line comments exist to control code folding.

    # PyFoldDefault
    def my_func()  # PyFoldDefault - will hide a function.