AssemblyReferenceCodeAction.cs 3.1 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4

5 6
#nullable enable

7 8 9 10 11 12 13
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Host;
using Roslyn.Utilities;

14
namespace Microsoft.CodeAnalysis.AddImport
15
{
16
    internal abstract partial class AbstractAddImportFeatureService<TSimpleNameSyntax>
17
    {
D
dotnet-bot 已提交
18 19 20 21 22 23
        private class AssemblyReferenceCodeAction : AddImportCodeAction
        {
            public AssemblyReferenceCodeAction(
                Document originalDocument,
                AddImportFixData fixData)
                : base(originalDocument, fixData)
C
CyrusNajmabadi 已提交
24
            {
D
dotnet-bot 已提交
25 26
                Contract.ThrowIfFalse(fixData.Kind == AddImportFixKind.ReferenceAssemblySymbol);
            }
27

28
            private Task<string?> ResolvePathAsync(CancellationToken cancellationToken)
D
dotnet-bot 已提交
29
            {
30
                var assemblyResolverService = OriginalDocument.Project.Solution.Workspace.Services.GetRequiredService<IFrameworkAssemblyPathResolver>();
31

32
                return assemblyResolverService.ResolveAssemblyPathAsync(
D
dotnet-bot 已提交
33 34
                    OriginalDocument.Project.Id,
                    FixData.AssemblyReferenceAssemblyName,
35 36
                    FixData.AssemblyReferenceFullyQualifiedTypeName,
                    cancellationToken);
D
dotnet-bot 已提交
37
            }
38

39 40
            protected override Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(CancellationToken cancellationToken)
                => ComputeOperationsAsync(isPreview: true, cancellationToken);
41

42 43
            protected override Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(CancellationToken cancellationToken)
                => ComputeOperationsAsync(isPreview: false, cancellationToken);
44

45
            private async Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(bool isPreview, CancellationToken cancellationToken)
D
dotnet-bot 已提交
46 47
            {
                var newDocument = await GetUpdatedDocumentAsync(cancellationToken).ConfigureAwait(false);
48
                var newProject = newDocument.Project;
49

D
dotnet-bot 已提交
50
                // Now add the actual assembly reference.
51 52 53 54 55 56 57 58 59 60 61
                if (!isPreview)
                {
                    var resolvedPath = await ResolvePathAsync(cancellationToken).ConfigureAwait(false);
                    if (!string.IsNullOrWhiteSpace(resolvedPath))
                    {
                        var service = OriginalDocument.Project.Solution.Workspace.Services.GetRequiredService<IMetadataService>();
                        var reference = service.GetReference(resolvedPath, MetadataReferenceProperties.Assembly);
                        newProject = newProject.WithMetadataReferences(
                            newProject.MetadataReferences.Concat(reference));
                    }
                }
62

D
dotnet-bot 已提交
63 64
                var operation = new ApplyChangesOperation(newProject.Solution);
                return SpecializedCollections.SingletonEnumerable<CodeActionOperation>(operation);
65
            }
D
dotnet-bot 已提交
66
        }
67
    }
S
Sam Harwell 已提交
68
}