mirror of
https://github.com/microsoft/vscode.git
synced 2026-07-13 21:47:12 -05:00
editors - tweak readonly usage
This commit is contained in:
@@ -545,7 +545,11 @@ export abstract class TextEditorInput extends EditorInput {
|
||||
return this.resource;
|
||||
}
|
||||
|
||||
save(groupId: GroupIdentifier, options?: ITextFileSaveOptions): Promise<boolean> {
|
||||
async save(groupId: GroupIdentifier, options?: ITextFileSaveOptions): Promise<boolean> {
|
||||
if (this.isReadonly()) {
|
||||
return false; // return early if editor is readonly
|
||||
}
|
||||
|
||||
return this.textFileService.save(this.resource, options);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<IEditorGroup>, options?: ISaveEditorsOptions): Promise<void> {
|
||||
function saveDirtyEditorsOfGroups(accessor: ServicesAccessor, groups: ReadonlyArray<IEditorGroup>, options?: ISaveEditorsOptions): Promise<void> {
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -712,21 +712,21 @@ export class EditorService extends Disposable implements EditorServiceImpl {
|
||||
}
|
||||
|
||||
saveAll(options?: ISaveAllEditorsOptions): Promise<boolean> {
|
||||
return this.save(this.getSaveableEditors(!!options?.includeUntitled), options);
|
||||
return this.save(this.getAllDirtyEditors(!!options?.includeUntitled), options);
|
||||
}
|
||||
|
||||
async revertAll(options?: IRevertOptions): Promise<boolean> {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user