Skip to content

Commit a16b085

Browse files
authored
Don't always break attributes on property accessors (#1560)
closes #1558
1 parent b0cb0c2 commit a16b085

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ class ClassName
88
private string Field;
99

1010
[Obsolete]
11-
public string Property
12-
{
13-
[Obsolete]
14-
get;
15-
[Obsolete]
16-
set;
17-
}
11+
public string Property { [Obsolete] get; [Obsolete] set; }
1812

1913
[Obsolete, NonSerialized]
2014
void MethodName([In] string value) { }
@@ -129,3 +123,22 @@ public class ClassName { }
129123
LongName______________________
130124
>]
131125
public class ClassName { }
126+
127+
public class ClassName
128+
{
129+
public int ShortProperty { get; [SomeAttribute] init; } = 20;
130+
131+
public int CommandTimeout
132+
{
133+
get;
134+
[SomeAttribute]
135+
[SomeOtherAttribute]
136+
set;
137+
}
138+
139+
public int CommandTimeout
140+
{
141+
[SomeAttribute(someValue)]
142+
get;
143+
}
144+
}

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ public class ClassName
4646
}
4747
}
4848

49-
public string Property
50-
{
51-
[Obsolete]
52-
get;
53-
[Obsolete]
54-
set;
55-
}
49+
public string Property { [Obsolete] get; [Obsolete] set; }
5650

5751
public virtual ICollection<SomeObject> SomeLongNameThatForcesALineBreak______________________ { get; set; } =
5852
new HashSet<SomeObject>();

Src/CSharpier/SyntaxPrinter/AttributeLists.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PrintingContext context
1818
is TypeParameterSyntax
1919
or ParameterSyntax
2020
or ParenthesizedLambdaExpressionSyntax
21+
or AccessorDeclarationSyntax
2122
? Doc.Line
2223
: Doc.HardLine;
2324

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BasePropertyDeclaration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ PrintingContext context
113113
)
114114
{
115115
var docs = new List<Doc>();
116-
if (node.AttributeLists.Count > 0 || node.Body != null || node.ExpressionBody != null)
116+
if (
117+
node.AttributeLists.Count > 1
118+
|| node.Body != null
119+
|| node.ExpressionBody != null
120+
|| (
121+
node.AttributeLists.FirstOrDefault() is { } attributeListSyntax
122+
&& attributeListSyntax.Attributes.First().ArgumentList?.Arguments.Count > 0
123+
)
124+
)
117125
{
118126
docs.Add(Doc.HardLine);
119127
}

0 commit comments

Comments
 (0)