From f0df635671909c8cddba49ca9f27ece8c315dec6 Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Thu, 14 May 2020 12:46:20 +0300 Subject: [PATCH] Select created new files and folders in the navigator Signed-off-by: Igor Vinokur --- .../src/browser/navigator-contribution.ts | 70 ++++++++++++++++--- .../src/browser/workspace-commands.ts | 32 ++++++++- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/packages/navigator/src/browser/navigator-contribution.ts b/packages/navigator/src/browser/navigator-contribution.ts index 1b1010300fc28..5c04049d1cbb2 100644 --- a/packages/navigator/src/browser/navigator-contribution.ts +++ b/packages/navigator/src/browser/navigator-contribution.ts @@ -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 = { @@ -134,6 +162,9 @@ export class FileNavigatorContribution extends AbstractViewContribution this.onDidCreateNewResource(event)); + this.workspaceCommandContribution.onDidCreateNewFolder(async event => this.onDidCreateNewResource(event)); + } + + private async onDidCreateNewResource(event: DidCreateNewResourceEvent): Promise { + 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); + if (DirNode.is(node)) { + this.openView({ activate: true }); + } + } } async onStart(app: FrontendApplication): Promise { diff --git a/packages/workspace/src/browser/workspace-commands.ts b/packages/workspace/src/browser/workspace-commands.ts index 0a9ef07795c47..7f32253ee3a3e 100644 --- a/packages/workspace/src/browser/workspace-commands.ts +++ b/packages/workspace/src/browser/workspace-commands.ts @@ -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'); @@ -169,6 +170,11 @@ export class EditMenuContribution implements MenuContribution { } +export interface DidCreateNewResourceEvent { + uri: URI + parent: URI +} + @injectable() export class WorkspaceCommandContribution implements CommandContribution { @@ -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(); + private readonly onDidCreateNewFolderEmitter = new Emitter(); + + get onDidCreateNewFile(): Event { + return this.onDidCreateNewFileEmitter.event; + } + + get onDidCreateNewFolder(): Event { + 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) { @@ -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); }); } @@ -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()); + this.fireCreateNewFile({ parent: parentUri, uri: folderUri }); } }); }