Repository navigation
fix: ignore folding ranges with lines outside the document - #1725
Merged
angelozerr merged 1 commit intoOct 19, 2026
Merged
Conversation
LSPFoldingRangeBuilder.getStartOffset clamped only negative start lines and getEndOffset only end lines past the document, so a start line after the last line or a negative end line reached Document.getLineEndOffset and threw IndexOutOfBoundsException. The exception left buildLanguageFoldRegions, LanguageFolding logged it as an error, and none of the server's ranges were folded in that pass. A reply computed before lines were deleted can name such lines. LSP says of both lines: "To be valid, the end must be zero or larger and smaller than the number of lines in the document", and "Clients are free to ignore invalid ranges." Both methods now clamp the line in both directions; the existing checks then drop the empty or inverted range. Validation: - New OutOfBoundsFoldingRangeTest runs the folding pass on a 6-line file with a valid comment range and an invalid one, starting on line 50 or ending on line -5. On main both tests fail with "IndexOutOfBoundsException: Wrong line: 50. Available lines count: 6" (and -5); with the fix both pass and the valid range is folded. - ./gradlew check: 582 tests, 0 failures, the 3 existing skips.
Contributor
|
Nice fix! |
This was referenced Oct 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A folding range whose start line is after the last line of the document, or whose end line is negative, is now ignored. Before, either one threw an
IndexOutOfBoundsException, and none of the server's folding ranges were applied to the file.Problem
LSPFoldingRangeBuilder.getStartOffsetandgetEndOffsetmap a line without a character to the end of that line. They are meant to "be defensive against language servers that return lines that are out of bounds for the document", but each clamps only one side:getStartOffsetusesMath.max(startLine, 0), so a start line after the last line reachesDocument.getLineEndOffset.getEndOffsetusesMath.min(endLine, lineCount - 1), so a negative end line does.getLineEndOffsetthen throws, for exampleIndexOutOfBoundsException: Wrong line: 50. Available lines count: 6. The exception leavesbuildLanguageFoldRegions, andLanguageFoldinglogs it as an error. So in that folding pass none of the server's ranges are folded, not just the bad one, and the IDE reports an internal error.A start line beyond the document does not need a faulty server. A reply computed before the user deleted lines describes the longer text.
Why this is a fix
FoldingRange says about both
startLineandendLine: "To be valid, the end must be zero or larger and smaller than the number of lines in the document." And about such ranges: "Clients are free to ignore invalid ranges."Ignoring an invalid range is allowed; failing the whole folding pass because of one is not what a client should do. Positions that carry a character already go through
LSPIJUtils.toOffset, which clamps both sides, as vscode-languageserver-node does.Change
Both methods clamp the line to the document's lines in both directions (
toDocumentLine). A range that becomes empty or inverted is then dropped by the existing checks ingetTextRangeandbuildLanguageFoldRegions.Validation
New
OutOfBoundsFoldingRangeTest, which runs the real folding pass throughLSPFoldingRangeFixtureTestCaseon a 6-line file. Each test has a valid comment range and one invalid range, and requires the valid range to be folded:main, fails withIndexOutOfBoundsException: Wrong line: 50. Available lines count: 6. With the fix, passes.main, fails withWrong line: -5. With the fix, passes../gradlew check: 582 tests, 0 failures, the 3 existing skips.