Exemple #1
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ClassDeclarationSyntax classDeclaration,
            IMethodSymbol[] constructorSymbols,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxList <MemberDeclarationSyntax> members = classDeclaration.Members;

            string name = classDeclaration.Identifier.ValueText;

            int insertIndex = Inserter.GetMemberInsertIndex(members, SyntaxKind.ConstructorDeclaration);

            int position = (insertIndex == 0)
                ? classDeclaration.OpenBraceToken.FullSpan.End
                : members[insertIndex - 1].FullSpan.End;

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            IEnumerable <ConstructorDeclarationSyntax> constructors = constructorSymbols
                                                                      .Select(symbol => CreateConstructor(symbol, name, semanticModel, position));

            ClassDeclarationSyntax newClassDeclaration = classDeclaration
                                                         .WithMembers(members.InsertRange(insertIndex, constructors));

            return(await document.ReplaceNodeAsync(classDeclaration, newClassDeclaration, cancellationToken).ConfigureAwait(false));
        }