为python源代码应用黑色格式化程序
blacken-selection的Python项目详细描述
应用Blackpython源代码的工具 将代码格式化程序转换为单个python源代码而不是整个源代码 文件或项目。在
使用
$ pip install blacken-selection $ blacken-selection -h usage: Apply the black code formatter for a piece of python source code positional arguments: SOURCE The python source code to be formatted. If not provided then source is read from stdin. optional arguments: -h, --help show this help message and exit -l LINE_LENGTH, --line-length LINE_LENGTH How many characters per line to allow. [default: 88] $ blacken-selection "def main(): pass" def main(): pass
linux上PyCharm的用法
不幸的是,PyCharm外部工具不能直接使用。这个 $SelectedText$宏将所选内容作为命令行参数传递给 但是没有办法使用double正确引用python字符串 引用。在
但有一个解决方案:
- 安装
xclip
命令行工具,我们将使用它来获取 通过剪贴板在PyCharm中选择代码 - 如上所述安装blacken_selection
- 获取简单的shell脚本blacken-selection-xclip.sh
在PyCharm中将blacken-selection-xclip.sh
设置为外部工具:
单击+图标以添加具有以下值的新外部工具:
Name: blacken
Description: Blacken a piece of python code
Program: <absolute path for your blacken-selection-xclip.sh>
Arguments:
在编辑器中选择一段python源代码,然后右键单击 上下文菜单选择:
External Tools -> blacken
或者,您可以指定一个快捷方式(如Shift+Alt+B)在以下位置触发工具:
Settings -> Keymap-> External Tools -> blacken
该工具将从剪贴板抓取选定的文本,用黑色格式化,
然后将格式化的代码复制回剪贴板。所以你基本上可以做到
PyCharm中的select
、shift+alt+B
、insert
来格式化一篇文章
黑色的python代码。在
- 项目
标签: