Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit ee08886

Browse files
committed
Added a rule to remove #region/#endregion directives
1 parent 8d049ad commit ee08886

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
9797
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
9898
<Compile Include="Rules\MarkReadonlyFieldTests.cs" />
99+
<Compile Include="Rules\RemoveRegionsTests.cs" />
99100
<Compile Include="Rules\UsingLocationRuleTests.cs" />
100101
</ItemGroup>
101102
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.DotNet.CodeFormatting.Tests
7+
{
8+
public class RemoveRegionsTests : SyntaxRuleTestBase
9+
{
10+
internal override ISyntaxFormattingRule Rule
11+
{
12+
get
13+
{
14+
return new Rules.RemoveRegionsRule();
15+
}
16+
}
17+
18+
[Fact]
19+
public void TestRemoveRegions()
20+
{
21+
var text = @"
22+
#region Region 1
23+
24+
//comment
25+
#endregion Region 1
26+
27+
class WithRegions
28+
{
29+
#region have region here
30+
public static void DoNothing()
31+
{
32+
#region inside method
33+
34+
#endregion inside method
35+
}
36+
#endregion
37+
}
38+
";
39+
var expected = @"
40+
41+
//comment
42+
43+
class WithRegions
44+
{
45+
public static void DoNothing()
46+
{
47+
48+
}
49+
}
50+
";
51+
Verify(text, expected);
52+
}
53+
}
54+
}

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="NameHelper.cs" />
7373
<Compile Include="ResponseFileWorkspace.cs" />
7474
<Compile Include="Rules\MarkReadonlyFieldsRule.cs" />
75+
<Compile Include="Rules\RemoveRegionsRule.cs" />
7576
<Compile Include="SemaphoreLock.cs" />
7677
<Compile Include="SyntaxUtil.cs" />
7778
<Compile Include="Filters\FilenameFilter.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright(c) Microsoft.All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Rules
11+
{
12+
[SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule)]
13+
internal sealed class RemoveRegionsRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
14+
{
15+
internal const string Name = "RemoveRegions";
16+
internal const string Description = "Removes all regions";
17+
18+
public SyntaxNode Process(SyntaxNode targetNode, string languageName)
19+
{
20+
var finder = new RegionFinder();
21+
finder.Visit(targetNode);
22+
var results = finder.Results;
23+
24+
if (results.Count > 0)
25+
{
26+
return targetNode.ReplaceTrivia(results,
27+
(arg1, arg2) => new SyntaxTrivia());
28+
}
29+
return targetNode;
30+
}
31+
32+
class RegionFinder : CSharpSyntaxWalker
33+
{
34+
public List<SyntaxTrivia> Results { get; } = new List<SyntaxTrivia>();
35+
36+
public RegionFinder()
37+
: base(SyntaxWalkerDepth.StructuredTrivia)
38+
{
39+
}
40+
41+
public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node)
42+
{
43+
Results.Add(node.ParentTrivia);
44+
}
45+
46+
public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
47+
{
48+
Results.Add(node.ParentTrivia);
49+
}
50+
}
51+
}
52+
}

src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ internal static class SyntaxRuleOrder
1818
public const int BraceNewLineRule = 4;
1919
public const int NonAsciiChractersAreEscapedInLiterals = 5;
2020
public const int CopyrightHeaderRule = 6;
21-
}
21+
public const int RemoveRegionsRule = 7;
22+
}
2223

2324
// Please keep these values sorted by number, not rule name.
2425
internal static class LocalSemanticRuleOrder

0 commit comments

Comments
 (0)