From 6cff1353c19a943c0f9ebb0823df276a58146a86 Mon Sep 17 00:00:00 2001 From: isidor Date: Fri, 15 Dec 2017 18:15:58 +0100 Subject: [PATCH] open editors: more actions to contributed commands --- .../browser/parts/editor/editorCommands.ts | 6 +- src/vs/workbench/common/editor.ts | 4 +- src/vs/workbench/parts/files/common/files.ts | 2 + .../files/electron-browser/fileActions.ts | 157 ++++------------ .../files/electron-browser/fileCommands.ts | 169 +++++++++++++++++- .../electron-browser/views/openEditorsView.ts | 60 ++----- 6 files changed, 215 insertions(+), 183 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorCommands.ts b/src/vs/workbench/browser/parts/editor/editorCommands.ts index fc9011e50fe..b79b87a2753 100644 --- a/src/vs/workbench/browser/parts/editor/editorCommands.ts +++ b/src/vs/workbench/browser/parts/editor/editorCommands.ts @@ -317,8 +317,7 @@ function registerEditorCommands() { command: { id: CLOSE_UNMODIFIED_EDITORS_COMMAND_ID, title: CLOSE_UNMODIFIED_EDITORS_LABEL - }, - when: EditorFocusedInOpenEditorsContext + } }); CommandsRegistry.registerCommand({ @@ -348,8 +347,7 @@ function registerEditorCommands() { command: { id: CLOSE_EDITORS_IN_GROUP_COMMAND_ID, title: CLOSE_EDITORS_IN_GROUP_LABEL - }, - when: EditorFocusedInOpenEditorsContext + } }); CommandsRegistry.registerCommand({ diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index f245278434d..1aba8016519 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -13,11 +13,13 @@ import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle'; import { IEditor, IEditorViewState, IModel, ScrollType } from 'vs/editor/common/editorCommon'; import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, Position, Verbosity, IEditor as IBaseEditor, IRevertOptions } from 'vs/platform/editor/common/editor'; import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation'; -import { RawContextKey } from 'vs/platform/contextkey/common/contextkey'; +import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { Registry } from 'vs/platform/registry/common/platform'; export const TextCompareEditorVisible = new RawContextKey('textCompareEditorVisible', false); export const EditorFocusedInOpenEditorsContext = new RawContextKey('editorFocusedInOpenEditors', false); +export const UntitledEditorFocusedInOpenEditorsContext = new RawContextKey('untitledEditorFocusedInOpenEditors', false); +export const UntitledEditorNotFocusedInOpenEditorsContext: ContextKeyExpr = UntitledEditorFocusedInOpenEditorsContext.toNegated(); export const GroupFocusedInOpenEditorsContext = new RawContextKey('groupFocusedInOpenEditors', false); export enum ConfirmResult { diff --git a/src/vs/workbench/parts/files/common/files.ts b/src/vs/workbench/parts/files/common/files.ts index b817c709192..b61ded59645 100644 --- a/src/vs/workbench/parts/files/common/files.ts +++ b/src/vs/workbench/parts/files/common/files.ts @@ -44,6 +44,7 @@ const openEditorsVisibleId = 'openEditorsVisible'; const openEditorsFocusId = 'openEditorsFocus'; const explorerViewletFocusId = 'explorerViewletFocus'; const explorerResourceIsFolderId = 'explorerResourceIsFolder'; +const autoSaveDisabled = 'autoSaveDisabled'; export const ExplorerViewletVisibleContext = new RawContextKey(explorerViewletVisibleId, true); export const ExplorerFolderContext = new RawContextKey(explorerResourceIsFolderId, false); @@ -51,6 +52,7 @@ export const FilesExplorerFocusedContext = new RawContextKey(filesExplo export const OpenEditorsVisibleContext = new RawContextKey(openEditorsVisibleId, false); export const OpenEditorsFocusedContext = new RawContextKey(openEditorsFocusId, true); export const ExplorerFocusedContext = new RawContextKey(explorerViewletFocusId, true); +export const AutoSaveDisabledContext = new RawContextKey(autoSaveDisabled, true); export const OpenEditorsVisibleCondition = ContextKeyExpr.has(openEditorsVisibleId); export const FilesExplorerFocusCondition = ContextKeyExpr.and(ContextKeyExpr.has(explorerViewletVisibleId), ContextKeyExpr.has(filesExplorerFocusId), ContextKeyExpr.not(InputFocusedContextKey)); diff --git a/src/vs/workbench/parts/files/electron-browser/fileActions.ts b/src/vs/workbench/parts/files/electron-browser/fileActions.ts index 999a7ab49ea..487437fd7d5 100644 --- a/src/vs/workbench/parts/files/electron-browser/fileActions.ts +++ b/src/vs/workbench/parts/files/electron-browser/fileActions.ts @@ -39,11 +39,10 @@ import { Position, IResourceInput, IUntitledResourceInput } from 'vs/platform/ed import { IInstantiationService, IConstructorSignature2, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IMessageService, IMessageWithAction, IConfirmation, Severity, CancelAction, IConfirmationResult } from 'vs/platform/message/common/message'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; -import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService'; -import { IEditorViewState, IModel } from 'vs/editor/common/editorCommon'; +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, OPEN_TO_SIDE_COMMAND_ID, COMPARE_WITH_SAVED_SCHEMA, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID, globalResourceToCompare, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, computeLabelForCompare } from 'vs/workbench/parts/files/electron-browser/fileCommands'; +import { withFocusedFilesExplorer, REVERT_FILE_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID, COMPARE_WITH_SAVED_SCHEMA, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID, globalResourceToCompare, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, computeLabelForCompare, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, SAVE_FILE_AS_LABEL } from 'vs/workbench/parts/files/electron-browser/fileCommands'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { ITextModelService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService'; import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration'; @@ -1247,154 +1246,63 @@ export class RefreshViewExplorerAction extends Action { } } -export abstract class BaseSaveFileAction extends BaseErrorReportingAction { +export class SaveFileAction extends BaseErrorReportingAction { + + public static readonly ID = 'workbench.action.files.save'; + public static readonly LABEL = SAVE_FILE_LABEL; + + private resource: URI; + constructor( id: string, label: string, - messageService: IMessageService + @ICommandService private commandService: ICommandService, + @IMessageService messageService: IMessageService ) { super(id, label, messageService); } + public setResource(resource: URI): void { + this.resource = resource; + } + public run(context?: any): TPromise { - return this.doRun(context).then(() => true, error => { + return this.commandService.executeCommand(SAVE_FILE_COMMAND_ID, { resource: this.resource }).then(() => true, error => { this.onError(error); return null; }); } - - protected abstract doRun(context?: any): TPromise; } -export abstract class BaseSaveOneFileAction extends BaseSaveFileAction { +export class SaveFileAsAction extends BaseErrorReportingAction { + + public static readonly ID = 'workbench.action.files.saveAs'; + public static readonly LABEL = SAVE_FILE_AS_LABEL; + private resource: URI; constructor( id: string, label: string, - @IWorkbenchEditorService private editorService: IWorkbenchEditorService, - @ITextFileService private textFileService: ITextFileService, - @IEditorGroupService private editorGroupService: IEditorGroupService, - @IUntitledEditorService private untitledEditorService: IUntitledEditorService, - @IMessageService messageService: IMessageService, - @IFileService private fileService: IFileService + @ICommandService private commandService: ICommandService, + @IMessageService messageService: IMessageService ) { super(id, label, messageService); - - this.enabled = true; } - public abstract isSaveAs(): boolean; - public setResource(resource: URI): void { this.resource = resource; } - protected doRun(context: any): TPromise { - let source: URI; - if (this.resource) { - source = this.resource; - } else { - source = toResource(this.editorService.getActiveEditorInput(), { supportSideBySide: true }); - } - - if (source && (this.fileService.canHandleResource(source) || source.scheme === 'untitled')) { - - // Save As (or Save untitled with associated path) - if (this.isSaveAs() || source.scheme === 'untitled') { - let encodingOfSource: string; - if (source.scheme === 'untitled') { - encodingOfSource = this.untitledEditorService.getEncoding(source); - } else if (source.scheme === 'file') { - const textModel = this.textFileService.models.get(source); - encodingOfSource = textModel && textModel.getEncoding(); // text model can be null e.g. if this is a binary file! - } - - let viewStateOfSource: IEditorViewState; - const activeEditor = this.editorService.getActiveEditor(); - const editor = getCodeEditor(activeEditor); - if (editor) { - const activeResource = toResource(activeEditor.input, { supportSideBySide: true }); - if (activeResource && (this.fileService.canHandleResource(activeResource) || source.scheme === 'untitled') && activeResource.toString() === source.toString()) { - viewStateOfSource = editor.saveViewState(); - } - } - - // Special case: an untitled file with associated path gets saved directly unless "saveAs" is true - let savePromise: TPromise; - if (!this.isSaveAs() && source.scheme === 'untitled' && this.untitledEditorService.hasAssociatedFilePath(source)) { - savePromise = this.textFileService.save(source).then((result) => { - if (result) { - return URI.file(source.fsPath); - } - - return null; - }); - } - - // Otherwise, really "Save As..." - else { - savePromise = this.textFileService.saveAs(source); - } - - return savePromise.then((target) => { - if (!target || target.toString() === source.toString()) { - return void 0; // save canceled or same resource used - } - - const replaceWith: IResourceInput = { - resource: target, - encoding: encodingOfSource, - options: { - pinned: true, - viewState: viewStateOfSource - } - }; - - return this.editorService.replaceEditors([{ - toReplace: { resource: source }, - replaceWith - }]).then(() => true); - }); - } - - // Pin the active editor if we are saving it - if (!this.resource) { - const editor = this.editorService.getActiveEditor(); - if (editor) { - this.editorGroupService.pinEditor(editor.position, editor.input); - } - } - - // Just save - return this.textFileService.save(source, { force: true /* force a change to the file to trigger external watchers if any */ }); - } - - return TPromise.as(false); - } -} - -export class SaveFileAction extends BaseSaveOneFileAction { - - public static readonly ID = 'workbench.action.files.save'; - public static readonly LABEL = nls.localize('save', "Save"); - - public isSaveAs(): boolean { - return false; - } -} - -export class SaveFileAsAction extends BaseSaveOneFileAction { - - public static readonly ID = 'workbench.action.files.saveAs'; - public static readonly LABEL = nls.localize('saveAs', "Save As..."); - - public isSaveAs(): boolean { - return true; + public run(context?: any): TPromise { + return this.commandService.executeCommand(SAVE_FILE_AS_COMMAND_ID, { resource: this.resource }).then(() => true, error => { + this.onError(error); + return null; + }); } } -export abstract class BaseSaveAllAction extends BaseSaveFileAction { +export abstract class BaseSaveAllAction extends BaseErrorReportingAction { private toDispose: IDisposable[]; private lastIsDirty: boolean; @@ -1440,6 +1348,13 @@ export abstract class BaseSaveAllAction extends BaseSaveFileAction { } } + public run(context?: any): TPromise { + return this.doRun(context).then(() => true, error => { + this.onError(error); + return null; + }); + } + protected doRun(context: any): TPromise { const stacks = this.editorGroupService.getStacksModel(); diff --git a/src/vs/workbench/parts/files/electron-browser/fileCommands.ts b/src/vs/workbench/parts/files/electron-browser/fileCommands.ts index 9a255fb5d0c..f7525924315 100644 --- a/src/vs/workbench/parts/files/electron-browser/fileCommands.ts +++ b/src/vs/workbench/parts/files/electron-browser/fileCommands.ts @@ -13,13 +13,13 @@ import * as labels from 'vs/base/common/labels'; import * as resources from 'vs/base/common/resources'; import URI from 'vs/base/common/uri'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; -import { toResource, IEditorContext } from 'vs/workbench/common/editor'; +import { toResource, IEditorContext, EditorFocusedInOpenEditorsContext, UntitledEditorNotFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext } from 'vs/workbench/common/editor'; import { IWindowsService } from 'vs/platform/windows/common/windows'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; import { ExplorerViewlet } from 'vs/workbench/parts/files/electron-browser/explorerViewlet'; -import { VIEWLET_ID, explorerItemToFileResource } from 'vs/workbench/parts/files/common/files'; +import { VIEWLET_ID, explorerItemToFileResource, AutoSaveDisabledContext } from 'vs/workbench/parts/files/common/files'; import { FileStat, OpenEditor } from 'vs/workbench/parts/files/common/explorerModel'; import errors = require('vs/base/common/errors'); import { ITree } from 'vs/base/parts/tree/browser/tree'; @@ -35,6 +35,12 @@ import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/c import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions'; import { isWindows, isMacintosh } from 'vs/base/common/platform'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; +import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; +import { IResourceInput } from 'vs/platform/editor/common/editor'; +import { IFileService } from 'vs/platform/files/common/files'; +import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService'; +import { IEditorViewState } from 'vs/editor/common/editorCommon'; +import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService'; // Commands @@ -48,6 +54,13 @@ export const COMPARE_WITH_SAVED_COMMAND_ID = 'workbench.files.command.compareWit export const COMPARE_WITH_SAVED_SCHEMA = 'showModifications'; export const COPY_PATH_COMMAND_ID = 'workbench.command.files.copyPath'; +export const SAVE_FILE_AS_COMMAND_ID = 'workbench.command.files.saveAs'; +export const SAVE_FILE_AS_LABEL = nls.localize('saveAs', "Save As..."); +export const SAVE_FILE_COMMAND_ID = 'workbench.command.files.save'; +export const SAVE_FILE_LABEL = nls.localize('save', "Save"); +export const BASE_SAVE_ONE_FILE_COMMAND_ID = 'workbench.command.files.saveAs'; + + registerFileCommands(); export const openWindowCommand = (accessor: ServicesAccessor, paths: string[], forceNewWindow: boolean) => { @@ -269,6 +282,15 @@ function registerFileCommands(): void { } }); + MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, { + group: 'save', + command: { + id: REVERT_FILE_COMMAND_ID, + title: nls.localize('revert', "Revert File") + }, + when: ContextKeyExpr.and(EditorFocusedInOpenEditorsContext, AutoSaveDisabledContext, UntitledEditorNotFocusedInOpenEditorsContext) + }); + CommandsRegistry.registerCommand({ id: OPEN_TO_SIDE_COMMAND_ID, handler: (accessor, args) => { const editorService = accessor.get(IWorkbenchEditorService); @@ -294,7 +316,8 @@ function registerFileCommands(): void { command: { id: OPEN_TO_SIDE_COMMAND_ID, title: nls.localize('openToSide', "Open to the Side") - } + }, + when: EditorFocusedInOpenEditorsContext }); CommandsRegistry.registerCommand({ @@ -321,11 +344,11 @@ function registerFileCommands(): void { MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, { group: 'compare', - command: { id: COMPARE_WITH_SAVED_COMMAND_ID, title: nls.localize('compareWithSaved', "Compare with Saved") - } + }, + when: EditorFocusedInOpenEditorsContext }); CommandsRegistry.registerCommand({ @@ -348,7 +371,8 @@ function registerFileCommands(): void { command: { id: SELECT_FOR_COMPARE_COMMAND_ID, title: nls.localize('compareSource', "Select for Compare") - } + }, + when: EditorFocusedInOpenEditorsContext }); CommandsRegistry.registerCommand({ @@ -374,7 +398,8 @@ function registerFileCommands(): void { command: { id: COMPARE_RESOURCE_COMMAND_ID, title: nls.localize('compareWithChosen', "Compare With Chosen") - } + }, + when: EditorFocusedInOpenEditorsContext }); CommandsRegistry.registerCommand({ @@ -428,7 +453,8 @@ function registerFileCommands(): void { command: { id: COPY_PATH_COMMAND_ID, title: nls.localize('copyPath', "Copy Path") - } + }, + when: EditorFocusedInOpenEditorsContext }); CommandsRegistry.registerCommand({ @@ -460,6 +486,133 @@ function registerFileCommands(): void { command: { id: REVEAL_IN_EXPLORER_COMMAND_ID, title: isWindows ? nls.localize('revealInWindows', "Reveal in Explorer") : isMacintosh ? nls.localize('revealInMac', "Reveal in Finder") : nls.localize('openContainer', "Open Containing Folder") + }, + when: EditorFocusedInOpenEditorsContext + }); + + CommandsRegistry.registerCommand({ + id: SAVE_FILE_AS_COMMAND_ID, + handler: (accessor, args: IEditorContext) => { + const commandService = accessor.get(ICommandService); + return commandService.executeCommand(BASE_SAVE_ONE_FILE_COMMAND_ID, args.resource, true); + } + }); + + MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, { + group: 'save', + command: { + id: SAVE_FILE_AS_COMMAND_ID, + title: SAVE_FILE_AS_LABEL + }, + when: ContextKeyExpr.and(EditorFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext) + }); + + CommandsRegistry.registerCommand({ + id: SAVE_FILE_COMMAND_ID, + handler: (accessor, args: IEditorContext) => { + const commandService = accessor.get(ICommandService); + return commandService.executeCommand(BASE_SAVE_ONE_FILE_COMMAND_ID, args.resource, false); + } + }); + + MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, { + group: 'save', + command: { + id: SAVE_FILE_AS_COMMAND_ID, + title: SAVE_FILE_LABEL + }, + when: ContextKeyExpr.and(EditorFocusedInOpenEditorsContext) + }); + + CommandsRegistry.registerCommand({ + id: BASE_SAVE_ONE_FILE_COMMAND_ID, + handler: (accessor, resource: URI, isSaveAs: boolean) => { + const editorService = accessor.get(IWorkbenchEditorService); + const fileService = accessor.get(IFileService); + const untitledEditorService = accessor.get(IUntitledEditorService); + const textFileService = accessor.get(ITextFileService); + const editorGroupService = accessor.get(IEditorGroupService); + + let source: URI; + if (resource) { + source = resource; + } else { + source = toResource(editorService.getActiveEditorInput(), { supportSideBySide: true }); + } + + if (source && (fileService.canHandleResource(source) || source.scheme === 'untitled')) { + + // Save As (or Save untitled with associated path) + if (isSaveAs || source.scheme === 'untitled') { + let encodingOfSource: string; + if (source.scheme === 'untitled') { + encodingOfSource = untitledEditorService.getEncoding(source); + } else if (source.scheme === 'file') { + const textModel = textFileService.models.get(source); + encodingOfSource = textModel && textModel.getEncoding(); // text model can be null e.g. if this is a binary file! + } + + let viewStateOfSource: IEditorViewState; + const activeEditor = editorService.getActiveEditor(); + const editor = getCodeEditor(activeEditor); + if (editor) { + const activeResource = toResource(activeEditor.input, { supportSideBySide: true }); + if (activeResource && (fileService.canHandleResource(activeResource) || source.scheme === 'untitled') && activeResource.toString() === source.toString()) { + viewStateOfSource = editor.saveViewState(); + } + } + + // Special case: an untitled file with associated path gets saved directly unless "saveAs" is true + let savePromise: TPromise; + if (!isSaveAs && source.scheme === 'untitled' && untitledEditorService.hasAssociatedFilePath(source)) { + savePromise = textFileService.save(source).then((result) => { + if (result) { + return URI.file(source.fsPath); + } + + return null; + }); + } + + // Otherwise, really "Save As..." + else { + savePromise = textFileService.saveAs(source); + } + + return savePromise.then((target) => { + if (!target || target.toString() === source.toString()) { + return void 0; // save canceled or same resource used + } + + const replaceWith: IResourceInput = { + resource: target, + encoding: encodingOfSource, + options: { + pinned: true, + viewState: viewStateOfSource + } + }; + + return editorService.replaceEditors([{ + toReplace: { resource: source }, + replaceWith + }]).then(() => true); + }); + } + + // Pin the active editor if we are saving it + if (!resource) { + const editor = editorService.getActiveEditor(); + if (editor) { + editorGroupService.pinEditor(editor.position, editor.input); + } + } + + // Just save + return textFileService.save(source, { force: true /* force a change to the file to trigger external watchers if any */ }); + } + + return TPromise.as(false); } }); } diff --git a/src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts b/src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts index 7af940b21bc..a942a5b3cc5 100644 --- a/src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts +++ b/src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts @@ -14,8 +14,8 @@ import { IEditorGroupService } from 'vs/workbench/services/group/common/groupSer import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { Position, IEditorInput } from 'vs/platform/editor/common/editor'; -import { IEditorStacksModel, IStacksModelChangeEvent, IEditorGroup, EditorFocusedInOpenEditorsContext, GroupFocusedInOpenEditorsContext } from 'vs/workbench/common/editor'; -import { SaveAllAction, SaveAllInGroupAction, SaveFileAction, RevertFileAction, SaveFileAsAction } from 'vs/workbench/parts/files/electron-browser/fileActions'; +import { IEditorStacksModel, IStacksModelChangeEvent, IEditorGroup, EditorFocusedInOpenEditorsContext, GroupFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext } from 'vs/workbench/common/editor'; +import { SaveAllAction, SaveAllInGroupAction } from 'vs/workbench/parts/files/electron-browser/fileActions'; import { IViewletViewOptions, IViewOptions, ViewsViewletPanel } from 'vs/workbench/browser/parts/views/viewsViewlet'; import { OpenEditorsFocusedContext, ExplorerFocusedContext, IFilesConfiguration } from 'vs/workbench/parts/files/common/files'; import { ITextFileService, AutoSaveMode } from 'vs/workbench/services/textfile/common/textfiles'; @@ -60,6 +60,7 @@ export class OpenEditorsView extends ViewsViewletPanel { private contributedContextMenu: IMenu; private needsRefresh: boolean; private editorFocusedContext: IContextKey; + private untitledEditorFocusedContext: IContextKey; private groupFocusedContext: IContextKey; constructor( @@ -153,15 +154,19 @@ export class OpenEditorsView extends ViewsViewletPanel { OpenEditorsFocusedContext.bindTo(this.list.contextKeyService); ExplorerFocusedContext.bindTo(this.list.contextKeyService); this.editorFocusedContext = EditorFocusedInOpenEditorsContext.bindTo(this.contextKeyService); + this.untitledEditorFocusedContext = UntitledEditorFocusedInOpenEditorsContext.bindTo(this.contextKeyService); this.groupFocusedContext = GroupFocusedInOpenEditorsContext.bindTo(this.contextKeyService); this.disposables.push(this.list.onContextMenu(e => this.onListContextMenu(e))); this.list.onFocusChange(e => { this.editorFocusedContext.reset(); this.groupFocusedContext.reset(); - if (e.elements.length && e.elements[0] instanceof OpenEditor) { + this.untitledEditorFocusedContext.reset(); + const element = e.elements.length ? e.elements[0] : undefined; + if (element instanceof OpenEditor) { this.editorFocusedContext.set(true); - } else if (e.elements.length) { + this.untitledEditorFocusedContext.set(element.isUntitled()); + } else if (!!element) { this.groupFocusedContext.set(true); } }); @@ -218,7 +223,7 @@ export class OpenEditorsView extends ViewsViewletPanel { @memoize private get actionProvider(): ActionProvider { - return new ActionProvider(this.instantiationService, this.textFileService, this.untitledEditorService); + return new ActionProvider(this.instantiationService, this.textFileService); } private get elements(): (IEditorGroup | OpenEditor)[] { @@ -592,8 +597,7 @@ export class ActionProvider extends ContributableActionProvider { constructor( @IInstantiationService private instantiationService: IInstantiationService, - @ITextFileService private textFileService: ITextFileService, - @IUntitledEditorService private untitledEditorService: IUntitledEditorService + @ITextFileService private textFileService: ITextFileService ) { super(); } @@ -607,48 +611,6 @@ export class ActionProvider extends ContributableActionProvider { result.push(this.instantiationService.createInstance(SaveAllInGroupAction, SaveAllInGroupAction.ID, nls.localize('saveAll', "Save All"))); result.push(new Separator()); } - - result.push(this.instantiationService.createInstance(CloseUnmodifiedEditorsInGroupAction, CloseUnmodifiedEditorsInGroupAction.ID, nls.localize('closeAllUnmodified', "Close Unmodified"))); - result.push(this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All"))); - } else { - const openEditor = element; - const resource = openEditor.getResource(); - if (resource) { - - if (!openEditor.isUntitled()) { - - // Files: Save / Revert - if (!autoSaveEnabled) { - result.push(new Separator()); - - const saveAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL); - saveAction.setResource(resource); - saveAction.enabled = openEditor.isDirty(); - result.push(saveAction); - - const revertAction = this.instantiationService.createInstance(RevertFileAction, RevertFileAction.ID, RevertFileAction.LABEL); - revertAction.setResource(resource); - revertAction.enabled = openEditor.isDirty(); - result.push(revertAction); - } - } - - // Untitled: Save / Save As - if (openEditor.isUntitled()) { - result.push(new Separator()); - - if (this.untitledEditorService.hasAssociatedFilePath(resource)) { - let saveUntitledAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL); - saveUntitledAction.setResource(resource); - result.push(saveUntitledAction); - } - - let saveAsAction = this.instantiationService.createInstance(SaveFileAsAction, SaveFileAsAction.ID, SaveFileAsAction.LABEL); - saveAsAction.setResource(resource); - result.push(saveAsAction); - } - - } } return TPromise.as(result); -- GitLab