AbstractConvertTupleToStructCodeRefactoringProvider.cs 42.0 KB
Newer Older
C
Cyrus Najmabadi 已提交
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3 4 5 6 7 8 9 10 11 12 13

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Editing;
14
using Microsoft.CodeAnalysis.FindSymbols;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ConvertTupleToStruct
{
    internal abstract partial class AbstractConvertTupleToStructCodeRefactoringProvider<
        TExpressionSyntax,
        TNameSyntax,
        TIdentifierNameSyntax,
31
        TLiteralExpressionSyntax,
32 33
        TObjectCreationExpressionSyntax,
        TTupleExpressionSyntax,
34
        TArgumentSyntax,
35 36 37 38 39 40 41
        TTupleTypeSyntax,
        TTypeBlockSyntax,
        TNamespaceDeclarationSyntax>
        : CodeRefactoringProvider
        where TExpressionSyntax : SyntaxNode
        where TNameSyntax : TExpressionSyntax
        where TIdentifierNameSyntax : TNameSyntax
42
        where TLiteralExpressionSyntax : TExpressionSyntax
43 44
        where TObjectCreationExpressionSyntax : TExpressionSyntax
        where TTupleExpressionSyntax : TExpressionSyntax
45
        where TArgumentSyntax: SyntaxNode
46 47 48 49 50 51 52 53 54 55 56 57 58
        where TTupleTypeSyntax : SyntaxNode
        where TTypeBlockSyntax : SyntaxNode
        where TNamespaceDeclarationSyntax : SyntaxNode
    {
        private enum Scope
        {
            ContainingMember,
            ContainingType,
            ContainingProject,
            DependentProjects
        }

        protected abstract TObjectCreationExpressionSyntax CreateObjectCreationExpression(
59
            TNameSyntax nameNode, SyntaxToken openParen, SeparatedSyntaxList<TArgumentSyntax> arguments, SyntaxToken closeParen);
60

61 62 63 64 65 66 67 68 69 70 71 72 73
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;
            var cancellationToken = context.CancellationToken;

            var (tupleExprOrTypeNode, tupleType) = await TryGetTupleInfoAsync(
                document, context.Span, cancellationToken).ConfigureAwait(false);

            if (tupleExprOrTypeNode == null || tupleType == null)
            {
                return;
            }

C
CyrusNajmabadi 已提交
74
            // Check if the tuple type actually references another anonymous type inside of it.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            // If it does, we can't convert this.  There is no way to describe this anonymous type
            // in the concrete type we create.
            var fields = tupleType.TupleElements;
            var containsAnonymousType = fields.Any(p => p.Type.ContainsAnonymousType());
            if (containsAnonymousType)
            {
                return;
            }

            var capturedTypeParameters =
                fields.Select(p => p.Type)
                      .SelectMany(t => t.GetReferencedTypeParameters())
                      .Distinct()
                      .ToImmutableArray();

            var scopes = ArrayBuilder<CodeAction>.GetInstance();
            scopes.Add(CreateAction(context, Scope.ContainingMember));

C
CyrusNajmabadi 已提交
93 94
            // If we captured any Method type-parameters, we can only replace the tuple types we
            // find in the containing method.  No other tuple types in other members would be able
95 96
            // to reference this type parameter.
            if (!capturedTypeParameters.Any(tp => tp.TypeParameterKind == TypeParameterKind.Method))
97
            {
98 99 100 101 102
                var containingType = tupleExprOrTypeNode.GetAncestor<TTypeBlockSyntax>();
                if (containingType != null)
                {
                    scopes.Add(CreateAction(context, Scope.ContainingType));
                }
103

C
CyrusNajmabadi 已提交
104 105 106
                // If we captured any Type type-parameters, we can only replace the tuple
                // types we find in the containing type.  No other tuple types in other
                // types would be able to reference this type parameter.
107 108 109 110 111 112 113 114 115 116 117 118 119 120
                if (!capturedTypeParameters.Any(tp => tp.TypeParameterKind == TypeParameterKind.Type))
                {
                    // To do a global find/replace of matching tuples, we need to search for documents
                    // containing tuples *and* which have the names of the tuple fields in them.  That means
                    // the tuple field name must exist in the document.
                    //
                    // this means we can only find tuples like ```(x: 1, ...)``` but not ```(1, 2)```.  The
                    // latter has members called Item1 and Item2, but those names don't show up in source.
                    if (fields.All(f => f.CorrespondingTupleField != f))
                    {
                        scopes.Add(CreateAction(context, Scope.ContainingProject));
                        scopes.Add(CreateAction(context, Scope.DependentProjects));
                    }
                }
121
            }
122 123 124 125 126 127 128 129 130 131

            context.RegisterRefactoring(new CodeAction.CodeActionWithNestedActions(
                FeaturesResources.Convert_to_struct,
                scopes.ToImmutableAndFree(),
                isInlinable: false));
        }

        private CodeAction CreateAction(CodeRefactoringContext context, Scope scope)
            => new MyCodeAction(GetTitle(scope), c => ConvertToStructAsync(context.Document, context.Span, scope, c));

C
CyrusNajmabadi 已提交
132
        private static string GetTitle(Scope scope)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        {
            switch (scope)
            {
                case Scope.ContainingMember: return FeaturesResources.and_update_usages_in_containing_member;
                case Scope.ContainingType: return FeaturesResources.and_update_usages_in_containing_type;
                case Scope.ContainingProject: return FeaturesResources.and_update_usages_in_containing_project;
                case Scope.DependentProjects: return FeaturesResources.and_update_usages_in_dependent_projects;
                default:
                    throw ExceptionUtilities.UnexpectedValue(scope);
            }
        }

        private async Task<(SyntaxNode, INamedTypeSymbol)> TryGetTupleInfoAsync(
            Document document, TextSpan span, CancellationToken cancellationToken)
        {
            var position = span.Start;
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var token = root.FindToken(position);

            // Span actually has to be within the token (i.e. not in trivia around it).
            if (!token.Span.IntersectsWith(position))
            {
                return default;
            }

            if (!span.IsEmpty && span != token.Span)
            {
                // if there is a selection, it has to be of the whole token.
                return default;
            }

            var tupleExprNode = token.Parent as TTupleExpressionSyntax;
            var tupleTypeNode = token.Parent as TTupleTypeSyntax;
            if (tupleExprNode == null && tupleTypeNode == null)
            {
                return default;
            }

            var expressionOrType = tupleExprNode ?? (SyntaxNode)tupleTypeNode;

            // The position/selection must be of the open paren for the tuple, or the entire tuple.
            if (expressionOrType.GetFirstToken() != token)
            {
                return default;
            }

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var tupleType = semanticModel.GetTypeInfo(expressionOrType, cancellationToken).Type as INamedTypeSymbol;
            if (tupleType?.IsTupleType != true)
            {
                return default;
            }

            return (expressionOrType, tupleType);
        }

        private async Task<Solution> ConvertToStructAsync(
            Document document, TextSpan span, Scope scope, CancellationToken cancellationToken)
        {
            var (tupleExprOrTypeNode, tupleType) = await TryGetTupleInfoAsync(
                document, span, cancellationToken).ConfigureAwait(false);

            Debug.Assert(tupleExprOrTypeNode != null);
            Debug.Assert(tupleType != null);

            var position = span.Start;
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var container = tupleExprOrTypeNode.GetAncestor<TNamespaceDeclarationSyntax>() ?? root;
            var containingNamespace = container is TNamespaceDeclarationSyntax namespaceDecl
                ? (INamespaceSymbol)semanticModel.GetDeclaredSymbol(namespaceDecl, cancellationToken)
                : semanticModel.Compilation.GlobalNamespace;

            // Generate a unique name for the struct we're creating.  We'll also add a rename
            // annotation so the user can pick the right name for the type afterwards.
            var structName = NameGenerator.GenerateUniqueName(
                "NewStruct", n => semanticModel.LookupSymbols(position, name: n).IsEmpty);

212 213 214 215 216 217
            var capturedTypeParameters =
                tupleType.TupleElements.Select(p => p.Type)
                                       .SelectMany(t => t.GetReferencedTypeParameters())
                                       .Distinct()
                                       .ToImmutableArray();

218
            // Next, generate the full struct that will be used to replace all instances of this
C
CyrusNajmabadi 已提交
219
            // tuple type.
220
            var namedTypeSymbol = await GenerateFinalNamedTypeAsync(
221
                document, scope, structName, capturedTypeParameters, tupleType, cancellationToken).ConfigureAwait(false);
222 223 224 225 226 227 228 229 230 231

            var documentToEditorMap = new Dictionary<Document, SyntaxEditor>();
            var documentsToUpdate = await GetDocumentsToUpdateAsync(
                document, tupleExprOrTypeNode, tupleType, scope, cancellationToken).ConfigureAwait(false);

            // Next, go through and replace all matching tuple expressions and types in the appropriate
            // scope with the new named type we've generated.  
            await ReplaceExpressionAndTypesInScopeAsync(
                documentToEditorMap, documentsToUpdate, 
                tupleExprOrTypeNode, tupleType,
232 233
                structName, capturedTypeParameters,
                containingNamespace, cancellationToken).ConfigureAwait(false);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

            await GenerateStructIntoContainingNamespaceAsync(
                document, tupleExprOrTypeNode, namedTypeSymbol,
                documentToEditorMap, cancellationToken).ConfigureAwait(false);

            var updatedSolution = await ApplyChangesAsync(
                document, documentToEditorMap, cancellationToken).ConfigureAwait(false);

            return updatedSolution;
        }

        private async Task ReplaceExpressionAndTypesInScopeAsync(
            Dictionary<Document, SyntaxEditor> documentToEditorMap, 
            ImmutableArray<DocumentToUpdate> documentsToUpdate,
            SyntaxNode tupleExprOrTypeNode, INamedTypeSymbol tupleType, 
249 250
            string structName, ImmutableArray<ITypeParameterSymbol> typeParameters,
            INamespaceSymbol containingNamespace, CancellationToken cancellationToken)
251 252 253 254 255
        {
            // Process the documents one project at a time.
            foreach (var group in documentsToUpdate.GroupBy(d => d.Document.Project))
            {
                // grab the compilation and keep it around as long as we're processing
C
Cyrus Najmabadi 已提交
256 257 258 259
                // the project so we don't clean things up in the middle.  To do this
                // we use a GC.KeepAlive below so that we can mark that this compilation
                // should stay around (even though we don't reference is directly in 
                // any other way here).
260 261 262 263 264 265 266 267 268 269 270 271
                var project = group.Key;
                var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

                var generator = project.LanguageServices.GetService<SyntaxGenerator>();

                // Get the fully qualified name for the new type we're creating.  We'll use this
                // at replacement points so that we can find the right type even if we're in a 
                // different namespace.

                // If the struct is being injected into the global namespace, then reference it with
                // "global::NewStruct",  Otherwise, get the full name to the namespace, and append
                // the NewStruct name to it.
272 273 274
                var structNameNode = CreateStructNameNode(
                    generator, structName, typeParameters, addRenameAnnotation: false);

275 276 277 278 279 280 281 282 283 284 285 286 287
                var fullTypeName = containingNamespace.IsGlobalNamespace
                    ? (TNameSyntax)generator.GlobalAliasedName(structNameNode)
                    : (TNameSyntax)generator.QualifiedName(generator.NameExpression(containingNamespace), structNameNode);

                fullTypeName = fullTypeName.WithAdditionalAnnotations(Simplifier.Annotation)
                                           .WithAdditionalAnnotations(DoNotAllowVarAnnotation.Annotation);

                foreach (var documentToUpdate in group)
                {
                    var document = documentToUpdate.Document;
                    var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
                    var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

C
Cyrus Najmabadi 已提交
288 289
                    // We should only ever get a default array (meaning, update the root), or a
                    // non-empty array.  We should never be asked to update exactly '0' nodes.
C
Cyrus Najmabadi 已提交
290 291
                    Debug.Assert(documentToUpdate.NodesToUpdate.IsDefault ||
                                 documentToUpdate.NodesToUpdate.Length >= 1);
C
Cyrus Najmabadi 已提交
292

293 294
                    // If we were given specific nodes to update, only update those.  Otherwise
                    // updated everything from the root down.
C
Cyrus Najmabadi 已提交
295
                    var nodesToUpdate = documentToUpdate.NodesToUpdate.IsDefault
296 297 298 299 300 301 302 303 304 305 306
                        ? ImmutableArray.Create(syntaxRoot)
                        : documentToUpdate.NodesToUpdate;

                    var editor = new SyntaxEditor(syntaxRoot, generator);

                    var replaced = false;

                    foreach (var container in nodesToUpdate)
                    {
                        replaced |= await ReplaceTupleExpressionsAndTypesInDocumentAsync(
                            document, editor, tupleExprOrTypeNode, tupleType,
307 308
                            fullTypeName, structName, typeParameters,
                            container, cancellationToken).ConfigureAwait(false);
309 310 311 312 313 314 315 316 317 318 319 320 321 322
                    }

                    if (replaced)
                    {
                        // We made a replacement.  Keep track of this so we can update our solution
                        // later.
                        documentToEditorMap.Add(document, editor);
                    }
                }

                GC.KeepAlive(compilation);
            }
        }

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        private static TNameSyntax CreateStructNameNode(
            SyntaxGenerator generator, string structName,
            ImmutableArray<ITypeParameterSymbol> typeParameters, bool addRenameAnnotation)
        {
            var structNameToken = generator.Identifier(structName);
            if (addRenameAnnotation)
            {
                structNameToken = structNameToken.WithAdditionalAnnotations(RenameAnnotation.Create());
            }

            return typeParameters.Length == 0
                ? (TNameSyntax)generator.IdentifierName(structNameToken)
                : (TNameSyntax)generator.GenericName(structNameToken, typeParameters.Select(tp => generator.IdentifierName(tp.Name)));
        }

C
Cyrus Najmabadi 已提交
338
        private static async Task<ImmutableArray<DocumentToUpdate>> GetDocumentsToUpdateAsync(
339 340 341 342 343 344 345 346
            Document document, SyntaxNode tupleExprOrTypeNode,
            INamedTypeSymbol tupleType, Scope scope, CancellationToken cancellationToken)
        {
            switch (scope)
            {
                case Scope.ContainingMember:
                    return GetDocumentsToUpdateForContainingMember(document, tupleExprOrTypeNode);
                case Scope.ContainingType:
C
Cyrus Najmabadi 已提交
347 348
                    return await GetDocumentsToUpdateForContainingTypeAsync(
                        document, tupleExprOrTypeNode, cancellationToken).ConfigureAwait(false);
349
                case Scope.ContainingProject:
350 351
                    return await GetDocumentsToUpdateForContainingProjectAsync(
                        document.Project, tupleType, cancellationToken).ConfigureAwait(false);
352
                case Scope.DependentProjects:
353 354
                    return await GetDocumentsToUpdateForDependentProjectAsync(
                        document.Project, tupleType, cancellationToken).ConfigureAwait(false);
355
                default:
C
Cyrus Najmabadi 已提交
356
                    throw ExceptionUtilities.UnexpectedValue(scope);
357 358 359
            }
        }

C
Cyrus Najmabadi 已提交
360
        private static async Task<ImmutableArray<DocumentToUpdate>> GetDocumentsToUpdateForDependentProjectAsync(
361 362 363 364
            Project startingProject, INamedTypeSymbol tupleType, CancellationToken cancellationToken)
        {
            var solution = startingProject.Solution;
            var graph = solution.GetProjectDependencyGraph();
C
Cyrus Najmabadi 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

            // Note: there are a couple of approaches we can take here.  Processing 'direct'
            // dependencies, or processing 'transitive' dependencies.  Both have pros/cons:
            //
            // Direct Dependencies:
            //  Pros:
            //      All updated projects are able to see the newly added type.
            //      Transitive deps won't be updated to use a type they can't actually use.
            //  Cons:
            //      If that project then exports that new type, then transitive deps will
            //      break if they use those exported APIs since they won't know about the
            //      type.
            //
            // Transitive Dependencies:
            //  Pros:
            //      All affected code is updated.
            //  Cons: 
            //      Non-direct deps will not compile unless the take a reference on the
            //      starting project.

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            var dependentProjects = graph.GetProjectsThatDirectlyDependOnThisProject(startingProject.Id);
            var allProjects = dependentProjects.Select(solution.GetProject).Concat(startingProject).ToSet();

            var result = ArrayBuilder<DocumentToUpdate>.GetInstance();
            var tupleFieldNames = tupleType.TupleElements.SelectAsArray(f => f.Name);

            foreach (var project in allProjects)
            {
                await AddDocumentsToUpdateForProjectAsync(
                    project, result, tupleFieldNames, cancellationToken).ConfigureAwait(false);
            }

            return result.ToImmutableAndFree();
        }

C
Cyrus Najmabadi 已提交
400
        private static async Task<ImmutableArray<DocumentToUpdate>> GetDocumentsToUpdateForContainingProjectAsync(
401 402 403 404
            Project project, INamedTypeSymbol tupleType, CancellationToken cancellationToken)
        {
            var result = ArrayBuilder<DocumentToUpdate>.GetInstance();
            var tupleFieldNames = tupleType.TupleElements.SelectAsArray(f => f.Name);
405 406 407 408 409 410 411

            await AddDocumentsToUpdateForProjectAsync(
                project, result, tupleFieldNames, cancellationToken).ConfigureAwait(false);

            return result.ToImmutableAndFree();
        }

C
Cyrus Najmabadi 已提交
412
        private static async Task AddDocumentsToUpdateForProjectAsync(Project project, ArrayBuilder<DocumentToUpdate> result, ImmutableArray<string> tupleFieldNames, CancellationToken cancellationToken)
413
        {
414 415 416 417 418 419 420 421 422 423 424 425
            foreach (var document in project.Documents)
            {
                var info = await document.GetSyntaxTreeIndexAsync(cancellationToken).ConfigureAwait(false);
                if (info.ContainsTupleExpressionOrTupleType &&
                    InfoProbablyContainsTupleFieldNames(info, tupleFieldNames))
                {
                    // Use 'default' for nodesToUpdate so we walk the entire document
                    result.Add(new DocumentToUpdate(document, nodesToUpdate: default));
                }
            }
        }

C
Cyrus Najmabadi 已提交
426
        private static bool InfoProbablyContainsTupleFieldNames(SyntaxTreeIndex info, ImmutableArray<string> tupleFieldNames)
427 428 429 430 431 432 433 434 435 436 437 438
        {
            foreach (var name in tupleFieldNames)
            {
                if (!info.ProbablyContainsIdentifier(name))
                {
                    return false;
                }
            }

            return true;
        }

C
Cyrus Najmabadi 已提交
439
        private static async Task<ImmutableArray<DocumentToUpdate>> GetDocumentsToUpdateForContainingTypeAsync(
440 441 442 443 444 445 446 447 448 449 450 451
            Document startingDocument, SyntaxNode tupleExprOrTypeNode, CancellationToken cancellationToken)
        {
            var containingType = tupleExprOrTypeNode.GetAncestor<TTypeBlockSyntax>();
            Debug.Assert(containingType != null,
                "We should always get a containing scope since we already checked for that to support Scope.ContainingType.");

            var solution = startingDocument.Project.Solution;
            var semanticModel = await startingDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var typeSymbol = (INamedTypeSymbol)semanticModel.GetDeclaredSymbol(containingType, cancellationToken);

            var result = ArrayBuilder<DocumentToUpdate>.GetInstance();

C
Cyrus Najmabadi 已提交
452 453
            var declarationService = startingDocument.GetLanguageService<ISymbolDeclarationService>();
            foreach (var group in declarationService.GetDeclarations(typeSymbol).GroupBy(r => r.SyntaxTree))
454 455 456 457 458 459 460 461 462 463
            {
                var document = solution.GetDocument(group.Key);
                var nodes = group.SelectAsArray(r => r.GetSyntax(cancellationToken));

                result.Add(new DocumentToUpdate(document, nodes));
            }

            return result.ToImmutableAndFree();
        }

C
Cyrus Najmabadi 已提交
464
        private static ImmutableArray<DocumentToUpdate> GetDocumentsToUpdateForContainingMember(
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
            Document document, SyntaxNode tupleExprOrTypeNode)
        {
            var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
            var containingMember = tupleExprOrTypeNode.FirstAncestorOrSelf<SyntaxNode>(syntaxFacts.IsMethodLevelMember) ?? tupleExprOrTypeNode;

            return ImmutableArray.Create(new DocumentToUpdate(
                document, ImmutableArray.Create(containingMember)));
        }

        private static async Task GenerateStructIntoContainingNamespaceAsync(
            Document document, SyntaxNode tupleExprOrTypeNode, INamedTypeSymbol namedTypeSymbol, 
            Dictionary<Document, SyntaxEditor> documentToEditorMap, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            // If we don't already have an editor for the containing document, then make one.
            if (!documentToEditorMap.TryGetValue(document, out var editor))
            {
                var generator = SyntaxGenerator.GetGenerator(document);
                editor = new SyntaxEditor(root, generator);

                documentToEditorMap.Add(document, editor);
            }

            var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
            var container = tupleExprOrTypeNode.GetAncestor<TNamespaceDeclarationSyntax>() ?? root;

            // Then, actually insert the new class in the appropriate container.
            editor.ReplaceNode(container, (currentContainer, _) =>
            {
                var codeGenService = document.GetLanguageService<ICodeGenerationService>();
                var options = new CodeGenerationOptions(
                    generateMembers: true,
                    sortMembers: false,
                    autoInsertionLocation: false);

                return codeGenService.AddNamedType(
                    currentContainer, namedTypeSymbol, options, cancellationToken);
            });
        }

C
Cyrus Najmabadi 已提交
506
        private static async Task<Solution> ApplyChangesAsync(
507 508 509 510 511 512
            Document startingDocument, Dictionary<Document, SyntaxEditor> documentToEditorMap, CancellationToken cancellationToken)
        {
            var currentSolution = startingDocument.Project.Solution;

            foreach (var (currentDoc, editor) in documentToEditorMap)
            {
513 514 515 516
                var docId = currentDoc.Id;
                var newRoot = editor.GetChangedRoot();
                var updatedDocument = currentSolution.WithDocumentSyntaxRoot(docId, newRoot, PreservationMode.PreserveIdentity)
                                                     .GetDocument(docId);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

                if (currentDoc == startingDocument)
                {
                    // If this is the starting document, format using the equals+getHashCode service
                    // so that our generated methods follow any special formatting rules specific to
                    // them.
                    var equalsAndGetHashCodeService = startingDocument.GetLanguageService<IGenerateEqualsAndGetHashCodeService>();
                    updatedDocument = await equalsAndGetHashCodeService.FormatDocumentAsync(
                        updatedDocument, cancellationToken).ConfigureAwait(false);
                }

                currentSolution = updatedDocument.Project.Solution;
            }

            return currentSolution;
        }

        private async Task<bool> ReplaceTupleExpressionsAndTypesInDocumentAsync(
535 536 537
            Document document, SyntaxEditor editor, SyntaxNode startingNode, 
            INamedTypeSymbol tupleType, TNameSyntax fullyQualifiedStructName, 
            string structName, ImmutableArray<ITypeParameterSymbol> typeParameters,
538 539 540 541 542
            SyntaxNode containerToUpdate, CancellationToken cancellationToken)
        {
            var changed = false;
            changed |= await ReplaceMatchingTupleExpressionsAsync(
                document, editor, startingNode, tupleType,
543
                fullyQualifiedStructName, structName, typeParameters,
544 545 546 547
                containerToUpdate, cancellationToken).ConfigureAwait(false);

            changed |= await ReplaceMatchingTupleTypesAsync(
                document, editor, startingNode, tupleType,
548
                fullyQualifiedStructName, structName, typeParameters,
549 550 551 552 553 554
                containerToUpdate, cancellationToken).ConfigureAwait(false);

            return changed;
        }

        private async Task<bool> ReplaceMatchingTupleExpressionsAsync(
555 556 557 558
            Document document, SyntaxEditor editor, SyntaxNode startingNode, 
            INamedTypeSymbol tupleType, TNameSyntax qualifiedTypeName, 
            string typeName, ImmutableArray<ITypeParameterSymbol> typeParameters,
            SyntaxNode containingMember, CancellationToken cancellationToken)
559
        {
560 561
            var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
            var comparer = syntaxFacts.StringComparer;
562 563 564 565 566 567 568 569
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var childCreationNodes = containingMember.DescendantNodesAndSelf()
                                                     .OfType<TTupleExpressionSyntax>();

            var changed = false;
            foreach (var childCreation in childCreationNodes)
            {
570
                var childType = semanticModel.GetTypeInfo(childCreation, cancellationToken).Type as INamedTypeSymbol;
571 572
                if (childType == null)
                {
C
CyrusNajmabadi 已提交
573
                    Debug.Fail("We should always be able to get an tuple type for any tuple expression node.");
574 575 576
                    continue;
                }

577
                if (AreEquivalent(comparer, tupleType, childType))
578 579 580
                {
                    changed = true;
                    ReplaceWithObjectCreation(
581
                        syntaxFacts, editor, typeName, typeParameters, 
582
                        qualifiedTypeName, startingNode, childCreation);
583 584 585 586 587 588
                }
            }

            return changed;
        }

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        private static bool AreEquivalent(StringComparer comparer, INamedTypeSymbol tupleType, INamedTypeSymbol childType)
            => SymbolEquivalenceComparer.Instance.Equals(tupleType, childType) &&
               NamesMatch(comparer, tupleType.TupleElements, childType.TupleElements);

        private static bool NamesMatch(
            StringComparer comparer, ImmutableArray<IFieldSymbol> fields1, ImmutableArray<IFieldSymbol> fields2)
        {
            if (fields1.Length != fields2.Length)
            {
                return false;
            }

            for (var i = 0; i < fields1.Length; i++)
            {
                if (!comparer.Equals(fields1[i].Name, fields2[i].Name))
                {
                    return false;
                }
            }

            return true;
        }

612
        private void ReplaceWithObjectCreation(
613
            ISyntaxFactsService syntaxFacts, SyntaxEditor editor, string typeName, ImmutableArray<ITypeParameterSymbol> typeParameters,
614
            TNameSyntax qualifiedTypeName, SyntaxNode startingCreationNode, TTupleExpressionSyntax childCreation)
615
        {
C
CyrusNajmabadi 已提交
616
            // Use the callback form as tuples types may be nested, and we want to
617 618 619 620 621 622 623 624 625
            // properly replace them even in that case.
            editor.ReplaceNode(
                childCreation,
                (currentNode, g) =>
                {
                    var currentTupleExpr = (TTupleExpressionSyntax)currentNode;

                    // If we hit the node the user started on, then add the rename annotation here.
                    var typeNameNode = startingCreationNode == childCreation
626
                        ? CreateStructNameNode(g, typeName, typeParameters, addRenameAnnotation: true)
627 628
                        : qualifiedTypeName;

629 630 631 632 633
                    syntaxFacts.GetPartsOfTupleExpression<TArgumentSyntax>(
                        currentTupleExpr, out var openParen, out var arguments, out var closeParen);
                    arguments = ConvertArguments(syntaxFacts, g, arguments);

                    return CreateObjectCreationExpression(typeNameNode, openParen, arguments, closeParen)
634 635 636 637
                        .WithAdditionalAnnotations(Formatter.Annotation);
                });
        }

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
        private SeparatedSyntaxList<TArgumentSyntax> ConvertArguments(ISyntaxFactsService syntaxFacts, SyntaxGenerator generator, SeparatedSyntaxList<TArgumentSyntax> arguments)
            => generator.SeparatedList<TArgumentSyntax>(ConvertArguments(syntaxFacts, generator, arguments.GetWithSeparators()));

        private SyntaxNodeOrTokenList ConvertArguments(ISyntaxFactsService syntaxFacts, SyntaxGenerator generator, SyntaxNodeOrTokenList list)
            => new SyntaxNodeOrTokenList(list.Select(v => ConvertArgumentOrToken(syntaxFacts, generator, v)));

        private SyntaxNodeOrToken ConvertArgumentOrToken(ISyntaxFactsService syntaxFacts, SyntaxGenerator generator, SyntaxNodeOrToken arg)
            => arg.IsToken
                ? arg
                : ConvertArgument(syntaxFacts, generator, (TArgumentSyntax)arg.AsNode());

        private TArgumentSyntax ConvertArgument(
            ISyntaxFactsService syntaxFacts, SyntaxGenerator generator, TArgumentSyntax argument)
        {
            // Keep named arguments for literal args.  It helps keep the code self-documenting.
            // Remove for complex args as it's most likely just clutter a person doesn't need
            // when instantiating their new type.
            var expr = syntaxFacts.GetExpressionOfArgument(argument);
            if (expr is TLiteralExpressionSyntax)
            {
                return argument;
            }

            return (TArgumentSyntax)generator.Argument(expr).WithTriviaFrom(argument);
        }

664
        private async Task<bool> ReplaceMatchingTupleTypesAsync(
665 666 667
            Document document, SyntaxEditor editor, SyntaxNode startingNode, 
            INamedTypeSymbol tupleType, TNameSyntax qualifiedTypeName, 
            string typeName, ImmutableArray<ITypeParameterSymbol> typeParameters,
668 669
            SyntaxNode containingMember, CancellationToken cancellationToken)
        {
670
            var comparer = document.GetLanguageService<ISyntaxFactsService>().StringComparer;
671 672 673 674 675 676 677 678
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var childTupleNodes = containingMember.DescendantNodesAndSelf()
                                                  .OfType<TTupleTypeSyntax>();

            var changed = false;
            foreach (var childTupleType in childTupleNodes)
            {
679
                var childType = semanticModel.GetTypeInfo(childTupleType, cancellationToken).Type as INamedTypeSymbol;
680 681
                if (childType == null)
                {
C
CyrusNajmabadi 已提交
682
                    Debug.Fail("We should always be able to get an tuple type for any tuple type syntax node.");
683 684 685
                    continue;
                }

686
                if (AreEquivalent(comparer, tupleType, childType))
687 688 689
                {
                    changed = true;
                    ReplaceWithTypeNode(
690
                        editor, typeName, typeParameters, qualifiedTypeName, startingNode, childTupleType);
691 692 693 694 695 696 697
                }
            }

            return changed;
        }

        private void ReplaceWithTypeNode(
698 699
            SyntaxEditor editor, string typeName, ImmutableArray<ITypeParameterSymbol> typeParameters,
            TNameSyntax qualifiedTypeName, SyntaxNode startingNode, TTupleTypeSyntax childTupleType)
700
        {
C
CyrusNajmabadi 已提交
701
            // Use the callback form as tuple types may be nested, and we want to
702 703 704 705 706 707 708
            // properly replace them even in that case.
            editor.ReplaceNode(
                childTupleType,
                (currentNode, g) =>
                {
                    // If we hit the node the user started on, then add the rename annotation here.
                    var typeNameNode = startingNode == childTupleType
709
                        ? CreateStructNameNode(g, typeName, typeParameters, addRenameAnnotation: true)
710 711 712 713 714 715 716
                        : qualifiedTypeName;

                    return typeNameNode.WithTriviaFrom(currentNode);
                });
        }

        private static async Task<INamedTypeSymbol> GenerateFinalNamedTypeAsync(
717
            Document document, Scope scope, string structName, 
718
            ImmutableArray<ITypeParameterSymbol> typeParameters,
719
            INamedTypeSymbol tupleType, CancellationToken cancellationToken)
720 721 722 723 724 725 726 727 728 729 730 731
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var compilation = semanticModel.Compilation;

            var fields = tupleType.TupleElements;

            // Now try to generate all the members that will go in the new class. This is a bit
            // circular.  In order to generate some of the members, we need to know about the type.
            // But in order to create the type, we need the members.  To address this we do two
            // passes. First, we create an empty version of the class.  This can then be used to
            // help create members like Equals/GetHashCode.  Then, once we have all the members we
            // create the final type.
732
            var namedTypeWithoutMembers = CreateNamedType(
733
                scope, structName, typeParameters, members: default);
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

            var generator = SyntaxGenerator.GetGenerator(document);
            var constructor = CreateConstructor(compilation, structName, fields, generator);

            // Generate Equals/GetHashCode.  We can defer to our existing language service for this
            // so that we generate the same Equals/GetHashCode that our other IDE features generate.
            var equalsAndGetHashCodeService = document.GetLanguageService<IGenerateEqualsAndGetHashCodeService>();
                        
            var equalsMethod = await equalsAndGetHashCodeService.GenerateEqualsMethodAsync(
                document, namedTypeWithoutMembers, ImmutableArray<ISymbol>.CastUp(fields), 
                localNameOpt: ICodeDefinitionFactoryExtensions.OtherName, cancellationToken).ConfigureAwait(false);
            var getHashCodeMethod = await equalsAndGetHashCodeService.GenerateGetHashCodeMethodAsync(
                document, namedTypeWithoutMembers,
                ImmutableArray<ISymbol>.CastUp(fields), cancellationToken).ConfigureAwait(false);

            var members = ArrayBuilder<ISymbol>.GetInstance();
            members.AddRange(fields);
            members.Add(constructor);
            members.Add(equalsMethod);
            members.Add(getHashCodeMethod);
            members.Add(GenerateDeconstructMethod(semanticModel, generator, tupleType, constructor));
            AddConversions(generator, members, tupleType, namedTypeWithoutMembers);

757
            var namedTypeSymbol = CreateNamedType(scope, structName, typeParameters, members.ToImmutableAndFree());
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
            return namedTypeSymbol;
        }

        private static IMethodSymbol GenerateDeconstructMethod(
            SemanticModel model, SyntaxGenerator generator, 
            INamedTypeSymbol tupleType, IMethodSymbol constructor)
        {
            var assignments = tupleType.TupleElements.Select(
                (field, index) => generator.ExpressionStatement(
                    generator.AssignmentStatement(
                        generator.IdentifierName(constructor.Parameters[index].Name),
                        generator.MemberAccessExpression(
                            generator.ThisExpression(),
                            field.Name)))).ToImmutableArray();

            return CodeGenerationSymbolFactory.CreateMethodSymbol(
                attributes: default,
                Accessibility.Public,
                modifiers: default,
                model.Compilation.GetSpecialType(SpecialType.System_Void),
                RefKind.None,
                explicitInterfaceImplementations: default,
                WellKnownMemberNames.DeconstructMethodName,
                typeParameters: default,
                constructor.Parameters.SelectAsArray(p => 
                    CodeGenerationSymbolFactory.CreateParameterSymbol(RefKind.Out, p.Type, p.Name)),
                assignments);
        }

        private static void AddConversions(
            SyntaxGenerator generator, ArrayBuilder<ISymbol> members,
            INamedTypeSymbol tupleType, INamedTypeSymbol structType)
        {
            const string valueName = "value";

            var valueNode = generator.IdentifierName(valueName);
            var arguments = tupleType.TupleElements.SelectAsArray(
                field => generator.Argument(
                    generator.MemberAccessExpression(valueNode, field.Name)));

            var convertToTupleStatement = generator.ReturnStatement(
                generator.TupleExpression(arguments));

            var convertToStructStatement = generator.ReturnStatement(
                generator.ObjectCreationExpression(structType, arguments));

            members.Add(CodeGenerationSymbolFactory.CreateConversionSymbol(
                attributes: default,
                Accessibility.Public,
                DeclarationModifiers.Static,
                tupleType,
                CodeGenerationSymbolFactory.CreateParameterSymbol(structType, valueName),
                isImplicit: true,
                ImmutableArray.Create(convertToTupleStatement)));
            members.Add(CodeGenerationSymbolFactory.CreateConversionSymbol(
                attributes: default,
                Accessibility.Public,
                DeclarationModifiers.Static,
                structType,
                CodeGenerationSymbolFactory.CreateParameterSymbol(tupleType, valueName),
                isImplicit: true,
                ImmutableArray.Create(convertToStructStatement)));
        }

        private static INamedTypeSymbol CreateNamedType(
823 824
            Scope scope, string structName, 
            ImmutableArray<ITypeParameterSymbol> typeParameters, ImmutableArray<ISymbol> members)
825
        {
826 827 828
            var accessibility = scope == Scope.DependentProjects
                ? Accessibility.Public
                : Accessibility.Internal;
829
            return CodeGenerationSymbolFactory.CreateNamedTypeSymbol(
830
                attributes: default, accessibility, modifiers: default, 
831
                TypeKind.Struct, structName, typeParameters, members: members);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
        }

        private static IMethodSymbol CreateConstructor(
            Compilation compilation, string className,
            ImmutableArray<IFieldSymbol> fields, SyntaxGenerator generator)
        {
            // For every property, create a corresponding parameter, as well as an assignment
            // statement from that parameter to the property.
            var parameterToPropMap = new Dictionary<string, ISymbol>();
            var parameters = fields.SelectAsArray(field =>
            {
                var parameter = CodeGenerationSymbolFactory.CreateParameterSymbol(
                    field.Type, field.Name.ToCamelCase(trimLeadingTypePrefix: false));

                parameterToPropMap[parameter.Name] = field;

                return parameter;
            });

            var assignmentStatements = generator.CreateAssignmentStatements(
                compilation, parameters, parameterToPropMap, ImmutableDictionary<string, string>.Empty,
                addNullChecks: false, preferThrowExpression: false);

            var constructor = CodeGenerationSymbolFactory.CreateConstructorSymbol(
                attributes: default, Accessibility.Public, modifiers: default,
                className, parameters, assignmentStatements);

            return constructor;
        }

        private class MyCodeAction : CodeAction.SolutionChangeAction
        {
            public MyCodeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution)
                : base(title, createChangedSolution)
            {
            }
        }
    }
}