Pycharm 代码重新格式化:按运算符对齐行 [英] Pycharm code reformatting: align lines by operator

查看:38
本文介绍了Pycharm 代码重新格式化:按运算符对齐行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jetbrains Pycharm (Professional, 2016.1) 中,是否有一种方法(内置或第三方)可以修改 Python 中的代码样式以通过运算符对齐连续行,尤其是赋值语句?例如,我想

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? For example, I would like

var1 = 5
variable2 = 10
varbl3 = 15

重新格式化为

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.

我知道这不符合 PEP-8 标准,我不希望就对齐代码的优点/缺点进行辩论.有没有办法让重新格式化代码选项自动为我处理这个问题?

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. 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. 将光标放在第 1 行,双击 ⌥ 并按住然后按下箭头要编辑的行数.

  1. Put the cursor on line 1, double tap ⌥ and hold then press the down arrow the number of lines to edit.

放开 ⌥(让 Pycharm 退出添加更多光标模式"),然后使用将插入符号移到下一个单词"⌥→ 将光标移动到每个上的 = 之前线.

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.

添加一堆空格,直到所有等号都超过对齐点.

Add a bunch of spaces till all the equal signs are past the alignment point.

将光标放回行首,使其再次对齐.

Put the cursors back to the start of the line to get them aligned again.

将它们移动到对齐点.

Shift ⌥ → 突出显示直到 = 的空格.

Shift ⌥ → to highlight the space up to the =.

然后按退格键将所有内容对齐.

Then backspace to line everything up.

** 我使用的是 mac,所以其他操作系统的密钥会有所不同.

这篇关于Pycharm 代码重新格式化:按运算符对齐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆