@@ -11,6 +11,12 @@ type Range = { line: number; start: number; end: number }
1111
1212const logger = getLogger ( 'nextEditPrediction' )
1313export const imageVerticalOffset = 1
14+ export const emptyDiffSvg = {
15+ svgImage : vscode . Uri . parse ( '' ) ,
16+ startLine : 0 ,
17+ newCode : '' ,
18+ originalCodeHighlightRange : [ ] ,
19+ }
1420
1521export class SvgGenerationService {
1622 /**
@@ -27,7 +33,7 @@ export class SvgGenerationService {
2733 svgImage : vscode . Uri
2834 startLine : number
2935 newCode : string
30- origionalCodeHighlightRange : Range [ ]
36+ originalCodeHighlightRange : Range [ ]
3137 } > {
3238 const textDoc = await vscode . workspace . openTextDocument ( filePath )
3339 const originalCode = textDoc . getText ( ) . replaceAll ( '\r\n' , '\n' )
@@ -52,6 +58,21 @@ export class SvgGenerationService {
5258
5359 // Get edit diffs with highlight
5460 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+
5576 const highlightRanges = this . generateHighlightRanges ( removedLines , addedLines , modifiedLines )
5677 const diffAddedWithHighlight = this . getHighlightEdit ( addedLines , highlightRanges . addedRanges )
5778
@@ -61,13 +82,6 @@ export class SvgGenerationService {
6182 registerWindow ( window , document )
6283 const draw = SVG ( document . documentElement ) as any
6384
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- )
7185 const { width, height } = this . calculateDimensions ( addedLines , currentTheme )
7286 draw . size ( width + offset , height )
7387
@@ -86,7 +100,7 @@ export class SvgGenerationService {
86100 svgImage : vscode . Uri . parse ( svgResult ) ,
87101 startLine : editStartLine ,
88102 newCode : newCode ,
89- origionalCodeHighlightRange : highlightRanges . removedRanges ,
103+ originalCodeHighlightRange : highlightRanges . removedRanges ,
90104 }
91105 }
92106
@@ -356,12 +370,23 @@ export class SvgGenerationService {
356370 newLines : string [ ] ,
357371 diffLines : string [ ] ,
358372 theme : editorThemeInfo
359- ) : { offset : number ; editStartLine : number } {
373+ ) : { offset : number ; editStartLine : number ; isPositionValid : boolean } {
360374 // Determine the starting line of the edit in the original file
361375 let editStartLineInOldFile = 0
362376 const maxLength = Math . min ( originalLines . length , newLines . length )
363377
364378 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+ }
365390 if ( originalLines [ i ] !== newLines [ i ] || i === maxLength ) {
366391 editStartLineInOldFile = i
367392 break
@@ -386,7 +411,7 @@ export class SvgGenerationService {
386411 const startLineLength = originalLines [ startLine ] ?. length || 0
387412 const offset = ( maxLineLength - startLineLength ) * theme . fontSize * 0.7 + 10 // padding
388413
389- return { offset, editStartLine : editStartLineInOldFile }
414+ return { offset, editStartLine : editStartLineInOldFile , isPositionValid : true }
390415 }
391416
392417 private escapeHtml ( text : string ) : string {
0 commit comments