Skip to content

fix(chat): preserve #file reference when editing text before it - #333965

Merged
Justin Chen (justschen) merged 9 commits into
microsoft:mainfrom
AnupamKumar-1:fix/preserve-file-reference-before-edit
Sep 18, 2026
Merged

Justin Chen (justschen) merged 9 commits into
microsoft:mainfrom
AnupamKumar-1:fix/preserve-file-reference-before-edit

Conversation

@AnupamKumar-1

Copy link
Copy Markdown
Contributor

Fixes #333941

Editing text before an inline #file: reference (for example, pressing
Backspace immediately before it or retyping an earlier word) could delete
the reference entirely.

The existing recovery logic handled replacements that enclosed the reference,
but did not preserve the reference when unrelated edits changed its offset.
Such edits could therefore fall through to the removal path.

This adds a touchesReference check to distinguish edits that overlap the
reference from edits that occur outside it. For non-overlapping edits, the
reference range is recomputed from the net offset delta instead of removing
the reference. Edits that touch the reference continue to remove it.

Added regression tests covering deletion before the reference, replacement
before it, deletion touching its first character, and edits both before and
after the reference in a single change event.

Manually verified the behavior with a real #file: reference in a running
dev build.

How to test

  1. Open Chat.
  2. Type a sentence, e.g. Compare the ownership section of .
  3. Type #, pick a file so #file:README.md is inserted inline, then continue typing with the current draft.
  4. Move the caret before the reference and edit there — e.g. press Backspace immediately before it, or retype a word right in front of it.
  5. Confirm the #file:README.md reference is preserved instead of being deleted.

Also covered by the added tests in chatVariables.test.ts (delete-before, replace-before, delete-touching-first-char, edits-before-and-after).

Copilot AI balanced review requested due to automatic review settings September 2, 2026 10:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Enclosing replacements can incorrectly trigger a second edit that deletes unrelated text.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Preserves inline chat references when unrelated edits shift their offsets.

Changes:

  • Detects whether edits overlap a reference.
  • Recomputes ranges after non-overlapping edits.
  • Adds regression tests for related editing scenarios.
File summaries
File Description
chatDynamicVariables.ts Adds overlap detection and range recovery.
chatVariables.test.ts Adds reference-editing regression tests.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@AnupamKumar-1
Anupam Kumar (AnupamKumar-1) force-pushed the fix/preserve-file-reference-before-edit branch from 82e9a7d to 21dda2f Compare September 2, 2026 10:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The range-tracking fix is focused and adequately covered by regression tests.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@justschen Justin Chen (justschen) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!!!

roblourens
roblourens previously approved these changes Sep 3, 2026
@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

Justin Chen (@justschen), Giuseppe Cianci (@Giuspepe), roblourens — I’ve addressed the CI failure. This dismissed your prior approvals, so could you please re-review and retrigger the CI when you get a chance? Thanks!

auto-merge was automatically disabled September 4, 2026 20:45

Head branch was pushed to by a user without write access

@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

Hi Justin Chen (@justschen) , thanks for the review! I’ve resolved the merge conflicts. Could you please retrigger CI and re-review the PR?

@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

Hi Justin Chen (@justschen) — just checking in on this PR now that the merge conflicts are resolved and it’s ready for review. When you have a chance, could you take another look? Let me know if anything else is blocking it. Thanks!

@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

Hi roblourens, Giuseppe Cianci (@Giuspepe) — the PR is ready for review again. When you have a chance, could you please take another look? Thanks!

@justschen
Justin Chen (justschen) merged commit d750fe2 into microsoft:main Sep 18, 2026
33 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.139.0 milestone Sep 18, 2026
Abdullah Atrash (abdullahatrash) added a commit to abdullahatrash/flowleap-agent-v2 that referenced this pull request Sep 20, 2026
* port(chat-ui): preserve #file reference when editing text before it (#394)

Upstream d750fe2 (microsoft#333965), applied clean with its tests.
Typing before an existing #file attachment silently deleted the attachment.
Adds touchesReference and findReferenceRangeForNonOverlappingEdit.

* port(chat-ui): highlight generic tool output by content type (#394)

Upstream 63acf08 (microsoft#333923). chatInputOutputMarkdownProgressPart.ts
applied clean; the chatToolOutputContentSubPart.ts hunk was hand-applied because the
fork's adjacent-code-part loop predates upstream's refactor. The agentHost
stateToProgressAdapter.ts hunk and its test are dropped (Agent Host only).
Our patent tools return large JSON payloads into exactly this part.

* port(chat-ui): fix IME handling in the chat find widget (#394)

Upstream b9861ad (microsoft#332950), applied clean with its tests.
Composition input (CJK) broke search in the transcript find widget.

* port(chat-ui): fix transcript Find keybindings (#394)

Upstream cefc831 (microsoft#333135). chatFindActions.ts, chatFindWidget.ts,
chatListWidget.ts and the find tests applied clean; the chatListWidget.test.ts hunk was
hand-applied because that test file is diverged. Enter/Escape/F3 inside the transcript
find widget went to the generic tree find instead of chat Find.

* port(chat-ui): configurable paste-to-attachment threshold and Insert in Prompt (#394)

Upstream e1801a0 (microsoft#331978). chatPasteProviders.ts, chatCopyActions.ts,
constants.ts, chatAccessibilityHelp.ts, the chat.shared.contribution.ts configuration
registration and the paste tests all applied clean. The src/vs/sessions hunks are left
out: the fork has no caller of createPastedTextArtifact there, and sessions files belong
to the deferred wave W.

* port(chat-ui): remove the line requirement for paste attachments (#394)

Upstream 36053db (microsoft#335100), hand-applied: the fork's
chatPasteProviders.ts lacks the GitHub-URL pattern that surrounds the hunk, and its
chatAccessibilityHelp.ts and both test files are behind upstream's base. A short paste
now becomes an attachment on character count alone, so pasting a single-line claim
works like pasting a multi-line one.

* port(chat-ui): reuse text buffers for checkpoint replay (#394)

Upstream 903d0d0 (microsoft#336095), applied clean with its tests.
Checkpoint restore rebuilt a text model per replayed edit.

* port(chat-ui): constrain symbol tools to the active workspace (#394)

Upstream d8cfdc7 (microsoft#333144), applied clean with its tests.
renameTool and usagesTool no longer reach outside the active workspace folder.
@AnupamKumar-1
Anupam Kumar (AnupamKumar-1) deleted the fix/preserve-file-reference-before-edit branch September 22, 2026 16:16
@AshleyGrant

Copy link
Copy Markdown

Anupam Kumar (@AnupamKumar-1) thank you so much for fixing this bug! It has been annoying me for weeks. I really appreciate the effort you put towards resolving it! Cheers!

@pkshrivastav

Copy link
Copy Markdown

nice work

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat: editing text before an inline #file: reference deletes the reference

9 participants