diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a40eef..d4574b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- `cssModulesIntellisense.classNameCacheSize` setting to configure the LRU + class name cache size (default: 5). + ### Fixed - Minor Bugs diff --git a/docs/TODO.md b/docs/TODO.md index dae2fbc..48044b0 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -20,7 +20,4 @@ ## Future Improvements -- Add an extension setting to configure the class name cache size (LRU max entries). - Currently hardcoded to 3, which causes frequent cache evictions and re-parsing - in projects with more than 3 CSS module files. Should be user-configurable via - `cssModules.classNameCacheSize` in extension settings. + diff --git a/package.json b/package.json index 29f86be..0787975 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,12 @@ "**/*.test.ts", "**/*.spec.tsx" ] + }, + "cssModulesIntellisense.classNameCacheSize": { + "type": "number", + "description": "Maximum number of CSS module files to keep in the class name cache (LRU). Increase for projects with many CSS module files.", + "default": 5, + "minimum": 1 } } }, diff --git a/src/config.ts b/src/config.ts index 845c60a..5884be7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -52,6 +52,9 @@ export const CONFIGURATIONS = { BLACKLIST_PATTERNS: "blacklistPatterns", PROCESS_ON_EDIT: "processOnEdit", PROCESS_ON_SAVE: "processOnSave", + CLASS_NAME_CACHE_SIZE: "classNameCacheSize", }; +export const DEFAULT_CLASS_NAME_CACHE_SIZE = 5; + export const CSS_MODULES_CACHE_FILENAME = "cache.json"; diff --git a/src/libs/cache.ts b/src/libs/cache.ts index 99afd82..04dfa84 100644 --- a/src/libs/cache.ts +++ b/src/libs/cache.ts @@ -2,6 +2,7 @@ import * as fs from "fs"; import * as path from "path"; import type * as vscode from "vscode"; import { CSS_MODULES_CACHE_FILENAME, DEBOUNCE_TIMER } from "../config"; +import { getClassNameCacheSize } from "./vsConfig"; import { type CacheJsonObject, ClassNameCache, @@ -24,7 +25,9 @@ export default class Cache { * Cache mapping from imported CSS module paths (relative to workspace) * to the set of document paths that import them. */ - static classNameCache = new ClassNameCache(this.pathMapCache, { max: 3 }); + static classNameCache = new ClassNameCache(this.pathMapCache, { + max: getClassNameCacheSize(), + }); private static _context: vscode.ExtensionContext; /** diff --git a/src/libs/vsConfig.ts b/src/libs/vsConfig.ts index 59a82de..e9bdf3c 100644 --- a/src/libs/vsConfig.ts +++ b/src/libs/vsConfig.ts @@ -1,5 +1,9 @@ import * as vscode from "vscode"; -import { CONFIGURATION_KEY, CONFIGURATIONS } from "../config"; +import { + CONFIGURATION_KEY, + CONFIGURATIONS, + DEFAULT_CLASS_NAME_CACHE_SIZE, +} from "../config"; export const getVsConfig = () => vscode.workspace.getConfiguration(CONFIGURATION_KEY); @@ -9,3 +13,9 @@ export const getAliasMap = (): Record => export const getBlacklistPatterns = (): string[] => getVsConfig().get(CONFIGURATIONS.BLACKLIST_PATTERNS, []); + +export const getClassNameCacheSize = (): number => + getVsConfig().get( + CONFIGURATIONS.CLASS_NAME_CACHE_SIZE, + DEFAULT_CLASS_NAME_CACHE_SIZE + );