自动重新格式化文档字符串以尊重右边距
automatically reformat docstrings to respect right margin
我经常编辑文档字符串,发现我的编辑将行的宽度推到所需的右边距之外。因此,在我的文档字符串再次被接受之前,此编辑下方的许多文本行可能需要重新格式化。
自动修复此问题的简单安全方法是什么?
例如:
class WellDocumentedClass:
"""The first line of this is <72 characters wide. And is above lots|
more text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.|
et mauris ac eros placerat auctor. Mauris mollis est scelerisse |
accumsan dapibus. Ut imperdiet suscipit lacinia. Maecenas volutpat |
iaculis malesuada. Sed venenatis ipsum gravida molestolaoreet. Fuse|
facilisis neque nec mauris maximus rutrum. Suspendisse at vestibulo|
orci, ut feugiat odio. Aliquam erat volutpat. Nulla accumsan justo |
ligula, at imperdiet quam ultrices non. Cras vitae vehicula ligula.|
Quisque quam massa, dignissim in volutpat in, mattis eu urna. |
"""
pass
哦不!我不小心从第一行省略了 "docstring" 这个词。格式太完美了!
class WellDocumentedClass:
"""The first line of this docstring is <72 characters wide. And is |above lots
more text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.|
et mauris ac eros placerat auctor. Mauris mollis est scelerisse |
accumsan dapibus. Ut imperdiet suscipit lacinia. Maecenas volutpat |
iaculis malesuada. Sed venenatis ipsum gravida molestolaoreet. Fuse|
facilisis neque nec mauris maximus rutrum. Suspendisse at vestibulo|
orci, ut feugiat odio. Aliquam erat volutpat. Nulla accumsan justo |
ligula, at imperdiet quam ultrices non. Cras vitae vehicula ligula.|
Quisque quam massa, dignissim in volutpat in, mattis eu urna. |
"""
pass
唉。是时候经常使用我的鼠标并按回车键了……除非……你在这样的时刻做什么?
您可以通过创建(相对)简单的插件在 Sublime Text 3 中实现此目的:
- 工具菜单 -> 开发人员 -> 新插件...
- Select 全部替换为以下内容
import sublime
import sublime_plugin
import textwrap
class WrapTextCommand(sublime_plugin.TextCommand):
def run(self, edit, width=0):
# use the narrowest ruler from the view if no width specified, or default to 72 if no rulers are enabled
width = width or next(iter(self.view.settings().get('rulers', [])), 72)
new_sel = list()
# loop through the selections in reverse order so that the selection positions don't move when the selected text changes size
for sel in reversed(self.view.sel()):
# make sure the leading indentation is selected, for `dedent` to work properly
sel = sel.cover(self.view.line(sel.begin()))
# determine how much indentation is at the first selected line
indentation_amount = self.view.indentation_level(sel.begin()) * self.view.settings().get('tab_size', 4)
# create a wrapper that will keep that indentation
wrapper = textwrap.TextWrapper(drop_whitespace=True, width=width, initial_indent=' ' * indentation_amount, subsequent_indent=' ' * indentation_amount)
# unindent the selected text before then reformatting the text to fill the available (column) space
text = wrapper.fill(textwrap.dedent(self.view.substr(sel)))
# replace the selected text with the rewrapped text
self.view.replace(edit, sel, text)
# resize the selection to match the new wrapped text size
new_sel.append(sublime.Region(sel.begin(), sel.begin() + len(text)))
self.view.sel().clear()
self.view.sel().add_all(new_sel)
- 保存在 ST 推荐的文件夹中 (
Packages/User/
),如 wraptext.py
- 创建一个键绑定以调用新的包装功能:
{ "keys": ["alt+."], "command": "wrap_text" },
- 享受
在pycharm中标记docstring并点击Edit -> Fill Paragraph
我经常编辑文档字符串,发现我的编辑将行的宽度推到所需的右边距之外。因此,在我的文档字符串再次被接受之前,此编辑下方的许多文本行可能需要重新格式化。
自动修复此问题的简单安全方法是什么?
例如:
class WellDocumentedClass:
"""The first line of this is <72 characters wide. And is above lots|
more text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.|
et mauris ac eros placerat auctor. Mauris mollis est scelerisse |
accumsan dapibus. Ut imperdiet suscipit lacinia. Maecenas volutpat |
iaculis malesuada. Sed venenatis ipsum gravida molestolaoreet. Fuse|
facilisis neque nec mauris maximus rutrum. Suspendisse at vestibulo|
orci, ut feugiat odio. Aliquam erat volutpat. Nulla accumsan justo |
ligula, at imperdiet quam ultrices non. Cras vitae vehicula ligula.|
Quisque quam massa, dignissim in volutpat in, mattis eu urna. |
"""
pass
哦不!我不小心从第一行省略了 "docstring" 这个词。格式太完美了!
class WellDocumentedClass:
"""The first line of this docstring is <72 characters wide. And is |above lots
more text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.|
et mauris ac eros placerat auctor. Mauris mollis est scelerisse |
accumsan dapibus. Ut imperdiet suscipit lacinia. Maecenas volutpat |
iaculis malesuada. Sed venenatis ipsum gravida molestolaoreet. Fuse|
facilisis neque nec mauris maximus rutrum. Suspendisse at vestibulo|
orci, ut feugiat odio. Aliquam erat volutpat. Nulla accumsan justo |
ligula, at imperdiet quam ultrices non. Cras vitae vehicula ligula.|
Quisque quam massa, dignissim in volutpat in, mattis eu urna. |
"""
pass
唉。是时候经常使用我的鼠标并按回车键了……除非……你在这样的时刻做什么?
您可以通过创建(相对)简单的插件在 Sublime Text 3 中实现此目的:
- 工具菜单 -> 开发人员 -> 新插件...
- Select 全部替换为以下内容
import sublime
import sublime_plugin
import textwrap
class WrapTextCommand(sublime_plugin.TextCommand):
def run(self, edit, width=0):
# use the narrowest ruler from the view if no width specified, or default to 72 if no rulers are enabled
width = width or next(iter(self.view.settings().get('rulers', [])), 72)
new_sel = list()
# loop through the selections in reverse order so that the selection positions don't move when the selected text changes size
for sel in reversed(self.view.sel()):
# make sure the leading indentation is selected, for `dedent` to work properly
sel = sel.cover(self.view.line(sel.begin()))
# determine how much indentation is at the first selected line
indentation_amount = self.view.indentation_level(sel.begin()) * self.view.settings().get('tab_size', 4)
# create a wrapper that will keep that indentation
wrapper = textwrap.TextWrapper(drop_whitespace=True, width=width, initial_indent=' ' * indentation_amount, subsequent_indent=' ' * indentation_amount)
# unindent the selected text before then reformatting the text to fill the available (column) space
text = wrapper.fill(textwrap.dedent(self.view.substr(sel)))
# replace the selected text with the rewrapped text
self.view.replace(edit, sel, text)
# resize the selection to match the new wrapped text size
new_sel.append(sublime.Region(sel.begin(), sel.begin() + len(text)))
self.view.sel().clear()
self.view.sel().add_all(new_sel)
- 保存在 ST 推荐的文件夹中 (
Packages/User/
),如wraptext.py
- 创建一个键绑定以调用新的包装功能:
{ "keys": ["alt+."], "command": "wrap_text" },
- 享受
在pycharm中标记docstring并点击Edit -> Fill Paragraph