private async Task <Document> ConvertVariableAssignmentToLINQ(Document document, VariableDeclaratorSyntax variableDeclarator, ForEachStatementSyntax foreachStatement, CancellationToken c) { var generator = SyntaxGenerator.GetGenerator(document); var oldVariableName = foreachStatement.Identifier.Text; var newVariableName = variableDeclarator.Identifier.Text; var oldVariables = variableDeclarator.Initializer.Value.DescendantNodesAndSelf().OfType <IdentifierNameSyntax>().Where(x => x.Identifier.Text == oldVariableName); var selectCall = generator.InvocationExpression( generator.MemberAccessExpression(foreachStatement.Expression, "Select"), generator.Argument( generator.ValueReturningLambdaExpression( new[] { generator.LambdaParameter("x") }, variableDeclarator.Initializer.Value.ReplaceNodes(oldVariables, (x, y) => generator.IdentifierName("x"))))); var root = await document.GetSyntaxRootAsync(c); Debug.WriteLine(variableDeclarator.Parent); var restOfForeachBody = foreachStatement.Statement .RemoveNode(variableDeclarator, SyntaxRemoveOptions.AddElasticMarker) .WithAdditionalAnnotations(Formatter.Annotation); var newFeStatement = foreachStatement .WithExpression((ExpressionSyntax)selectCall) .WithIdentifier(generator.IdentifierName(newVariableName).GetFirstToken()) .WithStatement(restOfForeachBody); var newRoot = root.ReplaceNode(foreachStatement, newFeStatement); var withEmptyVariableDeclarationsRemoved = new RemoveEmptyVariableDeclarationSyntax().Visit(newRoot); return(document.WithSyntaxRoot(withEmptyVariableDeclarationsRemoved)); }
private async Task<Document> ConvertVariableAssignmentToLINQ(Document document, VariableDeclaratorSyntax variableDeclarator, ForEachStatementSyntax foreachStatement, CancellationToken c) { var generator = SyntaxGenerator.GetGenerator(document); var oldVariableName = foreachStatement.Identifier.Text; var newVariableName = variableDeclarator.Identifier.Text; var oldVariables = variableDeclarator.Initializer.Value.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Where(x => x.Identifier.Text == oldVariableName); var selectCall = generator.InvocationExpression( generator.MemberAccessExpression(foreachStatement.Expression, "Select"), generator.Argument( generator.ValueReturningLambdaExpression( new[] { generator.LambdaParameter("x") }, variableDeclarator.Initializer.Value.ReplaceNodes(oldVariables, (x, y) => generator.IdentifierName("x"))))); var root = await document.GetSyntaxRootAsync(c); Debug.WriteLine(variableDeclarator.Parent); var restOfForeachBody = foreachStatement.Statement .RemoveNode(variableDeclarator, SyntaxRemoveOptions.AddElasticMarker) .WithAdditionalAnnotations(Formatter.Annotation); var newFeStatement = foreachStatement .WithExpression((ExpressionSyntax)selectCall) .WithIdentifier(generator.IdentifierName(newVariableName).GetFirstToken()) .WithStatement(restOfForeachBody); var newRoot = root.ReplaceNode(foreachStatement, newFeStatement); var withEmptyVariableDeclarationsRemoved = new RemoveEmptyVariableDeclarationSyntax().Visit(newRoot); return document.WithSyntaxRoot(withEmptyVariableDeclarationsRemoved); }