@@ -4,6 +4,8 @@ import { ProjectCreatorString } from '../../common/localize';
44import { showOpenDialog , showWarningMessage } from '../../common/window.apis' ;
55import { PythonProjectManager } from '../../internal.api' ;
66import { traceInfo } from '../../common/logging' ;
7+ import { Uri , window , workspace } from 'vscode' ;
8+ import { traceLog } from '../../common/logging' ;
79
810export class ExistingProjects implements PythonProjectCreator {
911 public readonly name = 'existingProjects' ;
@@ -23,6 +25,7 @@ export class ExistingProjects implements PythonProjectCreator {
2325 } ) ;
2426
2527 if ( ! results || results . length === 0 ) {
28+ // User cancelled the dialog & doesn't want to add any projects
2629 return ;
2730 }
2831
@@ -49,9 +52,46 @@ export class ExistingProjects implements PythonProjectCreator {
4952 return ;
5053 }
5154
52- return filtered . map ( ( r ) => ( {
53- name : path . basename ( r . fsPath ) ,
54- uri : r ,
55- } ) ) ;
55+ // for all the selected files / folders, check to make sure they are in the workspace
56+ const resultsOutsideWorkspace : Uri [ ] = [ ] ;
57+ const workspaceRoots : Uri [ ] = workspace . workspaceFolders ?. map ( ( w ) => w . uri ) || [ ] ;
58+ const resultsInWorkspace = filtered . filter ( ( r ) => {
59+ const exists = workspaceRoots . some ( ( w ) => r . fsPath . startsWith ( w . fsPath ) ) ;
60+ if ( ! exists ) {
61+ traceLog ( `File ${ r . fsPath } is not in the workspace, ignoring it from 'add projects' list.` ) ;
62+ resultsOutsideWorkspace . push ( r ) ;
63+ }
64+ return exists ;
65+ } ) ;
66+ if ( resultsInWorkspace . length === 0 ) {
67+ // Show a single error message with option to add to workspace
68+ const response = await window . showErrorMessage (
69+ 'Selected items are not in the current workspace.' ,
70+ 'Add to Workspace' ,
71+ 'Cancel' ,
72+ ) ;
73+
74+ if ( response === 'Add to Workspace' ) {
75+ // Use the command palette to let user adjust which folders to add
76+ // Add folders programmatically using workspace API
77+ for ( const r of resultsOutsideWorkspace ) {
78+ // if the user selects a file, add that file to the workspace
79+ await // if the user selects a folder, add that folder to the workspace
80+ await workspace . updateWorkspaceFolders (
81+ workspace . workspaceFolders ?. length || 0 , // Start index
82+ 0 , // Delete count
83+ {
84+ uri : r ,
85+ } ,
86+ ) ;
87+ }
88+ }
89+ return ;
90+ } else {
91+ return resultsInWorkspace . map ( ( uri ) => ( {
92+ name : path . basename ( uri . fsPath ) ,
93+ uri,
94+ } ) ) as PythonProject [ ] ;
95+ }
5696 }
5797}
0 commit comments