简体   繁体   English

Pycharm代码重新格式化:按运算符对齐行

[英]Pycharm code reformatting: align lines by operator

In Jetbrains Pycharm (Professional, 2016.1), is there a way (built in or third-party) to modify the code style in python to align consecutive lines by an operator, particularly an assignment one? 在Jetbrains Pycharm(Professional,2016.1)中,是否有一种方法(内置或第三方)来修改python中的代码样式以使运算符对齐连续的行,特别是赋值行? For example, I would like 例如,我想

var1 = 5
variable2 = 10
varbl3 = 15

to be reformatted to 要重新格式化

var1      = 5
variable2 = 10
varbl3    = 15

I am aware of several packages that let me do this manually with an external command (I am currently using Front-End Alignment, but String Manupulation also works decently), but it precludes me from also using code reformatting (which I otherwise find very useful) on the same piece of code. 我知道有几个软件包允许我使用外部命令手动执行此操作(我目前正在使用前端对齐,但String Manupulation也可以正常工作),但它也阻止我使用代码重新格式化(我发现它非常有用) )在同一段代码上。

I am aware this does not follow PEP-8 standards, and I don't wish to have a debate about the merits/demerits of aligning code. 我知道这不遵循PEP-8标准,我不希望就对齐代码的优点/缺点进行辩论。 Is there a way to allow the reformat code option to automatically take care of this for me? 有没有办法允许重新格式化代码选项自动为我处理这个?

To my knowledge, there isn't an automatic way. 据我所知,没有一种自动方式。

That said, something I found very helpful is "Add multiline carets on double ⌥ with arrow keys" (found in General>Smart Keys) along with "Move Caret to Next Word". 也就是说,我发现非常有用的是“使用箭头键在双⌥上添加多行插入符号”(在常规>智能键中找到)以及“将插入符号移到下一个字”。

  1. Put the cursor on line 1, double tap ⌥ and hold then press the down arrow the number of lines to edit. 将光标放在第1行,双击⌥并按住,然后按向下箭头键进行编辑。

  2. Let go of ⌥ (to get Pycharm out of 'add more cursors mode') then using "Move Caret to Next Word" ⌥→ move the cursors just before the = on each line. 放开⌥(让Pycharm退出'添加更多游标模式')然后使用“将Caret移动到下一个单词”⌥→在每行的=之前移动游标。

  3. Add a bunch of spaces till all the equal signs are past the alignment point. 添加一堆空格,直到所有等号都超过对齐点。

  4. Put the cursors back to the start of the line to get them aligned again. 将光标放回到行的开头,使它们再次对齐。

  5. Move them to the alignment point. 将它们移动到对齐点。

  6. Shift ⌥ → to highlight the space up to the =. Shift⌥→突出显示=的空格。

  7. Then backspace to line everything up. 然后退格以排列所有内容。

** I'm on mac, so the keys are going to be different for other OS. ** 我在Mac上,因此其他操作系统的密钥会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM