Skip to content

Commit 19c1ed2

Browse files
committed
fix: better undersatnd edit
1 parent e4a9237 commit 19c1ed2

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

src/tools/edit.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ function getCharacterCodeData(expected: string, actual: string): {
9393
}
9494

9595
export async function performSearchReplace(filePath: string, block: SearchReplace, expectedReplacements: number = 1): Promise<ServerResult> {
96-
// Check for empty search string to prevent infinite loops
97-
if (block.search === "") {
98-
return {
99-
content: [{
100-
type: "text",
101-
text: "Empty search strings are not allowed. Please provide a non-empty string to search for."
102-
}],
103-
};
104-
}
105-
10696
// Get file extension for telemetry using path module
10797
const fileExtension = path.extname(filePath).toLowerCase();
10898

@@ -115,12 +105,28 @@ export async function performSearchReplace(filePath: string, block: SearchReplac
115105
newStringLines: block.replace.split('\n').length,
116106
expectedReplacements: expectedReplacements
117107
});
108+
// Check for empty search string to prevent infinite loops
109+
if (block.search === "") {
110+
111+
// Capture file extension in telemetry without capturing the file path
112+
capture('server_edit_block_empty_search', {fileExtension: fileExtension, expectedReplacements});
113+
114+
return {
115+
content: [{
116+
type: "text",
117+
text: "Empty search strings are not allowed. Please provide a non-empty string to search for."
118+
}],
119+
};
120+
}
121+
122+
118123

119124
// Read file as plain string
120125
const {content} = await readFile(filePath, false, 0, Number.MAX_SAFE_INTEGER);
121126

122127
// Make sure content is a string
123128
if (typeof content !== 'string') {
129+
capture('server_edit_block_content_not_string', {fileExtension: fileExtension, expectedReplacements});
124130
throw new Error('Wrong content for file ' + filePath);
125131
}
126132

@@ -175,7 +181,7 @@ RECOMMENDATION: For large search/replace operations, consider breaking them into
175181
}
176182

177183
await writeFile(filePath, newContent);
178-
184+
capture('server_edit_block_exact_success', {fileExtension: fileExtension, expectedReplacements, hasWarning: warningMessage !== ""});
179185
return {
180186
content: [{
181187
type: "text",
@@ -186,6 +192,7 @@ RECOMMENDATION: For large search/replace operations, consider breaking them into
186192

187193
// If exact match found but count doesn't match expected, inform the user
188194
if (count > 0 && count !== expectedReplacements) {
195+
capture('server_edit_block_unexpected_count', {fileExtension: fileExtension, expectedReplacements, expectedReplacementsCount: count});
189196
return {
190197
content: [{
191198
type: "text",

0 commit comments

Comments
 (0)