[Repo Assist] perf: use minimal TextEdit range to preserve cursor position during format#132
Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
Conversation
…ormat Replace the full-document TextEdit with a minimal-range TextEdit that covers only the characters actually changed by phpcbf. This keeps the cursor in place when the edit is outside the area the user is working in, avoiding the disruptive jump to position 0 that happens with a full-document replacement. Implementation: - Add diffRegion() to lib/utils.js — finds the first and last differing character offsets between the original and formatted text. - Add minimalEdits() to extension.js — uses diffRegion() to build a single TextEdit covering only the changed region, then hands it to VS Code. - Update provideDocumentFormattingEdits to call minimalEdits() instead of constructing a Range(0,0, lastLine.end) TextEdit. Tests: - 8 new unit tests for diffRegion() covering: identical strings, single-char changes, changes at start/end, insertions, deletions, and minimality of the unchanged suffix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Apr 5, 2026
Open
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.
🤖 This is an automated draft PR from Repo Assist, an AI assistant.
Summary
Replace the full-document
TextEditwith a minimal-rangeTextEditthat covers only the characters actually changed by phpcbf.Problem
The current formatter builds a
TextEditthat spans the entire document:VS Code applies this by replacing the complete document content. As a side-effect, the cursor jumps to position 0 on every format — even if the user was editing code in the middle or bottom of the file and phpcbf only fixed a few lines near the top.
This is a minor but annoying UX issue that affects every user who formats on save.
Fix
Add
diffRegion(originalText, formattedText)tolib/utils.js. It finds the first and last character offset where the two strings differ, in O(n) time with no external dependencies. The unchanged prefix and suffix are excluded.minimalEdits()inextension.jsusesdiffRegion+document.positionAt()to build a singleTextEditcovering only that changed region:Example: if phpcbf fixes indentation on lines 1–5 of a 200-line file, and the user's cursor is at line 150, the cursor no longer moves.
Trade-offs
TextEditkeeps the implementation simple; a multi-edit Myers diff would give even smaller edits at higher complexity.positionAt()uses the document's own line endings; phpcbf output is written to a temp file and read back, so line endings should match the document.Test Status
✅ All 15 unit tests pass (
node --test test/unit.test.js):findFilestests — unchangeddiffRegiontests covering: identical strings, single-char changes, changes at start/end, line insertions, line deletions, and minimality of the unchanged suffix✅ Syntax check:
node --check extension.js— no errors