在(Python)注释中删除不规则的行

时间:2018-03-11 04:19:08

标签: pycharm comments appearance

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

**是否有"反向"功能?*通常,由于添加和删除评论中的想法,他们最终看起来非常糟糕,并且像这样衣衫褴褛:

# 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没有这个,你知道一个可以做到这一点的文本处理器吗?

1 个答案:

答案 0 :(得分:1)

默认情况下,这不能在pycharm中完成。没有这样的插件可用。您可以在类似的帖子中看到答案

Wrapping comments with line breaks in PyCharm

现在您可以考虑以下选项

  • 用sed或awk写一些东西,但是这不会直接在windows上工作
  • 为PyCharm写一个插件。这有一个巨大的学习曲线和太多的努力
  • 编写脚本以执行您想要的操作

我会选择最后一个选项,因为它是努力方面最快的解决方案。你可以选择你想要的语言,我选择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