Skip to content

fix: clamp format result range to document bounds (#153)#242

Open
maruthang wants to merge 1 commit into
microsoft:mainfrom
maruthang:fix/issue-153-format-range-exceeding-file-length
Open

fix: clamp format result range to document bounds (#153)#242
maruthang wants to merge 1 commit into
microsoft:mainfrom
maruthang:fix/issue-153-format-range-exceeding-file-length

Conversation

@maruthang

Copy link
Copy Markdown
Contributor

Summary

Fixes #153

Bug: format could return a TextEdit with a range end exceeding the document length, causing downstream consumers to fail.

Root Cause: The formatter used the initial full-document range (lines 0 to MAX_VALUE) without clamping it back to actual document bounds before returning, and also returned a redundant edit when formatting produced no changes.

Fix: Added a no-op optimization that returns an empty array when the formatted result equals the original text, and added defensive range clamping that limits the edit range end to the document's actual end position.

Changes

  • src/services/htmlFormatter.ts: Return early with [] when formatted output matches input; clamp range.end to document.positionAt(text.length) before constructing the returned TextEdit.
  • src/test/formatter.test.ts: Added 6 regression tests covering empty documents, whitespace-only, newline-only, CRLF-only, endWithNewline on empty input, and already-formatted content — all asserting edit ranges stay within document bounds.

Testing

  • Added regression tests in src/test/formatter.test.ts that verify all returned edit ranges do not exceed document length
  • All 161 existing tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Executing vscode.executeFormatDocumentProvider with HTML files can return end range exceeding file length

1 participant