Skip to content

Commit e6ff1ee

Browse files
authored
Merge pull request #7778 from atonaamz/nep-flare
fix(amazonq): skip EDITS suggestion if there is no change between current and new code suggestion
2 parents e70b5ce + 0dd5bf4 commit e6ff1ee

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

packages/amazonq/src/app/inline/EditRendering/imageRenderer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export async function showEdits(
2626
const svgGenerationService = new SvgGenerationService()
2727
// Generate your SVG image with the file contents
2828
const currentFile = editor.document.uri.fsPath
29-
const { svgImage, startLine, newCode, origionalCodeHighlightRange } =
30-
await svgGenerationService.generateDiffSvg(currentFile, item.insertText as string)
29+
const { svgImage, startLine, newCode, originalCodeHighlightRange } = await svgGenerationService.generateDiffSvg(
30+
currentFile,
31+
item.insertText as string
32+
)
3133

3234
// TODO: To investigate why it fails and patch [generateDiffSvg]
3335
if (newCode.length === 0) {
@@ -42,7 +44,7 @@ export async function showEdits(
4244
svgImage,
4345
startLine,
4446
newCode,
45-
origionalCodeHighlightRange,
47+
originalCodeHighlightRange,
4648
session,
4749
languageClient,
4850
item,

packages/amazonq/src/app/inline/EditRendering/svgGenerator.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Range = { line: number; start: number; end: number }
1111

1212
const logger = getLogger('nextEditPrediction')
1313
export const imageVerticalOffset = 1
14+
export const emptyDiffSvg = {
15+
svgImage: vscode.Uri.parse(''),
16+
startLine: 0,
17+
newCode: '',
18+
originalCodeHighlightRange: [],
19+
}
1420

1521
export 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 {

packages/amazonq/test/unit/app/inline/EditRendering/imageRenderer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('showEdits', function () {
3030
svgImage: vscode.Uri.file('/path/to/generated.svg'),
3131
startLine: 5,
3232
newCode: 'console.log("Hello World");',
33-
origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }],
33+
originalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }],
3434
...overrides,
3535
}
3636
}
@@ -167,7 +167,7 @@ describe('showEdits', function () {
167167
mockSvgResult.svgImage,
168168
mockSvgResult.startLine,
169169
mockSvgResult.newCode,
170-
mockSvgResult.origionalCodeHighlightRange,
170+
mockSvgResult.originalCodeHighlightRange,
171171
sessionStub,
172172
languageClientStub,
173173
itemStub

0 commit comments

Comments
 (0)