@@ -101,27 +101,68 @@ export async function activate(context: vscode.ExtensionContext) {
101101 const contextProxy = await ContextProxy . getInstance ( context )
102102
103103 // Initialize code index managers for all workspace folders.
104- const codeIndexManagers : CodeIndexManager [ ] = [ ]
104+ const codeIndexManagers : Map < string , CodeIndexManager > = new Map ( )
105105
106+ // Helper function to initialize a CodeIndexManager for a folder
107+ async function initializeCodeIndexManagerForFolder ( folder : vscode . WorkspaceFolder ) : Promise < void > {
108+ const manager = CodeIndexManager . getInstance ( context , folder . uri . fsPath )
109+
110+ if ( manager ) {
111+ codeIndexManagers . set ( folder . uri . fsPath , manager )
112+
113+ try {
114+ await manager . initialize ( contextProxy )
115+ } catch ( error ) {
116+ outputChannel . appendLine (
117+ `[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for ${ folder . uri . fsPath } : ${ error . message || error } ` ,
118+ )
119+ }
120+
121+ context . subscriptions . push ( manager )
122+ }
123+ }
124+
125+ // Initialize managers for existing workspace folders
106126 if ( vscode . workspace . workspaceFolders ) {
107127 for ( const folder of vscode . workspace . workspaceFolders ) {
108- const manager = CodeIndexManager . getInstance ( context , folder . uri . fsPath )
109-
110- if ( manager ) {
111- codeIndexManagers . push ( manager )
128+ await initializeCodeIndexManagerForFolder ( folder )
129+ }
130+ }
112131
113- try {
114- await manager . initialize ( contextProxy )
115- } catch ( error ) {
132+ // Listen for workspace folder changes
133+ context . subscriptions . push (
134+ vscode . workspace . onDidChangeWorkspaceFolders ( async ( event ) => {
135+ // Handle removed folders
136+ for ( const removedFolder of event . removed ) {
137+ const manager = codeIndexManagers . get ( removedFolder . uri . fsPath )
138+ if ( manager ) {
116139 outputChannel . appendLine (
117- `[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for ${ folder . uri . fsPath } : ${ error . message || error } ` ,
140+ `[CodeIndexManager] Removing CodeIndexManager for removed folder: ${ removedFolder . uri . fsPath } ` ,
118141 )
142+ // Dispose the manager
143+ manager . dispose ( )
144+ // Remove from our map
145+ codeIndexManagers . delete ( removedFolder . uri . fsPath )
146+ // Remove from context subscriptions
147+ const index = context . subscriptions . indexOf ( manager )
148+ if ( index > - 1 ) {
149+ context . subscriptions . splice ( index , 1 )
150+ }
119151 }
152+ }
120153
121- context . subscriptions . push ( manager )
154+ // Handle added folders
155+ for ( const addedFolder of event . added ) {
156+ outputChannel . appendLine (
157+ `[CodeIndexManager] Adding CodeIndexManager for new folder: ${ addedFolder . uri . fsPath } ` ,
158+ )
159+ await initializeCodeIndexManagerForFolder ( addedFolder )
122160 }
123- }
124- }
161+
162+ // Notify the provider about workspace changes
163+ provider . handleWorkspaceFoldersChanged ( )
164+ } ) ,
165+ )
125166
126167 // Initialize the provider *before* the Roo Code Cloud service.
127168 const provider = new ClineProvider ( context , outputChannel , "sidebar" , contextProxy , mdmService )
0 commit comments