@@ -14,6 +14,7 @@ import { TerminalCapability, type ITerminalCapabilityStore } from '../../../../.
1414import { GeneralShellType , TerminalShellType } from '../../../../../platform/terminal/common/terminal.js' ;
1515import { TerminalSuggestSettingId } from '../common/terminalSuggestConfiguration.js' ;
1616import { TerminalCompletionItemKind , type ITerminalCompletion } from './terminalCompletionItem.js' ;
17+ import { env as processEnv } from '../../../../../base/common/process.js' ;
1718
1819export const ITerminalCompletionService = createDecorator < ITerminalCompletionService > ( 'terminalCompletionService' ) ;
1920
@@ -87,6 +88,7 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
8788
8889 constructor (
8990 @IConfigurationService private readonly _configurationService : IConfigurationService ,
91+
9092 @IFileService private readonly _fileService : IFileService
9193 ) {
9294 super ( ) ;
@@ -251,13 +253,9 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
251253 const type = lastWordFolderHasTildePrefix ? 'tilde' : isAbsolutePath ? 'absolute' : 'relative' ;
252254 switch ( type ) {
253255 case 'tilde' : {
254- const env = capabilities . get ( TerminalCapability . ShellEnvDetection ) ?. env ;
255- if ( env ) {
256- const home = useWindowsStylePath ? env . get ( 'USERPROFILE' ) : env . get ( 'HOME' ) ;
257- // TODO: Handle the case where the HOME environment variable is not set
258- if ( home ) {
259- lastWordFolderResource = URI . joinPath ( URI . file ( home ) , lastWordFolder . slice ( 1 ) . replaceAll ( '\\ ' , ' ' ) ) ;
260- }
256+ const home = getHomeDir ( useWindowsStylePath , capabilities ) ;
257+ if ( home ) {
258+ lastWordFolderResource = URI . joinPath ( URI . file ( home ) , lastWordFolder . slice ( 1 ) . replaceAll ( '\\ ' , ' ' ) ) ;
261259 }
262260 if ( ! lastWordFolderResource ) {
263261 // Use less strong wording here as it's not as strong of a concept on Windows
@@ -389,7 +387,7 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
389387 if ( promptValue . startsWith ( 'cd ' ) ) {
390388 const config = this . _configurationService . getValue ( TerminalSuggestSettingId . CdPath ) ;
391389 if ( config === 'absolute' || config === 'relative' ) {
392- const cdPath = capabilities . get ( TerminalCapability . ShellEnvDetection ) ?. env ?. get ( 'CDPATH' ) ;
390+ const cdPath = getEnvVar ( 'CDPATH' , capabilities ) ;
393391 if ( cdPath ) {
394392 const cdPathEntries = cdPath . split ( useWindowsStylePath ? ';' : ':' ) ;
395393 for ( const cdPathEntry of cdPathEntries ) {
@@ -446,14 +444,10 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
446444 //
447445 // - (relative) `|` -> `~`
448446 if ( type === 'relative' && ! lastWordFolder . match ( / [ \\ \/ ] / ) ) {
449- const env = capabilities . get ( TerminalCapability . ShellEnvDetection ) ?. env ;
450447 let homeResource : URI | string | undefined ;
451- if ( env ) {
452- const home = useWindowsStylePath ? env . get ( 'USERPROFILE' ) : env . get ( 'HOME' ) ;
453- // TODO: Handle the case where the HOME environment variable is not set
454- if ( home ) {
455- homeResource = URI . joinPath ( URI . file ( home ) , lastWordFolder . slice ( 1 ) . replaceAll ( '\\ ' , ' ' ) ) ;
456- }
448+ const home = getHomeDir ( useWindowsStylePath , capabilities ) ;
449+ if ( home ) {
450+ homeResource = URI . joinPath ( URI . file ( home ) , lastWordFolder . slice ( 1 ) . replaceAll ( '\\ ' , ' ' ) ) ;
457451 }
458452 if ( ! homeResource ) {
459453 // Use less strong wording here as it's not as strong of a concept on Windows
@@ -474,6 +468,18 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
474468 }
475469}
476470
471+ function getEnvVar ( key : string , capabilities : ITerminalCapabilityStore ) : string | undefined {
472+ const env = capabilities . get ( TerminalCapability . ShellEnvDetection ) ?. env ;
473+ if ( env ) {
474+ return env . get ( key ) ;
475+ }
476+ return processEnv [ key ] ;
477+ }
478+
479+ function getHomeDir ( useWindowsStylePath : boolean , capabilities : ITerminalCapabilityStore ) : string | undefined {
480+ return useWindowsStylePath ? getEnvVar ( 'USERPROFILE' , capabilities ) : getEnvVar ( 'HOME' , capabilities ) ;
481+ }
482+
477483function getFriendlyPath ( uri : URI , pathSeparator : string , kind : TerminalCompletionItemKind ) : string {
478484 let path = uri . fsPath ;
479485 // Ensure folders end with the path separator to differentiate presentation from files
0 commit comments