@@ -109,22 +109,21 @@ export class CopilotCompletionContextProvider implements ContextResolver<CodeSni
109109
110110 // Get the completion context with a timeout and a cancellation token.
111111 // The cancellationToken indicates that the value should not be returned nor cached.
112- private async getCompletionContextWithCancellation ( context : ResolveRequest , startTime : number , out : Logger ,
113- telemetry : CopilotCompletionContextTelemetry , token : vscode . CancellationToken ) :
112+ private async getCompletionContextWithCancellation ( context : ResolveRequest , featureFlag : CopilotCompletionContextFeatures ,
113+ startTime : number , out : Logger , telemetry : CopilotCompletionContextTelemetry , token : vscode . CancellationToken ) :
114114 Promise < CopilotCompletionContextResult | undefined > {
115115 const documentUri = context . documentContext . uri ;
116116 const caretOffset = context . documentContext . offset ;
117117 let logMessage = `Copilot: getCompletionContext(${ documentUri } :${ caretOffset } ):` ;
118118 try {
119- const featureNames : CopilotCompletionContextFeatures = await this . getEnabledFeatures ( context ) ;
120119 telemetry . addRequestMetadata ( documentUri , caretOffset , context . completionId ,
121- context . documentContext . languageId , { featureFlag : featureNames } ) ;
120+ context . documentContext . languageId , { featureFlag : featureFlag } ) ;
122121 const docUri = vscode . Uri . parse ( documentUri ) ;
123122 const client = clients . getClientFor ( docUri ) ;
124123 if ( ! client ) { throw WellKnownErrors . clientNotFound ( ) ; }
125124 const getCompletionContextStartTime = performance . now ( ) ;
126125 const copilotCompletionContext : CopilotCompletionContextResult =
127- await client . getCompletionContext ( docUri , caretOffset , featureNames , token ) ;
126+ await client . getCompletionContext ( docUri , caretOffset , featureFlag , token ) ;
128127 telemetry . addRequestId ( copilotCompletionContext . requestId ) ;
129128 logMessage += ` (id:${ copilotCompletionContext . requestId } )` ;
130129 if ( ! copilotCompletionContext . isResultMissing ) {
@@ -171,7 +170,6 @@ export class CopilotCompletionContextProvider implements ContextResolver<CodeSni
171170 telemetry . send ( "cache" ) ;
172171 }
173172 }
174- static readonly CppCodeSnippetsProviderEnabled = 'CppCodeSnippetsProviderEnabled' ;
175173 static readonly CppCodeSnippetsEnabledFeatures = 'CppCodeSnippetsEnabledFeatures' ;
176174 static readonly CppCodeSnippetsTimeBudgetFactor = 'CppCodeSnippetsTimeBudgetFactor' ;
177175 static readonly CppCodeSnippetsMaxDistanceToCaret = 'CppCodeSnippetsMaxDistanceToCaret' ;
@@ -207,11 +205,11 @@ export class CopilotCompletionContextProvider implements ContextResolver<CodeSni
207205 }
208206 }
209207
210- private async getEnabledFeatures ( context : ResolveRequest ) : Promise < CopilotCompletionContextFeatures > {
211- let result = CopilotCompletionContextFeatures . None ;
208+ private async getEnabledFeatureFlag ( context : ResolveRequest ) : Promise < CopilotCompletionContextFeatures | undefined > {
209+ let result ;
212210 for ( const featureName of await this . getEnabledFeatureNames ( context ) ?? [ ] ) {
213211 const flag = CopilotCompletionContextFeatures [ featureName as keyof typeof CopilotCompletionContextFeatures ] ;
214- if ( flag ) { result = result + flag ; }
212+ if ( flag !== undefined ) { result = ( result ?? 0 ) + flag ; }
215213 }
216214 return result ;
217215 }
@@ -242,17 +240,19 @@ export class CopilotCompletionContextProvider implements ContextResolver<CodeSni
242240 const timeBudgetFactor = await this . fetchTimeBudgetFactor ( context ) ;
243241 const maxCaretDistance = await this . fetchMaxDistanceToCaret ( context ) ;
244242 const telemetry = new CopilotCompletionContextTelemetry ( ) ;
245- telemetry . addRequestMetadata ( context . documentContext . uri , context . documentContext . offset ,
246- context . completionId , context . documentContext . languageId , { timeBudgetFactor, maxCaretDistance } ) ;
247243 let copilotCompletionContext : CopilotCompletionContextResult | undefined ;
248244 let copilotCompletionContextKind : CopilotCompletionKind = CopilotCompletionKind . Unknown ;
249245 try {
246+ const featureFlag : CopilotCompletionContextFeatures | undefined = await this . getEnabledFeatureFlag ( context ) ;
247+ telemetry . addRequestMetadata ( context . documentContext . uri , context . documentContext . offset ,
248+ context . completionId , context . documentContext . languageId , { featureFlag, timeBudgetFactor, maxCaretDistance } ) ;
249+ if ( featureFlag === undefined ) { return [ ] ; }
250250 this . completionContextCancellation . cancel ( ) ;
251251 this . completionContextCancellation = new vscode . CancellationTokenSource ( ) ;
252252 const docUri = context . documentContext . uri ;
253253 const cacheEntry : CacheEntry | undefined = this . completionContextCache . get ( docUri . toString ( ) ) ;
254254 const defaultValue = cacheEntry ?. [ 1 ] ;
255- const computeSnippetsPromise = this . getCompletionContextWithCancellation ( context ,
255+ const computeSnippetsPromise = this . getCompletionContextWithCancellation ( context , featureFlag ,
256256 resolveStartTime , out , telemetry . fork ( ) , this . completionContextCancellation . token ) ;
257257 [ copilotCompletionContext , copilotCompletionContextKind ] = await this . waitForCompletionWithTimeoutAndCancellation (
258258 computeSnippetsPromise , defaultValue , context . timeBudget * timeBudgetFactor , copilotCancel ) ;
@@ -302,23 +302,17 @@ export class CopilotCompletionContextProvider implements ContextResolver<CodeSni
302302 public async registerCopilotContextProvider ( ) : Promise < void > {
303303 const properties : Record < string , string > = { } ;
304304 const registerCopilotContextProvider = 'registerCopilotContextProvider' ;
305- let isCppCodeSnippetsProviderEnabled : boolean | undefined ;
306305 try {
307- isCppCodeSnippetsProviderEnabled = await telemetry . isExperimentEnabled ( CopilotCompletionContextProvider . CppCodeSnippetsProviderEnabled ) ;
308- if ( ! isCppCodeSnippetsProviderEnabled ) { isCppCodeSnippetsProviderEnabled = new CppSettings ( ) . cppCodeSnippetsFeatureNames !== undefined ; }
309- properties [ "isCppCodeSnippetsProviderEnabled" ] = isCppCodeSnippetsProviderEnabled . toString ( ) ;
310- if ( isCppCodeSnippetsProviderEnabled ) {
311- const copilotApi = await getCopilotApi ( ) ;
312- if ( ! copilotApi ) { throw new CopilotContextProviderException ( "getCopilotApi() returned null." ) ; }
313- const contextAPI = await copilotApi . getContextProviderAPI ( "v1" ) ;
314- if ( ! contextAPI ) { throw new CopilotContextProviderException ( "getContextProviderAPI(v1) returned null." ) ; }
315- this . contextProviderDisposable = contextAPI . registerContextProvider ( {
316- id : CopilotCompletionContextProvider . providerId ,
317- selector : CopilotCompletionContextProvider . defaultCppDocumentSelector ,
318- resolver : this
319- } ) ;
320- properties [ "cppCodeSnippetsProviderRegistered" ] = "true" ;
321- }
306+ const copilotApi = await getCopilotApi ( ) ;
307+ if ( ! copilotApi ) { throw new CopilotContextProviderException ( "getCopilotApi() returned null." ) ; }
308+ const contextAPI = await copilotApi . getContextProviderAPI ( "v1" ) ;
309+ if ( ! contextAPI ) { throw new CopilotContextProviderException ( "getContextProviderAPI(v1) returned null." ) ; }
310+ this . contextProviderDisposable = contextAPI . registerContextProvider ( {
311+ id : CopilotCompletionContextProvider . providerId ,
312+ selector : CopilotCompletionContextProvider . defaultCppDocumentSelector ,
313+ resolver : this
314+ } ) ;
315+ properties [ "cppCodeSnippetsProviderRegistered" ] = "true" ;
322316 } catch ( e ) {
323317 console . warn ( "Failed to register the Copilot Context Provider." ) ;
324318 properties [ "error" ] = "Failed to register the Copilot Context Provider" ;
0 commit comments