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 pathAttributeSeparateListsRule.cs
More file actions
84 lines (68 loc) · 3.73 KB
/
AttributeSeparateListsRule.cs
File metadata and controls
84 lines (68 loc) · 3.73 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.DotNet.CodeFormatting.Rules
{
[SyntaxRuleOrder(SyntaxRuleOrder.AttributeSeparateListsRule)]
internal sealed class AttributeSeparateListsRule : ISyntaxFormattingRule
{
public SyntaxNode Process(SyntaxNode syntaxRoot)
{
var rewriter = new AttributeListRewriter();
return rewriter.Visit(syntaxRoot);
}
private sealed class AttributeListRewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitParameter(ParameterSyntax node)
{
// We don't want to flatten the attribute lists for parameters. Those are
// usually short, such as [In, Out] and collapsing them can actually
// improve readability.
return node;
}
public override SyntaxList<TNode> VisitList<TNode>(SyntaxList<TNode> list)
{
list = base.VisitList(list);
if (typeof (TNode) != typeof (AttributeListSyntax))
return list;
var attributeLists = (SyntaxList<AttributeListSyntax>) (object) list;
return (SyntaxList<TNode>) (object) VisitAttributeLists(attributeLists);
}
private static SyntaxList<AttributeListSyntax> VisitAttributeLists(SyntaxList<AttributeListSyntax> attributeLists)
{
var result = new List<AttributeListSyntax>();
foreach (var attributeList in attributeLists)
{
var firstIndex = result.Count;
for (var i = 0; i < attributeList.Attributes.Count; i++)
{
var attribute = attributeList.Attributes[i];
var separatorTrivia = i < attributeList.Attributes.Count - 1
? attributeList.Attributes.GetSeparator(i).GetAllTrivia()
: Enumerable.Empty<SyntaxTrivia>();
var attributeWithoutTrivia = attribute.WithoutLeadingTrivia().WithoutTrailingTrivia();
var singletonList = SyntaxFactory.AttributeList(attributeList.Target, SyntaxFactory.SeparatedList(new[] { attributeWithoutTrivia }))
.WithLeadingTrivia(attribute.GetLeadingTrivia())
.WithTrailingTrivia(attribute.GetTrailingTrivia().Concat(separatorTrivia));
result.Add(singletonList);
}
var lastIndex = result.Count - 1;
var leadingTrivia = attributeList.GetLeadingTrivia()
.Concat(attributeList.OpenBracketToken.TrailingTrivia)
.Concat(result[firstIndex].GetLeadingTrivia());
var trailingTrivia = result[lastIndex].GetTrailingTrivia()
.Concat(attributeList.CloseBracketToken.LeadingTrivia)
.Concat(attributeList.GetTrailingTrivia());
result[firstIndex] = result[firstIndex].WithLeadingTrivia(leadingTrivia);
result[lastIndex] = result[lastIndex].WithTrailingTrivia(trailingTrivia);
}
return SyntaxFactory.List(result);
}
}
}
}