Browse Source

[CodeAction] Convert foreach to for now preserves old block.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
c4b9519ca5
  1. 18
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertForeachToForAction.cs

18
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertForeachToForAction.cs

@ -53,29 +53,37 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var id2 = id1.Clone(); var id2 = id1.Clone();
var id3 = id1.Clone(); var id3 = id1.Clone();
var variableDeclarationStatement = new VariableDeclarationStatement(
foreachStatement.VariableType.Clone(),
foreachStatement.VariableName,
new IndexerExpression(foreachStatement.InExpression.Clone(), id3)
);
var forStatement = new ForStatement() { var forStatement = new ForStatement() {
Initializers = { initializer }, Initializers = { initializer },
Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)), Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)),
Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) }, Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) },
EmbeddedStatement = new BlockStatement { EmbeddedStatement = new BlockStatement {
new VariableDeclarationStatement (foreachStatement.VariableType.Clone (), foreachStatement.VariableName, new IndexerExpression (foreachStatement.InExpression.Clone (), id3)) variableDeclarationStatement
} }
}; };
if (foreachStatement.EmbeddedStatement is BlockStatement) { if (foreachStatement.EmbeddedStatement is BlockStatement) {
foreach (var child in ((BlockStatement)foreachStatement.EmbeddedStatement).Statements) { variableDeclarationStatement.Remove();
forStatement.EmbeddedStatement.AddChild (child.Clone (), BlockStatement.StatementRole); var oldBlock = (BlockStatement)foreachStatement.EmbeddedStatement.Clone();
if (oldBlock.Statements.Any()) {
oldBlock.Statements.InsertBefore(oldBlock.Statements.First(), variableDeclarationStatement);
} else {
oldBlock.Statements.Add(variableDeclarationStatement);
} }
forStatement.EmbeddedStatement = oldBlock;
} else { } else {
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole); forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
} }
script.Replace (foreachStatement, forStatement); script.Replace (foreachStatement, forStatement);
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3); script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
}); });
} }
static string GetCountProperty(IType type) static string GetCountProperty(IType type)
{ {
if (type.Kind == TypeKind.Array) { if (type.Kind == TypeKind.Array) {

Loading…
Cancel
Save