1616
1717import { inject , injectable } from 'inversify' ;
1818import URI from '../common/uri' ;
19- import { MaybePromise , SelectionService , UriSelection } from '../common' ;
19+ import { MaybePromise , SelectionService , UNTITLED_SCHEME , UriSelection } from '../common' ;
2020import { EnvVariablesServer } from '../common/env-variables' ;
21+ import { FrontendApplication } from './frontend-application' ;
22+ import { FrontendApplicationContribution } from './frontend-application-contribution' ;
23+ import { Widget } from './widgets' ;
24+ import { Navigatable } from './navigatable-types' ;
2125
2226@injectable ( )
23- export class UserWorkingDirectoryProvider {
27+ export class UserWorkingDirectoryProvider implements FrontendApplicationContribution {
2428 @inject ( SelectionService ) protected readonly selectionService : SelectionService ;
2529 @inject ( EnvVariablesServer ) protected readonly envVariables : EnvVariablesServer ;
2630
31+ protected lastOpenResource : URI | undefined ;
32+
33+ configure ( app : FrontendApplication ) : void {
34+ app . shell . onDidChangeCurrentWidget ( e => this . setLastOpenResource ( e . newValue ?? undefined ) ) ;
35+ this . setLastOpenResource ( app . shell . currentWidget ) ;
36+ }
37+
38+ protected setLastOpenResource ( widget ?: Widget ) : void {
39+ if ( Navigatable . is ( widget ) ) {
40+ const uri = widget . getResourceUri ( ) ;
41+ if ( uri && uri . scheme !== UNTITLED_SCHEME ) {
42+ this . lastOpenResource = uri ;
43+ }
44+ }
45+ }
46+
2747 /**
2848 * @returns A {@link URI} that represents a good guess about the directory in which the user is currently operating.
2949 *
@@ -35,7 +55,16 @@ export class UserWorkingDirectoryProvider {
3555 }
3656
3757 protected getFromSelection ( ) : MaybePromise < URI | undefined > {
38- return this . ensureIsDirectory ( UriSelection . getUri ( this . selectionService . selection ) ) ;
58+ const uri = UriSelection . getUri ( this . selectionService . selection ) ;
59+ if ( uri ?. scheme === UNTITLED_SCHEME ) {
60+ // An untitled file is not a valid working directory context.
61+ return undefined ;
62+ }
63+ return this . ensureIsDirectory ( uri ) ;
64+ }
65+
66+ protected getFromLastOpenResource ( ) : MaybePromise < URI | undefined > {
67+ return this . ensureIsDirectory ( this . lastOpenResource ) ;
3968 }
4069
4170 protected getFromUserHome ( ) : MaybePromise < URI > {
0 commit comments