From 8efa538d7dfb4fa26fdb9525dcda1a19f0be769e Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 18 Nov 2019 17:08:55 +0100 Subject: [PATCH] editors - tweak readonly usage --- src/vs/workbench/common/editor.ts | 6 +++++- .../contrib/files/browser/fileCommands.ts | 19 +++++++------------ .../services/editor/browser/editorService.ts | 8 ++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index 403dee7e209..0fe32907c4e 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -545,7 +545,11 @@ export abstract class TextEditorInput extends EditorInput { return this.resource; } - save(groupId: GroupIdentifier, options?: ITextFileSaveOptions): Promise { + async save(groupId: GroupIdentifier, options?: ITextFileSaveOptions): Promise { + if (this.isReadonly()) { + return false; // return early if editor is readonly + } + return this.textFileService.save(this.resource, options); } diff --git a/src/vs/workbench/contrib/files/browser/fileCommands.ts b/src/vs/workbench/contrib/files/browser/fileCommands.ts index b638a347cba..962a8183061 100644 --- a/src/vs/workbench/contrib/files/browser/fileCommands.ts +++ b/src/vs/workbench/contrib/files/browser/fileCommands.ts @@ -314,19 +314,14 @@ function saveSelectedEditors(accessor: ServicesAccessor, options?: ISaveEditorsO const listService = accessor.get(IListService); const editorGroupsService = accessor.get(IEditorGroupsService); - let saveableEditors = getMultiSelectedEditors(listService, editorGroupsService); - if (!options?.saveAs) { - saveableEditors = saveableEditors.filter(({ editor }) => !editor.isReadonly()); // Save: only allow non-readonly editors - } - - return doSaveEditors(accessor, saveableEditors, options); + return doSaveEditors(accessor, getMultiSelectedEditors(listService, editorGroupsService), options); } -function saveEditorsOfGroups(accessor: ServicesAccessor, groups: ReadonlyArray, options?: ISaveEditorsOptions): Promise { +function saveDirtyEditorsOfGroups(accessor: ServicesAccessor, groups: ReadonlyArray, options?: ISaveEditorsOptions): Promise { const saveableEditors: IEditorIdentifier[] = []; for (const group of groups) { for (const editor of group.getEditors(EditorsOrder.MOST_RECENTLY_ACTIVE)) { - if (editor.isDirty() && !editor.isReadonly()) { + if (editor.isDirty()) { saveableEditors.push({ groupId: group.id, editor }); } } @@ -352,7 +347,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({ primary: KeyMod.CtrlCmd | KeyCode.KEY_S, id: SAVE_FILE_COMMAND_ID, handler: accessor => { - return saveSelectedEditors(accessor, { reason: SaveReason.EXPLICIT, force: true }); + return saveSelectedEditors(accessor, { reason: SaveReason.EXPLICIT, force: true /* force save even when non-dirty */ }); } }); @@ -363,7 +358,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({ win: { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_S) }, id: SAVE_FILE_WITHOUT_FORMATTING_COMMAND_ID, handler: accessor => { - return saveSelectedEditors(accessor, { reason: SaveReason.EXPLICIT, force: true, skipSaveParticipants: true }); + return saveSelectedEditors(accessor, { reason: SaveReason.EXPLICIT, force: true /* force save even when non-dirty */, skipSaveParticipants: true }); } }); @@ -380,7 +375,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({ CommandsRegistry.registerCommand({ id: SAVE_ALL_COMMAND_ID, handler: (accessor) => { - return saveEditorsOfGroups(accessor, accessor.get(IEditorGroupsService).getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE), { reason: SaveReason.EXPLICIT }); + return saveDirtyEditorsOfGroups(accessor, accessor.get(IEditorGroupsService).getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE), { reason: SaveReason.EXPLICIT }); } }); @@ -398,7 +393,7 @@ CommandsRegistry.registerCommand({ groups = coalesce(contexts.map(context => editorGroupService.getGroup(context.groupId))); } - return saveEditorsOfGroups(accessor, groups, { reason: SaveReason.EXPLICIT }); + return saveDirtyEditorsOfGroups(accessor, groups, { reason: SaveReason.EXPLICIT }); } }); diff --git a/src/vs/workbench/services/editor/browser/editorService.ts b/src/vs/workbench/services/editor/browser/editorService.ts index 6eec1f2f31c..4562afe63e5 100644 --- a/src/vs/workbench/services/editor/browser/editorService.ts +++ b/src/vs/workbench/services/editor/browser/editorService.ts @@ -712,21 +712,21 @@ export class EditorService extends Disposable implements EditorServiceImpl { } saveAll(options?: ISaveAllEditorsOptions): Promise { - return this.save(this.getSaveableEditors(!!options?.includeUntitled), options); + return this.save(this.getAllDirtyEditors(!!options?.includeUntitled), options); } async revertAll(options?: IRevertOptions): Promise { - const result = await Promise.all(this.getSaveableEditors(true /* include untitled */).map(async ({ editor }) => editor.revert(options))); + const result = await Promise.all(this.getAllDirtyEditors(true /* include untitled */).map(async ({ editor }) => editor.revert(options))); return result.every(success => !!success); } - private getSaveableEditors(includeUntitled: boolean): IEditorIdentifier[] { + private getAllDirtyEditors(includeUntitled: boolean): IEditorIdentifier[] { const editors: IEditorIdentifier[] = []; for (const group of this.editorGroupService.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE)) { for (const editor of group.getEditors(EditorsOrder.MOST_RECENTLY_ACTIVE)) { - if (editor.isDirty() && !editor.isReadonly() && (!editor.isUntitled() || includeUntitled)) { + if (editor.isDirty() && (!editor.isUntitled() || includeUntitled)) { editors.push({ groupId: group.id, editor }); } } -- GitLab