This repository was archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathRemoveRegionsRule.cs
More file actions
52 lines (44 loc) · 1.75 KB
/
RemoveRegionsRule.cs
File metadata and controls
52 lines (44 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}
public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
{
Results.Add(node.ParentTrivia);
}
}
}
}