Skip to content

Commit 1a0eaae

Browse files
committed
Implemented settings part
1 parent cb11b4d commit 1a0eaae

9 files changed

Lines changed: 121 additions & 92 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5353
/// </summary>
5454
/// <param name="document">The document to be changed.</param>
5555
/// <param name="diagnostic">The diagnostic to fix.</param>
56-
/// <param name="newlineAtEndOfFile">A <see cref="EndOfFileHandling"/> value indicating the desired behavior.</param>
56+
/// <param name="newlineAtEndOfFile">A <see cref="OptionSetting"/> value indicating the desired behavior.</param>
5757
/// <param name="cancellationToken">The cancellation token associated with the fix action.</param>
5858
/// <returns>The transformed document.</returns>
59-
private static async Task<Document> FixEndOfFileAsync(Document document, Diagnostic diagnostic, EndOfFileHandling newlineAtEndOfFile, CancellationToken cancellationToken)
59+
private static async Task<Document> FixEndOfFileAsync(Document document, Diagnostic diagnostic, OptionSetting newlineAtEndOfFile, CancellationToken cancellationToken)
6060
{
6161
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
62-
string replacement = newlineAtEndOfFile == EndOfFileHandling.Omit ? string.Empty : "\r\n";
62+
string replacement = newlineAtEndOfFile == OptionSetting.Omit ? string.Empty : "\r\n";
6363
return document.WithText(text.WithChanges(new TextChange(diagnostic.Location.SourceSpan, replacement)));
6464
}
6565

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1518UnitTests.cs

Lines changed: 75 additions & 75 deletions
Large diffs are not rendered by default.

StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public void VerifySettingsDefaults()
3131

3232
Assert.NotNull(styleCopSettings.OrderingRules);
3333
Assert.Equal(UsingDirectivesPlacement.InsideNamespace, styleCopSettings.OrderingRules.UsingDirectivesPlacement);
34+
Assert.Equal(OptionSetting.Allow, styleCopSettings.OrderingRules.UseBlankLinesBetweenUsingGroups);
3435

3536
Assert.NotNull(styleCopSettings.LayoutRules);
36-
Assert.Equal(EndOfFileHandling.Allow, styleCopSettings.LayoutRules.NewlineAtEndOfFile);
37+
Assert.Equal(OptionSetting.Allow, styleCopSettings.LayoutRules.NewlineAtEndOfFile);
3738

3839
Assert.NotNull(styleCopSettings.SpacingRules);
3940
Assert.NotNull(styleCopSettings.ReadabilityRules);
@@ -58,8 +59,12 @@ public async Task VerifySettingsAreReadCorrectlyAsync()
5859
""allowCommonHungarianPrefixes"": false,
5960
""allowedHungarianPrefixes"": [""a"", ""ab""]
6061
},
62+
""layoutRules"": {
63+
""newlineAtEndOfFile"": ""require""
64+
},
6165
""orderingRules"": {
62-
""usingDirectivesPlacement"": ""outsideNamespace""
66+
""usingDirectivesPlacement"": ""outsideNamespace"",
67+
""useBlankLinesBetweenUsingGroups"": ""omit"",
6368
}
6469
}
6570
}
@@ -73,8 +78,12 @@ public async Task VerifySettingsAreReadCorrectlyAsync()
7378
Assert.False(styleCopSettings.NamingRules.AllowCommonHungarianPrefixes);
7479
Assert.Equal(new[] { "a", "ab" }, styleCopSettings.NamingRules.AllowedHungarianPrefixes);
7580

81+
Assert.NotNull(styleCopSettings.LayoutRules);
82+
Assert.Equal(OptionSetting.Require, styleCopSettings.LayoutRules.NewlineAtEndOfFile);
83+
7684
Assert.NotNull(styleCopSettings.OrderingRules);
7785
Assert.Equal(UsingDirectivesPlacement.OutsideNamespace, styleCopSettings.OrderingRules.UsingDirectivesPlacement);
86+
Assert.Equal(OptionSetting.Omit, styleCopSettings.OrderingRules.UseBlankLinesBetweenUsingGroups);
7887
}
7988

8089
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518UseLineEndingsCorrectlyAtEndOfFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
144144
DiagnosticDescriptor descriptorToReport;
145145
switch (settings.LayoutRules.NewlineAtEndOfFile)
146146
{
147-
case EndOfFileHandling.Omit:
147+
case OptionSetting.Omit:
148148
if (firstNewline < 0)
149149
{
150150
return;
@@ -153,7 +153,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
153153
descriptorToReport = DescriptorOmit;
154154
break;
155155

156-
case EndOfFileHandling.Require:
156+
case OptionSetting.Require:
157157
if (firstNewline >= 0 && firstNewline == trailingWhitespaceText.Length - 1)
158158
{
159159
return;
@@ -162,7 +162,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
162162
descriptorToReport = DescriptorRequire;
163163
break;
164164

165-
case EndOfFileHandling.Allow:
165+
case OptionSetting.Allow:
166166
default:
167167
if (secondNewline < 0)
168168
{

StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/LayoutSettings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ internal class LayoutSettings
1212
/// This is the backing field for the <see cref="NewlineAtEndOfFile"/> property.
1313
/// </summary>
1414
[JsonProperty("newlineAtEndOfFile", DefaultValueHandling = DefaultValueHandling.Include)]
15-
private EndOfFileHandling newlineAtEndOfFile;
15+
private OptionSetting newlineAtEndOfFile;
1616

1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="LayoutSettings"/> class during JSON deserialization.
1919
/// </summary>
2020
[JsonConstructor]
2121
protected internal LayoutSettings()
2222
{
23-
this.newlineAtEndOfFile = EndOfFileHandling.Allow;
23+
this.newlineAtEndOfFile = OptionSetting.Allow;
2424
}
2525

26-
public EndOfFileHandling NewlineAtEndOfFile =>
26+
public OptionSetting NewlineAtEndOfFile =>
2727
this.newlineAtEndOfFile;
2828
}
2929
}

StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/EndOfFileHandling.cs renamed to StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/OptionSetting.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
namespace StyleCop.Analyzers.Settings.ObjectModel
55
{
66
/// <summary>
7-
/// Specifies the handling for newline characters which appear at the end of a file.
7+
/// Specifies the possible values for an option.
88
/// </summary>
9-
internal enum EndOfFileHandling
9+
internal enum OptionSetting
1010
{
1111
/// <summary>
12-
/// Files are allowed to end with a single newline character, but it is not required.
12+
/// The option is allowed, but not required.
1313
/// </summary>
1414
Allow,
1515

1616
/// <summary>
17-
/// Files are required to end with a single newline character.
17+
/// The option is required.
1818
/// </summary>
1919
Require,
2020

2121
/// <summary>
22-
/// Files may not end with a newline character.
22+
/// The option is not allowed.
2323
/// </summary>
2424
Omit
2525
}

StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/OrderingSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ internal class OrderingSettings
3535
[JsonProperty("usingDirectivesPlacement", DefaultValueHandling = DefaultValueHandling.Include)]
3636
private UsingDirectivesPlacement usingDirectivesPlacement;
3737

38+
/// <summary>
39+
/// This is the backing field for the <see cref="UseBlankLinesBetweenUsingGroups"/> property.
40+
/// </summary>
41+
[JsonProperty("useBlankLinesBetweenUsingGroups", DefaultValueHandling = DefaultValueHandling.Include)]
42+
private OptionSetting useBlankLinesBetweenUsingGroups;
43+
3844
/// <summary>
3945
/// Initializes a new instance of the <see cref="OrderingSettings"/> class during JSON deserialization.
4046
/// </summary>
@@ -44,6 +50,7 @@ protected internal OrderingSettings()
4450
this.elementOrder = ImmutableArray.CreateBuilder<OrderingTrait>();
4551
this.systemUsingDirectivesFirst = true;
4652
this.usingDirectivesPlacement = UsingDirectivesPlacement.InsideNamespace;
53+
this.useBlankLinesBetweenUsingGroups = OptionSetting.Allow;
4754
}
4855

4956
public ImmutableArray<OrderingTrait> ElementOrder
@@ -59,5 +66,8 @@ public ImmutableArray<OrderingTrait> ElementOrder
5966

6067
public UsingDirectivesPlacement UsingDirectivesPlacement =>
6168
this.usingDirectivesPlacement;
69+
70+
public OptionSetting UseBlankLinesBetweenUsingGroups =>
71+
this.useBlankLinesBetweenUsingGroups;
6272
}
6373
}

StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@
9090
"preserve"
9191
],
9292
"default": "insideNamespace"
93+
},
94+
"useBlankLinesBetweenUsingGroups": {
95+
"type": "string",
96+
"description": "Specifies if using groups should be separated by a blank line.\r\nallow: A blank line is allowed, but not required\r\nrequire: A blank line is required\r\nomit: A blank line is not allowed",
97+
"enum": [
98+
"allow",
99+
"require",
100+
"omit"
101+
],
102+
"default": "allow"
93103
}
94104
}
95105
},

StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
<Compile Include="ReadabilityRules\SX1101DoNotPrefixLocalMembersWithThis.cs" />
270270
<Compile Include="Settings\DeserializationFailureBehavior.cs" />
271271
<Compile Include="Settings\ObjectModel\DocumentationSettings.cs" />
272-
<Compile Include="Settings\ObjectModel\EndOfFileHandling.cs" />
272+
<Compile Include="Settings\ObjectModel\OptionSetting.cs" />
273273
<Compile Include="Settings\ObjectModel\FileNamingConvention.cs" />
274274
<Compile Include="Settings\ObjectModel\IndentationSettings.cs" />
275275
<Compile Include="Settings\ObjectModel\LayoutSettings.cs" />

0 commit comments

Comments
 (0)