Skip to content

Commit

Permalink
fixup! Select created new files and folders in the navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed May 13, 2020
1 parent 452d2fe commit 9afe9ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
25 changes: 16 additions & 9 deletions packages/navigator/src/browser/navigator-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Mutable
} from '@theia/core/lib/common';
import {
DidCreateNewResourceEvent,
WorkspaceCommandContribution,
WorkspaceCommands,
WorkspacePreferences,
Expand All @@ -62,7 +63,6 @@ import { NavigatorDiff, NavigatorDiffCommands } from './navigator-diff';
import { UriSelection } from '@theia/core/lib/common/selection';
import { DirNode } from '@theia/filesystem/lib/browser';
import { FileNavigatorModel } from './navigator-model';
import URI from '@theia/core/lib/common/uri';

export namespace FileNavigatorCommands {
export const REVEAL_IN_NAVIGATOR: Command = {
Expand Down Expand Up @@ -192,23 +192,30 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
};
updateFocusContextKeys();
this.shell.activeChanged.connect(updateFocusContextKeys);
this.workspaceCommandContribution.onDidCreateNewFile(async uri => this.onDidCreateNewResource(uri));
this.workspaceCommandContribution.onDidCreateNewFolder(async uri => this.onDidCreateNewResource(uri));
this.workspaceCommandContribution.onDidCreateNewFile(async event => this.onDidCreateNewResource(event));
this.workspaceCommandContribution.onDidCreateNewFolder(async event => this.onDidCreateNewResource(event));
}

private async onDidCreateNewResource(uri: URI): Promise<void> {
const navigator = await this.tryGetWidget();
private async onDidCreateNewResource(event: DidCreateNewResourceEvent): Promise<void> {
const navigator = this.tryGetWidget();
if (!navigator || !navigator.isVisible) {
return;
}
const model: FileNavigatorModel = navigator.model;
const parent = await model.revealFile(uri.parent);
if (DirNode.is(parent)) {
await model.refresh(parent);
for (const uri of event.uri.allLocations) {
if (event.parent.isEqualOrParent(uri)) {
const parent = await model.revealFile(uri);
if (DirNode.is(parent)) {
await model.refresh(parent);
}
}
}
const node = await model.revealFile(uri);
const node = await model.revealFile(event.uri);
if (SelectableTreeNode.is(node)) {
model.selectNode(node);
if (DirNode.is(node)) {
this.openView({ activate: true });
}
}
}

Expand Down
21 changes: 13 additions & 8 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ export class EditMenuContribution implements MenuContribution {

}

export interface DidCreateNewResourceEvent {
uri: URI
parent: URI
}

@injectable()
export class WorkspaceCommandContribution implements CommandContribution {

Expand All @@ -186,22 +191,22 @@ export class WorkspaceCommandContribution implements CommandContribution {
@inject(WorkspaceDuplicateHandler) protected readonly duplicateHandler: WorkspaceDuplicateHandler;
@inject(WorkspaceCompareHandler) protected readonly compareHandler: WorkspaceCompareHandler;

private readonly onDidCreateNewFileEmitter = new Emitter<URI>();
private readonly onDidCreateNewFolderEmitter = new Emitter<URI>();
private readonly onDidCreateNewFileEmitter = new Emitter<DidCreateNewResourceEvent>();
private readonly onDidCreateNewFolderEmitter = new Emitter<DidCreateNewResourceEvent>();

get onDidCreateNewFile(): Event<URI> {
get onDidCreateNewFile(): Event<DidCreateNewResourceEvent> {
return this.onDidCreateNewFileEmitter.event;
}

get onDidCreateNewFolder(): Event<URI> {
get onDidCreateNewFolder(): Event<DidCreateNewResourceEvent> {
return this.onDidCreateNewFolderEmitter.event;
}

protected fireCreateNewFile(uri: URI): void {
protected fireCreateNewFile(uri: DidCreateNewResourceEvent): void {
this.onDidCreateNewFileEmitter.fire(uri);
}

protected fireCreateNewFolder(uri: URI): void {
protected fireCreateNewFolder(uri: DidCreateNewResourceEvent): void {
this.onDidCreateNewFolderEmitter.fire(uri);
}

Expand Down Expand Up @@ -234,7 +239,7 @@ export class WorkspaceCommandContribution implements CommandContribution {
if (name) {
const fileUri = parentUri.resolve(name);
this.fileSystem.createFile(fileUri.toString()).then(async () => {
this.fireCreateNewFile(fileUri);
this.fireCreateNewFile({ parent: parentUri, uri: fileUri });
open(this.openerService, fileUri);
});
}
Expand All @@ -257,7 +262,7 @@ export class WorkspaceCommandContribution implements CommandContribution {
if (name) {
const folderUri = parentUri.resolve(name);
await this.fileSystem.createFolder(parentUri.resolve(name).toString());
this.fireCreateNewFile(folderUri);
this.fireCreateNewFile({ parent: parentUri, uri: folderUri });
}
});
}
Expand Down

0 comments on commit 9afe9ce

Please sign in to comment.