@@ -30,11 +30,13 @@ public IVsTextView NavigateToEquivalentPosition(IVsTextView sourceView, string t
3030 var view = OpenDocument ( targetFile ) ;
3131 var text2 = VsShellUtilities . GetRunningDocumentContents ( serviceProvider , targetFile ) ;
3232
33- var matchingLine = FindNearestMatchingLine ( text1 , text2 , line ) ;
33+ var fromLines = ReadLines ( text1 ) ;
34+ var toLines = ReadLines ( text2 ) ;
35+ var matchingLine = FindNearestMatchingLine ( fromLines , toLines , line ) ;
3436 if ( matchingLine == - 1 )
3537 {
3638 // If we can't match line use orignal as best guess.
37- matchingLine = line ;
39+ matchingLine = line < toLines . Count ? line : toLines . Count - 1 ;
3840 column = 0 ;
3941 }
4042
@@ -77,10 +79,9 @@ IVsTextView OpenDocument(string fullPath)
7779 return view ;
7880 }
7981
80- static int FindNearestMatchingLine ( string text1 , string text2 , int line )
82+ static int FindNearestMatchingLine ( IList < string > fromLines , IList < string > toLines , int line )
8183 {
82- var fromLines = ReadLines ( text1 ) ;
83- var toLines = ReadLines ( text2 ) ;
84+ line = line < fromLines . Count ? line : fromLines . Count - 1 ; // VS shows one extra line at end
8485 var fromLine = fromLines [ line ] ;
8586
8687 for ( var offset = 0 ; true ; offset ++ )
@@ -94,7 +95,7 @@ static int FindNearestMatchingLine(string text1, string text2, int line)
9495
9596 var lineBelow = line - offset ;
9697 var checkBelow = lineBelow >= 0 ;
97- if ( checkBelow && toLines [ lineBelow ] == fromLine )
98+ if ( checkBelow && lineBelow < toLines . Count && toLines [ lineBelow ] == fromLine )
9899 {
99100 return lineBelow ;
100101 }
0 commit comments