From 5abeb010f1f047b9c0693a04ab5d3dc5dc59650e Mon Sep 17 00:00:00 2001 From: isidor Date: Wed, 20 Dec 2017 18:09:06 +0100 Subject: [PATCH] new file command --- .../fileActions.contribution.ts | 16 ++++++++++++---- .../files/electron-browser/fileActions.ts | 18 +++++++++++++++--- .../files/electron-browser/fileCommands.ts | 10 ++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/vs/workbench/parts/files/electron-browser/fileActions.contribution.ts b/src/vs/workbench/parts/files/electron-browser/fileActions.contribution.ts index d9a2499be41..644c3f1c25b 100644 --- a/src/vs/workbench/parts/files/electron-browser/fileActions.contribution.ts +++ b/src/vs/workbench/parts/files/electron-browser/fileActions.contribution.ts @@ -9,7 +9,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { Action, IAction } from 'vs/base/common/actions'; import { ActionItem, BaseActionItem, Separator } from 'vs/base/browser/ui/actionbar/actionbar'; import { Scope, IActionBarRegistry, Extensions as ActionBarExtensions, ActionBarContributor } from 'vs/workbench/browser/actions'; -import { GlobalNewUntitledFileAction, SaveFileAsAction, ShowOpenedFileInNewWindow, CopyPathAction, GlobalCopyPathAction, RevealInOSAction, GlobalRevealInOSAction, pasteIntoFocusedFilesExplorerViewItem, FocusOpenEditorsView, FocusFilesExplorer, GlobalCompareResourcesAction, GlobalNewFileAction, GlobalNewFolderAction, RevertFileAction, SaveFilesAction, SaveAllAction, SaveFileAction, MoveFileToTrashAction, TriggerRenameFileAction, PasteFileAction, CopyFileAction, NewFolderAction, NewFileAction, ShowActiveFileInExplorer, CollapseExplorerView, RefreshExplorerView, CompareWithSavedAction, CompareWithClipboardAction } from 'vs/workbench/parts/files/electron-browser/fileActions'; +import { GlobalNewUntitledFileAction, SaveFileAsAction, ShowOpenedFileInNewWindow, CopyPathAction, GlobalCopyPathAction, RevealInOSAction, GlobalRevealInOSAction, pasteIntoFocusedFilesExplorerViewItem, FocusOpenEditorsView, FocusFilesExplorer, GlobalCompareResourcesAction, GlobalNewFileAction, GlobalNewFolderAction, RevertFileAction, SaveFilesAction, SaveAllAction, SaveFileAction, MoveFileToTrashAction, TriggerRenameFileAction, PasteFileAction, CopyFileAction, NewFolderAction, ShowActiveFileInExplorer, CollapseExplorerView, RefreshExplorerView, CompareWithSavedAction, CompareWithClipboardAction } from 'vs/workbench/parts/files/electron-browser/fileActions'; import { revertLocalChangesCommand, acceptLocalChangesCommand, CONFLICT_RESOLUTION_CONTEXT } from 'vs/workbench/parts/files/electron-browser/saveErrorHandler'; import { SyncActionDescriptor, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions'; import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions'; @@ -17,7 +17,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { FileStat, Model } from 'vs/workbench/parts/files/common/explorerModel'; import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes'; -import { copyFocusedFilesExplorerViewItem, openWindowCommand, deleteFocusedFilesExplorerViewItemCommand, moveFocusedFilesExplorerViewItemToTrashCommand, renameFocusedFilesExplorerViewItemCommand, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID, EditorWithResourceFocusedInOpenEditorsContext, REVERT_FILE_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, UntitledEditorFocusedInOpenEditorsContext, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_AS_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, GroupFocusedInOpenEditorsContext, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID, EditorFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands'; +import { copyFocusedFilesExplorerViewItem, openWindowCommand, deleteFocusedFilesExplorerViewItemCommand, moveFocusedFilesExplorerViewItemToTrashCommand, renameFocusedFilesExplorerViewItemCommand, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID, EditorWithResourceFocusedInOpenEditorsContext, REVERT_FILE_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, UntitledEditorFocusedInOpenEditorsContext, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_AS_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, GroupFocusedInOpenEditorsContext, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID, EditorFocusedInOpenEditorsContext, NEW_FILE_COMMAND_ID, NEW_FILE_LABEL } from 'vs/workbench/parts/files/electron-browser/fileCommands'; import { CommandsRegistry, ICommandHandler } from 'vs/platform/commands/common/commands'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry'; @@ -56,8 +56,6 @@ class FilesViewerActionContributor extends ActionBarContributor { // Directory Actions if (stat.isDirectory && !stat.nonexistentRoot) { - // New File - actions.push(this.instantiationService.createInstance(NewFileAction, tree, stat)); // New Folder actions.push(this.instantiationService.createInstance(NewFolderAction, tree, stat)); @@ -436,3 +434,13 @@ MenuRegistry.appendMenuItem(MenuId.ExplorerContext, { }, when: ContextKeyExpr.and(ExplorerRootContext, ExplorerFolderContext) }); + +MenuRegistry.appendMenuItem(MenuId.ExplorerContext, { + group: '3_create', + order: 10, + command: { + id: NEW_FILE_COMMAND_ID, + title: NEW_FILE_LABEL + }, + when: ContextKeyExpr.and(ExplorerFolderContext) +}); diff --git a/src/vs/workbench/parts/files/electron-browser/fileActions.ts b/src/vs/workbench/parts/files/electron-browser/fileActions.ts index 37ecb02b400..04188e664e3 100644 --- a/src/vs/workbench/parts/files/electron-browser/fileActions.ts +++ b/src/vs/workbench/parts/files/electron-browser/fileActions.ts @@ -41,14 +41,15 @@ import { IMessageService, IMessageWithAction, IConfirmation, Severity, CancelAct import { IModel } from 'vs/editor/common/editorCommon'; import { IBackupFileService } from 'vs/workbench/services/backup/common/backup'; import { IWindowsService } from 'vs/platform/windows/common/windows'; -import { withFocusedFilesExplorer, REVERT_FILE_COMMAND_ID, COMPARE_WITH_SAVED_COMMAND_ID, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, SAVE_FILE_AS_LABEL, SAVE_ALL_COMMAND_ID, SAVE_ALL_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, SAVE_FILES_COMMAND_ID, SAVE_FILES_LABEL, COMPARE_WITH_SAVED_SCHEMA } from 'vs/workbench/parts/files/electron-browser/fileCommands'; +import { withFocusedFilesExplorer, REVERT_FILE_COMMAND_ID, COMPARE_WITH_SAVED_COMMAND_ID, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, SAVE_FILE_AS_LABEL, SAVE_ALL_COMMAND_ID, SAVE_ALL_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, SAVE_FILES_COMMAND_ID, SAVE_FILES_LABEL, COMPARE_WITH_SAVED_SCHEMA, NEW_FILE_LABEL, NEW_FILE_COMMAND_ID, IExplorerContext } from 'vs/workbench/parts/files/electron-browser/fileCommands'; import { ITextModelService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService'; import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration'; import { once } from 'vs/base/common/event'; import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService'; import { IModeService } from 'vs/editor/common/services/modeService'; import { IModelService } from 'vs/editor/common/services/modelService'; -import { ICommandService } from 'vs/platform/commands/common/commands'; +import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands'; +import { IListService } from 'vs/platform/list/browser/listService'; export interface IEditableData { action: IAction; @@ -443,7 +444,7 @@ export class NewFileAction extends BaseNewAction { @ITextFileService textFileService: ITextFileService, @IInstantiationService instantiationService: IInstantiationService ) { - super('explorer.newFile', nls.localize('newFile', "New File"), tree, true, instantiationService.createInstance(CreateFileAction, element), null, fileService, messageService, textFileService); + super('explorer.newFile', NEW_FILE_LABEL, tree, true, instantiationService.createInstance(CreateFileAction, element), null, fileService, messageService, textFileService); this.class = 'explorer-action new-file'; this._updateEnablement(); @@ -1754,3 +1755,14 @@ if (!diag) { console.log(args[1] + ' - ' + args[0] + ' (time: ' + args[2].getTime() + ' [' + args[2].toUTCString() + '])'); }); } + +CommandsRegistry.registerCommand({ + id: NEW_FILE_COMMAND_ID, + handler: (accessor, resource: URI, explorerContext: IExplorerContext) => { + const instantationService = accessor.get(IInstantiationService); + const listService = accessor.get(IListService); + const newFileAction = instantationService.createInstance(NewFileAction, listService.lastFocusedList, explorerContext.stat); + + return newFileAction.run(explorerContext); + } +}); diff --git a/src/vs/workbench/parts/files/electron-browser/fileCommands.ts b/src/vs/workbench/parts/files/electron-browser/fileCommands.ts index e36959ea61a..fe356108ac8 100644 --- a/src/vs/workbench/parts/files/electron-browser/fileCommands.ts +++ b/src/vs/workbench/parts/files/electron-browser/fileCommands.ts @@ -39,6 +39,7 @@ import { IEditorViewState } from 'vs/editor/common/editorCommon'; import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService'; import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes'; +import { IEvent } from 'vs/platform/contextview/browser/contextView'; // Commands @@ -65,6 +66,9 @@ export const SAVE_ALL_IN_GROUP_COMMAND_ID = 'workbench.action.files.saveAllInGro export const SAVE_FILES_COMMAND_ID = 'workbench.command.files.saveFiles'; export const SAVE_FILES_LABEL = nls.localize('saveFiles', "Save All Files"); +export const NEW_FILE_COMMAND_ID = 'workbench.command.files.newFile'; +export const NEW_FILE_LABEL = nls.localize('newFile', "New File"); + export const EditorFocusedInOpenEditorsContext = new RawContextKey('editorFocusedInOpenEditors', false); export const EditorWithResourceFocusedInOpenEditorsContext = new RawContextKey('editorWithResourceFocusedInOpenEditors', false); export const UntitledEditorFocusedInOpenEditorsContext = new RawContextKey('untitledEditorFocusedInOpenEditors', false); @@ -75,6 +79,12 @@ export const openWindowCommand = (accessor: ServicesAccessor, paths: string[], f windowsService.openWindow(paths, { forceNewWindow }); }; +export interface IExplorerContext { + viewletState: any; + event: IEvent; + stat: FileStat; +} + function runActionOnFocusedFilesExplorerViewItem(accessor: ServicesAccessor, id: string, context?: any): void { withFocusedFilesExplorerViewItem(accessor).then(res => { if (res) { -- GitLab