diff --git a/core/autocomplete/snippets/diffSnippetCache.test.ts b/core/autocomplete/snippets/diffSnippetCache.test.ts new file mode 100644 index 000000000..526e10d15 --- /dev/null +++ b/core/autocomplete/snippets/diffSnippetCache.test.ts @@ -0,0 +1,50 @@ +import { DiffSnippetsCache } from "./diffSnippetCache"; + +test("DiffSnippetsCache getInstance returns singleton instance", () => { + const instance1 = DiffSnippetsCache.getInstance(); + const instance2 = DiffSnippetsCache.getInstance(); + expect(instance1).toBe(instance2); +}); + +test("DiffSnippetsCache set stores value and returns it", () => { + const cache = DiffSnippetsCache.getInstance(); + const timestamp = Date.now(); + const value = { test: "data" }; + + const result = cache.set(timestamp, value); + expect(result).toBe(value); + expect(cache.get(timestamp)).toBe(value); +}); + +test("DiffSnippetsCache get returns undefined for non-existent timestamp", () => { + const cache = DiffSnippetsCache.getInstance(); + const timestamp = Date.now(); + + expect(cache.get(timestamp + 1000)).toBeUndefined(); +}); + +test("DiffSnippetsCache set clears cache when timestamp changes", () => { + const cache = DiffSnippetsCache.getInstance(); + const timestamp1 = Date.now(); + const timestamp2 = timestamp1 + 1000; + const value1 = { test: "data1" }; + const value2 = { test: "data2" }; + + cache.set(timestamp1, value1); + cache.set(timestamp2, value2); + + expect(cache.get(timestamp1)).toBeUndefined(); + expect(cache.get(timestamp2)).toBe(value2); +}); + +test("DiffSnippetsCache set maintains cache with same timestamp", () => { + const cache = DiffSnippetsCache.getInstance(); + const timestamp = Date.now(); + const value1 = { test: "data1" }; + const value2 = { test: "data2" }; + + cache.set(timestamp, value1); + cache.set(timestamp, value2); + + expect(cache.get(timestamp)).toBe(value2); +}); diff --git a/core/autocomplete/snippets/diffSnippetCache.ts b/core/autocomplete/snippets/diffSnippetCache.ts new file mode 100644 index 000000000..dfa712b65 --- /dev/null +++ b/core/autocomplete/snippets/diffSnippetCache.ts @@ -0,0 +1,28 @@ +export class DiffSnippetsCache { + private static instance: DiffSnippetsCache; + private cache: Map = new Map(); + private lastTimestamp: number = 0; + + private constructor() {} + + public static getInstance(): DiffSnippetsCache { + if (!DiffSnippetsCache.instance) { + DiffSnippetsCache.instance = new DiffSnippetsCache(); + } + return DiffSnippetsCache.instance; + } + + public set(timestamp: number, value: T): T { + // Clear old cache entry if exists + if (this.lastTimestamp !== timestamp) { + this.cache.clear(); + } + this.lastTimestamp = timestamp; + this.cache.set(timestamp, value); + return value; + } + + public get(timestamp: number): any | undefined { + return this.cache.get(timestamp); + } +} diff --git a/core/autocomplete/snippets/getAllSnippets.ts b/core/autocomplete/snippets/getAllSnippets.ts index 4446df85a..48b77902d 100644 --- a/core/autocomplete/snippets/getAllSnippets.ts +++ b/core/autocomplete/snippets/getAllSnippets.ts @@ -3,6 +3,7 @@ import { findUriInDirs } from "../../util/uri"; import { ContextRetrievalService } from "../context/ContextRetrievalService"; import { GetLspDefinitionsFunction } from "../types"; import { HelperVars } from "../util/HelperVars"; +import { DiffSnippetsCache } from "./diffSnippetCache"; import { AutocompleteClipboardSnippet, @@ -23,35 +24,14 @@ export interface SnippetPayload { clipboardSnippets: AutocompleteClipboardSnippet[]; } -function racePromise(promise: Promise): Promise { +function racePromise(promise: Promise, timeout = 100): Promise { const timeoutPromise = new Promise((resolve) => { - setTimeout(() => resolve([]), 100); + setTimeout(() => resolve([]), timeout); }); return Promise.race([promise, timeoutPromise]); } -class DiffSnippetsCache { - private cache: Map = new Map(); - private lastTimestamp: number = 0; - - public set(timestamp: number, value: T): T { - // Clear old cache entry if exists - if (this.lastTimestamp !== timestamp) { - this.cache.clear(); - } - this.lastTimestamp = timestamp; - this.cache.set(timestamp, value); - return value; - } - - public get(timestamp: number): any | undefined { - return this.cache.get(timestamp); - } -} - -const diffSnippetsCache = new DiffSnippetsCache(); - // Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems, // or they might separately track recently edited ranges) async function getIdeSnippets( @@ -114,6 +94,7 @@ const getDiffSnippets = async ( ide: IDE, ideInfo: IdeInfo | undefined, ): Promise => { + const diffSnippetsCache = DiffSnippetsCache.getInstance(); if (ideInfo?.ideType !== "vscode") { // Disabling for non-vscode IDEs for now // See https://github.com/continuedev/continue/issues/4130