Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 7f37a14

Browse files
committed
Return number of matching lines when finding nearest
1 parent 4600c80 commit 7f37a14

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/GitHub.App/Services/NavigationService.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public IVsTextView NavigateToEquivalentPosition(IVsTextView sourceView, string t
3232

3333
var fromLines = ReadLines(text1);
3434
var toLines = ReadLines(text2);
35-
var matchingLine = FindNearestMatchingLine(fromLines, toLines, line);
35+
var matchingLine = FindMatchingLine(fromLines, toLines, line);
3636
if (matchingLine == -1)
3737
{
3838
// If we can't match line use orignal as best guess.
@@ -54,32 +54,52 @@ public IVsTextView FindActiveView()
5454
return hresult == VSConstants.S_OK ? view : null;
5555
}
5656

57-
public int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line)
57+
public int FindMatchingLine(IList<string> fromLines, IList<string> toLines, int line)
58+
{
59+
int matchedLines;
60+
return FindNearestMatchingLine(fromLines, toLines, line, out matchedLines);
61+
}
62+
63+
public int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line, out int matchedLines)
5864
{
5965
line = line < fromLines.Count ? line : fromLines.Count - 1; // VS shows one extra line at end
6066
var fromLine = fromLines[line];
6167

68+
matchedLines = 0;
69+
var matchingLine = -1;
6270
for (var offset = 0; true; offset++)
6371
{
6472
var lineAbove = line + offset;
6573
var checkAbove = lineAbove < toLines.Count;
6674
if (checkAbove && toLines[lineAbove] == fromLine)
6775
{
68-
return lineAbove;
76+
if (matchedLines == 0)
77+
{
78+
matchingLine = lineAbove;
79+
}
80+
81+
matchedLines++;
6982
}
7083

7184
var lineBelow = line - offset;
7285
var checkBelow = lineBelow >= 0;
73-
if (checkBelow && lineBelow < toLines.Count && toLines[lineBelow] == fromLine)
86+
if (checkBelow && offset > 0 && lineBelow < toLines.Count && toLines[lineBelow] == fromLine)
7487
{
75-
return lineBelow;
88+
if (matchedLines == 0)
89+
{
90+
matchingLine = lineBelow;
91+
}
92+
93+
matchedLines++;
7694
}
7795

7896
if (!checkAbove && !checkBelow)
7997
{
80-
return -1;
98+
break;
8199
}
82100
}
101+
102+
return matchingLine;
83103
}
84104

85105
string GetText(IVsTextView textView)

test/UnitTests/GitHub.App/Services/NavigationServiceTests.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ public class NavigationServiceTests
88
{
99
public class TheFindNearestMatchingLineMethod
1010
{
11-
[TestCase(new[] { "line" }, new[] { "line" }, 0, 0, Description = "Match same line")]
12-
[TestCase(new[] { "line" }, new[] { "line_no_match" }, 0, -1, Description = "No matching line")]
13-
[TestCase(new[] { "line" }, new[] { "", "line" }, 0, 1, Description = "Match line moved up")]
14-
[TestCase(new[] { "", "line" }, new[] { "line" }, 1, 0, Description = "Match line moved down")]
15-
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 0, 0, Description = "Match nearest line")]
16-
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 1, 1, Description = "Match nearest line")]
17-
[TestCase(new[] { "line" }, new[] { "line" }, 1, 0, Description = "Treat after last line the same as last line")]
18-
public void FindNearestMatching(IList<string> fromLines, IList<string> toLines, int line, int expectLine)
11+
[TestCase(new[] { "line" }, new[] { "line" }, 0, 0, 1, Description = "Match same line")]
12+
[TestCase(new[] { "line" }, new[] { "line_no_match" }, 0, -1, 0, Description = "No matching line")]
13+
[TestCase(new[] { "line" }, new[] { "", "line" }, 0, 1, 1, Description = "Match line moved up")]
14+
[TestCase(new[] { "", "line" }, new[] { "line" }, 1, 0, 1, Description = "Match line moved down")]
15+
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 0, 0, 2, Description = "Match nearest line")]
16+
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 1, 1, 2, Description = "Match nearest line")]
17+
[TestCase(new[] { "line" }, new[] { "line" }, 1, 0, 1, Description = "Treat after last line the same as last line")]
18+
public void FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line,
19+
int expectNearestLine, int expectMatchingLines)
1920
{
2021
var sp = Substitute.For<IServiceProvider>();
2122
var target = new NavigationService(sp);
2223

23-
var matchingLine = target.FindNearestMatchingLine(fromLines, toLines, line);
24+
int matchedLines;
25+
var nearestLine = target.FindNearestMatchingLine(fromLines, toLines, line, out matchedLines);
2426

25-
Assert.That(matchingLine, Is.EqualTo(expectLine));
27+
Assert.That(nearestLine, Is.EqualTo(expectNearestLine));
28+
Assert.That(matchedLines, Is.EqualTo(expectMatchingLines));
2629
}
2730
}
2831
}

0 commit comments

Comments
 (0)