Skip to content

Commit 1bd36f4

Browse files
Merge master into feature/code-diff
2 parents 21c2942 + 4543593 commit 1bd36f4

File tree

2 files changed

+6
-74
lines changed

2 files changed

+6
-74
lines changed

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

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

6-
import { diffChars } from 'diff'
6+
import { diffWordsWithSpace } 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'
@@ -413,45 +413,6 @@ export class SvgGenerationService {
413413
const originalRanges: Range[] = []
414414
const afterRanges: Range[] = []
415415

416-
/**
417-
* Merges ranges on the same line that are separated by only one character
418-
*/
419-
const mergeAdjacentRanges = (ranges: Range[]): Range[] => {
420-
const sortedRanges = [...ranges].sort((a, b) => {
421-
if (a.line !== b.line) {
422-
return a.line - b.line
423-
}
424-
return a.start - b.start
425-
})
426-
427-
const result: Range[] = []
428-
429-
// Process all ranges
430-
for (let i = 0; i < sortedRanges.length; i++) {
431-
const current = sortedRanges[i]
432-
433-
// If this is the last range or ranges are on different lines, add it directly
434-
if (i === sortedRanges.length - 1 || current.line !== sortedRanges[i + 1].line) {
435-
result.push(current)
436-
continue
437-
}
438-
439-
// Check if current range and next range can be merged
440-
const next = sortedRanges[i + 1]
441-
if (current.line === next.line && next.start - current.end <= 1) {
442-
sortedRanges[i + 1] = {
443-
line: current.line,
444-
start: current.start,
445-
end: Math.max(current.end, next.end),
446-
}
447-
} else {
448-
result.push(current)
449-
}
450-
}
451-
452-
return result
453-
}
454-
455416
// Create reverse mapping for quicker lookups
456417
const reverseMap = new Map<string, string>()
457418
for (const [original, modified] of modifiedLines.entries()) {
@@ -465,7 +426,7 @@ export class SvgGenerationService {
465426
// If line exists in modifiedLines as a key, process character diffs
466427
if (Array.from(modifiedLines.keys()).includes(line)) {
467428
const modifiedLine = modifiedLines.get(line)!
468-
const changes = diffChars(line, modifiedLine)
429+
const changes = diffWordsWithSpace(line, modifiedLine)
469430

470431
let charPos = 0
471432
for (const part of changes) {
@@ -497,7 +458,7 @@ export class SvgGenerationService {
497458

498459
if (reverseMap.has(line)) {
499460
const originalLine = reverseMap.get(line)!
500-
const changes = diffChars(originalLine, line)
461+
const changes = diffWordsWithSpace(originalLine, line)
501462

502463
let charPos = 0
503464
for (const part of changes) {
@@ -522,12 +483,9 @@ export class SvgGenerationService {
522483
}
523484
}
524485

525-
const mergedOriginalRanges = mergeAdjacentRanges(originalRanges)
526-
const mergedAfterRanges = mergeAdjacentRanges(afterRanges)
527-
528486
return {
529-
removedRanges: mergedOriginalRanges,
530-
addedRanges: mergedAfterRanges,
487+
removedRanges: originalRanges,
488+
addedRanges: afterRanges,
531489
}
532490
}
533491
}

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('SvgGenerationService', function () {
150150
})
151151

152152
describe('highlight ranges', function () {
153-
it('should generate highlight ranges for character-level changes', function () {
153+
it('should generate highlight ranges for word-level changes', function () {
154154
const originalCode = ['function test() {', ' return 42;', '}']
155155
const afterCode = ['function test() {', ' return 100;', '}']
156156
const modifiedLines = new Map([[' return 42;', ' return 100;']])
@@ -174,32 +174,6 @@ describe('SvgGenerationService', function () {
174174
assert.ok(addedRange.end > addedRange.start)
175175
})
176176

177-
it('should merge adjacent highlight ranges', function () {
178-
const originalCode = ['function test() {', ' return 42;', '}']
179-
const afterCode = ['function test() {', ' return 100;', '}']
180-
const modifiedLines = new Map([[' return 42;', ' return 100;']])
181-
182-
const generateHighlightRanges = (service as any).generateHighlightRanges.bind(service)
183-
const result = generateHighlightRanges(originalCode, afterCode, modifiedLines)
184-
185-
// Adjacent ranges should be merged
186-
const sortedRanges = [...result.addedRanges].sort((a, b) => {
187-
if (a.line !== b.line) {
188-
return a.line - b.line
189-
}
190-
return a.start - b.start
191-
})
192-
193-
// Check that no adjacent ranges exist
194-
for (let i = 0; i < sortedRanges.length - 1; i++) {
195-
const current = sortedRanges[i]
196-
const next = sortedRanges[i + 1]
197-
if (current.line === next.line) {
198-
assert.ok(next.start - current.end > 1, 'Adjacent ranges should be merged')
199-
}
200-
}
201-
})
202-
203177
it('should handle HTML escaping in highlight edits', function () {
204178
const newLines = ['function test() {', ' return "<script>alert(1)</script>";', '}']
205179
const highlightRanges = [{ line: 1, start: 10, end: 35 }]

0 commit comments

Comments
 (0)