private SyntaxNode ReplaceAnonymousWithLocalFunction( Workspace workspace, SyntaxNode currentRoot, LocalDeclarationStatementSyntax localDeclaration, LambdaExpressionSyntax lambda, INamedTypeSymbol delegateType, ParameterListSyntax parameterList, ImmutableArray <MemberAccessExpressionSyntax> explicitInvokeCalls, CancellationToken cancellationToken) { var newLocalFunctionStatement = CreateLocalFunctionStatement( localDeclaration, lambda, delegateType, parameterList, cancellationToken); newLocalFunctionStatement = newLocalFunctionStatement.WithTriviaFrom(localDeclaration) .WithAdditionalAnnotations(Formatter.Annotation); var editor = new SyntaxEditor(currentRoot, workspace); editor.ReplaceNode(localDeclaration, newLocalFunctionStatement); var lambdaStatement = lambda.GetAncestor <StatementSyntax>(); if (lambdaStatement != localDeclaration) { // This is the split decl+init form. Remove the second statement as we're // merging into the first one. editor.RemoveNode(lambdaStatement); } foreach (var usage in explicitInvokeCalls) { editor.ReplaceNode( usage.Parent, (usage.Parent as InvocationExpressionSyntax).WithExpression(usage.Expression).WithTriviaFrom(usage.Parent)); } return(editor.GetChangedRoot()); }