Skip to content

Commit 34b5ec2

Browse files
authored
Finding a fix for parameter being indented when the attribute on said parameter has a comment (#1559)
closes #1553
1 parent e18fc06 commit 34b5ec2

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ class ClassName
9191
TSomeMuchLongerName___________________________________,
9292
K
9393
>() { }
94+
95+
public void SomeMethod(
96+
// Some Comment does not indent parameter
97+
[SomeAttribute] string someParameter
98+
) { }
99+
100+
public void SomeMethod(
101+
[SomeLongAttribute(someLongValue___________________________________________)]
102+
string someParameter
103+
) { }
104+
105+
public void SomeMethod(
106+
// Some Comment
107+
[SomeAttribute]
108+
[SomeOtherAttribute]
109+
string someParameter
110+
) { }
94111
}
95112

96113
[

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/Parameter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ internal static class Parameter
55
public static Doc Print(ParameterSyntax node, PrintingContext context)
66
{
77
var hasAttribute = node.AttributeLists.Any();
8-
var groupId = hasAttribute ? Guid.NewGuid().ToString() : string.Empty;
98
var docs = new List<Doc>();
109

1110
if (hasAttribute)
1211
{
1312
docs.Add(AttributeLists.Print(node, node.AttributeLists, context));
14-
docs.Add(Doc.IndentIfBreak(Doc.Line, groupId));
13+
if (
14+
node.AttributeLists.Count < 2
15+
&& (
16+
node.GetLeadingTrivia().Any(o => o.IsComment())
17+
|| node.Parent is ParameterListSyntax { Parameters.Count: 0 }
18+
)
19+
)
20+
{
21+
docs.Add(" ");
22+
}
23+
else
24+
{
25+
docs.Add(Doc.Indent(Doc.Line));
26+
}
1527
}
1628

1729
if (node.Modifiers.Any())
@@ -30,7 +42,7 @@ public static Doc Print(ParameterSyntax node, PrintingContext context)
3042
docs.Add(EqualsValueClause.Print(node.Default, context));
3143
}
3244

33-
return hasAttribute ? Doc.GroupWithId(groupId, docs)
45+
return hasAttribute ? Doc.Group(docs)
3446
: docs.Count == 1 ? docs[0]
3547
: Doc.Concat(docs);
3648
}

0 commit comments

Comments
 (0)