提交 53d0f5ff 编写于 作者: M Manish Vasani

Address feedback and a more complex unit test with multiple Replace and Remove...

Address feedback and a more complex unit test with multiple Replace and Remove operations on new and old nodes in varying order.
上级 27544138
......@@ -133,7 +133,7 @@ public void ReplaceNode(SyntaxNode node, Func<SyntaxNode, SyntaxGenerator, Synta
}
_allowEditsOnLazilyCreatedTrackedNewNodes = true;
_changes.Add(new ReplaceChange(node, computeReplacement, ApplyTrackingToNewNode));
_changes.Add(new ReplaceChange(node, computeReplacement, this));
}
internal void ReplaceNode<TArgument>(SyntaxNode node, Func<SyntaxNode, SyntaxGenerator, TArgument, SyntaxNode> computeReplacement, TArgument argument)
......@@ -145,7 +145,7 @@ internal void ReplaceNode<TArgument>(SyntaxNode node, Func<SyntaxNode, SyntaxGen
}
_allowEditsOnLazilyCreatedTrackedNewNodes = true;
_changes.Add(new ReplaceChange<TArgument>(node, computeReplacement, argument, ApplyTrackingToNewNode));
_changes.Add(new ReplaceChange<TArgument>(node, computeReplacement, argument, this));
}
/// <summary>
......@@ -162,7 +162,7 @@ public void ReplaceNode(SyntaxNode node, SyntaxNode newNode)
}
newNode = ApplyTrackingToNewNode(newNode);
_changes.Add(new ReplaceChange(node, (n, g) => newNode, ApplyTrackingToNewNode));
_changes.Add(new ReplaceChange(node, (n, g) => newNode, this));
}
/// <summary>
......@@ -290,23 +290,23 @@ public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
private class ReplaceChange : Change
{
private readonly Func<SyntaxNode, SyntaxGenerator, SyntaxNode> _modifier;
private readonly Func<SyntaxNode, SyntaxNode> _applyTrackingToNewNode;
private readonly SyntaxEditor _editor;
public ReplaceChange(
SyntaxNode node,
Func<SyntaxNode, SyntaxGenerator, SyntaxNode> modifier,
Func<SyntaxNode, SyntaxNode> applyTrackingToNewNode)
SyntaxEditor editor)
: base(node)
{
_modifier = modifier;
_applyTrackingToNewNode = applyTrackingToNewNode;
_editor = editor;
}
public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
{
var current = root.GetCurrentNode(this.Node);
var newNode = _modifier(current, generator);
newNode = _applyTrackingToNewNode(newNode);
newNode = _editor.ApplyTrackingToNewNode(newNode);
return generator.ReplaceNode(root, current, newNode);
}
}
......@@ -315,25 +315,25 @@ private class ReplaceChange<TArgument> : Change
{
private readonly Func<SyntaxNode, SyntaxGenerator, TArgument, SyntaxNode> _modifier;
private readonly TArgument _argument;
private readonly Func<SyntaxNode, SyntaxNode> _applyTrackingToNewNode;
private readonly SyntaxEditor _editor;
public ReplaceChange(
SyntaxNode node,
Func<SyntaxNode, SyntaxGenerator, TArgument, SyntaxNode> modifier,
TArgument argument,
Func<SyntaxNode, SyntaxNode> applyTrackingToNewNode)
SyntaxEditor editor)
: base(node)
{
_modifier = modifier;
_argument = argument;
_applyTrackingToNewNode = applyTrackingToNewNode;
_editor = editor;
}
public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
{
var current = root.GetCurrentNode(this.Node);
var newNode = _modifier(current, generator, _argument);
newNode = _applyTrackingToNewNode(newNode);
newNode = _editor.ApplyTrackingToNewNode(newNode);
return generator.ReplaceNode(root, current, newNode);
}
}
......
......@@ -84,7 +84,7 @@ public class C
}
[Fact]
public async Task TestInterAfter()
public async Task TestInsertAfter()
{
var code = @"
public class C
......@@ -111,7 +111,7 @@ public class C
}
[Fact]
public async Task TestInterBefore()
public async Task TestInsertBefore()
{
var code = @"
public class C
......@@ -209,23 +209,127 @@ public class C
}");
}
[Fact]
public async Task TestReplaceWithTracking_02()
{
var code = @"
public class C
{
public int X;
public string X2;
public char X3;
}";
var cu = SyntaxFactory.ParseCompilationUnit(code);
var editor = GetEditor(cu);
var cls = cu.Members[0];
var fieldX = editor.Generator.GetMembers(cls)[0];
var fieldX2 = editor.Generator.GetMembers(cls)[1];
var fieldX3 = editor.Generator.GetMembers(cls)[2];
var newFieldY = editor.Generator.FieldDeclaration("Y", editor.Generator.TypeExpression(SpecialType.System_String), Accessibility.Public);
editor.ReplaceNode(fieldX, newFieldY);
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public string Y;
public string X2;
public char X3;
}");
var newFieldYType = newFieldY.DescendantNodes().Single(n => n.ToString() == "string");
var newType = editor.Generator.TypeExpression(SpecialType.System_Char);
editor.ReplaceNode(newFieldYType, newType);
newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public char Y;
public string X2;
public char X3;
}");
var newFieldY2 = editor.Generator.FieldDeclaration("Y2", editor.Generator.TypeExpression(SpecialType.System_Boolean), Accessibility.Private);
editor.ReplaceNode(fieldX2, newFieldY2);
newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public char Y;
private bool Y2;
public char X3;
}");
var newFieldZ = editor.Generator.FieldDeclaration("Z", editor.Generator.TypeExpression(SpecialType.System_Boolean), Accessibility.Public);
editor.ReplaceNode(newFieldY, newFieldZ);
newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public bool Z;
private bool Y2;
public char X3;
}");
var originalFieldX3Type = fieldX3.DescendantNodes().Single(n => n.ToString() == "char");
newType = editor.Generator.TypeExpression(SpecialType.System_Boolean);
editor.ReplaceNode(originalFieldX3Type, newType);
newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public bool Z;
private bool Y2;
public bool X3;
}");
editor.RemoveNode(newFieldY2);
editor.RemoveNode(fieldX3);
newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
newRoot,
@"
public class C
{
public bool Z;
}");
}
[Fact]
public async Task TestInsertAfterWithTracking()
{
// InsertAfter overload #1
await TestInterAfterWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
await TestInsertAfterWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
{
editor.InsertAfter(node, newNode);
});
// InsertAfter overload #2
await TestInterAfterWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
await TestInsertAfterWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
{
editor.InsertAfter(node, new[] { newNode });
});
}
private async Task TestInterAfterWithTrackingCoreAsync(Action<SyntaxNode, SyntaxNode, SyntaxEditor> insertAfterWithTracking)
private async Task TestInsertAfterWithTrackingCoreAsync(Action<SyntaxNode, SyntaxNode, SyntaxEditor> insertAfterWithTracking)
{
var code = @"
public class C
......@@ -269,19 +373,19 @@ public class C
public async Task TestInsertBeforeWithTracking()
{
// InsertBefore overload #1
await TestInterBeforeWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
await TestInsertBeforeWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
{
editor.InsertBefore(node, newNode);
});
// InsertBefore overload #2
await TestInterBeforeWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
await TestInsertBeforeWithTrackingCoreAsync((SyntaxNode node, SyntaxNode newNode, SyntaxEditor editor) =>
{
editor.InsertBefore(node, new[] { newNode });
});
}
private async Task TestInterBeforeWithTrackingCoreAsync(Action<SyntaxNode, SyntaxNode, SyntaxEditor> insertBeforeWithTracking)
private async Task TestInsertBeforeWithTrackingCoreAsync(Action<SyntaxNode, SyntaxNode, SyntaxEditor> insertBeforeWithTracking)
{
var code = @"
public class C
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册