ReSharper 格式化:对齐相等的操作数
Google 员工请注意,这个问题有些过时了,因为当前版本的 ReSharper 2017.3.1 现在支持所请求的功能
我喜欢格式化代码以对齐相等操作数的右侧。
就像这里:
bool canRead = false;
bool canReadClass = true;
string className = boType.Name;
我最近切换到 ReSharper,发现它非常有用,但找不到允许我以描述的方式格式化代码的选项。
你知道是否有这样的选项/插件?
也许您知道 ReSharp 解决方案之外的其他解决方案允许这样做?
编辑:
如何决定代码的哪一部分应该对齐?
我的惯例是对齐同一块中的所有变量。
我所说的“块”是指未被空行分隔的部分代码。
例如
// First block
int count = 10;
string name = "abc";
bool calculate = true;
.....
.....
// Second block
MyOwnType myType = new MyOwntype();
int count = 10;
Note to Googlers, this question is somewhat out of date as the requested feature is now supported in the current version of ReSharper 2017.3.1
I like to formatting my code to align right side of equal operands.
Like here:
bool canRead = false;
bool canReadClass = true;
string className = boType.Name;
I've switch to ReSharper recently and found it very useful but cannot find option allowing me format code in described way.
Do you know if there is such option / plugin?
Maybe you know other than ReSharp solution allowing that?
EDIT:
How to decide what part of code shall be aligned?
My convention is aligning all variables in same block.
By "block" I meant part of code not divided by empty lines.
eg
// First block
int count = 10;
string name = "abc";
bool calculate = true;
.....
.....
// Second block
MyOwnType myType = new MyOwntype();
int count = 10;
EDIT -2
I've opened R# ticket for this. If anyone interested please vote!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(目前)没有办法在 ReSharper 中开箱即用地执行此操作。幸运的是,ReSharper 拥有非常丰富的可扩展性 API(尽管文档很少)。我花了很多时间与 Reflector 一起尝试解决问题。
我们对我工作的公司中的类成员使用类似的对齐指南(极端的是,我们还对齐方法参数)。我为 ReSharper 编写了一个插件来帮助我做到这一点。它是一个“代码清理”模块,在代码清理期间运行(Ctrl-E、Ctrl-F) 并为您对齐代码。如果可能的话,它还会使类
密封
。一些示例:
方法参数:
(您需要将中的Wrap形参更改为Chop always)选项 -> 格式样式 -> 换行和换行才能正常工作)
常量:
You can download the source code from my SkyDrive.Edit I seem to have lost access to that SkyDrive, and lost the files too. This was before github :(
请注意,您需要一些东西来编译/调试它:
更新
命令行参数
在
Project 的
Debug
选项卡中具有正确路径的属性
输出DLL:
<块引用>
/ReSharper.Plugin
“X:\\MyCompany.CodeFormatter\MyCompany.CodeFormatter\bin\Debug\MyCompany.CodeFormatter.dll”
这允许通过调试插件
F5,它将是
自动安装在
新版中的 ReSharper 插件
Visual Studio 实例将
打开。
安装此后,只需运行
Code Cleanup
即可修复对齐(我从未收到 JetBrains 关于如何不幸的是,在大括号/分号格式化期间执行此操作)。大会被重命名以保护无辜者:)
祝你好运!
There is (currently) no way to do this out of the box in ReSharper. Fortunately, ReSharper has a very rich extensibility API (albeit poorly documented). I've spent a lot of time with Reflector trying to figure things out.
We use a similar alignment guideline for class members in a company I work for (to the extreme, we also align method parameters). I wrote a plugin for ReSharper to help me do just that. It's a "Code Cleanup" module, which runs sometime during the code cleanup (Ctrl-E, Ctrl-F) and aligns the code for you. It also makes the class
sealed
, if possible.Some examples:
Method parameters:
(you will need to change Wrap formal parameters to Chop always in Options->Formatting Style->Line Breaks and Wrapping for this to work properly)
Constants:
You can download the source code from my SkyDrive.Edit I seem to have lost access to that SkyDrive, and lost the files too. This was before github :(
Please note that you'll need several things to compile/debug it:
Update the
Command Line Arguments
in
Debug
tab inProject
with the correct path ofProperties
the output DLL:
This allows debugging the plugin via
F5, and it will be
automatically installed in
ReSharper's Plugins in the new
Visual Studio instance which will
open.
C:\Program Files\JetBrains\ReSharper
, you will have to fix the references.After you install this, just run
Code Cleanup
to fix your alignment (I never got a reply from JetBrains about how to do this during brace/semicolon formatting, unfortunately).Assembly was renamed to protect the innocent :)
Good luck!
我认为值得注意的是 Visual Studio 生产力工具 有一个 Align作业功能。
以下是 Visual Studio 2013 生产力强大工具的链接。
I think it is worth noting that the Visual Studio Productivity Power Tools have an Align Assignments feature.
Here's a link to the Visual Studio 2013 Productivity Power Tools.
您可以尝试以下操作:代码对齐
它支持
You can try this: Code Alignment
It supports
Productivity Power Tools 2012 也有一个用于此目的的命令:ctrl- alt-]
显然还有其他好处。
Productivity Power Tools 2012 also has a command for this: ctrl-alt-]
Other goodies are obviously there as well.
据我所知,不幸的是,使用 Resharper 无法做到这一点。
As far as I know, this is unfortunately not possible using Resharper.
晚了几年,但除了 @MickyD 的评论之外,Resharper 可以为您做到这一点,请参阅 Resharper 博客。转到 Resharper/选项/代码编辑/C#/制表符、缩进、对齐。滚动到右侧窗格中选项的底部,找到“在列中对齐相似代码”,单击内容,享受吧。
Years late, but further to the comment from @MickyD, Resharper can do this for you, see Resharper blog. Go to Resharper/ Options/ Code Editing/ C#/ Tabs, Indents, Alignment. Scroll to the bottom of the options in the right hand window pane to find "Align Similar Code in Columns", click things, enjoy.