aux window - more fixes to prevent data loss (#208085)

This commit is contained in:
Benjamin Pasero
2024-03-19 15:39:51 +01:00
committed by GitHub
parent 569caf471c
commit 78b04c02cc
5 changed files with 24 additions and 34 deletions
@@ -178,10 +178,10 @@ export class AuxiliaryEditorPart {
// will move the editors to the main window. As such,
// we need to validate that we can move and otherwise
// prevent the window from closing.
const canMove = editor.canMove(group.id, this.editorPartsView.mainPart.activeGroup.id);
if (typeof canMove === 'string') {
const canMoveVeto = editor.canMove(group.id, this.editorPartsView.mainPart.activeGroup.id);
if (typeof canMoveVeto === 'string') {
group.openEditor(editor);
event.veto(canMove);
event.veto(canMoveVeto);
break;
}
}
@@ -1335,9 +1335,10 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
// Validate that we can move
if (!keepCopy || editor.hasCapability(EditorInputCapabilities.Singleton) /* singleton editors will always move */) {
const canMove = editor.canMove(this.id, target.id);
if (typeof canMove === 'string') {
this.dialogService.error(canMove);
const canMoveVeto = editor.canMove(this.id, target.id);
if (typeof canMoveVeto === 'string') {
this.dialogService.error(canMoveVeto, localize('moveErrorDetails', "Try saving or reverting the editor first and then try again."));
return false;
}
}
@@ -438,7 +438,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
// into another window because that means, we potentally loose the modified
// state and thus trigger data loss.
return localize('editorCannotMove', "Unable to move the editor out of the window, it contains modifications that can only be saved in the this window.");
return localize('editorCannotMove', "Unable to move '{0}': editor contains changes that can only be saved in its current window.", this.getName());
}
}
@@ -49,17 +49,7 @@ export class NativeAuxiliaryWindow extends AuxiliaryWindow {
protected override async handleVetoBeforeClose(e: BeforeUnloadEvent, veto: string): Promise<void> {
this.preventUnload(e);
await this.dialogService.prompt({
type: 'error',
message: veto,
detail: localize('backupErrorDetails', "Try saving or reverting the editors with unsaved changes first and then try again."),
buttons: [
{
label: localize({ key: 'ok', comment: ['&& denotes a mnemonic'] }, "&&OK"),
run: () => false // veto
}
]
});
await this.dialogService.error(veto, localize('backupErrorDetails', "Try saving or reverting the editors with unsaved changes first and then try again."));
}
protected override async confirmBeforeClose(e: BeforeUnloadEvent): Promise<void> {
@@ -504,9 +504,14 @@ export class EditorResolverService extends Disposable implements IEditorResolver
// If the editor states it can only be opened once per resource we must close all existing ones except one and move the new one into the group
const singleEditorPerResource = typeof selectedEditor.options?.singlePerResource === 'function' ? selectedEditor.options.singlePerResource() : selectedEditor.options?.singlePerResource;
if (singleEditorPerResource) {
const foundInput = await this.moveExistingEditorForResource(resource, selectedEditor.editorInfo.id, group);
if (foundInput) {
return { editor: foundInput, options };
const existingEditors = this.findExistingEditorsForResource(resource, selectedEditor.editorInfo.id);
if (existingEditors.length) {
const editor = await this.moveExistingEditorForResource(existingEditors, group);
if (editor) {
return { editor, options };
} else {
return; // failed to move
}
}
}
@@ -524,27 +529,21 @@ export class EditorResolverService extends Disposable implements IEditorResolver
}
/**
* Moves an editor with the resource and viewtype to target group if one exists
* Moves the first existing editor for a resource to the target group unless already opened there.
* Additionally will close any other editors that are open for that resource and viewtype besides the first one found
* @param resource The resource of the editor
* @param viewType the viewtype of the editor
* @param targetGroup The group to move it to
* @returns An editor input if one exists, else undefined
* @returns The moved editor input or `undefined` if the editor could not be moved
*/
private async moveExistingEditorForResource(
resource: URI,
viewType: string,
existingEditorsForResource: Array<{ editor: EditorInput; group: IEditorGroup }>,
targetGroup: IEditorGroup,
): Promise<EditorInput | undefined> {
const editorInfoForResource = this.findExistingEditorsForResource(resource, viewType);
if (!editorInfoForResource.length) {
return;
}
const editorToUse = editorInfoForResource[0];
const editorToUse = existingEditorsForResource[0];
// We should only have one editor but if there are multiple we close the others
for (const { editor, group } of editorInfoForResource) {
for (const { editor, group } of existingEditorsForResource) {
if (editor !== editorToUse.editor) {
const closed = await group.closeEditor(editor);
if (!closed) {
@@ -559,9 +558,9 @@ export class EditorResolverService extends Disposable implements IEditorResolver
if (!moved) {
return;
}
return editorToUse.editor;
}
return;
return editorToUse.editor;
}
/**