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
|
// 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 Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using ICSharpCode.NRefactory6.CSharp.Features.ImplementAbstractClass;
using MonoDevelop.Core;
using ICSharpCode.NRefactory6.CSharp.Refactoring;
using ICSharpCode.NRefactory6.CSharp;
namespace MonoDevelop.CSharp.CodeFixes.ImplementAbstractClass
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ImplementAbstractClass), Shared]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateType)]
internal class ImplementAbstractClassCodeFixProvider : CodeFixProvider
{
private const string CS0534 = "CS0534"; // 'Program' does not implement inherited abstract member 'Foo.bar()'
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(CS0534); }
}
public sealed override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var token = root.FindToken(context.Span.Start);
if (!token.Span.IntersectsWith(context.Span))
{
return;
}
var classNode = token.Parent as ClassDeclarationSyntax;
if (classNode == null)
{
return;
}
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
if (model.IsFromGeneratedCode (context.CancellationToken))
return;
foreach (var baseTypeSyntax in classNode.BaseList.Types)
{
var node = baseTypeSyntax.Type;
if (service.CanImplementAbstractClass(
context.Document,
model,
node,
context.CancellationToken))
{
var title = GettextCatalog.GetString ("Implement Abstract Class");
var abstractType = model.GetTypeInfo(node, context.CancellationToken).Type;
var id = GetCodeActionId(abstractType.ContainingAssembly.Name, abstractType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
context.RegisterCodeFix(
new DocumentChangeAction(node.Span, DiagnosticSeverity.Error, title,
(c) => ImplementAbstractClassAsync(context.Document, node, c)),
context.Diagnostics);
return;
}
}
}
// internal for testing purposes.
internal static string GetCodeActionId(string assemblyName, string abstractTypeFullyQualifiedName)
{
return "ImplementAbstractClass;" +
assemblyName + ";" +
abstractTypeFullyQualifiedName;
}
static CSharpImplementAbstractClassService service = new CSharpImplementAbstractClassService ();
private async Task<Document> ImplementAbstractClassAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)
{
return await service.ImplementAbstractClassAsync(
document,
await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false),
node,
cancellationToken).ConfigureAwait(false);
}
}
}
|