Skip to content

Fix panic in SkipTriviaEx when printing type predicates with declaration maps enabled #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,13 @@ func getStartPositionOfRange(r core.TextRange, sourceFile *ast.SourceFile, inclu
if ast.PositionIsSynthesized(r.Pos()) {
return -1
}
return scanner.SkipTriviaEx(sourceFile.Text(), r.Pos(), &scanner.SkipTriviaOptions{StopAtComments: includeComments})
text := sourceFile.Text()
pos := r.Pos()
// Bounds check: if position is beyond text length, it's likely from a different file
if pos >= len(text) {
return pos // Return the original position without trying to skip trivia
}
return scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
}

func positionsAreOnSameLine(pos1 int, pos2 int, sourceFile *ast.SourceFile) bool {
Expand Down Expand Up @@ -388,19 +394,31 @@ func getLinesBetweenRangeEndAndRangeStart(range1 core.TextRange, range2 core.Tex
}

func getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
startPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
text := sourceFile.Text()
if pos >= len(text) {
return 0 // Can't determine line differences for out-of-bounds positions
}
startPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
prevPos := getPreviousNonWhitespacePosition(startPos, stopPos, sourceFile)
return getLinesBetweenPositions(sourceFile, core.IfElse(prevPos >= 0, prevPos, stopPos), startPos)
}

func getLinesBetweenPositionAndNextNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
nextPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
text := sourceFile.Text()
if pos >= len(text) {
return 0 // Can't determine line differences for out-of-bounds positions
}
nextPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
return getLinesBetweenPositions(sourceFile, pos, core.IfElse(stopPos < nextPos, stopPos, nextPos))
}

func getPreviousNonWhitespacePosition(pos int, stopPos int, sourceFile *ast.SourceFile) int {
text := sourceFile.Text()
for ; pos >= stopPos; pos-- {
if !stringutil.IsWhiteSpaceLike(rune(sourceFile.Text()[pos])) {
if pos >= len(text) {
continue // Skip out-of-bounds positions
}
if !stringutil.IsWhiteSpaceLike(rune(text[pos])) {
return pos
}
}
Expand Down
Loading