删除 (Python) 条评论中的参差不齐的行

Removing ragged lines in (Python) comments

PyCharm 可以包装比某个数字 n(本例中 =67)长的代码行,例如

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

变成

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

**是否有 "converse" 功能?* 通常,由于在评论中添加和删除想法,它们最终看起来非常糟糕且衣衫褴褛,如下所示:

# 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!

我希望 Pycharm(可能通过合并插件)能够重新格式化这些评论(填充最大行长度),使其看起来像这样:

# 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!

如果 Pycharm 没有这个,您知道可以执行此操作的文本处理器吗?

默认情况下 pycharm 无法完成此操作。没有这样的插件可用。你可以在类似的帖子中看到答案

Wrapping comments with line breaks in PyCharm

现在您可以考虑以下选项

  • 在 sed 或 awk 中写一些东西,但这不会直接在 windows 上工作
  • 为 PyCharm 写一个插件。这有一个巨大的学习曲线和太多的努力
  • 写一个脚本来做你想做的事

我会选择最后一个选项,因为它是最省力的解决方案。你可以选择你想要的语言,我选择 Python,因为我们知道 Python 在格式化 Python 文件以用于 Pycharm 时会出现 Python。

所以脚本的思路是合并连续的评论,然后按列宽换行

# 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