Skip to content

Commit abbfa10

Browse files
authored
Merge branch 'aws:master' into smai
2 parents bcfba0b + 6bfa34f commit abbfa10

File tree

7 files changed

+80
-68
lines changed

7 files changed

+80
-68
lines changed

buildspec/linuxTests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ phases:
4848
- VCS_COMMIT_ID="${CODEBUILD_RESOLVED_SOURCE_VERSION}"
4949
- CI_BUILD_URL=$(echo $CODEBUILD_BUILD_URL | sed 's/#/%23/g') # Encode `#` in the URL because otherwise the url is clipped in the Codecov.io site
5050
- CI_BUILD_ID="${CODEBUILD_BUILD_ID}"
51-
- test -n "${CODECOV_TOKEN}" && [ "$TARGET_BRANCH" = "master" ] && ./codecov --token=${CODECOV_TOKEN} --branch=${CODEBUILD_RESOLVED_SOURCE_VERSION} --repository=${CODEBUILD_SOURCE_REPO_URL} --file=./coverage/amazonq/lcov.info --file=./coverage/toolkit/lcov.info
51+
- test -n "${CODECOV_TOKEN}" && [ "$TARGET_BRANCH" = "master" ] && ./codecov --token=${CODECOV_TOKEN} --branch=${CODEBUILD_RESOLVED_SOURCE_VERSION} --repository=${CODEBUILD_SOURCE_REPO_URL} --file=./coverage/amazonq/lcov.info --file=./coverage/toolkit/lcov.info || true
5252

5353
reports:
5454
unit-test:

packages/amazonq/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,15 +852,21 @@
852852
},
853853
{
854854
"command": "aws.amazonq.inline.acceptEdit",
855-
"title": "%aws.amazonq.inline.acceptEdit%"
855+
"title": "%AWS.amazonq.inline.acceptEdit%",
856+
"category": "%AWS.amazonq.title%",
857+
"enablement": "aws.codewhisperer.connected"
856858
},
857859
{
858860
"command": "aws.amazonq.inline.rejectEdit",
859-
"title": "%aws.amazonq.inline.rejectEdit%"
861+
"title": "%AWS.amazonq.inline.rejectEdit%",
862+
"category": "%AWS.amazonq.title%",
863+
"enablement": "aws.codewhisperer.connected"
860864
},
861865
{
862866
"command": "aws.amazonq.toggleNextEditPredictionPanel",
863-
"title": "%aws.amazonq.toggleNextEditPredictionPanel%"
867+
"title": "%AWS.amazonq.toggleNextEditPredictionPanel%",
868+
"category": "%AWS.amazonq.title%",
869+
"enablement": "aws.codewhisperer.connected"
864870
}
865871
],
866872
"keybindings": [

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/amazonq/src/app/inline/recommendationService.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ import {
1212
import { CancellationToken, InlineCompletionContext, Position, TextDocument } from 'vscode'
1313
import { LanguageClient } from 'vscode-languageclient'
1414
import { SessionManager } from './sessionManager'
15-
import { AuthUtil, CodeWhispererStatusBarManager, vsCodeState } from 'aws-core-vscode/codewhisperer'
15+
import {
16+
AuthUtil,
17+
CodeWhispererConstants,
18+
CodeWhispererStatusBarManager,
19+
vsCodeState,
20+
} from 'aws-core-vscode/codewhisperer'
1621
import { TelemetryHelper } from './telemetryHelper'
1722
import { ICursorUpdateRecorder } from './cursorUpdateManager'
1823
import { getLogger } from 'aws-core-vscode/shared'
24+
import { asyncCallWithTimeout } from '../../util/timeoutUtil'
1925

2026
export interface GetAllRecommendationsOptions {
2127
emitTelemetry?: boolean
@@ -35,6 +41,23 @@ export class RecommendationService {
3541
this.cursorUpdateRecorder = recorder
3642
}
3743

44+
async getRecommendationsWithTimeout(
45+
languageClient: LanguageClient,
46+
request: InlineCompletionWithReferencesParams,
47+
token: CancellationToken
48+
) {
49+
const resultPromise: Promise<InlineCompletionListWithReferences> = languageClient.sendRequest(
50+
inlineCompletionWithReferencesRequestType.method,
51+
request,
52+
token
53+
)
54+
return await asyncCallWithTimeout<InlineCompletionListWithReferences>(
55+
resultPromise,
56+
`${inlineCompletionWithReferencesRequestType.method} time out`,
57+
CodeWhispererConstants.promiseTimeoutLimit * 1000
58+
)
59+
}
60+
3861
async getAllRecommendations(
3962
languageClient: LanguageClient,
4063
document: TextDocument,
@@ -93,11 +116,9 @@ export class RecommendationService {
93116
},
94117
})
95118
const t0 = performance.now()
96-
const result: InlineCompletionListWithReferences = await languageClient.sendRequest(
97-
inlineCompletionWithReferencesRequestType.method,
98-
request,
99-
token
100-
)
119+
120+
const result = await this.getRecommendationsWithTimeout(languageClient, request, token)
121+
101122
getLogger().info('Received inline completion response from LSP: %O', {
102123
sessionId: result.sessionId,
103124
latency: performance.now() - t0,
@@ -181,11 +202,7 @@ export class RecommendationService {
181202
while (nextToken) {
182203
const request = { ...initialRequest, partialResultToken: nextToken }
183204

184-
const result: InlineCompletionListWithReferences = await languageClient.sendRequest(
185-
inlineCompletionWithReferencesRequestType.method,
186-
request,
187-
token
188-
)
205+
const result = await this.getRecommendationsWithTimeout(languageClient, request, token)
189206
// when pagination is in progress, but user has already accepted or rejected an inline completion
190207
// then stop pagination
191208
if (this.sessionManager.getActiveSession() === undefined || vsCodeState.isCodeWhispererEditing) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export function asyncCallWithTimeout<T>(asyncPromise: Promise<T>, message: string, timeLimit: number): Promise<T> {
7+
let timeoutHandle: NodeJS.Timeout
8+
const timeoutPromise = new Promise((_resolve, reject) => {
9+
timeoutHandle = setTimeout(() => reject(new Error(message)), timeLimit)
10+
})
11+
return Promise.race([asyncPromise, timeoutPromise]).then((result) => {
12+
clearTimeout(timeoutHandle)
13+
return result as T
14+
})
15+
}

packages/core/package.nls.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@
358358
"AWS.amazonq.codewhisperer.title": "Amazon Q",
359359
"AWS.amazonq.toggleCodeSuggestion": "Toggle Auto-Suggestions",
360360
"AWS.amazonq.toggleCodeScan": "Toggle Auto-Scans",
361+
"AWS.amazonq.toggleNextEditPredictionPanel": "Toggle next edit suggestion",
361362
"AWS.amazonq.scans.scanProgress": "Sure. This may take a few minutes. I will send a notification when it’s complete if you navigate away from this panel.",
362363
"AWS.amazonq.scans.waitingForInput": "Waiting on your inputs...",
363364
"AWS.amazonq.scans.chooseScan.description": "Would you like to review your active file or the workspace you have open?",
@@ -465,6 +466,8 @@
465466
"AWS.amazonq.doc.pillText.reject": "Reject",
466467
"AWS.amazonq.doc.pillText.makeChanges": "Make changes",
467468
"AWS.amazonq.inline.invokeChat": "Inline chat",
469+
"AWS.amazonq.inline.acceptEdit": "Accept edit suggestion",
470+
"AWS.amazonq.inline.rejectEdit": "Reject edit suggestion",
468471
"AWS.amazonq.opensettings:": "Open settings",
469472
"AWS.toolkit.lambda.walkthrough.quickpickTitle": "Application Builder Walkthrough",
470473
"AWS.toolkit.lambda.walkthrough.title": "Get started building your application",

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)