Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve normalization to match idiomatic patterns for nested usings and fixed statements. #61533

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,10 +1215,27 @@ private static int GetDeclarationDepth(SyntaxNode? node)
return parentDepth;
}

if (node.Parent is BlockSyntax ||
(node is StatementSyntax && !(node is BlockSyntax)))
if (node.Parent is BlockSyntax)
{
// all nested statements are indented one level
return parentDepth + 1;
}

if (node is StatementSyntax && node is not BlockSyntax)
{
// Nested statements are normally indented one level.
//
// However, for chains of using-statements or fixed-statements, we'd like to follow the
// idiomatic pattern of:
//
// using ...
// using ...
// .. embedded statement ..
if (node is UsingStatementSyntax { Parent: UsingStatementSyntax })
return parentDepth;

if (node is FixedStatementSyntax { Parent: FixedStatementSyntax })
return parentDepth;

return parentDepth + 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,5 +1306,43 @@ public void TestNormalizeXmlArgumentsInDocComment7()
const string Text = @"/// Prefix <b b=""y""a=""x"" >S_OK</b> suffix";
TestNormalizeDeclaration(Text, Expected);
}

[Fact]
[WorkItem(61518, "https://github.com/dotnet/roslyn/issues/61518")]
public void TestNormalizeNestedUsingStatements1()
{
TestNormalizeStatement("using(a)using(b)c;", "using (a)\r\nusing (b)\r\n c;");
TestNormalizeStatement("using(a)using(b){c;}", "using (a)\r\nusing (b)\r\n{\r\n c;\r\n}");
TestNormalizeStatement("using(a)using(b)using(c)d;", "using (a)\r\nusing (b)\r\nusing (c)\r\n d;");
TestNormalizeStatement("using(a)using(b)using(c){d;}", "using (a)\r\nusing (b)\r\nusing (c)\r\n{\r\n d;\r\n}");

TestNormalizeStatement("using(a){using(b)c;}", "using (a)\r\n{\r\n using (b)\r\n c;\r\n}");
TestNormalizeStatement("using(a){using(b)using(c)d;}", "using (a)\r\n{\r\n using (b)\r\n using (c)\r\n d;\r\n}");
TestNormalizeStatement("using(a)using(b){using(c)d;}", "using (a)\r\nusing (b)\r\n{\r\n using (c)\r\n d;\r\n}");
TestNormalizeStatement("using(a){using(b){using(c)d;}}", "using (a)\r\n{\r\n using (b)\r\n {\r\n using (c)\r\n d;\r\n }\r\n}");
}

[Fact]
[WorkItem(61518, "https://github.com/dotnet/roslyn/issues/61518")]
public void TestNormalizeNestedFixedStatements1()
{
TestNormalizeStatement("fixed(int* a = null)fixed(int* b = null)c;", "fixed (int* a = null)\r\nfixed (int* b = null)\r\n c;");
TestNormalizeStatement("fixed(int* a = null)fixed(int* b = null){c;}", "fixed (int* a = null)\r\nfixed (int* b = null)\r\n{\r\n c;\r\n}");
TestNormalizeStatement("fixed(int* a = null)fixed(int* b = null)fixed(int* c = null)d;", "fixed (int* a = null)\r\nfixed (int* b = null)\r\nfixed (int* c = null)\r\n d;");
TestNormalizeStatement("fixed(int* a = null)fixed(int* b = null)fixed(int* c = null){d;}", "fixed (int* a = null)\r\nfixed (int* b = null)\r\nfixed (int* c = null)\r\n{\r\n d;\r\n}");

TestNormalizeStatement("fixed(int* a = null){fixed(int* b = null)c;}", "fixed (int* a = null)\r\n{\r\n fixed (int* b = null)\r\n c;\r\n}");
TestNormalizeStatement("fixed(int* a = null){fixed(int* b = null)fixed(int* c = null)d;}", "fixed (int* a = null)\r\n{\r\n fixed (int* b = null)\r\n fixed (int* c = null)\r\n d;\r\n}");
TestNormalizeStatement("fixed(int* a = null)fixed(int* b = null){fixed(int* c = null)d;}", "fixed (int* a = null)\r\nfixed (int* b = null)\r\n{\r\n fixed (int* c = null)\r\n d;\r\n}");
TestNormalizeStatement("fixed(int* a = null){fixed(int* b = null){fixed(int* c = null)d;}}", "fixed (int* a = null)\r\n{\r\n fixed (int* b = null)\r\n {\r\n fixed (int* c = null)\r\n d;\r\n }\r\n}");
}

[Fact]
[WorkItem(61518, "https://github.com/dotnet/roslyn/issues/61518")]
public void TestNormalizeNestedFixedUsingStatements1()
{
TestNormalizeStatement("using(a)fixed(int* b = null)c;", "using (a)\r\n fixed (int* b = null)\r\n c;");
TestNormalizeStatement("fixed(int* b = null)using(a)c;", "fixed (int* b = null)\r\n using (a)\r\n c;");
}
}
}