feat: Add file editing tools (replace_file_contents, insert_at_line, delete_lines) #2425
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 🚀