@@ -358,7 +358,13 @@ func getStartPositionOfRange(r core.TextRange, sourceFile *ast.SourceFile, inclu
358
358
if ast .PositionIsSynthesized (r .Pos ()) {
359
359
return - 1
360
360
}
361
- return scanner .SkipTriviaEx (sourceFile .Text (), r .Pos (), & scanner.SkipTriviaOptions {StopAtComments : includeComments })
361
+ text := sourceFile .Text ()
362
+ pos := r .Pos ()
363
+ // Bounds check: if position is beyond text length, it's likely from a different file
364
+ if pos >= len (text ) {
365
+ return pos // Return the original position without trying to skip trivia
366
+ }
367
+ return scanner .SkipTriviaEx (text , pos , & scanner.SkipTriviaOptions {StopAtComments : includeComments })
362
368
}
363
369
364
370
func positionsAreOnSameLine (pos1 int , pos2 int , sourceFile * ast.SourceFile ) bool {
@@ -388,19 +394,31 @@ func getLinesBetweenRangeEndAndRangeStart(range1 core.TextRange, range2 core.Tex
388
394
}
389
395
390
396
func getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter (pos int , stopPos int , sourceFile * ast.SourceFile , includeComments bool ) int {
391
- startPos := scanner .SkipTriviaEx (sourceFile .Text (), pos , & scanner.SkipTriviaOptions {StopAtComments : includeComments })
397
+ text := sourceFile .Text ()
398
+ if pos >= len (text ) {
399
+ return 0 // Can't determine line differences for out-of-bounds positions
400
+ }
401
+ startPos := scanner .SkipTriviaEx (text , pos , & scanner.SkipTriviaOptions {StopAtComments : includeComments })
392
402
prevPos := getPreviousNonWhitespacePosition (startPos , stopPos , sourceFile )
393
403
return getLinesBetweenPositions (sourceFile , core .IfElse (prevPos >= 0 , prevPos , stopPos ), startPos )
394
404
}
395
405
396
406
func getLinesBetweenPositionAndNextNonWhitespaceCharacter (pos int , stopPos int , sourceFile * ast.SourceFile , includeComments bool ) int {
397
- nextPos := scanner .SkipTriviaEx (sourceFile .Text (), pos , & scanner.SkipTriviaOptions {StopAtComments : includeComments })
407
+ text := sourceFile .Text ()
408
+ if pos >= len (text ) {
409
+ return 0 // Can't determine line differences for out-of-bounds positions
410
+ }
411
+ nextPos := scanner .SkipTriviaEx (text , pos , & scanner.SkipTriviaOptions {StopAtComments : includeComments })
398
412
return getLinesBetweenPositions (sourceFile , pos , core .IfElse (stopPos < nextPos , stopPos , nextPos ))
399
413
}
400
414
401
415
func getPreviousNonWhitespacePosition (pos int , stopPos int , sourceFile * ast.SourceFile ) int {
416
+ text := sourceFile .Text ()
402
417
for ; pos >= stopPos ; pos -- {
403
- if ! stringutil .IsWhiteSpaceLike (rune (sourceFile .Text ()[pos ])) {
418
+ if pos >= len (text ) {
419
+ continue // Skip out-of-bounds positions
420
+ }
421
+ if ! stringutil .IsWhiteSpaceLike (rune (text [pos ])) {
404
422
return pos
405
423
}
406
424
}
0 commit comments