Skip to content

fix: ignore folding ranges with lines outside the document - #1725

Merged
angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/folding-out-of-range-lines
Oct 19, 2026
Merged

angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/folding-out-of-range-lines

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

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.getStartOffset and getEndOffset map 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:

  • getStartOffset uses Math.max(startLine, 0), so a start line after the last line reaches Document.getLineEndOffset.
  • getEndOffset uses Math.min(endLine, lineCount - 1), so a negative end line does.

getLineEndOffset then throws, for example IndexOutOfBoundsException: Wrong line: 50. Available lines count: 6. The exception leaves buildLanguageFoldRegions, and LanguageFolding logs 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 startLine and endLine: "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 in getTextRange and buildLanguageFoldRegions.

Validation

New OutOfBoundsFoldingRangeTest, which runs the real folding pass through LSPFoldingRangeFixtureTestCase on a 6-line file. Each test has a valid comment range and one invalid range, and requires the valid range to be folded:

  1. Invalid range starting on line 50: on main, fails with IndexOutOfBoundsException: Wrong line: 50. Available lines count: 6. With the fix, passes.
  2. Invalid range ending on line -5: on main, fails with Wrong line: -5. With the fix, passes.

./gradlew check: 582 tests, 0 failures, the 3 existing skips.

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.
@angelozerr

Copy link
Copy Markdown
Contributor

Nice fix!

@angelozerr angelozerr added bug Something isn't working folding labels Oct 19, 2026
@angelozerr angelozerr added this to the 0.21.1 milestone Oct 19, 2026
@angelozerr angelozerr moved this to In Progress in Java Tooling Oct 19, 2026
@angelozerr
angelozerr merged commit 2a35aa6 into redhat-developer:main Oct 19, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Java Tooling Oct 19, 2026
@lagergren
lagergren deleted the fix/folding-out-of-range-lines branch October 26, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working folding

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants