Skip to content

Commit

Permalink
Save untitled files to the last active folder (#13184)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Dec 19, 2023
1 parent cdbfffc commit 2527596
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/src/browser/frontend-application-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ export const frontendApplicationModule = new ContainerModule((bind, _unbind, _is

bind(SaveResourceService).toSelf().inSingletonScope();
bind(UserWorkingDirectoryProvider).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(UserWorkingDirectoryProvider);

bind(HoverService).toSelf().inSingletonScope();

Expand Down
35 changes: 32 additions & 3 deletions packages/core/src/browser/user-working-directory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,34 @@

import { inject, injectable } from 'inversify';
import URI from '../common/uri';
import { MaybePromise, SelectionService, UriSelection } from '../common';
import { MaybePromise, SelectionService, UNTITLED_SCHEME, UriSelection } from '../common';
import { EnvVariablesServer } from '../common/env-variables';
import { FrontendApplication } from './frontend-application';
import { FrontendApplicationContribution } from './frontend-application-contribution';
import { Widget } from './widgets';
import { Navigatable } from './navigatable-types';

@injectable()
export class UserWorkingDirectoryProvider {
export class UserWorkingDirectoryProvider implements FrontendApplicationContribution {
@inject(SelectionService) protected readonly selectionService: SelectionService;
@inject(EnvVariablesServer) protected readonly envVariables: EnvVariablesServer;

protected lastOpenResource: URI | undefined;

configure(app: FrontendApplication): void {
app.shell.onDidChangeCurrentWidget(e => this.setLastOpenResource(e.newValue ?? undefined));
this.setLastOpenResource(app.shell.currentWidget);
}

protected setLastOpenResource(widget?: Widget): void {
if (Navigatable.is(widget)) {
const uri = widget.getResourceUri();
if (uri && uri.scheme !== UNTITLED_SCHEME) {
this.lastOpenResource = uri;
}
}
}

/**
* @returns A {@link URI} that represents a good guess about the directory in which the user is currently operating.
*
Expand All @@ -35,7 +55,16 @@ export class UserWorkingDirectoryProvider {
}

protected getFromSelection(): MaybePromise<URI | undefined> {
return this.ensureIsDirectory(UriSelection.getUri(this.selectionService.selection));
const uri = UriSelection.getUri(this.selectionService.selection);
if (uri?.scheme === UNTITLED_SCHEME) {
// An untitled file is not a valid working directory context.
return undefined;
}
return this.ensureIsDirectory(uri);
}

protected getFromLastOpenResource(): MaybePromise<URI | undefined> {
return this.ensureIsDirectory(this.lastOpenResource);
}

protected getFromUserHome(): MaybePromise<URI> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { injectable, inject } from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { MaybeArray, nls } from '@theia/core/lib/common';
import { MaybeArray, UNTITLED_SCHEME, nls } from '@theia/core/lib/common';
import { LabelProvider } from '@theia/core/lib/browser';
import { FileStat } from '../../common/files';
import { DirNode } from '../file-tree';
Expand Down Expand Up @@ -81,7 +81,9 @@ export class DefaultFileDialogService implements FileDialogService {
}

protected async getRootNode(folderToOpen?: FileStat): Promise<DirNode | undefined> {
const folderExists = folderToOpen && await this.fileService.exists(folderToOpen.resource);
const folderExists = folderToOpen
&& folderToOpen.resource.scheme !== UNTITLED_SCHEME
&& await this.fileService.exists(folderToOpen.resource);
const folder = folderToOpen && folderExists ? folderToOpen : {
resource: await this.rootProvider.getUserWorkingDir(),
isDirectory: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class WorkspaceUserWorkingDirectoryProvider extends UserWorkingDirectoryP

override async getUserWorkingDir(): Promise<URI> {
return await this.getFromSelection()
?? await this.getFromLastOpenResource()
?? await this.getFromWorkspace()
?? this.getFromUserHome();
}
Expand Down

0 comments on commit 2527596

Please sign in to comment.