<commit_before>﻿// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddPackage;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
    internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax> 
    {
        private partial class PackageReference
        {
            private class InstallPackageAndAddImportCodeAction : CodeAction
            {
                private readonly string _title;
                private readonly CodeActionPriority _priority;

                public override string Title => _title;
                public override string EquivalenceKey => _title;
                internal override CodeActionPriority Priority => _priority;

                /// <summary>
                /// The document before we added the import. Used so we can roll back if installing
                /// the package failed.
                /// </summary>
                public readonly Document OldDocument;

                /// <summary>
                /// The document after the import has been added.
                /// </summary>
                public readonly Document NewDocument;

                /// <summary>
                /// The operation that will actually install the nuget package.
                /// </summary>
                public readonly InstallPackageDirectlyCodeActionOperation InstallOperation;

                public InstallPackageAndAddImportCodeAction(
                    string title, 
                    CodeActionPriority priority,
                    Document oldDocument, Document newDocument,
                    InstallPackageDirectlyCodeActionOperation installOperation)
                {
                    _title = title;
                    _priority = priority;
                    OldDocument = oldDocument;
                    NewDocument = newDocument;
                    InstallOperation = installOperation;
                }

                /// <summary>
                /// For preview purposes we return all the operations in a list.  This way the 
                /// preview system stiches things together in the UI to make a suitable display.
                /// i.e. if we have a SolutionChangedOperation and some other operation with a 
                /// Title, then the UI will show that nicely to the user.
                /// </summary>
                protected override async Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(CancellationToken cancellationToken)
                {
                    // Make a SolutionChangeAction.  This way we can let it generate the diff
                    // preview appropriately.
                    var solutionChangeAction = new SolutionChangeAction(
                        "", c => Task.FromResult(NewDocument.Project.Solution));

                    var result = ArrayBuilder<CodeActionOperation>.GetInstance();
                    result.AddRange(await solutionChangeAction.GetPreviewOperationsAsync(cancellationToken).ConfigureAwait(false));
                    result.Add(InstallOperation);
                    return result.ToImmutableAndFree();<commit_msg>Make fields private.<commit_after>﻿// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddPackage;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
    internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax> 
    {
        private partial class PackageReference
        {
            private class InstallPackageAndAddImportCodeAction : CodeAction
            {
                private readonly string _title;
                private readonly CodeActionPriority _priority;

                public override string Title => _title;
                public override string EquivalenceKey => _title;
                internal override CodeActionPriority Priority => _priority;

                /// <summary>
                /// The document before we added the import. Used so we can roll back if installing
                /// the package failed.
                /// </summary>
                private readonly Document OldDocument;

                /// <summary>
                /// The document after the import has been added.
                /// </summary>
                private readonly Document NewDocument;

                /// <summary>
                /// The operation that will actually install the nuget package.
                /// </summary>
                private readonly InstallPackageDirectlyCodeActionOperation InstallOperation;

                public InstallPackageAndAddImportCodeAction(
                    string title, 
                    CodeActionPriority priority,
                    Document oldDocument, Document newDocument,
                    InstallPackageDirectlyCodeActionOperation installOperation)
                {
                    _title = title;
                    _priority = priority;
                    OldDocument = oldDocument;
                    NewDocument = newDocument;
                    InstallOperation = installOperation;
                }

                /// <summary>
                /// For preview purposes we return all the operations in a list.  This way the 
                /// preview system stiches things together in the UI to make a suitable display.
                /// i.e. if we have a SolutionChangedOperation and some other operation with a 
                /// Title, then the UI will show that nicely to the user.
                /// </summary>
                protected override async Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(CancellationToken cancellationToken)
                {
                    // Make a SolutionChangeAction.  This way we can let it generate the diff
                    // preview appropriately.
                    var solutionChangeAction = new SolutionChangeAction(
                        "", c => Task.FromResult(NewDocument.Project.Solution));

                    var result = ArrayBuilder<CodeActionOperation>.GetInstance();
                    result.AddRange(await solutionChangeAction.GetPreviewOperationsAsync(cancellationToken).ConfigureAwait(false));
                    result.Add(InstallOperation);
                    return result.ToImmutableAndFree();
{'fraction_non_alphanumeric': 0.0600511073253833, 'fraction_numerical': 0.0005678591709256105, 'mean_word_length': 1.809011164274322, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4745954', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1366, 'n_words': 477}