From 4868cdf8855612f166c37aceb5ea5d160edd8dc1 Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 4 Jul 2025 19:07:49 -0700 Subject: [PATCH] refactor findSearchMatch --- core/edit/searchAndReplace/findSearchMatch.ts | 76 ++++++++++++++----- .../definitions/searchAndReplaceInFile.ts | 2 +- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/core/edit/searchAndReplace/findSearchMatch.ts b/core/edit/searchAndReplace/findSearchMatch.ts index 3b1b8acf8..727e0b0ba 100644 --- a/core/edit/searchAndReplace/findSearchMatch.ts +++ b/core/edit/searchAndReplace/findSearchMatch.ts @@ -8,14 +8,61 @@ export interface SearchMatchResult { endIndex: number; } +/** + * Strategy function type for finding matches + */ +type MatchStrategy = ( + fileContent: string, + searchContent: string, +) => SearchMatchResult | null; + +/** + * Exact string matching strategy + */ +function exactMatch( + fileContent: string, + searchContent: string, +): SearchMatchResult | null { + const exactIndex = fileContent.indexOf(searchContent); + if (exactIndex !== -1) { + return { + startIndex: exactIndex, + endIndex: exactIndex + searchContent.length, + }; + } + return null; +} + +/** + * Trimmed content matching strategy + */ +function trimmedMatch( + fileContent: string, + searchContent: string, +): SearchMatchResult | null { + const trimmedSearchContent = searchContent.trim(); + const trimmedIndex = fileContent.indexOf(trimmedSearchContent); + if (trimmedIndex !== -1) { + return { + startIndex: trimmedIndex, + endIndex: trimmedIndex + trimmedSearchContent.length, + }; + } + return null; +} + +/** + * Ordered list of matching strategies to try + */ +const matchingStrategies: MatchStrategy[] = [exactMatch, trimmedMatch]; + /** * Find the exact match position for search content in file content. - * Uses simple exact matching with trimmed newlines for MVP. + * Uses multiple matching strategies in order of preference. * * Matching Strategy: * 1. If search content is empty, matches at the beginning of file (position 0) - * 2. Try exact string match first - * 3. Fallback to trimmed content match if exact match fails + * 2. Try each matching strategy in order until one succeeds * * @param fileContent - The complete content of the file to search in * @param searchContent - The content to search for @@ -25,7 +72,6 @@ export function findSearchMatch( fileContent: string, searchContent: string, ): SearchMatchResult | null { - // Strip leading/trailing newlines for basic matching strategy const trimmedSearchContent = searchContent.trim(); if (trimmedSearchContent === "") { @@ -33,22 +79,12 @@ export function findSearchMatch( return { startIndex: 0, endIndex: 0 }; } - // Try exact match first - const exactIndex = fileContent.indexOf(searchContent); - if (exactIndex !== -1) { - return { - startIndex: exactIndex, - endIndex: exactIndex + searchContent.length, - }; - } - - // Fallback: try trimmed content match - const trimmedIndex = fileContent.indexOf(trimmedSearchContent); - if (trimmedIndex !== -1) { - return { - startIndex: trimmedIndex, - endIndex: trimmedIndex + trimmedSearchContent.length, - }; + // Try each matching strategy in order + for (const strategy of matchingStrategies) { + const result = strategy(fileContent, searchContent); + if (result !== null) { + return result; + } } return null; diff --git a/core/tools/definitions/searchAndReplaceInFile.ts b/core/tools/definitions/searchAndReplaceInFile.ts index a861fbb1c..b654f9c32 100644 --- a/core/tools/definitions/searchAndReplaceInFile.ts +++ b/core/tools/definitions/searchAndReplaceInFile.ts @@ -22,7 +22,7 @@ export const searchAndReplaceInFileTool: Tool = { function: { name: BuiltInToolNames.SearchAndReplaceInFile, description: - "Description: Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.", + "Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.", parameters: { type: "object", required: ["filepath", "diff"],