-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(codewhisperer): remove duplicate code #4033
- Loading branch information
1 parent
4bdcca3
commit 1e48a31
Showing
3 changed files
with
109 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import * as vscode from 'vscode' | ||
import { ConfigurationEntry, GetRecommendationsResponse, vsCodeState } from '../models/model' | ||
import { isCloud9 } from '../../shared/extensionUtilities' | ||
import { isInlineCompletionEnabled } from '../util/commonUtil' | ||
import { CodewhispererAutomatedTriggerType, CodewhispererTriggerType } from '../../shared/telemetry/telemetry' | ||
import { AuthUtil } from '../util/authUtil' | ||
import { isIamConnection } from '../../auth/connection' | ||
import { RecommendationHandler } from '../service/recommendationHandler' | ||
import { InlineCompletionService } from '../service/inlineCompletionService' | ||
import { ClassifierTrigger } from './classifierTrigger' | ||
import { DefaultCodeWhispererClient } from '../client/codewhisperer' | ||
|
||
export class RecommendationService { | ||
static #instance: RecommendationService | ||
|
||
public static get instance() { | ||
return (this.#instance ??= new RecommendationService()) | ||
} | ||
|
||
async generateRecommendation( | ||
client: DefaultCodeWhispererClient, | ||
editor: vscode.TextEditor, | ||
triggerType: CodewhispererTriggerType, | ||
config: ConfigurationEntry, | ||
autoTriggerType?: CodewhispererAutomatedTriggerType, | ||
event?: vscode.TextDocumentChangeEvent | ||
) { | ||
if (isCloud9('any')) { | ||
// C9 manual trigger key alt/option + C is ALWAYS enabled because the VSC version C9 is on doesn't support setContextKey which is used for CODEWHISPERER_ENABLED | ||
// therefore we need a connection check if there is ANY connection(regardless of the connection's state) connected to CodeWhisperer on C9 | ||
if (triggerType === 'OnDemand' && !AuthUtil.instance.isConnected()) { | ||
return | ||
} | ||
|
||
if (RecommendationHandler.instance.isGenerateRecommendationInProgress) { | ||
return | ||
} | ||
|
||
RecommendationHandler.instance.checkAndResetCancellationTokens() | ||
vsCodeState.isIntelliSenseActive = false | ||
RecommendationHandler.instance.isGenerateRecommendationInProgress = true | ||
|
||
try { | ||
let response: GetRecommendationsResponse = { | ||
result: 'Failed', | ||
errorMessage: undefined, | ||
} | ||
|
||
if (isCloud9('classic') || isIamConnection(AuthUtil.instance.conn)) { | ||
response = await RecommendationHandler.instance.getRecommendations( | ||
client, | ||
editor, | ||
triggerType, | ||
config, | ||
autoTriggerType, | ||
false | ||
) | ||
} else { | ||
if (AuthUtil.instance.isConnectionExpired()) { | ||
await AuthUtil.instance.showReauthenticatePrompt() | ||
} | ||
response = await RecommendationHandler.instance.getRecommendations( | ||
client, | ||
editor, | ||
triggerType, | ||
config, | ||
autoTriggerType, | ||
true | ||
) | ||
} | ||
if (RecommendationHandler.instance.canShowRecommendationInIntelliSense(editor, true, response)) { | ||
await vscode.commands.executeCommand('editor.action.triggerSuggest').then(() => { | ||
vsCodeState.isIntelliSenseActive = true | ||
}) | ||
} | ||
} finally { | ||
RecommendationHandler.instance.isGenerateRecommendationInProgress = false | ||
} | ||
} else if (isInlineCompletionEnabled()) { | ||
if (triggerType === 'OnDemand') { | ||
ClassifierTrigger.instance.recordClassifierResultForManualTrigger(editor) | ||
} | ||
await InlineCompletionService.instance.getPaginatedRecommendation( | ||
client, | ||
editor, | ||
triggerType, | ||
config, | ||
autoTriggerType, | ||
event | ||
) | ||
} | ||
} | ||
} |