diff --git a/src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj b/src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj index 0e81ce9e..a76c65e5 100644 --- a/src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj +++ b/src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj @@ -96,6 +96,7 @@ + diff --git a/src/Microsoft.DotNet.CodeFormatting.Tests/Rules/RemoveRegionsTests.cs b/src/Microsoft.DotNet.CodeFormatting.Tests/Rules/RemoveRegionsTests.cs new file mode 100644 index 00000000..7e68f20f --- /dev/null +++ b/src/Microsoft.DotNet.CodeFormatting.Tests/Rules/RemoveRegionsTests.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Xunit; + +namespace Microsoft.DotNet.CodeFormatting.Tests +{ + public class RemoveRegionsTests : SyntaxRuleTestBase + { + internal override ISyntaxFormattingRule Rule + { + get + { + return new Rules.RemoveRegionsRule(); + } + } + + [Fact] + public void TestRemoveRegions() + { + var text = @" +#region Region 1 + +//comment +#endregion Region 1 + +class WithRegions +{ + #region have region here + public static void DoNothing() + { + #region inside method + + #endregion inside method + } +#endregion +} +"; + var expected = @" + +//comment + +class WithRegions +{ + public static void DoNothing() + { + + } +} +"; + Verify(text, expected); + } + } +} diff --git a/src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj b/src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj index 9ef1dc2a..180fdd8b 100644 --- a/src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj +++ b/src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj @@ -72,6 +72,7 @@ + diff --git a/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs new file mode 100644 index 00000000..7376fdc1 --- /dev/null +++ b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs @@ -0,0 +1,52 @@ +// Copyright(c) Microsoft.All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace Microsoft.DotNet.CodeFormatting.Rules +{ + [SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule, DefaultRule = false)] + internal sealed class RemoveRegionsRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule + { + internal const string Name = "RemoveRegions"; + internal const string Description = "Removes all regions"; + + public SyntaxNode Process(SyntaxNode targetNode, string languageName) + { + var finder = new RegionFinder(); + finder.Visit(targetNode); + var results = finder.Results; + + if (results.Count > 0) + { + return targetNode.ReplaceTrivia(results, + (arg1, arg2) => new SyntaxTrivia()); + } + return targetNode; + } + + private class RegionFinder : CSharpSyntaxWalker + { + public List Results { get; } = new List(); + + public RegionFinder() + : base(SyntaxWalkerDepth.StructuredTrivia) + { + } + + public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node) + { + Results.Add(node.ParentTrivia); + } + + public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node) + { + Results.Add(node.ParentTrivia); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs b/src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs index 83b4dbe5..23a20dbe 100644 --- a/src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs +++ b/src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs @@ -18,6 +18,7 @@ internal static class SyntaxRuleOrder public const int BraceNewLineRule = 4; public const int NonAsciiChractersAreEscapedInLiterals = 5; public const int CopyrightHeaderRule = 6; + public const int RemoveRegionsRule = 7; } // Please keep these values sorted by number, not rule name.