@@ -39,18 +39,25 @@ export class ContextProxy {
3939 private stateCache : GlobalState
4040 private secretCache : SecretState
4141 private _isInitialized = false
42+ private _workspacePath : string | undefined
4243
43- constructor ( context : vscode . ExtensionContext ) {
44+ constructor ( context : vscode . ExtensionContext , workspacePath ?: string ) {
4445 this . originalContext = context
4546 this . stateCache = { }
4647 this . secretCache = { }
4748 this . _isInitialized = false
49+ this . _workspacePath = workspacePath
4850 }
4951
5052 public get isInitialized ( ) {
5153 return this . _isInitialized
5254 }
5355
56+ // Public getter for workspacePath to allow checking current workspace
57+ public get workspacePath ( ) : string | undefined {
58+ return this . _workspacePath
59+ }
60+
5461 public async initialize ( ) {
5562 for ( const key of GLOBAL_STATE_KEYS ) {
5663 try {
@@ -290,6 +297,50 @@ export class ContextProxy {
290297 await this . initialize ( )
291298 }
292299
300+ /**
301+ * Get workspace-specific state value
302+ * Falls back to global state if workspace value doesn't exist
303+ */
304+ public getWorkspaceState < T > ( key : string , defaultValue ?: T ) : T | undefined {
305+ if ( ! this . _workspacePath ) {
306+ // If no workspace, fall back to global state
307+ return this . originalContext . globalState . get < T > ( key ) ?? defaultValue
308+ }
309+
310+ // Create a workspace-specific key
311+ const workspaceKey = `workspace:${ this . _workspacePath } :${ key } `
312+ const workspaceValue = this . originalContext . globalState . get < T > ( workspaceKey )
313+
314+ if ( workspaceValue !== undefined ) {
315+ return workspaceValue
316+ }
317+
318+ // Fall back to global state
319+ return this . originalContext . globalState . get < T > ( key ) ?? defaultValue
320+ }
321+
322+ /**
323+ * Update workspace-specific state value
324+ */
325+ public async updateWorkspaceState < T > ( key : string , value : T ) : Promise < void > {
326+ if ( ! this . _workspacePath ) {
327+ // If no workspace, update global state
328+ await this . originalContext . globalState . update ( key , value )
329+ return
330+ }
331+
332+ // Create a workspace-specific key
333+ const workspaceKey = `workspace:${ this . _workspacePath } :${ key } `
334+ await this . originalContext . globalState . update ( workspaceKey , value )
335+ }
336+
337+ /**
338+ * Set the workspace path for workspace-specific settings
339+ */
340+ public setWorkspacePath ( workspacePath : string | undefined ) : void {
341+ this . _workspacePath = workspacePath
342+ }
343+
293344 private static _instance : ContextProxy | null = null
294345
295346 static get instance ( ) {
@@ -300,12 +351,16 @@ export class ContextProxy {
300351 return this . _instance
301352 }
302353
303- static async getInstance ( context : vscode . ExtensionContext ) {
354+ static async getInstance ( context : vscode . ExtensionContext , workspacePath ?: string ) {
304355 if ( this . _instance ) {
356+ // Update workspace path if provided
357+ if ( workspacePath !== undefined ) {
358+ this . _instance . setWorkspacePath ( workspacePath )
359+ }
305360 return this . _instance
306361 }
307362
308- this . _instance = new ContextProxy ( context )
363+ this . _instance = new ContextProxy ( context , workspacePath )
309364 await this . _instance . initialize ( )
310365
311366 return this . _instance
0 commit comments