@@ -2504,6 +2504,59 @@ function doWithInstance(accessor: ServicesAccessor, resource: unknown): ITermina
25042504 return instance ;
25052505}
25062506
2507+ async function pickTerminalCwd ( accessor : ServicesAccessor , cancel ?: CancellationToken ) : Promise < WorkspaceFolderCwdPair | undefined > {
2508+ const quickInputService = accessor . get ( IQuickInputService ) ;
2509+ const labelService = accessor . get ( ILabelService ) ;
2510+ const contextService = accessor . get ( IWorkspaceContextService ) ;
2511+ const modelService = accessor . get ( IModelService ) ;
2512+ const languageService = accessor . get ( ILanguageService ) ;
2513+ const configurationService = accessor . get ( IConfigurationService ) ;
2514+ const configurationResolverService = accessor . get ( IConfigurationResolverService ) ;
2515+
2516+ const folders = contextService . getWorkspace ( ) . folders ;
2517+ if ( ! folders . length ) {
2518+ return ;
2519+ }
2520+
2521+ const folderCwdPairs = await Promise . all ( folders . map ( x => resolveWorkspaceFolderCwd ( x , configurationService , configurationResolverService ) ) ) ;
2522+ const shrinkedPairs = shrinkWorkspaceFolderCwdPairs ( folderCwdPairs ) ;
2523+
2524+ if ( shrinkedPairs . length === 1 ) {
2525+ return shrinkedPairs [ 0 ] ;
2526+ }
2527+
2528+ type Item = IQuickPickItem & { pair : WorkspaceFolderCwdPair } ;
2529+ const folderPicks : Item [ ] = shrinkedPairs . map ( pair => ( {
2530+ label : pair . folder . name ,
2531+ description : pair . isOverridden
2532+ ? localize ( 'workbench.action.terminal.overriddenCwdDescription' , "(Overriden) {0}" , labelService . getUriLabel ( pair . cwd , { relative : ! pair . isAbsolute } ) )
2533+ : labelService . getUriLabel ( dirname ( pair . cwd ) , { relative : true } ) ,
2534+ pair : pair ,
2535+ iconClasses : getIconClasses ( modelService , languageService , pair . cwd , FileKind . ROOT_FOLDER )
2536+ } ) ) ;
2537+ const options : IPickOptions < Item > = {
2538+ placeHolder : localize ( 'workbench.action.terminal.newWorkspacePlaceholder' , "Select current working directory for new terminal" ) ,
2539+ matchOnDescription : true ,
2540+ canPickMany : false ,
2541+ } ;
2542+
2543+ const token : CancellationToken = cancel || CancellationToken . None ;
2544+ const pick = await quickInputService . pick < Item > ( folderPicks , options , token ) ;
2545+ return pick ?. pair ;
2546+ }
2547+
2548+ async function resolveWorkspaceFolderCwd ( folder : IWorkspaceFolder , configurationService : IConfigurationService , configurationResolverService : IConfigurationResolverService ) : Promise < WorkspaceFolderCwdPair > {
2549+ const cwdConfig = configurationService . getValue ( TerminalSettingId . Cwd , { resource : folder . uri } ) ;
2550+ if ( typeof cwdConfig !== 'string' || cwdConfig . length === 0 ) {
2551+ return { folder, cwd : folder . uri , isAbsolute : false , isOverridden : false } ;
2552+ }
2553+
2554+ const resolvedCwdConfig = await configurationResolverService . resolveAsync ( folder , cwdConfig ) ;
2555+ return isAbsolute ( resolvedCwdConfig ) || resolvedCwdConfig . startsWith ( AbstractVariableResolverService . VARIABLE_LHS )
2556+ ? { folder, isAbsolute : true , isOverridden : true , cwd : URI . from ( { scheme : folder . uri . scheme , path : resolvedCwdConfig } ) }
2557+ : { folder, isAbsolute : false , isOverridden : true , cwd : URI . joinPath ( folder . uri , resolvedCwdConfig ) } ;
2558+ }
2559+
25072560/**
25082561 * Drops repeated CWDs, if any, by keeping the one which best matches the workspace folder. It also preserves the original order.
25092562 */
0 commit comments