Skip to content

Commit 77259e1

Browse files
authored
feat(amazonq): trigger inline suggestion via acceptance for EDITS
Merge pull request #7592 from atonaamz/nep-flare
2 parents d33bc14 + 4a1b444 commit 77259e1

File tree

7 files changed

+30
-108
lines changed

7 files changed

+30
-108
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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LogInlineCompletionSessionResultsParams } from '@aws/language-server-ru
1212
import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes/protocol'
1313
import path from 'path'
1414
import { imageVerticalOffset } from './svgGenerator'
15+
import { AmazonQInlineCompletionItemProvider } from '../completion'
1516

1617
export class EditDecorationManager {
1718
private imageDecorationType: vscode.TextEditorDecorationType
@@ -280,16 +281,15 @@ export async function displaySvgDecoration(
280281
session: CodeWhispererSession,
281282
languageClient: LanguageClient,
282283
item: InlineCompletionItemWithReferences,
283-
addedCharacterCount: number,
284-
deletedCharacterCount: number
284+
inlineCompletionProvider?: AmazonQInlineCompletionItemProvider
285285
) {
286286
const originalCode = editor.document.getText()
287287

288288
decorationManager.displayEditSuggestion(
289289
editor,
290290
svgImage,
291291
startLine,
292-
() => {
292+
async () => {
293293
// Handle accept
294294
getLogger().info('Edit suggestion accepted')
295295

@@ -315,10 +315,20 @@ export async function displaySvgDecoration(
315315
},
316316
totalSessionDisplayTime: Date.now() - session.requestStartTime,
317317
firstCompletionDisplayLatency: session.firstCompletionDisplayLatency,
318-
addedCharacterCount: addedCharacterCount,
319-
deletedCharacterCount: deletedCharacterCount,
320318
}
321319
languageClient.sendNotification('aws/logInlineCompletionSessionResults', params)
320+
if (inlineCompletionProvider) {
321+
await inlineCompletionProvider.provideInlineCompletionItems(
322+
editor.document,
323+
endPosition,
324+
{
325+
triggerKind: vscode.InlineCompletionTriggerKind.Automatic,
326+
selectedCompletionInfo: undefined,
327+
},
328+
new vscode.CancellationTokenSource().token,
329+
{ emitTelemetry: false, showUi: false }
330+
)
331+
}
322332
},
323333
() => {
324334
// Handle reject
@@ -333,8 +343,6 @@ export async function displaySvgDecoration(
333343
discarded: false,
334344
},
335345
},
336-
// addedCharacterCount: addedCharacterCount,
337-
// deletedCharacterCount: deletedCharacterCount,
338346
}
339347
languageClient.sendNotification('aws/logInlineCompletionSessionResults', params)
340348
},

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { getLogger } from 'aws-core-vscode/shared'
1010
import { LanguageClient } from 'vscode-languageclient'
1111
import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes/protocol'
1212
import { CodeWhispererSession } from '../sessionManager'
13+
import { AmazonQInlineCompletionItemProvider } from '../completion'
1314

1415
export async function showEdits(
1516
item: InlineCompletionItemWithReferences,
1617
editor: vscode.TextEditor | undefined,
1718
session: CodeWhispererSession,
18-
languageClient: LanguageClient
19+
languageClient: LanguageClient,
20+
inlineCompletionProvider?: AmazonQInlineCompletionItemProvider
1921
) {
2022
if (!editor) {
2123
return
@@ -24,14 +26,8 @@ export async function showEdits(
2426
const svgGenerationService = new SvgGenerationService()
2527
// Generate your SVG image with the file contents
2628
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)
29+
const { svgImage, startLine, newCode, origionalCodeHighlightRange } =
30+
await svgGenerationService.generateDiffSvg(currentFile, item.insertText as string)
3531

3632
if (svgImage) {
3733
// display the SVG image
@@ -44,8 +40,7 @@ export async function showEdits(
4440
session,
4541
languageClient,
4642
item,
47-
addedCharacterCount,
48-
deletedCharacterCount
43+
inlineCompletionProvider
4944
)
5045
} else {
5146
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/src/app/inline/completion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ ${itemLog}
354354
if (item.isInlineEdit) {
355355
// Check if Next Edit Prediction feature flag is enabled
356356
if (Experiments.instance.isExperimentEnabled('amazonqLSPNEP')) {
357-
void showEdits(item, editor, session, this.languageClient).then(() => {
357+
void showEdits(item, editor, session, this.languageClient, this).then(() => {
358358
const t3 = performance.now()
359359
logstr = logstr + `- duration since trigger to NEP suggestion is displayed: ${t3 - t0}ms`
360360
this.logger.info(logstr)

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)