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

发布于 2024-08-02 11:25:44 字数 989 浏览 5 评论 0原文

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;

编辑-2
我为此打开了 R# 票。如果有人感兴趣请投票

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

圈圈圆圆圈圈 2024-08-09 11:25:44

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

我们对我工作的公司中的类成员使用类似的对齐指南(极端的是,我们还对齐方法参数)。我为 ReSharper 编写了一个插件来帮助我做到这一点。它是一个“代码清理”模块,在代码清理期间运行(Ctrl-ECtrl-F) 并为您对齐代码。如果可能的话,它还会使类密封

一些示例

方法参数:

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

(您需要将中的Wrap形参更改为Chop always)选项 -> 格式样式 -> 换行和换行才能正常工作)

常量:

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. 更新命令行参数
    Project 的 Debug 选项卡中
    具有正确路径的属性

    输出DLL:

    <块引用>

    /ReSharper.Plugin
    “X:\\MyCompany.CodeFormatter\MyCompany.CodeFormatter\bin\Debug\MyCompany.CodeFormatter.dll”

    这允许通过调试插件
    F5,它将是
    自动安装在
    新版中的 ReSharper 插件
    Visual Studio 实例将
    打开。

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

安装此后,只需运行 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:

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

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

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

Please note that you'll need several things to compile/debug it:

  1. Update the Command Line Arguments
    in Debug tab in Project
    Properties
    with the correct path of
    the output 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. The plugin is for ReSharper 4.5 and it references the DLLs of this version. If you installed ReSharper anywhere else except C:\Program Files\JetBrains\ReSharper, you will have to fix the references.
  3. This does not align variables inside methods, but it shouldn't be hard to add :)

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!

南街女流氓 2024-08-09 11:25:44

我认为值得注意的是 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.

酷遇一生 2024-08-09 11:25:44

您可以尝试以下操作:代码对齐

它支持

  • 对齐方式...(对话框)
  • 对齐按位置...(对话框)
  • 按等于对齐
  • 按 m_
  • 对齐“
  • 按 对齐。
  • 按空格对齐

You can try this: Code Alignment

It supports

  • Align by... (Dialog)
  • Align by position... (Dialog)
  • Align by Equals
  • Align by m_
  • Align by "
  • Align by .
  • Align by Space
離人涙 2024-08-09 11:25:44

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.

许仙没带伞 2024-08-09 11:25:44

据我所知,不幸的是,使用 Resharper 无法做到这一点。

As far as I know, this is unfortunately not possible using Resharper.

凡尘雨 2024-08-09 11:25:44

晚了几年,但除了 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文