@@ -11,6 +11,12 @@ type Range = { line: number; start: number; end: number }
11
11
12
12
const logger = getLogger ( 'nextEditPrediction' )
13
13
export const imageVerticalOffset = 1
14
+ export const emptyDiffSvg = {
15
+ svgImage : vscode . Uri . parse ( '' ) ,
16
+ startLine : 0 ,
17
+ newCode : '' ,
18
+ originalCodeHighlightRange : [ ] ,
19
+ }
14
20
15
21
export class SvgGenerationService {
16
22
/**
@@ -27,7 +33,7 @@ export class SvgGenerationService {
27
33
svgImage : vscode . Uri
28
34
startLine : number
29
35
newCode : string
30
- origionalCodeHighlightRange : Range [ ]
36
+ originalCodeHighlightRange : Range [ ]
31
37
} > {
32
38
const textDoc = await vscode . workspace . openTextDocument ( filePath )
33
39
const originalCode = textDoc . getText ( ) . replaceAll ( '\r\n' , '\n' )
@@ -52,6 +58,21 @@ export class SvgGenerationService {
52
58
53
59
// Get edit diffs with highlight
54
60
const { addedLines, removedLines } = this . getEditedLinesFromDiff ( udiff )
61
+
62
+ // Calculate dimensions based on code content
63
+ const { offset, editStartLine, isPositionValid } = this . calculatePosition (
64
+ originalCode . split ( '\n' ) ,
65
+ newCode . split ( '\n' ) ,
66
+ addedLines ,
67
+ currentTheme
68
+ )
69
+
70
+ // if the position for the EDITS suggestion is not valid (there is no difference between new
71
+ // and current code content), return EMPTY_DIFF_SVG and skip the suggestion.
72
+ if ( ! isPositionValid ) {
73
+ return emptyDiffSvg
74
+ }
75
+
55
76
const highlightRanges = this . generateHighlightRanges ( removedLines , addedLines , modifiedLines )
56
77
const diffAddedWithHighlight = this . getHighlightEdit ( addedLines , highlightRanges . addedRanges )
57
78
@@ -61,13 +82,6 @@ export class SvgGenerationService {
61
82
registerWindow ( window , document )
62
83
const draw = SVG ( document . documentElement ) as any
63
84
64
- // Calculate dimensions based on code content
65
- const { offset, editStartLine } = this . calculatePosition (
66
- originalCode . split ( '\n' ) ,
67
- newCode . split ( '\n' ) ,
68
- addedLines ,
69
- currentTheme
70
- )
71
85
const { width, height } = this . calculateDimensions ( addedLines , currentTheme )
72
86
draw . size ( width + offset , height )
73
87
@@ -86,7 +100,7 @@ export class SvgGenerationService {
86
100
svgImage : vscode . Uri . parse ( svgResult ) ,
87
101
startLine : editStartLine ,
88
102
newCode : newCode ,
89
- origionalCodeHighlightRange : highlightRanges . removedRanges ,
103
+ originalCodeHighlightRange : highlightRanges . removedRanges ,
90
104
}
91
105
}
92
106
@@ -356,12 +370,23 @@ export class SvgGenerationService {
356
370
newLines : string [ ] ,
357
371
diffLines : string [ ] ,
358
372
theme : editorThemeInfo
359
- ) : { offset : number ; editStartLine : number } {
373
+ ) : { offset : number ; editStartLine : number ; isPositionValid : boolean } {
360
374
// Determine the starting line of the edit in the original file
361
375
let editStartLineInOldFile = 0
362
376
const maxLength = Math . min ( originalLines . length , newLines . length )
363
377
364
378
for ( let i = 0 ; i <= maxLength ; i ++ ) {
379
+ // if there is no difference between the original lines and the new lines, skip calculating for the start position.
380
+ if ( i === maxLength && originalLines [ i ] === newLines [ i ] && originalLines . length === newLines . length ) {
381
+ logger . info (
382
+ 'There is no difference between current and new code suggestion. Skip calculating for start position.'
383
+ )
384
+ return {
385
+ offset : 0 ,
386
+ editStartLine : 0 ,
387
+ isPositionValid : false ,
388
+ }
389
+ }
365
390
if ( originalLines [ i ] !== newLines [ i ] || i === maxLength ) {
366
391
editStartLineInOldFile = i
367
392
break
@@ -386,7 +411,7 @@ export class SvgGenerationService {
386
411
const startLineLength = originalLines [ startLine ] ?. length || 0
387
412
const offset = ( maxLineLength - startLineLength ) * theme . fontSize * 0.7 + 10 // padding
388
413
389
- return { offset, editStartLine : editStartLineInOldFile }
414
+ return { offset, editStartLine : editStartLineInOldFile , isPositionValid : true }
390
415
}
391
416
392
417
private escapeHtml ( text : string ) : string {
0 commit comments