Skip to content

Commit 6642840

Browse files
authored
fix(amazonq): use current file content to calculate highlighted ranges (#7792)
## Problem The unifiedDiff from suggestionResponse is outdated due to the change of context, resulting to the wrong line getting highlighted while rendering. ## Solution Calculate the addedLines and removedLines using the current code content and new code suggestion. --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9eaeb1c commit 6642840

File tree

2 files changed

+24
-53
lines changed

2 files changed

+24
-53
lines changed

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

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { diffWordsWithSpace } from 'diff'
6+
import { diffWordsWithSpace, diffLines } from 'diff'
77
import * as vscode from 'vscode'
88
import { ToolkitError, getLogger } from 'aws-core-vscode/shared'
99
import { diffUtilities } from 'aws-core-vscode/shared'
@@ -42,10 +42,6 @@ export class SvgGenerationService {
4242
throw new ToolkitError('udiff format error')
4343
}
4444
const newCode = await diffUtilities.getPatchedCode(filePath, udiff)
45-
const modifiedLines = diffUtilities.getModifiedLinesFromUnifiedDiff(udiff)
46-
// TODO remove
47-
// eslint-disable-next-line aws-toolkits/no-json-stringify-in-log
48-
logger.info(`Line mapping: ${JSON.stringify(modifiedLines)}`)
4945

5046
const { createSVGWindow } = await import('svgdom')
5147

@@ -57,7 +53,12 @@ export class SvgGenerationService {
5753
const currentTheme = this.getEditorTheme()
5854

5955
// Get edit diffs with highlight
60-
const { addedLines, removedLines } = this.getEditedLinesFromDiff(udiff)
56+
const { addedLines, removedLines } = this.getEditedLinesFromCode(originalCode, newCode)
57+
58+
const modifiedLines = diffUtilities.getModifiedLinesFromCode(addedLines, removedLines)
59+
// TODO remove
60+
// eslint-disable-next-line aws-toolkits/no-json-stringify-in-log
61+
logger.info(`Line mapping: ${JSON.stringify(modifiedLines)}`)
6162

6263
// Calculate dimensions based on code content
6364
const { offset, editStartLine, isPositionValid } = this.calculatePosition(
@@ -175,43 +176,25 @@ export class SvgGenerationService {
175176
}
176177

177178
/**
178-
* Extract added and removed lines from the unified diff
179-
* @param unifiedDiff The unified diff string
179+
* Extract added and removed lines by comparing original and new code
180+
* @param originalCode The original code string
181+
* @param newCode The new code string
180182
* @returns Object containing arrays of added and removed lines
181183
*/
182-
private getEditedLinesFromDiff(unifiedDiff: string): { addedLines: string[]; removedLines: string[] } {
184+
private getEditedLinesFromCode(
185+
originalCode: string,
186+
newCode: string
187+
): { addedLines: string[]; removedLines: string[] } {
183188
const addedLines: string[] = []
184189
const removedLines: string[] = []
185-
const diffLines = unifiedDiff.split('\n')
186-
187-
// Find all hunks in the diff
188-
const hunkStarts = diffLines
189-
.map((line, index) => (line.startsWith('@@ ') ? index : -1))
190-
.filter((index) => index !== -1)
191190

192-
// Process each hunk to find added and removed lines
193-
for (const hunkStart of hunkStarts) {
194-
const hunkHeader = diffLines[hunkStart]
195-
const match = hunkHeader.match(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/)
191+
const changes = diffLines(originalCode, newCode)
196192

197-
if (!match) {
198-
continue
199-
}
200-
201-
// Extract the content lines for this hunk
202-
let i = hunkStart + 1
203-
while (i < diffLines.length && !diffLines[i].startsWith('@@')) {
204-
// Include lines that were added (start with '+')
205-
if (diffLines[i].startsWith('+') && !diffLines[i].startsWith('+++')) {
206-
const lineContent = diffLines[i].substring(1)
207-
addedLines.push(lineContent)
208-
}
209-
// Include lines that were removed (start with '-')
210-
else if (diffLines[i].startsWith('-') && !diffLines[i].startsWith('---')) {
211-
const lineContent = diffLines[i].substring(1)
212-
removedLines.push(lineContent)
213-
}
214-
i++
193+
for (const change of changes) {
194+
if (change.added) {
195+
addedLines.push(...change.value.split('\n').filter((line) => line.length > 0))
196+
} else if (change.removed) {
197+
removedLines.push(...change.value.split('\n').filter((line) => line.length > 0))
215198
}
216199
}
217200

packages/core/src/shared/utilities/diffUtils.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,12 @@ export function getDiffCharsAndLines(
152152
}
153153

154154
/**
155-
* Extracts modified lines from a unified diff string.
156-
* @param unifiedDiff The unified diff patch as a string.
155+
* Extracts modified lines by comparing added and removed lines.
156+
* @param addedLines The array of added lines.
157+
* @param removedLines The array of removed lines.
157158
* @returns A Map where keys are removed lines and values are the corresponding modified (added) lines.
158159
*/
159-
export function getModifiedLinesFromUnifiedDiff(unifiedDiff: string): Map<string, string> {
160-
const removedLines: string[] = []
161-
const addedLines: string[] = []
162-
163-
// Parse the unified diff to extract removed and added lines
164-
const lines = unifiedDiff.split('\n')
165-
for (const line of lines) {
166-
if (line.startsWith('-') && !line.startsWith('---')) {
167-
removedLines.push(line.slice(1))
168-
} else if (line.startsWith('+') && !line.startsWith('+++')) {
169-
addedLines.push(line.slice(1))
170-
}
171-
}
172-
160+
export function getModifiedLinesFromCode(addedLines: string[], removedLines: string[]): Map<string, string> {
173161
const modifiedMap = new Map<string, string>()
174162
let addedIndex = 0
175163

0 commit comments

Comments
 (0)