From 85dd9db5977948556bfc4d1cd4c9abf2e14a4410 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Fri, 20 Jun 2025 13:48:52 -0700 Subject: [PATCH] make the class a singleton, save user diff --- core/nextEdit/NextEditProvider.ts | 40 ++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/core/nextEdit/NextEditProvider.ts b/core/nextEdit/NextEditProvider.ts index cc5ec2dfe..5bf121f53 100644 --- a/core/nextEdit/NextEditProvider.ts +++ b/core/nextEdit/NextEditProvider.ts @@ -40,6 +40,8 @@ const ERRORS_TO_IGNORE = [ ]; export class NextEditProvider { + private static instance: NextEditProvider | null = null; + private autocompleteCache = AutocompleteLruCache.get(); public errorsShown: Set = new Set(); private bracketMatchingService = new BracketMatchingService(); @@ -48,8 +50,9 @@ export class NextEditProvider { private loggingService = new AutocompleteLoggingService(); private contextRetrievalService: ContextRetrievalService; private endpointType: "default" | "fineTuned"; + private diffContext: string = ""; - constructor( + private constructor( private readonly configHandler: ConfigHandler, private readonly ide: IDE, private readonly _injectedGetLlm: () => Promise, @@ -62,6 +65,40 @@ export class NextEditProvider { this.endpointType = endpointType; } + public static initialize( + configHandler: ConfigHandler, + ide: IDE, + injectedGetLlm: () => Promise, + onError: (e: any) => void, + getDefinitionsFromLsp: GetLspDefinitionsFunction, + endpointType: "default" | "fineTuned", + ): NextEditProvider { + if (!NextEditProvider.instance) { + NextEditProvider.instance = new NextEditProvider( + configHandler, + ide, + injectedGetLlm, + onError, + getDefinitionsFromLsp, + endpointType, + ); + } + return NextEditProvider.instance; + } + + public static getInstance(): NextEditProvider { + if (!NextEditProvider.instance) { + throw new Error( + "NextEditProvider has not been initialized. Call initialize() first.", + ); + } + return NextEditProvider.instance; + } + + public addDiffToContext(diff: string): void { + this.diffContext = diff; + } + private async _prepareLlm(): Promise { const llm = await this._injectedGetLlm(); @@ -206,6 +243,7 @@ export class NextEditProvider { snippetPayload, this.ide, helper, + this.diffContext, ), ); }