From b283f1b4024361e6713c41905795b4ff9a16fea0 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 9 Jan 2025 10:14:26 -0800 Subject: [PATCH] update control for posthog --- core/util/posthog.ts | 5 ++-- .../RecentlyVisitedRangesService.ts | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/core/util/posthog.ts b/core/util/posthog.ts index fa0c29f87..198041207 100644 --- a/core/util/posthog.ts +++ b/core/util/posthog.ts @@ -12,7 +12,7 @@ export enum PosthogFeatureFlag { export const EXPERIMENTS: { [key in PosthogFeatureFlag]: { - [key: string]: { value: number }; + [key: string]: { value: any }; }; } = { [PosthogFeatureFlag.AutocompleteTimeout]: { @@ -22,7 +22,8 @@ export const EXPERIMENTS: { "450": { value: 450 }, }, [PosthogFeatureFlag.RecentlyVisitedRangesNumSurroundingLines]: { - control: { value: 5 }, + control: { value: null }, + "5": { value: 5 }, "10": { value: 10 }, "15": { value: 15 }, "20": { value: 20 }, diff --git a/extensions/vscode/src/autocomplete/RecentlyVisitedRangesService.ts b/extensions/vscode/src/autocomplete/RecentlyVisitedRangesService.ts index 5c083b841..38240d9e1 100644 --- a/extensions/vscode/src/autocomplete/RecentlyVisitedRangesService.ts +++ b/extensions/vscode/src/autocomplete/RecentlyVisitedRangesService.ts @@ -19,6 +19,7 @@ export class RecentlyVisitedRangesService { private numSurroundingLines = 5; private maxRecentFiles = 3; private maxSnippetsPerFile = 3; + private isActive: boolean = false; constructor(private readonly ide: IDE) { this.cache = new LRUCache< @@ -31,18 +32,23 @@ export class RecentlyVisitedRangesService { void this.setNumSurroundingLinesFromPostHogExperiment(); vscode.window.onDidChangeTextEditorSelection( - this.handleOnDidChangeTextEditorSelection, + this.cacheCurrentSelectionContext, ); } private async setNumSurroundingLinesFromPostHogExperiment() { - this.numSurroundingLines = - (await Telemetry.getValueForFeatureFlag( - PosthogFeatureFlag.AutocompleteTimeout, - )) ?? this.numSurroundingLines; + const recentlyVisitedRangesNumSurroundingLines = + await Telemetry.getValueForFeatureFlag( + PosthogFeatureFlag.RecentlyVisitedRangesNumSurroundingLines, + ); + + if (recentlyVisitedRangesNumSurroundingLines) { + this.numSurroundingLines = recentlyVisitedRangesNumSurroundingLines; + this.isActive = true; + } } - private handleOnDidChangeTextEditorSelection = async ( + private cacheCurrentSelectionContext = async ( event: vscode.TextEditorSelectionChangeEvent, ) => { const filepath = event.textEditor.document.uri.toString(); @@ -76,7 +82,7 @@ export class RecentlyVisitedRangesService { this.cache.set(filepath, newSnippets); } catch (err) { console.error( - "Error caching recently visited ranges for autocomplete", + "Error caching recently visited ranges for autocomplete: ", err, ); return; @@ -89,6 +95,12 @@ export class RecentlyVisitedRangesService { * @returns Array of code snippets from recently visited files */ public getSnippets(): AutocompleteCodeSnippet[] { + // If the feature is not active (e.g. user is in control group), + // return an empty array to exclude recently visited ranges snippets + if (!this.isActive) { + return []; + } + const currentFilepath = vscode.window.activeTextEditor?.document.uri.toString(); let allSnippets: Array =