Skip to content

Commit f6a02ae

Browse files
committed
refactor(codewhisperer): use globalState abstraction
1 parent f9ef131 commit f6a02ae

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

packages/core/src/codewhisperer/commands/onInlineAcceptance.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,25 @@ export const acceptSuggestion = Commands.declare(
5050
const editor = vscode.window.activeTextEditor
5151
await Container.instance.lineAnnotationController.refresh(editor, 'codewhisperer')
5252
const onAcceptanceFunc = isInlineCompletionEnabled() ? onInlineAcceptance : onAcceptance
53-
await onAcceptanceFunc(
54-
{
55-
editor,
56-
range,
57-
effectiveRange,
58-
acceptIndex,
59-
recommendation,
60-
requestId,
61-
sessionId,
62-
triggerType,
63-
completionType,
64-
language,
65-
references,
66-
},
67-
context.extensionContext.globalState
68-
)
53+
await onAcceptanceFunc({
54+
editor,
55+
range,
56+
effectiveRange,
57+
acceptIndex,
58+
recommendation,
59+
requestId,
60+
sessionId,
61+
triggerType,
62+
completionType,
63+
language,
64+
references,
65+
})
6966
}
7067
)
7168
/**
7269
* This function is called when user accepts a intelliSense suggestion or an inline suggestion
7370
*/
74-
export async function onInlineAcceptance(
75-
acceptanceEntry: OnRecommendationAcceptanceEntry,
76-
globalStorage: vscode.Memento
77-
) {
71+
export async function onInlineAcceptance(acceptanceEntry: OnRecommendationAcceptanceEntry) {
7872
RecommendationHandler.instance.cancelPaginatedRequest()
7973
RecommendationHandler.instance.disposeInlineCompletion()
8074

@@ -121,7 +115,7 @@ export async function onInlineAcceptance(
121115
language: languageContext.language,
122116
})
123117
const insertedCoderange = new vscode.Range(start, end)
124-
CodeWhispererCodeCoverageTracker.getTracker(languageContext.language, globalStorage)?.countAcceptedTokens(
118+
CodeWhispererCodeCoverageTracker.getTracker(languageContext.language)?.countAcceptedTokens(
125119
insertedCoderange,
126120
acceptanceEntry.editor.document.getText(insertedCoderange),
127121
acceptanceEntry.editor.document.fileName

packages/core/src/codewhisperer/tracker/codewhispererCodeCoverageTracker.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,7 @@ export class CodeWhispererCodeCoverageTracker {
383383

384384
public static readonly instances = new Map<CodewhispererLanguage, CodeWhispererCodeCoverageTracker>()
385385

386-
public static getTracker(
387-
language: string,
388-
memeto: vscode.Memento = globals.context.globalState
389-
): CodeWhispererCodeCoverageTracker | undefined {
386+
public static getTracker(language: string): CodeWhispererCodeCoverageTracker | undefined {
390387
if (!runtimeLanguageContext.isLanguageSupported(language)) {
391388
return undefined
392389
}

packages/core/src/codewhispererChat/editor/codelens.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export class TryChatCodeLensProvider implements vscode.CodeLensProvider {
3333
/** How many times we want to show the CodeLens */
3434
static readonly maxCount = 10
3535
static readonly debounceMillis: number = 700
36-
static readonly showCodeLensId = `aws.amazonq.showTryChatCodeLens`
3736

3837
private static providerDisposable: vscode.Disposable | undefined = undefined
3938
private disposables: vscode.Disposable[] = []
@@ -58,7 +57,7 @@ export class TryChatCodeLensProvider implements vscode.CodeLensProvider {
5857
}
5958

6059
static async register(isAmazonQVisible: vscode.Event<boolean>): Promise<boolean> {
61-
const shouldShow = globals.context.globalState.get(this.showCodeLensId, true)
60+
const shouldShow = globals.globalState.tryGet('aws.amazonq.showTryChatCodeLens', Boolean, true)
6261
if (!shouldShow) {
6362
return false
6463
}
@@ -85,7 +84,7 @@ export class TryChatCodeLensProvider implements vscode.CodeLensProvider {
8584
* - lineAnnotationController is visible (i.e not in end state)
8685
* - Amazon Q chat is visible
8786
*/
88-
const isLineAnnotationVisible = globals.context.globalState.get<string>(inlinehintKey)
87+
const isLineAnnotationVisible = globals.globalState.tryGet(inlinehintKey, String)
8988
if ((isLineAnnotationVisible && isLineAnnotationVisible !== EndState.id) || this.isAmazonQVisible) {
9089
return resolve([])
9190
}
@@ -150,7 +149,7 @@ export class TryChatCodeLensProvider implements vscode.CodeLensProvider {
150149
}
151150

152151
dispose() {
153-
void globals.context.globalState.update(TryChatCodeLensProvider.showCodeLensId, false)
152+
globals.globalState.tryUpdate('aws.amazonq.showTryChatCodeLens', false)
154153
TryChatCodeLensProvider.providerDisposable?.dispose()
155154
this.disposables.forEach((d) => d.dispose())
156155
}

packages/core/src/shared/globalState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type globalKey =
2626
| ToolIdStateKey
2727
| 'aws.amazonq.codewhisperer.newCustomizations'
2828
| 'aws.amazonq.hasShownWalkthrough'
29+
| 'aws.amazonq.showTryChatCodeLens'
2930
| 'aws.downloadPath'
3031
| 'aws.lastTouchedS3Folder'
3132
| 'aws.lastUploadedToS3Folder'

packages/core/src/test/codewhispererChat/editor/codelens.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ describe('TryChatCodeLensProvider', () => {
9393
it('does not register the provider if we do not want to show the code lens', async function () {
9494
await TryChatCodeLensProvider.register(isAmazonQVisibleEvent)
9595
// indicate we do not want to show it
96-
await globals.context.globalState.update(TryChatCodeLensProvider.showCodeLensId, false)
96+
await globals.globalState.update('aws.amazonq.showTryChatCodeLens', false)
9797
// ensure we do not show it
9898
assert.deepStrictEqual(await TryChatCodeLensProvider.register(isAmazonQVisibleEvent), false)
9999

100100
// indicate we want to show it
101-
await globals.context.globalState.update(TryChatCodeLensProvider.showCodeLensId, true)
101+
await globals.globalState.update('aws.amazonq.showTryChatCodeLens', true)
102102
// The general toolkit activation will have already registered this provider, so it throws when we try again
103103
// But if it throws it implies it tried to register it.
104104
await assert.rejects(TryChatCodeLensProvider.register(isAmazonQVisibleEvent), {
@@ -125,7 +125,7 @@ describe('TryChatCodeLensProvider', () => {
125125
stubConnection('connected')
126126
isAmazonQVisibleEventEmitter.fire(false)
127127
// indicate lineAnnotationController is not visible and in end state
128-
await globals.context.globalState.update(inlinehintKey, EndState.id)
128+
await globals.globalState.update(inlinehintKey, EndState.id)
129129

130130
let codeLensCount = 0
131131
const modifierKey = resolveModifierKey()
@@ -165,7 +165,7 @@ describe('TryChatCodeLensProvider', () => {
165165

166166
lineAnnotationControllerStates.forEach((id: string) => {
167167
it(`id - ${id}`, async () => {
168-
await globals.context.globalState.update(inlinehintKey, id)
168+
await globals.globalState.update(inlinehintKey, id)
169169

170170
const emptyResult = await instance.provideCodeLenses(
171171
{} as any,

0 commit comments

Comments
 (0)