@@ -14,6 +14,9 @@ public class NavigationService : INavigationService
1414 {
1515 readonly IServiceProvider serviceProvider ;
1616
17+ // If the target line doesn't have a unique match, search this number of lines above looking for a match.
18+ public const int MatchLinesAboveTarget = 4 ;
19+
1720 [ ImportingConstructor ]
1821 public NavigationService ( [ Import ( typeof ( SVsServiceProvider ) ) ] IServiceProvider serviceProvider )
1922 {
@@ -56,8 +59,39 @@ public IVsTextView FindActiveView()
5659
5760 public int FindMatchingLine ( IList < string > fromLines , IList < string > toLines , int line )
5861 {
59- int matchedLines ;
60- return FindNearestMatchingLine ( fromLines , toLines , line , out matchedLines ) ;
62+ var matchingLine = - 1 ;
63+ var minMatchedLines = - 1 ;
64+ for ( var offset = 0 ; offset <= MatchLinesAboveTarget ; offset ++ )
65+ {
66+ var targetLine = line - offset ;
67+ if ( targetLine < 0 )
68+ {
69+ break ;
70+ }
71+
72+ int matchedLines ;
73+ var nearestLine = FindNearestMatchingLine ( fromLines , toLines , targetLine , out matchedLines ) ;
74+ if ( nearestLine != - 1 )
75+ {
76+ if ( matchingLine == - 1 || minMatchedLines >= matchedLines )
77+ {
78+ matchingLine = nearestLine + offset ;
79+ minMatchedLines = matchedLines ;
80+ }
81+
82+ if ( minMatchedLines == 1 )
83+ {
84+ break ; // We've found a unique matching line!
85+ }
86+ }
87+ }
88+
89+ if ( matchingLine >= toLines . Count )
90+ {
91+ matchingLine = toLines . Count - 1 ;
92+ }
93+
94+ return matchingLine ;
6195 }
6296
6397 public int FindNearestMatchingLine ( IList < string > fromLines , IList < string > toLines , int line , out int matchedLines )
0 commit comments