PreferFrameworkTypeCodeFixProvider.cs 3.1 KB
Newer Older
1 2
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

B
Balaji Krishnan 已提交
3
using System;
4
using System.Collections.Immutable;
5
using System.Composition;
6 7 8
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
9
using Microsoft.CodeAnalysis.CodeFixes;
10 11 12
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
13
using Roslyn.Utilities;
14

15
namespace Microsoft.CodeAnalysis.PreferFrameworkType
16
{
17 18
    [ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic,
        Name = PredefinedCodeFixProviderNames.PreferFrameworkType), Shared]
19
    internal class PreferFrameworkTypeCodeFixProvider : SyntaxEditorBasedCodeFixProvider
20
    {
C
Cyrus Najmabadi 已提交
21
        public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
22
            IDEDiagnosticIds.PreferBuiltInOrFrameworkTypeDiagnosticId);
23

24
        public override Task RegisterCodeFixesAsync(CodeFixContext context)
25
        {
26
            var diagnostic = context.Diagnostics[0];
27 28 29 30 31 32 33
            if (diagnostic.Properties.ContainsKey(PreferFrameworkTypeConstants.PreferFrameworkType))
            {
                context.RegisterCodeFix(
                    new PreferFrameworkTypeCodeAction(
                        c => this.FixAsync(context.Document, diagnostic, c)),
                    context.Diagnostics);
            }
34

35
            return Task.CompletedTask;
36 37
        }

38 39 40
        protected override async Task FixAllAsync(
            Document document, ImmutableArray<Diagnostic> diagnostics, 
            SyntaxEditor editor, CancellationToken cancellationToken)
41
        {
42
            var generator = document.GetLanguageService<SyntaxGenerator>();
43
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
44

45 46 47 48 49 50 51 52 53 54 55 56
            foreach (var diagnostic in diagnostics)
            {
                var node = diagnostic.Location.FindNode(
                    findInsideTrivia: true, getInnermostNodeForTie: true, cancellationToken);

                var typeSymbol = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol as ITypeSymbol;
                if (typeSymbol != null)
                {
                    var replacementNode = generator.TypeExpression(typeSymbol).WithTriviaFrom(node);
                    editor.ReplaceNode(node, replacementNode);
                }
            }
57 58
        }

59
        protected override bool IncludeDiagnosticDuringFixAll(FixAllState state, Diagnostic diagnostic)
60
            => diagnostic.Properties.ContainsKey(PreferFrameworkTypeConstants.PreferFrameworkType);
61

B
Balaji Krishnan 已提交
62
        private class PreferFrameworkTypeCodeAction : CodeAction.DocumentChangeAction
63
        {
64
            public PreferFrameworkTypeCodeAction(
65 66
                Func<CancellationToken, Task<Document>> createChangedDocument)
                : base(FeaturesResources.Use_framework_type, createChangedDocument, FeaturesResources.Use_framework_type)
B
Balaji Krishnan 已提交
67 68
            {
            }
69 70 71
        }
    }
}