1

I use PyCharm Professional 2020.3 with Python 3.8. I want to unwrap automatically and then rewrap multiline comments and docstrings when I change the maximum line length. Is there a tool or add-in I can install to achieve this? If not, is there some other solution?

For example, say I have set my maximum line length to be 88 characters (the Black default). Then a multiline comment and docstring will wrap like this:

"""The summary line of a multi-line docstring should be automatically wrapped. That is to
say that its line break should be moved so that it complies with the required maximum
line length.

:param a_param: int, optional
    This is an example parameter, which would exist if this was the docstring for a
    function. It should also be automatically wrapped and indented. However, the line
    breaks that separate the list of parameters/returned variables must not be removed
    when formatted.
"""

# This is an example of a multiline comment that has been wrapped to the given maximum
# line length. Behold how it is both a comment and has been wrapped.

Then, say I set my maximum line width to 120 characters (the PyCharm default). I would then like my multiline comments and docstring to be reformatted like so:

"""The summary line of a multi-line docstring should be automatically wrapped. That is to say that its line break should
be moved so that it complies with the required maximum line length.

:param a_param: int, optional
    This is an example parameter, which would exist if this was the docstring for a function. It should also be
    automatically wrapped and indented. However, the line breaks that separate the list of parameters/returned variables
    must not be removed when formatted.
"""

# This is an example of a multiline comment that has been wrapped to the given maximum line length. Behold how it is
# both a comment and has been wrapped.

Also, I am trying to implement my docstrings with the reStructured Text format. However, I'm still new to documentation in Python, so feel free to critique the above examples.

wingedNorthropi
  • 149
  • 2
  • 16

1 Answers1

0

I believe you are looking for the "textwrap" module, which controls denting, dedenting, paragraph width, etc.

Bobby Ocean
  • 3,120
  • 1
  • 8
  • 15