From 6cd9c4988a3c497e9719e51dee25384b147e94f0 Mon Sep 17 00:00:00 2001 From: Jake Date: Fri, 6 Nov 2015 17:15:31 -0700 Subject: [PATCH 1/3] Added a rule to remove #region/#endregion directives --- ...crosoft.DotNet.CodeFormatting.Tests.csproj | 1 + .../Rules/RemoveRegionsTests.cs | 54 +++++++++++++++++++ .../Microsoft.DotNet.CodeFormatting.csproj | 1 + .../Rules/RemoveRegionsRule.cs | 52 ++++++++++++++++++ .../Rules/RuleOrder.cs | 1 + 5 files changed, 109 insertions(+) create mode 100644 src/Microsoft.DotNet.CodeFormatting.Tests/Rules/RemoveRegionsTests.cs create mode 100644 src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs 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..6cc83641 --- /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)] + 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; + } + + 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. From 509440053669a8f1bfe14b602409120ffbbf9181 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 7 Nov 2015 08:48:59 -0700 Subject: [PATCH 2/3] Added explicit visibility to RegionFinder. --- .../Rules/RemoveRegionsRule.cs | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs index 6cc83641..243e6f93 100644 --- a/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs +++ b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs @@ -9,44 +9,44 @@ namespace Microsoft.DotNet.CodeFormatting.Rules { - [SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule)] - 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; - } - - 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); - } - } - } + [SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule)] + 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 From 45ebac5f635d82b83356ec379e3119d7658388af Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 7 Nov 2015 12:04:00 -0700 Subject: [PATCH 3/3] Made rule off by default. --- src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs index 243e6f93..7376fdc1 100644 --- a/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs +++ b/src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs @@ -9,7 +9,7 @@ namespace Microsoft.DotNet.CodeFormatting.Rules { - [SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule)] + [SyntaxRule(RemoveRegionsRule.Name, RemoveRegionsRule.Description, SyntaxRuleOrder.RemoveRegionsRule, DefaultRule = false)] internal sealed class RemoveRegionsRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule { internal const string Name = "RemoveRegions";