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

Insert blank line before using directives in file-scoped namespace #3447

Merged
merged 1 commit into from
Jan 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public List<UsingDirectiveSyntax> GetContainedUsings(TreeTextSpan directiveSpan)
return result;
}

public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan directiveSpan, string indentation, bool withTrailingBlankLine, bool qualifyNames)
public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan directiveSpan, string indentation, bool withLeadingBlankLine, bool withTrailingBlankLine, bool qualifyNames)
{
var usingList = new List<UsingDirectiveSyntax>();
List<SyntaxTrivia> triviaToMove = new List<SyntaxTrivia>();
Expand All @@ -107,6 +107,12 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan direc
usingList[0] = usingList[0].WithLeadingTrivia(newLeadingTrivia);
}

if (withLeadingBlankLine && usingList.Count > 0)
{
var firstUsing = usingList[0];
usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
}

if (withTrailingBlankLine && (usingList.Count > 0))
{
var lastUsing = usingList[usingList.Count - 1];
Expand All @@ -116,7 +122,7 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan direc
return SyntaxFactory.List(usingList);
}

public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiveSyntax> usingsList, string indentation, bool withTrailingBlankLine, bool qualifyNames)
public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiveSyntax> usingsList, string indentation, bool withLeadingBlankLine, bool withTrailingBlankLine, bool qualifyNames)
{
var usingList = new List<UsingDirectiveSyntax>();
List<SyntaxTrivia> triviaToMove = new List<SyntaxTrivia>();
Expand All @@ -133,6 +139,12 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiv
usingList[0] = usingList[0].WithLeadingTrivia(newLeadingTrivia);
}

if (withLeadingBlankLine && usingList.Count > 0)
{
var firstUsing = usingList[0];
usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
}

if (withTrailingBlankLine && (usingList.Count > 0))
{
var lastUsing = usingList[usingList.Count - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ private static void BuildReplaceMapForNamespaces(UsingsSorter usingsHelper, Dict
}

var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);
var withLeadingBlankLine = usingList[0].Parent.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration);

var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, false, qualifyNames);
var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, withLeadingBlankLine, withTrailingBlankLine: false, qualifyNames);

for (var i = 0; i < usingList.Count; i++)
{
Expand Down Expand Up @@ -254,7 +255,7 @@ private static void BuildReplaceMapForConditionalDirectives(UsingsSorter usingsH

var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);

var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, qualifyNames: false);
var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, false, qualifyNames: false);

for (var i = 0; i < originalUsings.Count; i++)
{
Expand All @@ -274,9 +275,10 @@ private static int CompareSpanStart(UsingDirectiveSyntax left, UsingDirectiveSyn
private static SyntaxNode AddUsingsToNamespace(SyntaxNode newSyntaxRoot, UsingsSorter usingsHelper, string usingsIndentation, bool hasConditionalDirectives)
{
var rootNamespace = (BaseNamespaceDeclarationSyntaxWrapper)((CompilationUnitSyntax)newSyntaxRoot).Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
var withLeadingBlankLine = rootNamespace.SyntaxNode.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration);
var withTrailingBlankLine = hasConditionalDirectives || rootNamespace.Members.Any() || rootNamespace.Externs.Any();

var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: false);
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine, withTrailingBlankLine, qualifyNames: false);
groupedUsings = groupedUsings.AddRange(rootNamespace.Usings);

var newRootNamespace = rootNamespace.WithUsings(groupedUsings);
Expand All @@ -290,7 +292,7 @@ private static SyntaxNode AddUsingsToCompilationRoot(SyntaxNode newSyntaxRoot, U
var newCompilationUnit = (CompilationUnitSyntax)newSyntaxRoot;
var withTrailingBlankLine = hasConditionalDirectives || newCompilationUnit.AttributeLists.Any() || newCompilationUnit.Members.Any() || newCompilationUnit.Externs.Any();

var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: true);
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine: false, withTrailingBlankLine, qualifyNames: true);
groupedUsings = groupedUsings.AddRange(newCompilationUnit.Usings);
newSyntaxRoot = newCompilationUnit.WithUsings(groupedUsings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace TestNamespace;
";

var fixedTestCode = @"namespace TestNamespace;
using System;
using System.Threading;
";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public async Task TestWhenSystemUsingDirectivesAreNotOnTopInFileScopedNamespaceA
{
TestSources =
{
"namespace Xyz {}",
"namespace AnotherNamespace {}",
"namespace Xyz;",
"namespace AnotherNamespace;",
@"
namespace Test;
Expand All @@ -38,10 +38,11 @@ class A
},
FixedSources =
{
"namespace Xyz {}",
"namespace AnotherNamespace {}",
"namespace Xyz;",
"namespace AnotherNamespace;",
@"
namespace Test;
using System;
using System.IO;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class A
}
";
var fixedTestCodeNamespace = @"namespace Test;
using System.IO;
using System.Net;
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
FixedSources =
{
@"namespace Food;
using System;
using System.Threading;
",
@"namespace Bar;
using Bar;
using Food;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
FixedSources =
{
@"namespace Foo;
using System;
using System.Threading;
",
@"namespace Bar;
using System;
using System.Threading;
using Bar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
FixedSources =
{
@"namespace Foo;
using System;
using character = System.Char;
using \u0069nt = System.Int32;
",
@"namespace Bar;
using System;
using MemoryStream = System.IO.MemoryStream;
using Stream = System.IO.Stream;
using StringBuilder = System.Text.StringBuilder;
using StringWriter = System.IO.StringWriter;
",
@"namespace Spam;
using System;
using Character = System.Char;
using @int = System.Int32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
FixedSources =
{
@"namespace Foo;
using System;
using static System.Math;
using Execute = System.Action;
",
@"namespace Bar;
using System;
using static System.Array;
using static System.Math;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
FixedSources =
{
@"namespace Foo;
using System;
using static System.Array;
using static System.Math;
using Execute = System.Action;
",
@"namespace Bar;
using System;
using static System.Array;
using static System.Math;
Expand Down