@@ -11,6 +11,7 @@ import { IConfigurationService } from '../../../platform/configuration/common/co
11
11
import { ICustomInstructionsService } from '../../../platform/customInstructions/common/customInstructionsService' ;
12
12
import { OffsetLineColumnConverter } from '../../../platform/editing/common/offsetLineColumnConverter' ;
13
13
import { TextDocumentSnapshot } from '../../../platform/editing/common/textDocumentSnapshot' ;
14
+ import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService' ;
14
15
import { IAlternativeNotebookContentService } from '../../../platform/notebook/common/alternativeContent' ;
15
16
import { INotebookService } from '../../../platform/notebook/common/notebookService' ;
16
17
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService' ;
@@ -23,7 +24,6 @@ import { URI } from '../../../util/vs/base/common/uri';
23
24
import { Position as EditorPosition } from '../../../util/vs/editor/common/core/position' ;
24
25
import { ServicesAccessor } from '../../../util/vs/platform/instantiation/common/instantiation' ;
25
26
import { EndOfLine , MarkdownString , Position , Range , TextEdit } from '../../../vscodeTypes' ;
26
- import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService' ;
27
27
28
28
// Simplified Hunk type for the patch
29
29
interface Hunk {
@@ -349,42 +349,47 @@ function trySimilarityMatch(text: string, oldStr: string, newStr: string, eol: s
349
349
return { text, editPosition : [ ] , type : 'none' } ;
350
350
}
351
351
352
- let bestMatch = { index : - 1 , similarity : 0 , length : 0 } ;
352
+ let bestMatch = { startLine : - 1 , startOffset : 0 , oldLength : 0 , similarity : 0 } ;
353
+ let startOffset = 0 ;
353
354
354
355
// Sliding window approach to find the best matching section
355
356
for ( let i = 0 ; i <= lines . length - oldLines . length ; i ++ ) {
356
357
let totalSimilarity = 0 ;
358
+ let oldLength = 0 ;
357
359
358
360
// Calculate similarity for each line in the window
359
361
for ( let j = 0 ; j < oldLines . length ; j ++ ) {
360
362
const similarity = calculateSimilarity ( oldLines [ j ] , lines [ i + j ] ) ;
361
363
totalSimilarity += similarity ;
364
+ oldLength += lines [ i + j ] . length ;
362
365
}
363
366
364
367
const avgSimilarity = totalSimilarity / oldLines . length ;
365
368
if ( avgSimilarity > threshold && avgSimilarity > bestMatch . similarity ) {
366
- bestMatch = { index : i , similarity : avgSimilarity , length : oldLines . length } ;
369
+ bestMatch = { startLine : i , startOffset , similarity : avgSimilarity , oldLength : oldLength + ( oldLines . length - 1 ) * eol . length } ;
367
370
}
368
- }
369
371
370
- if ( bestMatch . index !== - 1 ) {
371
- // Found a match with similarity above the threshold
372
- const startIndex = bestMatch . index ;
373
-
374
- // Replace the matched section
375
- const newLines = [ ...lines ] ;
376
- newLines . splice ( startIndex , bestMatch . length , ...newStr . split ( eol ) ) ;
372
+ startOffset += lines [ i ] . length + eol . length ;
373
+ }
377
374
378
- return {
379
- text : newLines . join ( eol ) ,
380
- type : 'similarity' ,
381
- editPosition : [ [ startIndex , startIndex + bestMatch . length ] ] ,
382
- similarity : bestMatch . similarity ,
383
- suggestion : `Used similarity matching (${ ( bestMatch . similarity * 100 ) . toFixed ( 1 ) } % similar). Verify the replacement.`
384
- } ;
375
+ if ( bestMatch . startLine === - 1 ) {
376
+ return { text, editPosition : [ ] , type : 'none' } ;
385
377
}
386
378
387
- return { text, editPosition : [ ] , type : 'none' } ;
379
+ // Replace the matched section
380
+ const newLines = [
381
+ ...lines . slice ( 0 , bestMatch . startLine ) ,
382
+ ...newStr . split ( eol ) ,
383
+ ...lines . slice ( bestMatch . startLine + oldLines . length )
384
+ ] ;
385
+
386
+ return {
387
+ text : newLines . join ( eol ) ,
388
+ type : 'similarity' ,
389
+ editPosition : [ [ bestMatch . startOffset , bestMatch . startOffset + bestMatch . oldLength ] ] ,
390
+ similarity : bestMatch . similarity ,
391
+ suggestion : `Used similarity matching (${ ( bestMatch . similarity * 100 ) . toFixed ( 1 ) } % similar). Verify the replacement.`
392
+ } ;
388
393
}
389
394
390
395
// Function to generate a simple patch
0 commit comments