Skip to content

Commit 40adfc7

Browse files
committed
fix: removing logic to calculate addedCharacterCount and deletedCharacterCount
1 parent bfdb0eb commit 40adfc7

File tree

6 files changed

+12
-107
lines changed

6 files changed

+12
-107
lines changed

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

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@ export type LineDiff =
1616
* @param unifiedDiff The unified diff content
1717
* @returns The modified code after applying the diff
1818
*/
19-
export function applyUnifiedDiff(
20-
docText: string,
21-
unifiedDiff: string
22-
): { appliedCode: string; addedCharacterCount: number; deletedCharacterCount: number } {
19+
export function applyUnifiedDiff(docText: string, unifiedDiff: string): string {
2320
try {
24-
const { addedCharacterCount, deletedCharacterCount } = getAddedAndDeletedCharCount(unifiedDiff)
2521
// First try the standard diff package
2622
try {
2723
const result = applyPatch(docText, unifiedDiff)
2824
if (result !== false) {
29-
return {
30-
appliedCode: result,
31-
addedCharacterCount: addedCharacterCount,
32-
deletedCharacterCount: deletedCharacterCount,
33-
}
25+
return result
3426
}
3527
} catch (error) {}
3628

@@ -94,49 +86,8 @@ export function applyUnifiedDiff(
9486
// Replace the text
9587
result = result.replace(textToReplace, newText)
9688
}
97-
return {
98-
appliedCode: result,
99-
addedCharacterCount: addedCharacterCount,
100-
deletedCharacterCount: deletedCharacterCount,
101-
}
89+
return result
10290
} catch (error) {
103-
return {
104-
appliedCode: docText, // Return original text if all methods fail
105-
addedCharacterCount: 0,
106-
deletedCharacterCount: 0,
107-
}
108-
}
109-
}
110-
111-
export function getAddedAndDeletedCharCount(diff: string): {
112-
addedCharacterCount: number
113-
deletedCharacterCount: number
114-
} {
115-
let addedCharacterCount = 0
116-
let deletedCharacterCount = 0
117-
let i = 0
118-
const lines = diff.split('\n')
119-
while (i < lines.length) {
120-
const line = lines[i]
121-
if (line.startsWith('+') && !line.startsWith('+++')) {
122-
addedCharacterCount += line.length - 1
123-
} else if (line.startsWith('-') && !line.startsWith('---')) {
124-
const removedLine = line.substring(1)
125-
deletedCharacterCount += removedLine.length
126-
127-
// Check if this is a modified line rather than a pure deletion
128-
const nextLine = lines[i + 1]
129-
if (nextLine && nextLine.startsWith('+') && !nextLine.startsWith('+++') && nextLine.includes(removedLine)) {
130-
// This is a modified line, not a pure deletion
131-
// We've already counted the deletion, so we'll just increment i to skip the next line
132-
// since we'll process the addition on the next iteration
133-
i += 1
134-
}
135-
}
136-
i += 1
137-
}
138-
return {
139-
addedCharacterCount,
140-
deletedCharacterCount,
91+
return docText // Return original text if all methods fail
14192
}
14293
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ export async function displaySvgDecoration(
279279
originalCodeHighlightRanges: Array<{ line: number; start: number; end: number }>,
280280
session: CodeWhispererSession,
281281
languageClient: LanguageClient,
282-
item: InlineCompletionItemWithReferences,
283-
addedCharacterCount: number,
284-
deletedCharacterCount: number
282+
item: InlineCompletionItemWithReferences
285283
) {
286284
const originalCode = editor.document.getText()
287285

@@ -315,8 +313,6 @@ export async function displaySvgDecoration(
315313
},
316314
totalSessionDisplayTime: Date.now() - session.requestStartTime,
317315
firstCompletionDisplayLatency: session.firstCompletionDisplayLatency,
318-
addedCharacterCount: addedCharacterCount,
319-
deletedCharacterCount: deletedCharacterCount,
320316
}
321317
languageClient.sendNotification('aws/logInlineCompletionSessionResults', params)
322318
},
@@ -333,8 +329,6 @@ export async function displaySvgDecoration(
333329
discarded: false,
334330
},
335331
},
336-
// addedCharacterCount: addedCharacterCount,
337-
// deletedCharacterCount: deletedCharacterCount,
338332
}
339333
languageClient.sendNotification('aws/logInlineCompletionSessionResults', params)
340334
},

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ export async function showEdits(
2424
const svgGenerationService = new SvgGenerationService()
2525
// Generate your SVG image with the file contents
2626
const currentFile = editor.document.uri.fsPath
27-
const {
28-
svgImage,
29-
startLine,
30-
newCode,
31-
origionalCodeHighlightRange,
32-
addedCharacterCount,
33-
deletedCharacterCount,
34-
} = await svgGenerationService.generateDiffSvg(currentFile, item.insertText as string)
27+
const { svgImage, startLine, newCode, origionalCodeHighlightRange } =
28+
await svgGenerationService.generateDiffSvg(currentFile, item.insertText as string)
3529

3630
if (svgImage) {
3731
// display the SVG image
@@ -43,9 +37,7 @@ export async function showEdits(
4337
origionalCodeHighlightRange,
4438
session,
4539
languageClient,
46-
item,
47-
addedCharacterCount,
48-
deletedCharacterCount
40+
item
4941
)
5042
} else {
5143
getLogger('nextEditPrediction').error('SVG image generation returned an empty result.')

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { diffChars } 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'
10-
import { applyUnifiedDiff } from './diffUtils'
1110
type Range = { line: number; start: number; end: number }
1211

1312
const logger = getLogger('nextEditPrediction')
@@ -29,16 +28,13 @@ export class SvgGenerationService {
2928
startLine: number
3029
newCode: string
3130
origionalCodeHighlightRange: Range[]
32-
addedCharacterCount: number
33-
deletedCharacterCount: number
3431
}> {
3532
const textDoc = await vscode.workspace.openTextDocument(filePath)
3633
const originalCode = textDoc.getText()
3734
if (originalCode === '') {
3835
logger.error(`udiff format error`)
3936
throw new ToolkitError('udiff format error')
4037
}
41-
const { addedCharacterCount, deletedCharacterCount } = applyUnifiedDiff(originalCode, udiff)
4238
const newCode = await diffUtilities.getPatchedCode(filePath, udiff)
4339
const modifiedLines = diffUtilities.getModifiedLinesFromUnifiedDiff(udiff)
4440
// TODO remove
@@ -91,8 +87,6 @@ export class SvgGenerationService {
9187
startLine: editStartLine,
9288
newCode: newCode,
9389
origionalCodeHighlightRange: highlightRanges.removedRanges,
94-
addedCharacterCount,
95-
deletedCharacterCount,
9690
}
9791
}
9892

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { applyUnifiedDiff, getAddedAndDeletedCharCount } from '../../../../../src/app/inline/EditRendering/diffUtils'
7+
import { applyUnifiedDiff } from '../../../../../src/app/inline/EditRendering/diffUtils'
88

99
describe('diffUtils', function () {
1010
describe('applyUnifiedDiff', function () {
@@ -27,35 +27,13 @@ describe('diffUtils', function () {
2727
const expectedResult = 'function add(a, b) {\n // Add two numbers\n return a + b; // Return the sum\n}'
2828

2929
// Apply the diff
30-
const { appliedCode } = applyUnifiedDiff(originalCode, unifiedDiff)
30+
const appliedCode = applyUnifiedDiff(originalCode, unifiedDiff)
3131

3232
// Verify the result
3333
assert.strictEqual(appliedCode, expectedResult)
3434
})
3535
})
3636

37-
describe('getAddedAndDeletedCharCount', function () {
38-
it('should correctly calculate added and deleted character counts', function () {
39-
// Unified diff with additions and deletions
40-
const unifiedDiff =
41-
'--- a/file.js\n' +
42-
'+++ b/file.js\n' +
43-
'@@ -1,3 +1,4 @@\n' +
44-
' function add(a, b) {\n' +
45-
'+ // Add two numbers\n' +
46-
'- return a + b;\n' +
47-
'+ return a + b; // Return the sum\n' +
48-
' }'
49-
50-
// Calculate character counts
51-
const { addedCharacterCount, deletedCharacterCount } = getAddedAndDeletedCharCount(unifiedDiff)
52-
53-
// Verify the counts with the actual values from the implementation
54-
assert.strictEqual(addedCharacterCount, 20)
55-
assert.strictEqual(deletedCharacterCount, 15)
56-
})
57-
})
58-
5937
describe('applyUnifiedDiff with complex changes', function () {
6038
it('should handle multiple hunks in a diff', function () {
6139
// Original code with multiple functions
@@ -96,7 +74,7 @@ describe('diffUtils', function () {
9674
'}'
9775

9876
// Apply the diff
99-
const { appliedCode } = applyUnifiedDiff(originalCode, unifiedDiff)
77+
const appliedCode = applyUnifiedDiff(originalCode, unifiedDiff)
10078

10179
// Verify the result
10280
assert.strictEqual(appliedCode, expectedResult)

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ describe('showEdits', function () {
3131
startLine: 5,
3232
newCode: 'console.log("Hello World");',
3333
origionalCodeHighlightRange: [{ line: 5, start: 0, end: 10 }],
34-
addedCharacterCount: 25,
35-
deletedCharacterCount: 0,
3634
...overrides,
3735
}
3836
}
@@ -170,9 +168,7 @@ describe('showEdits', function () {
170168
mockSvgResult.origionalCodeHighlightRange,
171169
sessionStub,
172170
languageClientStub,
173-
itemStub,
174-
mockSvgResult.addedCharacterCount,
175-
mockSvgResult.deletedCharacterCount
171+
itemStub
176172
)
177173

178174
// Verify no errors were logged

0 commit comments

Comments
 (0)