有没有办法标记代码以告诉 ReSharper 不要格式化它?

发布于 2024-08-20 00:41:12 字数 560 浏览 6 评论 0原文

我经常使用 ReSharper“清理代码”命令将我的代码格式化为在将其签入源代码管理之前我们的编码风格。一般来说,这工作得很好,但有些代码最好手动格式化(例如,由于 ReSharper 中的缩进规则,链式 linq 方法或多行三元运算符之类的东西有一个奇怪的缩进,将它们推到右侧)。

有没有办法标记文件的某些部分以告诉 ReSharper 不要格式化该区域?我希望有某种类似于 ReSharper 如何抑制其他警告/功能的标记。如果没有,是否有某种方法可以更改设置组合以使 ReSharper 正确格式化缩进?

编辑:

我在ReSharper论坛中找到了这篇帖子表示生成的代码部分(如 ReSharper 选项页面中定义)在代码清理中被忽略。但尝试过之后,它似乎并没有被忽视。

I quite often use the ReSharper "Clean Up Code" command to format my code to our coding style before checking it into source control. This works well in general, but some bits of code are better formatted manually (eg. because of the indenting rules in ReSharper, things like chained linq methods or multi-line ternary operators have a strange indent that pushes them way to the right).

Is there any way to mark up parts of a file to tell ReSharper not to format that area? I'm hoping for some kind of markup similar to how ReSharper suppresses other warnings/features. If not, is there some way of changing a combination of settings to get ReSharper to format the indenting correctly?

EDIT:

I have found this post from the ReSharper forums that says that generated code sections (as defined in the ReSharper options page) are ignored in code cleanup. Having tried it though, it doesn't seem to get ignored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

坏尐絯℡ 2024-08-27 00:41:12

Resharper>选项>语言>C#>格式样式>其他>

取消选中“缩进匿名方法体”和“缩进数组、对象和集合初始化程序块”以及任何其他您感兴趣的内容。

Resharper>Options>Languages>C#>Formatting Style>Other>

Uncheck "Indent anonymous method body" and "Indent array, object and collection initilizer blocks" and anything else that strikes your fancy.

调妓 2024-08-27 00:41:12

作为最后的手段,如果您不想格式化旧代码,但希望对类的添加进行良好的格式化,则使类部分化并将新代码放入新文件中。

As a last resort, if you've got legacy code that you don't want to format but you want additions to the class to be nicely formatted, then make the class partial and put new code in the new file.

信愁 2024-08-27 00:41:12

看看我问的这个问题涉及同样的问题:Resharper将代码格式化为单个line

我在那里得到的答案对我来说非常有用。

Check out this question I asked that involves the same issue: Resharper formatting code into a single line

The answer I got there work really good for me.

山川志 2024-08-27 00:41:12

很晚了,但显然有一种方法可以根据 https://www.jetbrains.com/help/resharper/Configure_Code_Formatting_Rules.html#using-comments-to-configure-formatter :将所需部分包装在

// @formatter: off

...

// @formatter: on

“注意” 中这个空间看起来很重要,没有它,里面的代码仍然会被重新格式化。

Very late, but apparently there is a way to do this according to https://www.jetbrains.com/help/resharper/Configure_Code_Formatting_Rules.html#using-comments-to-configure-formatter : wrap the desired section in

// @formatter: off

...

// @formatter: on

Note that the space appears to be significant, without it the code inside will still be reformatted.

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