11import { DiffStrategy , DiffResult } from "../types"
2+ import { addLineNumbers } from "../../../integrations/misc/extract-text"
3+
4+ const BUFFER_LINES = 5 ; // Number of extra context lines to show before and after matches
25
36function levenshteinDistance ( a : string , b : string ) : number {
47 const matrix : number [ ] [ ] = [ ] ;
@@ -48,10 +51,12 @@ function getSimilarity(original: string, search: string): number {
4851
4952export class SearchReplaceDiffStrategy implements DiffStrategy {
5053 private fuzzyThreshold : number ;
54+ public debugEnabled : boolean ;
5155
52- constructor ( fuzzyThreshold ?: number ) {
56+ constructor ( fuzzyThreshold ?: number , debugEnabled ?: boolean ) {
5357 // Default to exact matching (1.0) unless fuzzy threshold specified
5458 this . fuzzyThreshold = fuzzyThreshold ?? 1.0 ;
59+ this . debugEnabled = debugEnabled ?? false ;
5560 }
5661
5762 getToolDescription ( cwd : string ) : string {
@@ -119,15 +124,11 @@ Your search/replace content here
119124 // Extract the search and replace blocks
120125 const match = diffContent . match ( / < < < < < < < S E A R C H \n ( [ \s \S ] * ?) \n = = = = = = = \n ( [ \s \S ] * ?) \n > > > > > > > R E P L A C E / ) ;
121126 if ( ! match ) {
122- // Log detailed format information
123- console . log ( 'Invalid Diff Format Debug:' , {
124- expectedFormat : "<<<<<<< SEARCH\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE" ,
125- tip : "Make sure to include both SEARCH and REPLACE sections with correct markers"
126- } ) ;
127+ const debugInfo = this . debugEnabled ? `\n\nDebug Info:\n- Expected Format: <<<<<<< SEARCH\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE\n- Tip: Make sure to include both SEARCH and REPLACE sections with correct markers` : '' ;
127128
128129 return {
129130 success : false ,
130- error : " Invalid diff format - missing required SEARCH/REPLACE sections"
131+ error : ` Invalid diff format - missing required SEARCH/REPLACE sections${ debugInfo } `
131132 } ;
132133 }
133134
@@ -161,21 +162,17 @@ Your search/replace content here
161162 let bestMatchScore = 0 ;
162163 let bestMatchContent = "" ;
163164
164- if ( startLine !== undefined && endLine !== undefined ) {
165+ if ( startLine && endLine ) {
165166 // Convert to 0-based index
166167 const exactStartIndex = startLine - 1 ;
167168 const exactEndIndex = endLine - 1 ;
168169
169- if ( exactStartIndex < 0 || exactEndIndex >= originalLines . length ) {
170- // Log detailed debug information
171- console . log ( 'Invalid Line Range Debug:' , {
172- requestedRange : { start : startLine , end : endLine } ,
173- fileBounds : { start : 1 , end : originalLines . length }
174- } ) ;
175-
170+ if ( exactStartIndex < 0 || exactEndIndex >= originalLines . length || exactStartIndex > exactEndIndex ) {
171+ const debugInfo = this . debugEnabled ? `\n\nDebug Info:\n- Requested Range: lines ${ startLine } -${ endLine } \n- File Bounds: lines 1-${ originalLines . length } ` : '' ;
172+
176173 return {
177174 success : false ,
178- error : `Line range ${ startLine } -${ endLine } is invalid (file has ${ originalLines . length } lines)` ,
175+ error : `Line range ${ startLine } -${ endLine } is invalid (file has ${ originalLines . length } lines)${ debugInfo } ` ,
179176 } ;
180177 }
181178
@@ -196,13 +193,13 @@ Your search/replace content here
196193 let searchStartIndex = 0 ;
197194 let searchEndIndex = originalLines . length ;
198195
199- if ( startLine !== undefined || endLine !== undefined ) {
196+ if ( startLine || endLine ) {
200197 // Convert to 0-based index and add buffer
201- if ( startLine !== undefined ) {
202- searchStartIndex = Math . max ( 0 , startLine - 6 ) ;
198+ if ( startLine ) {
199+ searchStartIndex = Math . max ( 0 , startLine - ( BUFFER_LINES + 1 ) ) ;
203200 }
204- if ( endLine !== undefined ) {
205- searchEndIndex = Math . min ( originalLines . length , endLine + 5 ) ;
201+ if ( endLine ) {
202+ searchEndIndex = Math . min ( originalLines . length , endLine + BUFFER_LINES ) ;
206203 }
207204 }
208205
@@ -224,17 +221,27 @@ Your search/replace content here
224221 // Require similarity to meet threshold
225222 if ( matchIndex === - 1 || bestMatchScore < this . fuzzyThreshold ) {
226223 const searchChunk = searchLines . join ( '\n' ) ;
227- // Log detailed debug information to console
228- console . log ( 'Search/Replace Debug Info:' , {
229- similarity : bestMatchScore ,
230- threshold : this . fuzzyThreshold ,
231- searchContent : searchChunk ,
232- bestMatch : bestMatchContent || undefined
233- } ) ;
234-
224+ const originalContentSection = startLine !== undefined && endLine !== undefined
225+ ? `\n\nOriginal Content:\n${ addLineNumbers (
226+ originalLines . slice (
227+ Math . max ( 0 , startLine - 1 - BUFFER_LINES ) ,
228+ Math . min ( originalLines . length , endLine + BUFFER_LINES )
229+ ) . join ( '\n' ) ,
230+ Math . max ( 1 , startLine - BUFFER_LINES )
231+ ) } `
232+ : `\n\nOriginal Content:\n${ addLineNumbers ( originalLines . join ( '\n' ) ) } ` ;
233+
234+ const bestMatchSection = bestMatchContent
235+ ? `\n\nBest Match Found:\n${ addLineNumbers ( bestMatchContent , matchIndex + 1 ) } `
236+ : `\n\nBest Match Found:\n(no match)` ;
237+
238+ const debugInfo = this . debugEnabled ? `\n\nDebug Info:\n- Similarity Score: ${ Math . floor ( bestMatchScore * 100 ) } %\n- Required Threshold: ${ Math . floor ( this . fuzzyThreshold * 100 ) } %\n- Search Range: ${ startLine && endLine ? `lines ${ startLine } -${ endLine } ` : 'start to end' } \n\nSearch Content:\n${ searchChunk } ${ bestMatchSection } ${ originalContentSection } ` : '' ;
239+
240+ const lineRange = startLine || endLine ?
241+ ` at ${ startLine ? `start: ${ startLine } ` : 'start' } to ${ endLine ? `end: ${ endLine } ` : 'end' } ` : '' ;
235242 return {
236243 success : false ,
237- error : `No sufficiently similar match found${ startLine !== undefined ? ` near lines ${ startLine } - ${ endLine } ` : '' } (${ Math . round ( bestMatchScore * 100 ) } % similar, needs ${ Math . round ( this . fuzzyThreshold * 100 ) } %)`
244+ error : `No sufficiently similar match found${ lineRange } (${ Math . floor ( bestMatchScore * 100 ) } % similar, needs ${ Math . floor ( this . fuzzyThreshold * 100 ) } %)${ debugInfo } `
238245 } ;
239246 }
240247
0 commit comments