Skip to content

Commit 6bbb150

Browse files
committed
Refactor search and replace tool
1 parent 61e23cc commit 6bbb150

File tree

4 files changed

+310
-275
lines changed

4 files changed

+310
-275
lines changed

src/core/prompts/__tests__/__snapshots__/system.test.ts.snap

Lines changed: 56 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -659,53 +659,39 @@ Example: Insert a new function and its import statement
659659
</insert_content>
660660

661661
## search_and_replace
662-
Description: Request to perform search and replace operations on a file. Each operation can specify a search pattern (string or regex) and replacement text, with optional line range restrictions and regex flags. Shows a diff preview before applying changes.
663-
Parameters:
664-
- path: (required) The path of the file to modify (relative to the current workspace directory /test/path)
665-
- operations: (required) A JSON array of search/replace operations. Each operation is an object with:
666-
* search: (required) The text or pattern to search for
667-
* replace: (required) The text to replace matches with. If multiple lines need to be replaced, use "
668-
" for newlines
669-
* start_line: (optional) Starting line number for restricted replacement
670-
* end_line: (optional) Ending line number for restricted replacement
671-
* use_regex: (optional) Whether to treat search as a regex pattern
672-
* ignore_case: (optional) Whether to ignore case when matching
673-
* regex_flags: (optional) Additional regex flags when use_regex is true
674-
Usage:
675-
<search_and_replace>
676-
<path>File path here</path>
677-
<operations>[
678-
{
679-
"search": "text to find",
680-
"replace": "replacement text",
681-
"start_line": 1,
682-
"end_line": 10
683-
}
684-
]</operations>
685-
</search_and_replace>
686-
Example: Replace "foo" with "bar" in lines 1-10 of example.ts
662+
Description: Request to perform a search and replace operation on a file. Supports both literal text and regex patterns. Shows a diff preview before applying changes.
663+
664+
Required Parameters:
665+
- path: The path of the file to modify (relative to the current workspace directory /test/path)
666+
- search: The text or pattern to search for
667+
- replace: The text to replace matches with
668+
669+
Optional Parameters:
670+
- start_line: Starting line number for restricted replacement (1-based)
671+
- end_line: Ending line number for restricted replacement (1-based)
672+
- use_regex: Set to "true" to treat search as a regex pattern (default: false)
673+
- ignore_case: Set to "true" to ignore case when matching (default: false)
674+
675+
Notes:
676+
- When use_regex is true, the search parameter is treated as a regular expression pattern
677+
- When ignore_case is true, the search is case-insensitive regardless of regex mode
678+
679+
Examples:
680+
681+
1. Simple text replacement:
687682
<search_and_replace>
688683
<path>example.ts</path>
689-
<operations>[
690-
{
691-
"search": "foo",
692-
"replace": "bar",
693-
"start_line": 1,
694-
"end_line": 10
695-
}
696-
]</operations>
684+
<search>oldText</search>
685+
<replace>newText</replace>
697686
</search_and_replace>
698-
Example: Replace all occurrences of "old" with "new" using regex
687+
688+
2. Case-insensitive regex pattern:
699689
<search_and_replace>
700690
<path>example.ts</path>
701-
<operations>[
702-
{
703-
"search": "old\\w+",
704-
"replace": "new$&",
705-
"use_regex": true,
706-
"ignore_case": true
707-
}
708-
]</operations>
691+
<search>oldw+</search>
692+
<replace>new$&</replace>
693+
<use_regex>true</use_regex>
694+
<ignore_case>true</ignore_case>
709695
</search_and_replace>
710696

711697
## execute_command
@@ -1133,53 +1119,39 @@ Example: Requesting to append to a log file
11331119
</append_to_file>
11341120

11351121
## search_and_replace
1136-
Description: Request to perform search and replace operations on a file. Each operation can specify a search pattern (string or regex) and replacement text, with optional line range restrictions and regex flags. Shows a diff preview before applying changes.
1137-
Parameters:
1138-
- path: (required) The path of the file to modify (relative to the current workspace directory /test/path)
1139-
- operations: (required) A JSON array of search/replace operations. Each operation is an object with:
1140-
* search: (required) The text or pattern to search for
1141-
* replace: (required) The text to replace matches with. If multiple lines need to be replaced, use "
1142-
" for newlines
1143-
* start_line: (optional) Starting line number for restricted replacement
1144-
* end_line: (optional) Ending line number for restricted replacement
1145-
* use_regex: (optional) Whether to treat search as a regex pattern
1146-
* ignore_case: (optional) Whether to ignore case when matching
1147-
* regex_flags: (optional) Additional regex flags when use_regex is true
1148-
Usage:
1149-
<search_and_replace>
1150-
<path>File path here</path>
1151-
<operations>[
1152-
{
1153-
"search": "text to find",
1154-
"replace": "replacement text",
1155-
"start_line": 1,
1156-
"end_line": 10
1157-
}
1158-
]</operations>
1159-
</search_and_replace>
1160-
Example: Replace "foo" with "bar" in lines 1-10 of example.ts
1122+
Description: Request to perform a search and replace operation on a file. Supports both literal text and regex patterns. Shows a diff preview before applying changes.
1123+
1124+
Required Parameters:
1125+
- path: The path of the file to modify (relative to the current workspace directory /test/path)
1126+
- search: The text or pattern to search for
1127+
- replace: The text to replace matches with
1128+
1129+
Optional Parameters:
1130+
- start_line: Starting line number for restricted replacement (1-based)
1131+
- end_line: Ending line number for restricted replacement (1-based)
1132+
- use_regex: Set to "true" to treat search as a regex pattern (default: false)
1133+
- ignore_case: Set to "true" to ignore case when matching (default: false)
1134+
1135+
Notes:
1136+
- When use_regex is true, the search parameter is treated as a regular expression pattern
1137+
- When ignore_case is true, the search is case-insensitive regardless of regex mode
1138+
1139+
Examples:
1140+
1141+
1. Simple text replacement:
11611142
<search_and_replace>
11621143
<path>example.ts</path>
1163-
<operations>[
1164-
{
1165-
"search": "foo",
1166-
"replace": "bar",
1167-
"start_line": 1,
1168-
"end_line": 10
1169-
}
1170-
]</operations>
1144+
<search>oldText</search>
1145+
<replace>newText</replace>
11711146
</search_and_replace>
1172-
Example: Replace all occurrences of "old" with "new" using regex
1147+
1148+
2. Case-insensitive regex pattern:
11731149
<search_and_replace>
11741150
<path>example.ts</path>
1175-
<operations>[
1176-
{
1177-
"search": "old\\w+",
1178-
"replace": "new$&",
1179-
"use_regex": true,
1180-
"ignore_case": true
1181-
}
1182-
]</operations>
1151+
<search>oldw+</search>
1152+
<replace>new$&</replace>
1153+
<use_regex>true</use_regex>
1154+
<ignore_case>true</ignore_case>
11831155
</search_and_replace>
11841156

11851157
## execute_command

src/core/prompts/tools/search-and-replace.ts

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,38 @@ import { ToolArgs } from "./types"
22

33
export function getSearchAndReplaceDescription(args: ToolArgs): string {
44
return `## search_and_replace
5-
Description: Request to perform search and replace operations on a file. Each operation can specify a search pattern (string or regex) and replacement text, with optional line range restrictions and regex flags. Shows a diff preview before applying changes.
6-
Parameters:
7-
- path: (required) The path of the file to modify (relative to the current workspace directory ${args.cwd.toPosix()})
8-
- operations: (required) A JSON array of search/replace operations. Each operation is an object with:
9-
* search: (required) The text or pattern to search for
10-
* replace: (required) The text to replace matches with. If multiple lines need to be replaced, use "\n" for newlines
11-
* start_line: (optional) Starting line number for restricted replacement
12-
* end_line: (optional) Ending line number for restricted replacement
13-
* use_regex: (optional) Whether to treat search as a regex pattern
14-
* ignore_case: (optional) Whether to ignore case when matching
15-
* regex_flags: (optional) Additional regex flags when use_regex is true
16-
Usage:
17-
<search_and_replace>
18-
<path>File path here</path>
19-
<operations>[
20-
{
21-
"search": "text to find",
22-
"replace": "replacement text",
23-
"start_line": 1,
24-
"end_line": 10
25-
}
26-
]</operations>
27-
</search_and_replace>
28-
Example: Replace "foo" with "bar" in lines 1-10 of example.ts
5+
Description: Request to perform a search and replace operation on a file. Supports both literal text and regex patterns. Shows a diff preview before applying changes.
6+
7+
Required Parameters:
8+
- path: The path of the file to modify (relative to the current workspace directory ${args.cwd.toPosix()})
9+
- search: The text or pattern to search for
10+
- replace: The text to replace matches with
11+
12+
Optional Parameters:
13+
- start_line: Starting line number for restricted replacement (1-based)
14+
- end_line: Ending line number for restricted replacement (1-based)
15+
- use_regex: Set to "true" to treat search as a regex pattern (default: false)
16+
- ignore_case: Set to "true" to ignore case when matching (default: false)
17+
18+
Notes:
19+
- When use_regex is true, the search parameter is treated as a regular expression pattern
20+
- When ignore_case is true, the search is case-insensitive regardless of regex mode
21+
22+
Examples:
23+
24+
1. Simple text replacement:
2925
<search_and_replace>
3026
<path>example.ts</path>
31-
<operations>[
32-
{
33-
"search": "foo",
34-
"replace": "bar",
35-
"start_line": 1,
36-
"end_line": 10
37-
}
38-
]</operations>
27+
<search>oldText</search>
28+
<replace>newText</replace>
3929
</search_and_replace>
40-
Example: Replace all occurrences of "old" with "new" using regex
30+
31+
2. Case-insensitive regex pattern:
4132
<search_and_replace>
4233
<path>example.ts</path>
43-
<operations>[
44-
{
45-
"search": "old\\w+",
46-
"replace": "new$&",
47-
"use_regex": true,
48-
"ignore_case": true
49-
}
50-
]</operations>
34+
<search>old\w+</search>
35+
<replace>new$&</replace>
36+
<use_regex>true</use_regex>
37+
<ignore_case>true</ignore_case>
5138
</search_and_replace>`
5239
}

0 commit comments

Comments
 (0)