c# - Resharper C# 格式样式在截断长行时在新行而不是同一行上显示 "new"

标签 c# resharper code-formatting

所以 Resharper 在重新格式化代码时在我的代码中的“new”之前放置了一个换行符,如下所示:

var foo = new Foo
{
    Bar = null,
    Baz =
        new Baz
        {
            Bap = null,
            Bork = null,
            Help =
                new PweaseHelp
                {
                    Korben = null,
                    Dallas = null,
                    Multipass = null
                },
            Me =
                new ClearlyMyAbilityToUnderstandResharperSettingsIs(
                    null),
        }
};

但我真的很想这样做:

var foo = new Foo
{
    Bar = null,
    Baz = new Baz
    {
        Bap = null,
        Bork = null,
        Help = new PweaseHelp
        {
            Korben = null,
            Dallas = null,
            Multipass = null
        },
        Me = new ClearlyMyAbilityToUnderstandResharperSettingsIs(null),
    }
};

我已经深入研究了我的 .DotSettings 文件中的所有设置,但我无法弄清楚是什么原因造成的...任何帮助将不胜感激:)

编辑 2(更新)

这里是 R# 设置,似乎让我接近我列出的内容,你仍然会看到等号之后的新行(使用列出的配置),除非你为“wrap”选择“chop always”调用参数”和“包装对象和集合初始值设定项”(如 Kristian 所建议的那样)。

“chop always”的问题是你会有非常短的方法调用和对象/集合初始化器也一直在切断,这看起来很糟糕,所以我认为我们想要的是:

不要在方法调用/对象/集合初始值设定项的等号后换行(但我无法在任何地方找到该设置,所以它可能是 R 的错误或功能#).

我会尝试在 R# 论坛/支持上提出它并报告我的发现。

<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_ARRAY_AND_OBJECT_INITIALIZER/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CONTINUOUS_INDENT_MULTIPLIER/@EntryValue">1</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EMPTY_BLOCK_STYLE/@EntryValue">TOGETHER_SAME_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_ANONYMOUS_METHOD_BLOCK/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TRAILING_COMMENT/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">150</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>

最佳答案

如果您使用的是 R# 7.1,可能是因为新的 Code Formatting Improvements .所有这些行为都可以轻松在 R# 选项下配置:

Resharper C# Formatting Style

编辑:根据我的设置,我最接近您建议的解决方案是将Wrap object and collection initializer设置为Chop always ,但是随后,出于某种原因,它忽略了 Braces 布局 下的“数组和对象初始化器” 设置,并将左大括号放在同一行,如下所示:

var foo = new Foo {
    Bar = null,
    Baz = new Baz {
        Bap = null,
        Bork = null,
        Help = new PweaseHelp {
            Korben = null,
            Dallas = null,
            Multipass = null
        },
        Me = new ClearlyMyAbilityToUnderstandResharperSettingsIs(null),
    }
};

这可能是我的其他一些设置导致的,但对我来说这看起来像是有问题。也许你应该联系 ReSharper Support让他们看看...

关于c# - Resharper C# 格式样式在截断长行时在新行而不是同一行上显示 "new",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13985739/

相关文章:

Python 正确的代码格式 (PEP8)

c# - JSON.NET 中基于属性的类型解析

c# - 在图片框中打开之前如何调整图像大小

visual-studio - 从接口(interface)生成类

c - 从 C 源代码中删除字符串

html - 如何避免换行填充?

c# - 为什么这个程序会挂起?

c# - 清空 ListView

visual-studio-2010 - 为 VS2010 购买 R# 真的值得吗?

c# - 程序集中的所有 NUnit 测试都会被跳过,因为我在引用的项目中添加了空参数检查