Skip to content
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

Refactoring Preview option in Command Palette (part of #151140) #151566

Merged
merged 6 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/vs/editor/contrib/codeAction/browser/codeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeat

export const codeActionCommandId = 'editor.action.codeAction';
export const refactorCommandId = 'editor.action.refactor';
export const refactorPreviewCommandId = 'editor.action.refactor.preview';
export const sourceActionCommandId = 'editor.action.sourceAction';
export const organizeImportsCommandId = 'editor.action.organizeImports';
export const fixAllCommandId = 'editor.action.fixAll';
Expand Down
68 changes: 54 additions & 14 deletions src/vs/editor/contrib/codeAction/browser/codeActionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { CodeActionTriggerType } from 'vs/editor/common/languages';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { codeActionCommandId, CodeActionItem, CodeActionSet, fixAllCommandId, organizeImportsCommandId, refactorCommandId, sourceActionCommandId } from 'vs/editor/contrib/codeAction/browser/codeAction';
import { codeActionCommandId, CodeActionItem, CodeActionSet, fixAllCommandId, organizeImportsCommandId, refactorCommandId, refactorPreviewCommandId, sourceActionCommandId } from 'vs/editor/contrib/codeAction/browser/codeAction';
import { CodeActionUi } from 'vs/editor/contrib/codeAction/browser/codeActionUi';
import { MessageController } from 'vs/editor/contrib/message/browser/messageController';
import * as nls from 'vs/nls';
Expand All @@ -27,8 +27,8 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { CodeActionModel, CodeActionsState, SUPPORTED_CODE_ACTIONS } from './codeActionModel';
import { CodeActionAutoApply, CodeActionCommandArgs, CodeActionFilter, CodeActionKind, CodeActionTrigger } from './types';
Expand Down Expand Up @@ -92,11 +92,12 @@ export class QuickFixController extends Disposable implements IEditorContributio
this._model = this._register(new CodeActionModel(this._editor, languageFeaturesService.codeActionProvider, markerService, contextKeyService, progressService));
this._register(this._model.onDidChangeState(newState => this.update(newState)));


this._ui = new Lazy(() =>
this._register(new CodeActionUi(editor, QuickFixAction.Id, AutoFixAction.Id, {
applyCodeAction: async (action, retrigger) => {
applyCodeAction: async (action, retrigger, showPreview) => {
try {
await this._applyCodeAction(action);
await this._applyCodeAction(action, showPreview);
} finally {
if (retrigger) {
this._trigger({ type: CodeActionTriggerType.Auto, filter: {} });
Expand All @@ -118,30 +119,31 @@ export class QuickFixController extends Disposable implements IEditorContributio
public manualTriggerAtCurrentPosition(
notAvailableMessage: string,
filter?: CodeActionFilter,
autoApply?: CodeActionAutoApply
autoApply?: CodeActionAutoApply,
preview?: boolean
): void {
if (!this._editor.hasModel()) {
return;
}

MessageController.get(this._editor)?.closeMessage();
const triggerPosition = this._editor.getPosition();
this._trigger({ type: CodeActionTriggerType.Invoke, filter, autoApply, context: { notAvailableMessage, position: triggerPosition } });
this._trigger({ type: CodeActionTriggerType.Invoke, filter, autoApply, context: { notAvailableMessage, position: triggerPosition }, preview });
}

private _trigger(trigger: CodeActionTrigger) {
return this._model.trigger(trigger);
}

private _applyCodeAction(action: CodeActionItem): Promise<void> {
return this._instantiationService.invokeFunction(applyCodeAction, action, this._editor);
private _applyCodeAction(action: CodeActionItem, showPreview: boolean): Promise<void> {
return this._instantiationService.invokeFunction(applyCodeAction, action, { showPreviewPane: showPreview, editor: this._editor });
}
}

export async function applyCodeAction(
accessor: ServicesAccessor,
item: CodeActionItem,
editor?: ICodeEditor,
options?: { showPreviewPane?: boolean; editor?: ICodeEditor }
): Promise<void> {
const bulkEditService = accessor.get(IBulkEditService);
const commandService = accessor.get(ICommandService);
Expand Down Expand Up @@ -171,11 +173,12 @@ export async function applyCodeAction(

if (item.action.edit) {
await bulkEditService.apply(ResourceEdit.convert(item.action.edit), {
editor,
editor: options?.editor,
label: item.action.title,
quotableLabel: item.action.title,
code: 'undoredo.codeAction',
respectAutoSaveConfig: true
respectAutoSaveConfig: true,
showPreview: options?.showPreviewPane,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Potentially align these names: showPreview vs showPreviewPane vs preview

});
}

Expand Down Expand Up @@ -206,12 +209,13 @@ function triggerCodeActionsForEditorSelection(
editor: ICodeEditor,
notAvailableMessage: string,
filter: CodeActionFilter | undefined,
autoApply: CodeActionAutoApply | undefined
autoApply: CodeActionAutoApply | undefined,
preview: boolean = false
): void {
if (editor.hasModel()) {
const controller = QuickFixController.get(editor);
if (controller) {
controller.manualTriggerAtCurrentPosition(notAvailableMessage, filter, autoApply);
controller.manualTriggerAtCurrentPosition(notAvailableMessage, filter, autoApply, preview);
}
}
}
Expand Down Expand Up @@ -320,12 +324,48 @@ export class RefactorAction extends EditorAction {
: nls.localize('editor.action.refactor.noneMessage', "No refactorings available"),
{
include: CodeActionKind.Refactor.contains(args.kind) ? args.kind : CodeActionKind.None,
onlyIncludePreferredActions: args.preferred,
onlyIncludePreferredActions: args.preferred
},
args.apply);
}
}

export class RefactorPreview extends EditorAction {

constructor() {
super({
id: refactorPreviewCommandId,
label: nls.localize('refactor.preview.label', "Refactor Preview..."),
mjbvz marked this conversation as resolved.
Show resolved Hide resolved
alias: 'Refactor Preview...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
description: {
description: 'Refactor Preview...',
args: [{ name: 'args', schema: argsSchema }]
}
});
}

public run(_accessor: ServicesAccessor, editor: ICodeEditor, userArgs: any): void {
const args = CodeActionCommandArgs.fromUser(userArgs, {
kind: CodeActionKind.Refactor,
apply: CodeActionAutoApply.Never
});
return triggerCodeActionsForEditorSelection(editor,
typeof userArgs?.kind === 'string'
mjbvz marked this conversation as resolved.
Show resolved Hide resolved
? args.preferred
? nls.localize('editor.action.refactor.noneMessage.preferred.kind', "No preferred refactorings for '{0}' available", userArgs.kind)
: nls.localize('editor.action.refactor.noneMessage.kind', "No refactorings for '{0}' available", userArgs.kind)
: args.preferred
? nls.localize('editor.action.refactor.noneMessage.preferred', "No preferred refactorings available")
: nls.localize('editor.action.refactor.noneMessage', "No refactorings available"),
{
include: CodeActionKind.Refactor.contains(args.kind) ? args.kind : CodeActionKind.None,
onlyIncludePreferredActions: args.preferred,
},
args.apply, true);
}
}

export class SourceAction extends EditorAction {

constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*--------------------------------------------------------------------------------------------*/

import { registerEditorAction, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { AutoFixAction, CodeActionCommand, FixAllAction, OrganizeImportsAction, QuickFixAction, QuickFixController, RefactorAction, SourceAction } from 'vs/editor/contrib/codeAction/browser/codeActionCommands';
import { AutoFixAction, CodeActionCommand, FixAllAction, OrganizeImportsAction, QuickFixAction, QuickFixController, RefactorAction, RefactorPreview, SourceAction } from 'vs/editor/contrib/codeAction/browser/codeActionCommands';


registerEditorContribution(QuickFixController.ID, QuickFixController);
registerEditorAction(QuickFixAction);
registerEditorAction(RefactorAction);
registerEditorAction(RefactorPreview);
registerEditorAction(SourceAction);
registerEditorAction(OrganizeImportsAction);
registerEditorAction(AutoFixAction);
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/codeAction/browser/codeActionMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';

interface CodeActionWidgetDelegate {
onSelectCodeAction: (action: CodeActionItem) => Promise<any>;
onSelectCodeAction: (action: CodeActionItem, trigger: CodeActionTrigger) => Promise<any>;
}

interface ResolveCodeActionKeybinding {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class CodeActionMenu extends Disposable {
actionsToShow: readonly CodeActionItem[],
documentation: readonly Command[]
): IAction[] {
const toCodeActionAction = (item: CodeActionItem): CodeActionAction => new CodeActionAction(item.action, () => this._delegate.onSelectCodeAction(item));
const toCodeActionAction = (item: CodeActionItem): CodeActionAction => new CodeActionAction(item.action, () => this._delegate.onSelectCodeAction(item, trigger));

const result: IAction[] = actionsToShow
.map(toCodeActionAction);
Expand Down
13 changes: 9 additions & 4 deletions src/vs/editor/contrib/codeAction/browser/codeActionUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ export class CodeActionUi extends Disposable {
quickFixActionId: string,
preferredFixActionId: string,
private readonly delegate: {
applyCodeAction: (action: CodeActionItem, regtriggerAfterApply: boolean) => Promise<void>;
applyCodeAction: (action: CodeActionItem, regtriggerAfterApply: boolean, preview: boolean) => Promise<void>;
},
@IInstantiationService instantiationService: IInstantiationService,
) {
super();

this._codeActionWidget = new Lazy(() => {
return this._register(instantiationService.createInstance(CodeActionMenu, this._editor, {
onSelectCodeAction: async (action) => {
this.delegate.applyCodeAction(action, /* retrigger */ true);
onSelectCodeAction: async (action, trigger) => {
if (trigger.preview) {
this.delegate.applyCodeAction(action, /* retrigger */ true, true);
mjbvz marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.delegate.applyCodeAction(action, /* retrigger */ true, false);
}

}
}));
});
Expand Down Expand Up @@ -85,7 +90,7 @@ export class CodeActionUi extends Disposable {
if (validActionToApply) {
try {
this._lightBulbWidget.getValue().hide();
await this.delegate.applyCodeAction(validActionToApply, false);
await this.delegate.applyCodeAction(validActionToApply, false, false);
} finally {
actions.dispose();
}
Expand Down
1 change: 1 addition & 0 deletions src/vs/editor/contrib/codeAction/browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface CodeActionTrigger {
readonly notAvailableMessage: string;
readonly position: Position;
};
readonly preview?: boolean;
}

export class CodeActionCommandArgs {
Expand Down