refactor findSearchMatch

This commit is contained in:
Nate 2025-07-04 19:07:49 -07:00
parent f90fafbe36
commit 4868cdf885
2 changed files with 57 additions and 21 deletions

View file

@ -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;

View file

@ -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"],