Pycharm 删除(Python)注释中不规则的行
PyCharm可以包装长度超过某个数字n(=67)的代码行,例如 变成Pycharm 删除(Python)注释中不规则的行,pycharm,comments,appearance,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 the first idea. # more
# 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!
# 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!
# 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无法做到这一点。没有这样的插件可用。你可以从类似的线索中看到答案 现在,您需要考虑以下选项
- 用sed或awk编写一些东西,但是这样就不能直接在windows上工作了
- 为PyCharm编写一个插件。这有一个巨大的学习曲线和太多的努力
- 写一个脚本来做你想做的事
# 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
真的,到目前为止还没人吗?也许吧?它将重新格式化整个文件。很抱歉回复太晚,一些问题使我在一段时间内无法登录stackoverflow。你的答案真的很好,非常感谢你写的这么详细和好的剧本。这肯定对其他人也有帮助!