private async Task<Solution> IntroduceField(Document document, ParameterSyntax paramDecl, CancellationToken cancellationToken) { var rootNode = await document.GetSyntaxRootAsync(); var rewriter = new IntroduceFieldRewriter(paramDecl); rootNode = rewriter.Visit(rootNode); var alterConstructorRewriter = new AlterConstructorRewriter(rewriter.GeneratedField, paramDecl.FirstAncestorOrSelf<ConstructorDeclarationSyntax>(), rewriteParams: false); rootNode = alterConstructorRewriter.Visit(rootNode); rootNode = Formatter.Format(rootNode, document.Project.Solution.Workspace); // Produce a new solution that has all references to that type renamed, including the declaration. return document.WithSyntaxRoot(rootNode).Project.Solution; }
private async Task<Solution> InjectViaConstructor(Document document, FieldDeclarationSyntax fieldDecl, CancellationToken cancellationToken) { var rootNode = await document.GetSyntaxRootAsync(); var constrLocatorVisitor = new ConstructorLocatorVisitor(); var targetConstructor = constrLocatorVisitor.GetTargetConstructor(rootNode); if(targetConstructor == null) { // Need to add new constructor var newConstructorWriter = new AddNewConstructorRewriter(); rootNode = newConstructorWriter.Visit(rootNode); } var rewriter = new AlterConstructorRewriter(fieldDecl, targetConstructor); rootNode = rewriter.Visit(rootNode); rootNode = Formatter.Format(rootNode, document.Project.Solution.Workspace); // Produce a new solution that has all references to that type renamed, including the declaration. return document.WithSyntaxRoot(rootNode).Project.Solution; }