Skip to content

Auto open diff editor on Refactor Preview #159283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions src/vs/workbench/contrib/bulkEdit/browser/preview/bulkEditPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { IOpenerService } from 'vs/platform/opener/common/opener';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
import { ButtonBar } from 'vs/base/browser/ui/button/button';
import { IEditorPane } from 'vs/workbench/common/editor';

const enum State {
Data = 'data',
Expand Down Expand Up @@ -67,6 +68,7 @@ export class BulkEditPane extends ViewPane {
private _currentResolve?: (edit?: ResourceEdit[]) => void;
private _currentInput?: BulkFileOperations;
private _currentProvider?: BulkEditPreviewProvider;
private _activeEditor?: IEditorPane;


constructor(
Expand Down Expand Up @@ -96,6 +98,22 @@ export class BulkEditPane extends ViewPane {
this._ctxHasCategories = BulkEditPane.ctxHasCategories.bindTo(contextKeyService);
this._ctxGroupByFile = BulkEditPane.ctxGroupByFile.bindTo(contextKeyService);
this._ctxHasCheckedChanges = BulkEditPane.ctxHasCheckedChanges.bindTo(contextKeyService);

this._disposables.add(this.onDidFocus(e => {
if (
!this._activeEditor
// Diff Editor associated with the Refactor Preview should reopen
// only if its not visible at the moment
|| this._activeEditor.isVisible()
|| !this._activeEditor.group?.previewEditor
) {
return;
}

this._activeEditor.group.openEditor(
this._activeEditor.group.previewEditor
);
}));
}

override dispose(): void {
Expand Down Expand Up @@ -209,6 +227,7 @@ export class BulkEditPane extends ViewPane {
this._currentResolve = resolve;
this._setTreeInput(input);

this._openEditorOnFirstApplicableEdit(input);
// refresh when check state changes
this._sessionDisposables.add(input.checked.onDidChange(() => {
this._tree.updateChildren();
Expand Down Expand Up @@ -245,6 +264,62 @@ export class BulkEditPane extends ViewPane {
}
}

private async _openEditorOnFirstApplicableEdit(input: BulkFileOperations) {
const elements = await this._treeDataSource.getChildren(input);
// if no item is selected, first FileElement is returned
const hasNoCheckedEdit = !input.checked.checkedCount;
const element = await this._getFirstApplicableElement(elements, hasNoCheckedEdit);
if (!element) {
return;
}

await this._openElementAsEditor({
editorOptions: {
pinned: false,
preserveFocus: true,
revealIfOpened: true
},
sideBySide: false,
element,
});
}

private async _getFirstApplicableElement(elements: BulkEditElement[], hasNoCheckedEdit: boolean):
Promise<FileElement | TextEditElement | undefined> {
const fileElementPromises = [];
const textEditElementPromises = [];

// the input always contains either file or category grouping,
// so the top-level type is either CategoryElements or FileElements
for (const element of elements) {
if (element instanceof CategoryElement) {
fileElementPromises.push(this._treeDataSource.getChildren(element) as Promise<FileElement[]>);
} else if (element instanceof FileElement) {
if (hasNoCheckedEdit) {
return element;
}
textEditElementPromises.push(this._treeDataSource.getChildren(element) as Promise<TextEditElement[]>);
}
}

for (const fileElements of await Promise.all(fileElementPromises) ?? []) {
for (const element of fileElements) {
if (hasNoCheckedEdit) {
return element;
}
textEditElementPromises.push(this._treeDataSource.getChildren(element) as Promise<TextEditElement[]>);
}
}

for (const textEdits of await Promise.all(textEditElementPromises) ?? []) {
const checkedEdit = textEdits.find(edit => edit.isChecked());
if (checkedEdit) {
return checkedEdit;
}
}
return undefined;
}

accept(): void {

const conflicts = this._currentInput?.conflicts.list();
Expand Down Expand Up @@ -336,11 +411,11 @@ export class BulkEditPane extends ViewPane {

if (fileElement.edit.type & BulkFileOperationType.Delete) {
// delete -> show single editor
this._editorService.openEditor({
this._activeEditor = await this._editorService.openEditor({
label: localize('edt.title.del', "{0} (delete, refactor preview)", basename(fileElement.edit.uri)),
resource: previewUri,
options
});
}, e.sideBySide ? SIDE_GROUP : ACTIVE_GROUP);

} else {
// rename, create, edits -> show diff editr
Expand All @@ -366,7 +441,7 @@ export class BulkEditPane extends ViewPane {
label = localize('edt.title.1', "{0} (refactor preview)", basename(fileElement.edit.uri));
}

this._editorService.openEditor({
this._activeEditor = await this._editorService.openEditor({
original: { resource: leftResource },
modified: { resource: previewUri },
label,
Expand Down