Skip to content

Commit ae7faa7

Browse files
authored
Merge pull request #3447 from sharwell/file-scoped-namespaces
Insert blank line before using directives in file-scoped namespace
2 parents e48ecc0 + 87a4d3f commit ae7faa7

10 files changed

+38
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public List<UsingDirectiveSyntax> GetContainedUsings(TreeTextSpan directiveSpan)
9090
return result;
9191
}
9292

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

110+
if (withLeadingBlankLine && usingList.Count > 0)
111+
{
112+
var firstUsing = usingList[0];
113+
usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
114+
}
115+
110116
if (withTrailingBlankLine && (usingList.Count > 0))
111117
{
112118
var lastUsing = usingList[usingList.Count - 1];
@@ -116,7 +122,7 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan direc
116122
return SyntaxFactory.List(usingList);
117123
}
118124

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

142+
if (withLeadingBlankLine && usingList.Count > 0)
143+
{
144+
var firstUsing = usingList[0];
145+
usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
146+
}
147+
136148
if (withTrailingBlankLine && (usingList.Count > 0))
137149
{
138150
var lastUsing = usingList[usingList.Count - 1];

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ private static void BuildReplaceMapForNamespaces(UsingsSorter usingsHelper, Dict
225225
}
226226

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

229-
var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, false, qualifyNames);
230+
var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, withLeadingBlankLine, withTrailingBlankLine: false, qualifyNames);
230231

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

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

257-
var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, qualifyNames: false);
258+
var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, false, qualifyNames: false);
258259

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

279-
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: false);
281+
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine, withTrailingBlankLine, qualifyNames: false);
280282
groupedUsings = groupedUsings.AddRange(rootNamespace.Usings);
281283

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

293-
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: true);
295+
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine: false, withTrailingBlankLine, qualifyNames: true);
294296
groupedUsings = groupedUsings.AddRange(newCompilationUnit.Usings);
295297
newSyntaxRoot = newCompilationUnit.WithUsings(groupedUsings);
296298

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1200CSharp10UnitTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace TestNamespace;
2525
";
2626

2727
var fixedTestCode = @"namespace TestNamespace;
28+
2829
using System;
2930
using System.Threading;
3031
";

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1208CSharp10UnitTests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public async Task TestWhenSystemUsingDirectivesAreNotOnTopInFileScopedNamespaceA
2121
{
2222
TestSources =
2323
{
24-
"namespace Xyz {}",
25-
"namespace AnotherNamespace {}",
24+
"namespace Xyz;",
25+
"namespace AnotherNamespace;",
2626
@"
2727
namespace Test;
2828
@@ -38,10 +38,11 @@ class A
3838
},
3939
FixedSources =
4040
{
41-
"namespace Xyz {}",
42-
"namespace AnotherNamespace {}",
41+
"namespace Xyz;",
42+
"namespace AnotherNamespace;",
4343
@"
4444
namespace Test;
45+
4546
using System;
4647
using System.IO;
4748
using System.Threading.Tasks;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1209CSharp10UnitTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class A
3131
}
3232
";
3333
var fixedTestCodeNamespace = @"namespace Test;
34+
3435
using System.IO;
3536
using System.Net;
3637
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10CombinedSystemDirectivesUnitTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
3636
FixedSources =
3737
{
3838
@"namespace Food;
39+
3940
using System;
4041
using System.Threading;
4142
",
4243
@"namespace Bar;
44+
4345
using Bar;
4446
using Food;
4547
using System;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
3737
FixedSources =
3838
{
3939
@"namespace Foo;
40+
4041
using System;
4142
using System.Threading;
4243
",
4344
@"namespace Bar;
45+
4446
using System;
4547
using System.Threading;
4648
using Bar;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1211CSharp10UnitTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
5050
FixedSources =
5151
{
5252
@"namespace Foo;
53+
5354
using System;
5455
using character = System.Char;
5556
using \u0069nt = System.Int32;
5657
",
5758
@"namespace Bar;
59+
5860
using System;
5961
using MemoryStream = System.IO.MemoryStream;
6062
using Stream = System.IO.Stream;
6163
using StringBuilder = System.Text.StringBuilder;
6264
using StringWriter = System.IO.StringWriter;
6365
",
6466
@"namespace Spam;
67+
6568
using System;
6669
using Character = System.Char;
6770
using @int = System.Int32;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1216CSharp10UnitTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
4242
FixedSources =
4343
{
4444
@"namespace Foo;
45+
4546
using System;
4647
using static System.Math;
4748
using Execute = System.Action;
4849
",
4950
@"namespace Bar;
51+
5052
using System;
5153
using static System.Array;
5254
using static System.Math;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1217CSharp10UnitTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ public async Task TestUsingDirectivesOrderingInFileScopedNamespaceAsync()
4343
FixedSources =
4444
{
4545
@"namespace Foo;
46+
4647
using System;
4748
using static System.Array;
4849
using static System.Math;
4950
using Execute = System.Action;
5051
",
5152
@"namespace Bar;
53+
5254
using System;
5355
using static System.Array;
5456
using static System.Math;

0 commit comments

Comments
 (0)