Removing ragged lines in comments (Python)

PyCharm can wrap lines of code that are longer than some number n (= 67 for this example), for example

# this is a really really really really really really really really really really really really really really long comment

becomes

# this is a really really really really really really really really
# really really really really really really long comment

** Is there a “reverse” functionality? * Often, due to the addition and removal of ideas from comments, they look very bad and are torn off like this:

# this is a the first idea.
# more bla bla, but still relating to first idea. bla bla.
# other bla bla.
# second brilliant idea. oh no, actually it breaks something, so watch out!

I would like Pycharm (possibly by including plugins) to reformat these comments (filling in the maximum line length) to look like this:

# this is a the first idea. more bla bla, but still relating to first 
# idea. bla bla. other bla bla. second brilliant idea. oh no, actually
# it breaks something, so watch out!

If Pycharm does not have this, do you know about a word processor that can do this?

+4
source share
1 answer

This cannot be done in pycharm by default. There is no plugin. You can see the answer in a similar thread

PyCharm line break wrapper comments

,

  • - sed awk,
  • PyCharm. .
  • script, ,

, . , Python, , Python Python Pycharm.

, script ,

# this is a test
# this is a best.
# This could be even worst when this is not even good.
# Why should one do it using whatever they have done
import io, re
from textwrap import TextWrapper

import os

current_file = __file__

f = io.open(current_file, 'r')
comment_pattern = re.compile(r"^(\s*)#(.*)")

in_comment = False


def spit_formatted_comments(initial_space, current_comment):
    if current_comment:
        wrapper = TextWrapper(initial_indent=initial_space + "#",
                              subsequent_indent=initial_space + "# ")

        wrapper.width = 80
        data = wrapper.wrap(" ".join(current_comment))
        print(os.linesep.join(data))


for line in f:
    match = comment_pattern.findall(line)

    if match:
        if not in_comment:
            in_comment = True
            current_comment = []
            initial_space = match[0][0]

        current_comment.append(match[0][1])
    elif in_comment:
        in_comment = False
        spit_formatted_comments(initial_space, current_comment)
        current_comment = []
        print(line, end='')
    else:
        print(line, end='')

spit_formatted_comments(initial_space, current_comment)

f.close()
# this is a last line comment to check border

# this is a test  this is a best.  This could be even worst when this is not
# even good.  Why should one do it using whatever they have done
import io, re
from textwrap import TextWrapper

import os

current_file = __file__

f = io.open(current_file, 'r')
comment_pattern = re.compile(r"^(\s*)#(.*)")

in_comment = False


def spit_formatted_comments(initial_space, current_comment):
    if current_comment:
        wrapper = TextWrapper(initial_indent=initial_space + "#",
                              subsequent_indent=initial_space + "# ")

        wrapper.width = 80
        data = wrapper.wrap(" ".join(current_comment))
        print(os.linesep.join(data))


for line in f:
    match = comment_pattern.findall(line)

    if match:
        if not in_comment:
            in_comment = True
            current_comment = []
            initial_space = match[0][0]

        current_comment.append(match[0][1])
    elif in_comment:
        in_comment = False
        spit_formatted_comments(initial_space, current_comment)
        current_comment = []
        print(line, end='')
    else:
        print(line, end='')

spit_formatted_comments(initial_space, current_comment)

f.close()
# this is a last line comment to check border
+1

Source: https://habr.com/ru/post/1694730/


All Articles