-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add file editing tools (replace_file_contents, insert_at_line, delete_lines) #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaufgang
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
jaufgang:jaufgang/replace-file-contents-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Add file editing tools (replace_file_contents, insert_at_line, delete_lines) #2425
jaufgang
wants to merge
5
commits into
microsoft:main
from
jaufgang:jaufgang/replace-file-contents-tool
Conversation
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
Implements a new tool that replaces entire file contents in a single operation, addressing the gap identified in microsoft/vscode#281417. ## Problem The existing editing tools (replace_string_in_file, multi_replace_string_in_file) are designed for surgical edits with exact string matching. For complete file rewrites like format conversions or major refactoring, these tools require many fragile sequential calls that can fail if the file is modified between calls. ## Solution This PR adds a new `replace_file_contents` tool that: - Accepts complete new file content as input - Uses processFullRewrite to stream edits via VS Code's WorkspaceEdit API - Integrates with VS Code's undo stack (Ctrl+Z works) - Shows diff preview for sensitive files via createEditConfirmation - Returns diagnostics via EditFileResult component ## When to use - Complete file rewrites (e.g., SQL schema → Mermaid ERD) - Major refactoring (e.g., class → functional React) - Format/convention changes affecting most lines For surgical edits, replace_string_in_file remains the better choice. Fixes microsoft/vscode#281417
Adds unit tests covering: - Full file content replacement - Structural rewrites (function syntax conversion) - Error handling for non-existent files - Result format verification
Extends the tool to handle notebook files (.ipynb) using the same pattern as createFileTool: - Uses processFullRewriteNotebook for notebook documents - Adds INotebookService, IAlternativeNotebookContentService dependencies - Tracks isNotebook in telemetry - Returns appropriate result format for notebooks vs text files
Author
|
@microsoft-github-policy-service agree |
Implements two new line-based editing tools as requested in microsoft/vscode#281417 follow-up comments: - insert_at_line: Insert content at a specific line number (before/after) - delete_lines: Delete lines by line number range (inclusive) These tools complement the existing editing tools by providing position-based operations that don't require exact text matching, making them more robust to formatting changes.
connor4312
requested changes
Dec 15, 2025
Member
connor4312
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Summary
This PR implements a comprehensive set of file editing tools to address gaps identified in microsoft/vscode#281417. It adds three new tools that complement the existing editing tools by providing different editing paradigms:
replace_file_contents- Whole-file replacement for complete rewritesinsert_at_line- Line-based insertion without text matchingdelete_lines- Line-based deletion without text matchingMotivation
This PR stems from a conversation where Claude (running as the Copilot Chat backend) was asked: "What editing tools would help you do your job most efficiently?" The answer identified specific gaps in the current toolset that make certain common editing tasks unnecessarily difficult or fragile.
The goal is to empower the AI with a complete toolkit for file editing, covering the full spectrum from surgical single-line changes to complete file rewrites.
Problem Statement
The existing editing tools (
replace_string_in_file,multi_replace_string_in_file) are optimized for surgical edits with exact string matching. This creates pain points for:replace_string_in_filecallsSolution: Three Complementary Tools
1.
replace_file_contents(Original Request)Replace entire file contents in a single atomic operation.
Use cases: Format conversions, major refactoring, complete restructuring
2.
insert_at_line(Follow-up Request)Insert content at a specific line number without matching existing text.
Use cases: Adding imports, inserting methods, adding list entries
3.
delete_lines(Follow-up Request)Delete lines by position without reproducing exact content.
Use cases: Removing deprecated code, cleaning up comments, deleting generated sections
Why These Tools Matter
Robustness to Formatting Changes
Line numbers are stable references; exact string content is not. IDE formatters, auto-save, and linters constantly modify whitespace, causing string-match failures.
Simpler Mental Model
"Insert at line 15" and "delete lines 45-50" map directly to how developers think about code changes.
Reduced Token Usage
Current approach requires sending content to match/delete. Line-based operations just need numbers.
Better Error Messages
"Line 45 doesn't exist" is clearer than "Could not find exact match for 47-character string..."
Tool Coverage Matrix
replace_string_in_filemulti_replace_string_in_filereplace_string_in_filereplace_file_contents✨replace_file_contents✨insert_at_line✨insert_at_line✨delete_lines✨delete_lines✨Implementation Details
All three tools:
WorkspaceEditAPI for proper change trackingcreateEditConfirmationTesting
Unit tests added for all tools covering:
Files Changed
src/extension/tools/node/replaceFileContentsTool.tsxsrc/extension/tools/node/insertAtLineTool.tsxsrc/extension/tools/node/deleteLinesTool.tsxsrc/extension/tools/node/test/*.spec.tsxsrc/extension/tools/common/toolNames.tssrc/extension/tools/node/allTools.tspackage.jsonpackage.nls.jsonOut of Scope: Enabling
apply_patchfor ClaudeThe original issue thread includes a follow-up comment suggesting enabling the existing
apply_patchtool for Claude models. This was intentionally not included in this PR because:Different type of change: The tools in this PR are additive (new capabilities). Enabling
apply_patchfor Claude is a behavioral change affecting all existing Claude users.Requires experimentation: Changing which edit tool a model uses by default should likely be A/B tested to ensure Claude produces correct patch syntax reliably.
Scope clarity: This PR focuses on providing new tools that any model can use. Model-specific capability flags are a separate concern.
If there's interest in enabling
apply_patchfor Claude, that would be a good candidate for a separate PR with its own testing/rollout plan.Splitting Consideration
This PR could be split into separate PRs for each tool if preferred:
replace_file_contents(original request)insert_at_line(follow-up This repo is missing a LICENSE file #1)delete_lines(follow-up Adding Microsoft SECURITY.MD #2)Submitted as a single PR for unified initial review, but happy to split if that's preferred for easier review/merge.
Fixes microsoft/vscode#281417
🤖 Meta: AI Building Tools for Itself
This PR was created by GitHub Copilot (Claude Opus 4.5) after a human asked: "What tools would help you edit files most efficiently?" The original issue was also written by Claude—while running as the Copilot Chat backend—after experiencing the tooling limitations firsthand during a real user session.
Full loop: AI identifies gap → AI proposes solution → Human posts issue → Human asks AI to implement → AI builds the tools → Human pushes PR 🚀