ReSharper 格式化:对齐相等的操作数

2024-03-09

谷歌员工请注意,这个问题有点过时了,因为当前版本的 ReSharper 2017.3.1 现在支持所请求的功能

我喜欢格式化我的代码以对齐相等操作数的右侧。
像这儿:

bool    canRead     = false;
bool    canReadClass    = true;
string  className   = boType.Name;

我最近切换到 ReSharper,发现它非常有用,但找不到允许我以描述的方式格式化代码的选项。

你知道是否有这样的选项/插件?
也许您知道除了 ReSharp 解决方案之外的允许这样做的解决方案?

EDIT:
如何决定代码的哪一部分应该对齐?
我的惯例是对齐同一块中的所有变量。
我所说的“块”是指未被空行分隔的部分代码。

eg

// First block
int      count     = 10;
string   name      = "abc";
bool     calculate = true;
.....
.....
// Second block
MyOwnType    myType   = new MyOwntype();
int          count    = 10;

EDIT -2
我已经打开了R# 票 http://youtrack.jetbrains.com/issue/RSRP-121267为了这。如果有人有兴趣请投票 http://youtrack.jetbrains.com/issue/RSRP-121267!


(目前)没有办法在 ReSharper 中开箱即用地执行此操作。幸运的是,ReSharper 拥有非常丰富的可扩展性 API(尽管文档很少)。我花了很多时间与 Reflector 一起尝试解决问题。

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.

一些例子:

方法参数:

public void DoSomething(string      name,
                        int         age,
                        IEnumerable coll)

(你需要改变包装形式参数 to 总是砍 in 选项->格式样式->换行和换行为了使其正常工作)

常数:

private const int    RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
private const int    CONNECT_COMMANDLINE        = 0x00000800;
private const int    CONNECT_INTERACTIVE        = 0x00000008;
private const string RESOURCE_NAME              = "Unknown";

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 :(

请注意,您需要一些东西来编译/调试它:

  1. 更新Command Line Arguments in Debug tab in Project Properties与正确的路径 输出DLL:

    /ReSharper.Plugin "X:\<projects>\MyCompany.CodeFormatter\MyCompany.CodeFormatter\bin\Debug\MyCompany.CodeFormatter.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.

  2. 该插件适用于 ReSharper 4.5,它引用了该版本的 DLL。如果您在其他地方安装了 ReSharper,除了C:\Program Files\JetBrains\ReSharper,您将必须修复参考。
  3. 这不会对齐方法内的变量,但添加应该不难:)

安装后,只需运行Code Cleanup修复您的对齐方式(不幸的是,我从未收到 JetBrains 关于如何在大括号/分号格式化期间执行此操作的回复)。

大会被重命名是为了保护无辜者:)

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ReSharper 格式化:对齐相等的操作数 的相关文章

随机推荐