提交 971aa63f 编写于 作者: I Ivan Basov 提交者: GitHub

workaround for Microsoft.CodeAnalysis.Differencing.Match makes not optimal matches (#18802)

上级 4f7b313c
......@@ -960,6 +960,36 @@ public void InterpolationFormatClause_update()
edits.VerifyEdits("Update [x = $\"Hello{123:N1}\"]@8 -> [x = $\"Hello{123:N2}\"]@8");
}
[Fact]
public void MatchCasePattern_UpdateDelete()
{
var src1 = @"
switch(shape)
{
case Point p: return 0;
case Circle c: return 1;
}
";
var src2 = @"
switch(shape)
{
case Circle circle: return 1;
}
";
var match = GetMethodMatches(src1, src2, kind: MethodKind.Regular);
var actual = ToMatchingPairs(match);
var expected = new MatchingPairs {
{ "switch(shape) { case Point p: return 0; case Circle c: return 1; }", "switch(shape) { case Circle circle: return 1; }" },
{ "case Circle c: return 1;", "case Circle circle: return 1;" },
{ "return 1;", "return 1;" }
};
expected.AssertEqual(actual);
}
#endregion
#region Variable Declaration
......@@ -1035,6 +1065,32 @@ public void Switch_Case_Update()
"Update [case 1: f(); break;]@18 -> [case 2: f(); break;]@18");
}
[Fact]
public void CasePatternLabel_UpdateDelete()
{
var src1 = @"
switch(shape)
{
case Point p: return 0;
case Circle c: return 1;
}
";
var src2 = @"
switch(shape)
{
case Circle circle: return 1;
}
";
var edits = GetMethodEdits(src1, src2);
edits.VerifyEdits(
"Update [case Circle c: return 1;]@55 -> [case Circle circle: return 1;]@26",
"Delete [case Point p: return 0;]@26",
"Delete [return 0;]@40");
}
#endregion
#region Try Catch Finally
......
......@@ -12,10 +12,9 @@ public sealed partial class Match<TNode>
{
private const double ExactMatchDistance = 0.0;
private const double EpsilonDistance = 0.00001;
private const double MatchingDistance1 = 0.5;
private const double MatchingDistance2 = 1.0;
private const double MatchingDistance3 = 1.5;
private const double MaxDistance = 2.0;
private const double MatchingDistance1 = 0.25;
private const double MatchingDistance2 = 0.5;
private const double MaxDistance = 1.0;
private readonly TreeComparer<TNode> _comparer;
private readonly TNode _root1;
......@@ -154,7 +153,6 @@ private void ComputeMatchForLabel(int label, List<TNode> s1, List<TNode> s2)
ComputeMatchForLabel(s1, s2, tiedToAncestor, EpsilonDistance); // almost exact match
ComputeMatchForLabel(s1, s2, tiedToAncestor, MatchingDistance1); // ok match
ComputeMatchForLabel(s1, s2, tiedToAncestor, MatchingDistance2); // ok match
ComputeMatchForLabel(s1, s2, tiedToAncestor, MatchingDistance3); // ok match
ComputeMatchForLabel(s1, s2, tiedToAncestor, MaxDistance); // any match
}
......@@ -168,6 +166,7 @@ private void ComputeMatchForLabel(List<TNode> s1, List<TNode> s2, int tiedToAnce
// So in the case of totally matching sequences, we process them in O(n) -
// both node1 and firstNonMatch2 will be advanced simultaneously.
Debug.Assert(maxAcceptableDistance >= ExactMatchDistance && maxAcceptableDistance <= MaxDistance);
int count1 = s1.Count;
int count2 = s2.Count;
int firstNonMatch2 = 0;
......@@ -184,7 +183,7 @@ private void ComputeMatchForLabel(List<TNode> s1, List<TNode> s2, int tiedToAnce
// Find node2 that matches node1 the best, i.e. has minimal distance.
double bestDistance = MaxDistance;
double bestDistance = MaxDistance * 2;
TNode bestMatch = default(TNode);
bool matched = false;
int i2;
......@@ -258,6 +257,11 @@ private void ComputeMatchForLabel(List<TNode> s1, List<TNode> s2, int tiedToAnce
{
firstNonMatch2 = i2 + 1;
}
if (firstNonMatch2 == count2)
{
return;
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册