Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select created new files and folders in the navigator #7762

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions packages/navigator/src/browser/navigator-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,55 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject, postConstruct } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
import {
Navigatable, SelectableTreeNode, Widget, KeybindingRegistry, CommonCommands,
OpenerService, FrontendApplicationContribution, FrontendApplication, CompositeTreeNode, PreferenceScope
CommonCommands,
CompositeTreeNode,
FrontendApplication,
FrontendApplicationContribution,
KeybindingRegistry,
Navigatable,
OpenerService,
PreferenceScope,
PreferenceService,
SelectableTreeNode,
SHELL_TABBAR_CONTEXT_MENU,
Widget
} from '@theia/core/lib/browser';
import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution';
import { CommandRegistry, MenuModelRegistry, MenuPath, isOSX, Command, DisposableCollection, Mutable } from '@theia/core/lib/common';
import { SHELL_TABBAR_CONTEXT_MENU } from '@theia/core/lib/browser';
import { WorkspaceCommands, WorkspaceService, WorkspacePreferences } from '@theia/workspace/lib/browser';
import { FILE_NAVIGATOR_ID, FileNavigatorWidget, EXPLORER_VIEW_CONTAINER_ID } from './navigator-widget';
import {
Command,
CommandRegistry,
DisposableCollection,
isOSX,
MenuModelRegistry,
MenuPath,
Mutable
} from '@theia/core/lib/common';
import {
DidCreateNewResourceEvent,
WorkspaceCommandContribution,
WorkspaceCommands,
WorkspacePreferences,
WorkspaceService
} from '@theia/workspace/lib/browser';
import { EXPLORER_VIEW_CONTAINER_ID, FILE_NAVIGATOR_ID, FileNavigatorWidget } from './navigator-widget';
import { FileNavigatorPreferences } from './navigator-preferences';
import { NavigatorKeybindingContexts } from './navigator-keybinding-context';
import { FileNavigatorFilter } from './navigator-filter';
import { WorkspaceNode } from './navigator-tree';
import { NavigatorContextKeyService } from './navigator-context-key-service';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import {
TabBarToolbarContribution,
TabBarToolbarItem,
TabBarToolbarRegistry
} from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
import { NavigatorDiff, NavigatorDiffCommands } from './navigator-diff';
import { UriSelection } from '@theia/core/lib/common/selection';
import { PreferenceService } from '@theia/core/lib/browser';
import { DirNode } from '@theia/filesystem/lib/browser';
import { FileNavigatorModel } from './navigator-model';

export namespace FileNavigatorCommands {
export const REVEAL_IN_NAVIGATOR: Command = {
Expand Down Expand Up @@ -134,6 +162,9 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;

@inject(WorkspaceCommandContribution)
protected readonly workspaceCommandContribution: WorkspaceCommandContribution;

constructor(
@inject(FileNavigatorPreferences) protected readonly fileNavigatorPreferences: FileNavigatorPreferences,
@inject(OpenerService) protected readonly openerService: OpenerService,
Expand Down Expand Up @@ -166,6 +197,27 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
};
updateFocusContextKeys();
this.shell.activeChanged.connect(updateFocusContextKeys);
this.workspaceCommandContribution.onDidCreateNewFile(async event => this.onDidCreateNewResource(event));
this.workspaceCommandContribution.onDidCreateNewFolder(async event => this.onDidCreateNewResource(event));
}

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(event.parent);
if (DirNode.is(parent)) {
await model.refresh(parent);
}
const node = await model.revealFile(event.uri);
if (SelectableTreeNode.is(node)) {
model.selectNode(node);
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
if (DirNode.is(node)) {
this.openView({ activate: true });
}
}
}

async onStart(app: FrontendApplication): Promise<void> {
Expand Down
32 changes: 30 additions & 2 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { WorkspaceCompareHandler } from './workspace-compare-handler';
import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution';
import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
import { WorkspaceInputDialog } from './workspace-input-dialog';
import { Emitter, Event } from '@theia/core/lib/common';

const validFilename: (arg: string) => boolean = require('valid-filename');

Expand Down Expand Up @@ -169,6 +170,11 @@ export class EditMenuContribution implements MenuContribution {

}

export interface DidCreateNewResourceEvent {
uri: URI
parent: URI
}

@injectable()
export class WorkspaceCommandContribution implements CommandContribution {

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

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

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

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

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

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

registerCommands(registry: CommandRegistry): void {
this.openerService.getOpeners().then(openers => {
for (const opener of openers) {
Expand Down Expand Up @@ -214,6 +239,7 @@ export class WorkspaceCommandContribution implements CommandContribution {
if (name) {
const fileUri = parentUri.resolve(name);
this.fileSystem.createFile(fileUri.toString()).then(() => {
this.fireCreateNewFile({ parent: parentUri, uri: fileUri });
open(this.openerService, fileUri);
});
}
Expand All @@ -232,9 +258,11 @@ export class WorkspaceCommandContribution implements CommandContribution {
initialValue: vacantChildUri.path.base,
validate: name => this.validateFileName(name, parent, true)
}, this.labelProvider);
dialog.open().then(name => {
dialog.open().then(async name => {
if (name) {
this.fileSystem.createFolder(parentUri.resolve(name).toString());
const folderUri = parentUri.resolve(name);
await this.fileSystem.createFolder(parentUri.resolve(name).toString());
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
this.fireCreateNewFile({ parent: parentUri, uri: folderUri });
}
});
}
Expand Down