Skip to content

Commit c384d0e

Browse files
authored
refactor(codewhisperer): unify isCWRunning state to one place #4336
Problem: There are 2 state variables maintained in CW which serve the same purpose to represent "CW is running/thinking to generate recommendations": 1. `InlineCompletionService.isPaginatedRunning` https://github.com/aws/aws-toolkit-vscode/blob/master/src/codewhisperer/service/inlineCompletionService.ts#L30 2. `RecommendationHandler.isGenerateRecommendationInProgress` https://github.com/aws/aws-toolkit-vscode/blob/master/src/codewhisperer/service/recommendationHandler.ts#L75 Soltuion: Merge these 2 variables into one at `RecommendationService` instead of maintaining 2 separately at their lower level component. / RecommendationHandler.getRecommendation (C9) RecommendationService \ InlineCompletionService.getPaginatedRecommendation (VSCode)
1 parent d36f4b4 commit c384d0e

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

src/codewhisperer/service/inlineCompletionService.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class InlineCompletionService {
2727
private maxPage = 100
2828
private statusBar: CodeWhispererStatusBar
2929
private _showRecommendationTimer?: NodeJS.Timer
30-
private _isPaginationRunning = false
3130

3231
constructor(statusBar: CodeWhispererStatusBar = CodeWhispererStatusBar.instance) {
3332
this.statusBar = statusBar
@@ -86,11 +85,7 @@ export class InlineCompletionService {
8685
autoTriggerType?: CodewhispererAutomatedTriggerType,
8786
event?: vscode.TextDocumentChangeEvent
8887
) {
89-
if (
90-
vsCodeState.isCodeWhispererEditing ||
91-
this._isPaginationRunning ||
92-
RecommendationHandler.instance.isSuggestionVisible()
93-
) {
88+
if (vsCodeState.isCodeWhispererEditing || RecommendationHandler.instance.isSuggestionVisible()) {
9489
return
9590
}
9691

@@ -164,10 +159,8 @@ export class InlineCompletionService {
164159
}
165160

166161
private async setState(state: keyof typeof states) {
167-
this._isPaginationRunning = false
168162
switch (state) {
169163
case 'loading': {
170-
this._isPaginationRunning = true
171164
await this.statusBar.setState('loading')
172165
break
173166
}
@@ -185,10 +178,6 @@ export class InlineCompletionService {
185178
}
186179
}
187180
}
188-
189-
isPaginationRunning(): boolean {
190-
return this._isPaginationRunning
191-
}
192181
}
193182

194183
/** The states that the completion service can be in */

src/codewhisperer/service/keyStrokeHandler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { RecommendationHandler } from './recommendationHandler'
1313
import { CodewhispererAutomatedTriggerType } from '../../shared/telemetry/telemetry'
1414
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
1515
import { isInlineCompletionEnabled } from '../util/commonUtil'
16-
import { InlineCompletionService } from './inlineCompletionService'
1716
import { ClassifierTrigger } from './classifierTrigger'
1817
import { extractContextForCodeWhisperer } from '../util/editorContext'
1918
import { RecommendationService } from './recommendationService'
@@ -79,10 +78,10 @@ export class KeyStrokeHandler {
7978
}
8079

8180
public shouldTriggerIdleTime(): boolean {
82-
if (isCloud9() && RecommendationHandler.instance.isGenerateRecommendationInProgress) {
81+
if (isCloud9() && RecommendationService.instance.isRunning) {
8382
return false
8483
}
85-
if (isInlineCompletionEnabled() && InlineCompletionService.instance.isPaginationRunning()) {
84+
if (isInlineCompletionEnabled() && RecommendationService.instance.isRunning) {
8685
return false
8786
}
8887
return true

src/codewhisperer/service/recommendationHandler.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export class RecommendationHandler {
7272
public requestId: string
7373
private nextToken: string
7474
private cancellationToken: vscode.CancellationTokenSource
75-
public isGenerateRecommendationInProgress: boolean
7675
private _onDidReceiveRecommendation: vscode.EventEmitter<void> = new vscode.EventEmitter<void>()
7776
public readonly onDidReceiveRecommendation: vscode.Event<void> = this._onDidReceiveRecommendation.event
7877
private inlineCompletionProvider?: CWInlineCompletionItemProvider
@@ -88,7 +87,6 @@ export class RecommendationHandler {
8887
this.nextToken = ''
8988
this.lastInvocationTime = performance.now() - CodeWhispererConstants.invocationTimeIntervalThreshold * 1000
9089
this.cancellationToken = new vscode.CancellationTokenSource()
91-
this.isGenerateRecommendationInProgress = false
9290
this.prev = new vscode.Disposable(() => {})
9391
this.next = new vscode.Disposable(() => {})
9492
this.reject = new vscode.Disposable(() => {})

src/codewhisperer/service/recommendationService.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import { DefaultCodeWhispererClient } from '../client/codewhisperer'
1717
export class RecommendationService {
1818
static #instance: RecommendationService
1919

20+
private _isRunning: boolean = false
21+
get isRunning() {
22+
return this._isRunning
23+
}
24+
2025
public static get instance() {
2126
return (this.#instance ??= new RecommendationService())
2227
}
@@ -29,20 +34,20 @@ export class RecommendationService {
2934
autoTriggerType?: CodewhispererAutomatedTriggerType,
3035
event?: vscode.TextDocumentChangeEvent
3136
) {
37+
if (this._isRunning) {
38+
return
39+
}
40+
3241
if (isCloud9('any')) {
3342
// 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
3443
// therefore we need a connection check if there is ANY connection(regardless of the connection's state) connected to CodeWhisperer on C9
3544
if (triggerType === 'OnDemand' && !AuthUtil.instance.isConnected()) {
3645
return
3746
}
3847

39-
if (RecommendationHandler.instance.isGenerateRecommendationInProgress) {
40-
return
41-
}
42-
4348
RecommendationHandler.instance.checkAndResetCancellationTokens()
4449
vsCodeState.isIntelliSenseActive = false
45-
RecommendationHandler.instance.isGenerateRecommendationInProgress = true
50+
this._isRunning = true
4651

4752
try {
4853
let response: GetRecommendationsResponse = {
@@ -78,20 +83,27 @@ export class RecommendationService {
7883
})
7984
}
8085
} finally {
81-
RecommendationHandler.instance.isGenerateRecommendationInProgress = false
86+
this._isRunning = false
8287
}
8388
} else if (isInlineCompletionEnabled()) {
8489
if (triggerType === 'OnDemand') {
8590
ClassifierTrigger.instance.recordClassifierResultForManualTrigger(editor)
8691
}
87-
await InlineCompletionService.instance.getPaginatedRecommendation(
88-
client,
89-
editor,
90-
triggerType,
91-
config,
92-
autoTriggerType,
93-
event
94-
)
92+
93+
this._isRunning = true
94+
95+
try {
96+
await InlineCompletionService.instance.getPaginatedRecommendation(
97+
client,
98+
editor,
99+
triggerType,
100+
config,
101+
autoTriggerType,
102+
event
103+
)
104+
} finally {
105+
this._isRunning = false
106+
}
95107
}
96108
}
97109
}

src/test/codewhisperer/service/keyStrokeHandler.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { isInlineCompletionEnabled } from '../../../codewhisperer/util/commonUti
2121
import { ClassifierTrigger } from '../../../codewhisperer/service/classifierTrigger'
2222
import { CodeWhispererUserGroupSettings } from '../../../codewhisperer/util/userGroupUtil'
2323
import * as CodeWhispererConstants from '../../../codewhisperer/models/constants'
24+
import { RecommendationService } from '../../../codewhisperer/service/recommendationService'
2425

2526
describe('keyStrokeHandler', function () {
2627
const config: ConfigurationEntry = {
@@ -192,7 +193,7 @@ describe('keyStrokeHandler', function () {
192193
describe('shouldTriggerIdleTime', function () {
193194
it('should return false when inline is enabled and inline completion is in progress ', function () {
194195
const keyStrokeHandler = new KeyStrokeHandler()
195-
sinon.stub(InlineCompletionService.instance, 'isPaginationRunning').returns(true)
196+
sinon.stub(RecommendationService.instance, 'isRunning').get(() => true)
196197
const result = keyStrokeHandler.shouldTriggerIdleTime()
197198
assert.strictEqual(result, !isInlineCompletionEnabled())
198199
})

0 commit comments

Comments
 (0)