-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAttributesOnSeparateLines.cs
More file actions
93 lines (79 loc) · 3.87 KB
/
AttributesOnSeparateLines.cs
File metadata and controls
93 lines (79 loc) · 3.87 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
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
namespace IntelliTect.Analyzer.CodeFixes
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AttributesOnSeparateLines))]
[Shared]
public class AttributesOnSeparateLines : CodeFixProvider
{
private const string Title = "Fix Format Violation: Put Attributes on separate Lines";
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(Analyzers.AttributesOnSeparateLines.DiagnosticId);
public sealed override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root is null)
{
return;
}
Diagnostic diagnostic = context.Diagnostics.First();
Microsoft.CodeAnalysis.Text.TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
SyntaxToken declaration = root.FindToken(diagnosticSpan.Start);
// Find the enclosing AttributeList
SyntaxNode? attributeList = declaration.Parent;
while (attributeList is not null && !attributeList.IsKind(SyntaxKind.AttributeList))
{
attributeList = attributeList.Parent;
}
// Get the member declaration adjacent to the AttributeList
if (attributeList?.Parent is not MemberDeclarationSyntax parentDeclaration)
{
return;
}
// Register a code action that will invoke the fix.
context.RegisterCodeFix(
CodeAction.Create(
title: Title,
createChangedDocument: c => PutOnSeparateLine(context.Document, parentDeclaration, c),
equivalenceKey: Title),
diagnostic);
}
private static async Task<Document> PutOnSeparateLine(Document document, MemberDeclarationSyntax parentDeclaration, CancellationToken cancellationToken)
{
var attributeLists = new SyntaxList<AttributeListSyntax>();
// put every attribute into it's own attributelist eg.: [A,B,C] => [A][B][C]
foreach (AttributeSyntax attribute in parentDeclaration.AttributeLists.SelectMany(l => l.Attributes))
{
attributeLists = attributeLists.Add(
SyntaxFactory.AttributeList(
SyntaxFactory.SeparatedList(
[SyntaxFactory.Attribute(
attribute.Name,
attribute.ArgumentList)])))
;
}
// the formatter-annotation will wrap every attribute on a separate line
MemberDeclarationSyntax newNode = parentDeclaration.WithAttributeLists(attributeLists)
.WithAdditionalAnnotations(Formatter.Annotation);
// Replace the old local declaration with the new local declaration.
SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("Could not get syntax root");
SyntaxNode newRoot = oldRoot.ReplaceNode(parentDeclaration, newNode);
return document.WithSyntaxRoot(newRoot);
}
}
}