Visual Studio/C# 自动格式化。我可以控制属性后的换行符吗

发布于 2024-08-14 09:16:48 字数 631 浏览 8 评论 0原文

Visual Studio 一直这样做:

[DataContract] 
public class MyContract 
{
    [DataMember]
    public bool MyBool { get; set; }
    [DataMember]
    public string MyString { get; set; } 
}

我希望这样:

[DataContract] 
public class MyContract 
{
    [DataMember] public bool MyBool { get; set; }
    [DataMember] public string MyString { get; set; } 
}

如果“public class MyContract”与 [DataContract] 位于同一行,那没什么大不了的。

Visual Studio 似乎有很多详细的自动格式化选项,但我找不到任何有关属性后换行符的选项。我在这里错过了什么吗?或者只是不可用。

编辑:至少,我想要一个“不要更改我输入的内容”格式选项,而不是“始终插入”或“始终删除”换行符选项。非常烦人的是,它在我输入后不断取消我的代码格式。

Visual studio keeps doing this:

[DataContract] 
public class MyContract 
{
    [DataMember]
    public bool MyBool { get; set; }
    [DataMember]
    public string MyString { get; set; } 
}

I would like this:

[DataContract] 
public class MyContract 
{
    [DataMember] public bool MyBool { get; set; }
    [DataMember] public string MyString { get; set; } 
}

No big deal if the 'public class MyContract' is on the same line as the [DataContract].

Visual studio seems to have a lot of detailed autoformatting options, but I can't find any regarding newlines after attributes. Am I missing something here? Or is it just not available.

EDIT: At very least, I'd like a "don't change what I entered" formatting option, as opposed to a "always insert" or "always remove" newline option. It's super-annoying that it keeps unformatting my code after I type.

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

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

发布评论

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

评论(6

白馒头 2024-08-21 09:16:48

我通常会在自动格式跳转到我不喜欢的地方时按下 Ctrl-Z。

即,在闭幕式上,它格式化了整个类或方法。输入结束赞誉,看看它如何改变你不喜欢的方式,然后按 Ctrl-Z。 (我知道您正在寻找实际的选择,我不知道是否存在)。

What I usually do is hit Ctrl-Z the very moment autoformat jumps in where I don't appreciate it.

I.e., on a closing accolade, which formats a whole class or method. Type the closing accolade, see it changing the way you don't like it and then hit Ctrl-Z. (I know you were looking for an actual option, I don't know if any exists).

守护在此方 2024-08-21 09:16:48

不确定它是否适用于属性,但请查看 Tools -> 选项 -> 文本编辑器 -> C# -> 格式化 -> 换行 -> 将块保留在单行上将语句和成员声明保留在同一行上

Not sure if it works for attributes, but look under Tools -> Options -> Text Editor -> C# -> Formatting -> Wrapping -> Leave block on single line or Leave statements and member declarations on the same line.

痴情 2024-08-21 09:16:48

ReSharper 可以做到这一点。它具有以下选项:

  • 将类型属性放在同一行
  • 将方法属性放在同一行
  • 将属性/索引器/事件属性放在同一行(这是您想要的)
  • 将字段属性放在同一行

这要花费几美元,但如果您'像我一样痴迷,每一分钱都是值得的。 ;)

我还将 Ctrl+K、Ctrl+D 重新映射到 ReSharper 的静默格式代码,以体验格式化的乐趣。

ReSharper can do that. It has options for:

  • Place type attribute on same line
  • Place method attribute on same line
  • Place property/indexer/event attribute on same line (this is the one you want)
  • Place field attribute on same line

It costs a few bucks but if you're as obsessive as I am it's worth every penny. ;)

I also remap Ctrl+K, Ctrl+D to ReSharper's silent format code to experience formatting bliss.

南风起 2024-08-21 09:16:48

对我来说最有效的是禁用自动格式;并粘贴。无论如何,我在这些时候几乎不需要自动格式化。这允许您输入属性并移动它们,而不会受到烦人的干扰。

选项>文本编辑器> C#>格式化>概述

设置截图

What worked best for me was to disable auto format on ; and paste. I hardly ever want auto format at these times anyways. This allows you to type out attributes and move them around without pesky interference.

Options > Text Editor > C# > Formatting > General

Settings screenshot

雨落星ぅ辰 2024-08-21 09:16:48

它是 ReSharper。 Extensions/ReSharper/Options/Code Editiong/C#/Formatting Style/ 换行和换行: 换行

和换行:
换行和换行

It is ReSharper. Extensions/ReSharper/Options/Code Editiong/C#/Formatting Style/ Line Breakes and Wrapping:

Line Breaks and Wrapping:
Line Breaks and Wrapping

夜唯美灬不弃 2024-08-21 09:16:48

是的,Ctrl+E, D 是你的朋友。您可以在文本编辑器选项中优化格式

Yeah, Ctrl+E, D is your friend. You can optimize the formatting in Text editor options

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