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

Added a rule to remove #region/#endregion directives. #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
<Compile Include="Rules\MarkReadonlyFieldTests.cs" />
<Compile Include="Rules\RemoveRegionsTests.cs" />
<Compile Include="Rules\UsingLocationRuleTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="NameHelper.cs" />
<Compile Include="ResponseFileWorkspace.cs" />
<Compile Include="Rules\MarkReadonlyFieldsRule.cs" />
<Compile Include="Rules\RemoveRegionsRule.cs" />
<Compile Include="SemaphoreLock.cs" />
<Compile Include="SyntaxUtil.cs" />
<Compile Include="Filters\FilenameFilter.cs" />
Expand Down
52 changes: 52 additions & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/RemoveRegionsRule.cs
Original file line number Diff line number Diff line change
@@ -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<SyntaxTrivia> Results { get; } = new List<SyntaxTrivia>();

public RegionFinder()
: base(SyntaxWalkerDepth.StructuredTrivia)
{
}

public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node)
{
Results.Add(node.ParentTrivia);

This comment was marked as spam.

}

public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
{
Results.Add(node.ParentTrivia);
}
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down