gpt4 book ai didi

pycharm - 删除(Python)注释中的参差不齐的行

转载 作者:行者123 更新时间:2023-12-02 02:56:17 26 4
gpt4 key购买 nike

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

**是否有“逆向”功能?*通常,由于在注释中添加和删除了想法,它们最终看起来真的很糟糕,像这样衣衫r:
# 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,因为我们知道在格式化供Pycharm使用的Python文件时会出现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

    关于pycharm - 删除(Python)注释中的参差不齐的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49216602/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com