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 没有这个,你知道一个文本处理器可以做到这一点吗?
默认情况下,这不能在 pycharm 中完成。没有这样的插件可用。您可以在类似的线程中看到答案
在 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