Skip to content

Commit

Permalink
support preview change when already previewing changes, #77728
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jan 16, 2020
1 parent 2717c6d commit 7652680
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
118 changes: 78 additions & 40 deletions src/vs/workbench/contrib/bulkEdit/browser/bulkEdit.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IBulkEditService, IBulkEditOptions } from 'vs/editor/browser/services/bulkEditService';
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { WorkspaceEdit } from 'vs/editor/common/modes';
import { BulkEditPane } from 'vs/workbench/contrib/bulkEdit/browser/bulkEditPane';
import { IViewContainersRegistry, Extensions as ViewContainerExtensions, ViewContainerLocation, IViewsRegistry } from 'vs/workbench/common/views';
import { localize } from 'vs/nls';
import { ViewPaneContainer, ViewPane } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { PaneCompositePanel } from 'vs/workbench/browser/panel';
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { RawContextKey, IContextKeyService, IContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
import { BulkEditPreviewProvider } from 'vs/workbench/contrib/bulkEdit/browser/bulkEditPreview';
Expand All @@ -26,6 +26,7 @@ import { URI } from 'vs/base/common/uri';
import { MenuId, registerAction2, Action2 } from 'vs/platform/actions/common/actions';
import { IEditorInput } from 'vs/workbench/common/editor';
import type { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { CancellationTokenSource } from 'vs/base/common/cancellation';

function getBulkEditPane(panelService: IPanelService): BulkEditPane | undefined {
let view: ViewPane | undefined;
Expand All @@ -39,33 +40,96 @@ function getBulkEditPane(panelService: IPanelService): BulkEditPane | undefined
return undefined;
}

class UXState {

private readonly _activePanel: string | undefined;

constructor(
@IPanelService private readonly _panelService: IPanelService,
@IEditorGroupsService private readonly _editorGroupsService: IEditorGroupsService,
) {
this._activePanel = _panelService.getActivePanel()?.getId();
}

restore(): void {

// (1) restore previous panel
if (typeof this._activePanel === 'string') {
this._panelService.openPanel(this._activePanel);
} else {
this._panelService.hideActivePanel();
}

// (2) close preview editors
for (let group of this._editorGroupsService.groups) {
let previewEditors: IEditorInput[] = [];
for (let input of group.editors) {

let resource: URI | undefined;
if (input instanceof DiffEditorInput) {
resource = input.modifiedInput.getResource();
} else {
resource = input.getResource();
}

if (resource?.scheme === BulkEditPreviewProvider.Schema) {
previewEditors.push(input);
}
}

if (previewEditors.length) {
group.closeEditors(previewEditors, { preserveFocus: true });
}
}
}
}

class PreviewSession {
constructor(
readonly uxState: UXState,
readonly cts: CancellationTokenSource = new CancellationTokenSource(),
) { }
}

class BulkEditPreviewContribution {

static readonly ctxEnabled = new RawContextKey('refactorPreview.enabled', false);

private readonly _ctxEnabled: IContextKey<boolean>;

private _activeSession: PreviewSession | undefined;

constructor(
@IPanelService private _panelService: IPanelService,
@IPanelService private readonly _panelService: IPanelService,
@IEditorGroupsService private readonly _editorGroupsService: IEditorGroupsService,
@IBulkEditService bulkEditService: IBulkEditService,
@IContextKeyService contextKeyService: IContextKeyService,
) {
bulkEditService.setPreviewHandler((edit, options) => this._previewEdit(edit, options));
bulkEditService.setPreviewHandler((edit) => this._previewEdit(edit));
this._ctxEnabled = BulkEditPreviewContribution.ctxEnabled.bindTo(contextKeyService);
}

private async _previewEdit(edit: WorkspaceEdit, options?: IBulkEditOptions) {
private async _previewEdit(edit: WorkspaceEdit) {
this._ctxEnabled.set(true);
const oldActivePanel = this._panelService.getActivePanel();

// session
let session: PreviewSession;
if (this._activeSession) {
this._activeSession.cts.dispose(true);
session = new PreviewSession(this._activeSession.uxState);
} else {
session = new PreviewSession(new UXState(this._panelService, this._editorGroupsService));
}
this._activeSession = session;

// the actual work...
try {
const view = getBulkEditPane(this._panelService);
if (!view) {
return edit;
}

const newEditOrUndefined = await view.setInput(edit, options?.label);
const newEditOrUndefined = await view.setInput(edit, session.cts.token);
if (!newEditOrUndefined) {
return { edits: [] };
}
Expand All @@ -74,37 +138,11 @@ class BulkEditPreviewContribution {

} finally {
// restore UX state

// (1) hide refactor panel
this._ctxEnabled.set(false);

// (2) restore previous panel
if (oldActivePanel) {
this._panelService.openPanel(oldActivePanel.getId());
} else {
this._panelService.hideActivePanel();
}

// (3) close preview editors
for (let group of this._editorGroupsService.groups) {
let previewEditors: IEditorInput[] = [];
for (let input of group.editors) {

let resource: URI | undefined;
if (input instanceof DiffEditorInput) {
resource = input.modifiedInput.getResource();
} else {
resource = input.getResource();
}

if (resource?.scheme === BulkEditPreviewProvider.Schema) {
previewEditors.push(input);
}
}

if (previewEditors.length) {
group.closeEditors(previewEditors, { preserveFocus: true });
}
if (this._activeSession === session) {
this._activeSession.uxState.restore();
this._activeSession.cts.dispose();
this._ctxEnabled.set(false);
this._activeSession = undefined;
}
}
}
Expand All @@ -129,8 +167,8 @@ registerAction2(class ApplyAction extends Action2 {
order: 1
}],
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
when: BulkEditPreviewContribution.ctxEnabled,
weight: KeybindingWeight.EditorContrib - 10,
when: ContextKeyExpr.and(BulkEditPreviewContribution.ctxEnabled, ContextKeyExpr.equals('activePanel', BulkEditPane.ID)),
primary: KeyMod.Shift + KeyCode.Enter,
}
});
Expand Down
5 changes: 4 additions & 1 deletion src/vs/workbench/contrib/bulkEdit/browser/bulkEditPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import type { IAction } from 'vs/base/common/actions';
import { createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import type { ITreeContextMenuEvent } from 'vs/base/browser/ui/tree/tree';
import { CancellationToken } from 'vs/base/common/cancellation';

const enum State {
Data = 'data',
Expand Down Expand Up @@ -135,7 +136,7 @@ export class BulkEditPane extends ViewPane {
this.element.dataset['state'] = state;
}

async setInput(edit: WorkspaceEdit, label?: string): Promise<WorkspaceEdit | undefined> {
async setInput(edit: WorkspaceEdit, token: CancellationToken): Promise<WorkspaceEdit | undefined> {
this._setState(State.Data);
this._sessionDisposables.clear();

Expand All @@ -153,6 +154,8 @@ export class BulkEditPane extends ViewPane {

return new Promise(async resolve => {

token.onCancellationRequested(() => resolve());

this._currentResolve = resolve;
await this._tree.setInput(input);
this._tree.domFocus();
Expand Down

0 comments on commit 7652680

Please sign in to comment.