-
Notifications
You must be signed in to change notification settings - Fork 457
/
OverrideMethodsOnComparableTypes.Fixer.cs
127 lines (101 loc) · 5.28 KB
/
OverrideMethodsOnComparableTypes.Fixer.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines
{
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic), Shared]
public sealed class OverrideMethodsOnComparableTypesFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(OverrideMethodsOnComparableTypesAnalyzer.RuleId);
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(context.Document);
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxNode declaration = root.FindNode(context.Span);
declaration = generator.GetDeclaration(declaration);
if (declaration == null)
{
return;
}
SemanticModel model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var typeSymbol = model.GetDeclaredSymbol(declaration) as INamedTypeSymbol;
if (typeSymbol?.TypeKind != TypeKind.Class &&
typeSymbol?.TypeKind != TypeKind.Struct)
{
return;
}
string title = MicrosoftCodeQualityAnalyzersResources.ImplementComparable;
context.RegisterCodeFix(
new MyCodeAction(title,
async ct => await ImplementComparableAsync(context.Document, declaration, typeSymbol, ct).ConfigureAwait(false),
equivalenceKey: title),
context.Diagnostics);
}
private static async Task<Document> ImplementComparableAsync(Document document, SyntaxNode declaration, INamedTypeSymbol typeSymbol, CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var generator = editor.Generator;
if (!typeSymbol.OverridesEquals())
{
var equalsMethod = generator.DefaultEqualsOverrideDeclaration(editor.SemanticModel.Compilation, typeSymbol);
editor.AddMember(declaration, equalsMethod);
}
if (!typeSymbol.OverridesGetHashCode())
{
var getHashCodeMethod = generator.DefaultGetHashCodeOverrideDeclaration(editor.SemanticModel.Compilation);
editor.AddMember(declaration, getHashCodeMethod);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.EqualityOperatorName))
{
var equalityOperator = generator.DefaultOperatorEqualityDeclaration(typeSymbol);
editor.AddMember(declaration, equalityOperator);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.InequalityOperatorName))
{
var inequalityOperator = generator.DefaultOperatorInequalityDeclaration(typeSymbol);
editor.AddMember(declaration, inequalityOperator);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.LessThanOperatorName))
{
var lessThanOperator = generator.DefaultOperatorLessThanDeclaration(typeSymbol);
editor.AddMember(declaration, lessThanOperator);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.LessThanOrEqualOperatorName))
{
var lessThanOrEqualOperator = generator.DefaultOperatorLessThanOrEqualDeclaration(typeSymbol);
editor.AddMember(declaration, lessThanOrEqualOperator);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.GreaterThanOperatorName))
{
var greaterThanOperator = generator.DefaultOperatorGreaterThanDeclaration(typeSymbol);
editor.AddMember(declaration, greaterThanOperator);
}
if (!typeSymbol.ImplementsOperator(WellKnownMemberNames.GreaterThanOrEqualOperatorName))
{
var greaterThanOrEqualOperator = generator.DefaultOperatorGreaterThanOrEqualDeclaration(typeSymbol);
editor.AddMember(declaration, greaterThanOrEqualOperator);
}
return editor.GetChangedDocument();
}
// Needed for Telemetry (https://github.com/dotnet/roslyn-analyzers/issues/192)
private class MyCodeAction : DocumentChangeAction
{
public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument, string equivalenceKey)
: base(title, createChangedDocument, equivalenceKey)
{
}
}
public override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
}
}