pythonsublimetext4

Sublime Text 4: folding Python functions with a line break


I'm running Sublime Text Build 4143. Given a function with parameters that spill over the 80 character limit like so:

def func(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6):
    """
    """

    print("hello world")

    return

ST will show a PEP8 E501: line too long warning and highlight the line (which is fine):

enter image description here

But I can fold this function appropriately:

enter image description here

If I modify it to avoid the PEP8 warning:

enter image description here

I can no longer fold it:

enter image description here

This changed in the last update I think, because I used to be able to fold these functions without issues. How can I get around ths?


Solution

  • This is not a "fair" answer, but may be helpful. If you use black-like line folding (or anything similar, but with one important property: the closing parenthesis should be on its own line and be indented to the same level as def, see below), then folding works even better:

    def func(
        parameter_1, 
        parameter_2, 
        parameter_3, 
        parameter_4, 
        parameter_5, 
        parameter_6,
    ):
        """
        """
    
        print("hello world")
        return
    

    Now you have three arrows: the first folds function arguments, the second folds all function body and the third folds only the docstring.

    You can wrap parameters in any way you like, if closing parenthesis remains in place. I personally prefer this style, and it can be auto-formatted with black. Your solution is PEP8-compatible, but ST doesn't like it. It folds to the next line with the same level of indentation (so I'm very surprised that it worked before). This line-wrapping style is especially cute if you use type hinting: every argument appears on its own line together with type, and return type is written on the last line - still separate).

    This problem also arises in languages with goto construct and labels, which can be indented to the same level as function body - wrapping dies as well.

    Behaviour screenshot