Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 (#6381)

* Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973

* disable strict null check
This commit is contained in:
Anthony Dresser
2019-07-15 22:35:46 -07:00
committed by GitHub
parent f720ec642f
commit 0b7e7ddbf9
2406 changed files with 59140 additions and 35464 deletions

View File

@@ -15,8 +15,14 @@ import { CodeAction, CodeActionContext, CodeActionProviderRegistry, CodeActionTr
import { IModelService } from 'vs/editor/common/services/modelService';
import { CodeActionFilter, CodeActionKind, CodeActionTrigger, filtersAction, mayIncludeActionsOfKind } from './codeActionTrigger';
import { TextModelCancellationTokenSource } from 'vs/editor/browser/core/editorState';
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
export class CodeActionSet {
export interface CodeActionSet extends IDisposable {
readonly actions: readonly CodeAction[];
readonly hasAutoFix: boolean;
}
class ManagedCodeActionSet extends Disposable implements CodeActionSet {
private static codeActionsComparator(a: CodeAction, b: CodeAction): number {
if (isNonEmptyArray(a.diagnostics)) {
@@ -34,8 +40,10 @@ export class CodeActionSet {
public readonly actions: readonly CodeAction[];
public constructor(actions: readonly CodeAction[]) {
this.actions = mergeSort([...actions], CodeActionSet.codeActionsComparator);
public constructor(actions: readonly CodeAction[], disposables: DisposableStore) {
super();
this._register(disposables);
this.actions = mergeSort([...actions], ManagedCodeActionSet.codeActionsComparator);
}
public get hasAutoFix() {
@@ -59,12 +67,14 @@ export function getCodeActions(
const cts = new TextModelCancellationTokenSource(model, token);
const providers = getCodeActionProviders(model, filter);
const disposables = new DisposableStore();
const promises = providers.map(provider => {
return Promise.resolve(provider.provideCodeActions(model, rangeOrSelection, codeActionContext, cts.token)).then(providedCodeActions => {
if (cts.token.isCancellationRequested || !Array.isArray(providedCodeActions)) {
if (cts.token.isCancellationRequested || !providedCodeActions) {
return [];
}
return providedCodeActions.filter(action => action && filtersAction(filter, action));
disposables.add(providedCodeActions);
return providedCodeActions.actions.filter(action => action && filtersAction(filter, action));
}, (err): CodeAction[] => {
if (isPromiseCanceledError(err)) {
throw err;
@@ -84,7 +94,7 @@ export function getCodeActions(
return Promise.all(promises)
.then(flatten)
.then(actions => new CodeActionSet(actions))
.then(actions => new ManagedCodeActionSet(actions, disposables))
.finally(() => {
listener.dispose();
cts.dispose();
@@ -106,7 +116,7 @@ function getCodeActionProviders(
});
}
registerLanguageCommand('_executeCodeActionProvider', function (accessor, args): Promise<ReadonlyArray<CodeAction>> {
registerLanguageCommand('_executeCodeActionProvider', async function (accessor, args): Promise<ReadonlyArray<CodeAction>> {
const { resource, range, kind } = args;
if (!(resource instanceof URI) || !Range.isIRange(range)) {
throw illegalArgument();
@@ -117,9 +127,12 @@ registerLanguageCommand('_executeCodeActionProvider', function (accessor, args):
throw illegalArgument();
}
return getCodeActions(
const codeActionSet = await getCodeActions(
model,
model.validateRange(range),
{ type: 'manual', filter: { includeSourceActions: true, kind: kind && kind.value ? new CodeActionKind(kind.value) : undefined } },
CancellationToken.None).then(actions => actions.actions);
CancellationToken.None);
setTimeout(() => codeActionSet.dispose(), 0);
return codeActionSet.actions;
});

View File

@@ -3,7 +3,6 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancelablePromise } from 'vs/base/common/async';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { Disposable } from 'vs/base/common/lifecycle';
import { escapeRegExpCharacters } from 'vs/base/common/strings';
@@ -13,21 +12,21 @@ import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { CodeAction } from 'vs/editor/common/modes';
import { CodeActionUi } from 'vs/editor/contrib/codeAction/codeActionUi';
import { MessageController } from 'vs/editor/contrib/message/messageController';
import * as nls from 'vs/nls';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { CodeActionModel, SUPPORTED_CODE_ACTIONS, CodeActionsState } from './codeActionModel';
import { CodeActionAutoApply, CodeActionFilter, CodeActionKind } from './codeActionTrigger';
import { CodeActionContextMenu } from './codeActionWidget';
import { LightBulbWidget } from './lightBulbWidget';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
import { CodeActionModel, CodeActionsState, SUPPORTED_CODE_ACTIONS } from './codeActionModel';
import { CodeActionAutoApply, CodeActionFilter, CodeActionKind, CodeActionTrigger } from './codeActionTrigger';
import { CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
import { IAnchor } from 'vs/base/browser/ui/contextview/contextview';
import { IPosition } from 'vs/editor/common/core/position';
function contextKeyForSupportedActions(kind: CodeActionKind) {
return ContextKeyExpr.regex(
@@ -35,6 +34,7 @@ function contextKeyForSupportedActions(kind: CodeActionKind) {
new RegExp('(\\s|^)' + escapeRegExpCharacters(kind.value) + '\\b'));
}
export class QuickFixController extends Disposable implements IEditorContribution {
private static readonly ID = 'editor.contrib.quickFixController';
@@ -45,104 +45,68 @@ export class QuickFixController extends Disposable implements IEditorContributio
private readonly _editor: ICodeEditor;
private readonly _model: CodeActionModel;
private readonly _codeActionContextMenu: CodeActionContextMenu;
private readonly _lightBulbWidget: LightBulbWidget;
private _activeRequest: CancelablePromise<CodeActionSet> | undefined;
private readonly _ui: CodeActionUi;
constructor(
editor: ICodeEditor,
@IMarkerService markerService: IMarkerService,
@IContextKeyService contextKeyService: IContextKeyService,
@IProgressService progressService: IProgressService,
@IEditorProgressService progressService: IEditorProgressService,
@IContextMenuService contextMenuService: IContextMenuService,
@IKeybindingService keybindingService: IKeybindingService,
@ICommandService private readonly _commandService: ICommandService,
@IKeybindingService private readonly _keybindingService: IKeybindingService,
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
) {
super();
this._editor = editor;
this._model = new CodeActionModel(this._editor, markerService, contextKeyService, progressService);
this._codeActionContextMenu = new CodeActionContextMenu(editor, contextMenuService, action => this._onApplyCodeAction(action));
this._lightBulbWidget = this._register(new LightBulbWidget(editor));
this._model = this._register(new CodeActionModel(this._editor, markerService, contextKeyService, progressService));
this._register(this._model.onDidChangeState((newState) => this.update(newState)));
this._updateLightBulbTitle();
this._register(this._codeActionContextMenu.onDidExecuteCodeAction(_ => this._model.trigger({ type: 'auto', filter: {} })));
this._register(this._lightBulbWidget.onClick(this._handleLightBulbSelect, this));
this._register(this._model.onDidChangeState(e => this._onDidChangeCodeActionsState(e)));
this._register(this._keybindingService.onDidUpdateKeybindings(this._updateLightBulbTitle, this));
}
public dispose(): void {
super.dispose();
this._model.dispose();
}
private _onDidChangeCodeActionsState(newState: CodeActionsState.State): void {
if (this._activeRequest) {
this._activeRequest.cancel();
this._activeRequest = undefined;
}
if (newState.type === CodeActionsState.Type.Triggered) {
this._activeRequest = newState.actions;
if (newState.trigger.filter && newState.trigger.filter.kind) {
// Triggered for specific scope
newState.actions.then(fixes => {
if (fixes.actions.length > 0) {
// Apply if we only have one action or requested autoApply
if (newState.trigger.autoApply === CodeActionAutoApply.First || (newState.trigger.autoApply === CodeActionAutoApply.IfSingle && fixes.actions.length === 1)) {
this._onApplyCodeAction(fixes.actions[0]);
return;
}
this._ui = this._register(new CodeActionUi(editor, QuickFixAction.Id, {
applyCodeAction: async (action, retrigger) => {
try {
await this._applyCodeAction(action);
} finally {
if (retrigger) {
this._trigger({ type: 'auto', filter: {} });
}
this._codeActionContextMenu.show(newState.actions, newState.position);
}).catch(onUnexpectedError);
} else if (newState.trigger.type === 'manual') {
this._codeActionContextMenu.show(newState.actions, newState.position);
} else {
// auto magically triggered
// * update an existing list of code actions
// * manage light bulb
if (this._codeActionContextMenu.isVisible) {
this._codeActionContextMenu.show(newState.actions, newState.position);
} else {
this._lightBulbWidget.tryShow(newState);
}
}
} else {
this._lightBulbWidget.hide();
}
}, contextMenuService, keybindingService));
}
private update(newState: CodeActionsState.State): void {
this._ui.update(newState);
}
public showCodeActions(actions: CodeActionSet, at: IAnchor | IPosition) {
return this._ui.showCodeActionList(actions, at);
}
public getId(): string {
return QuickFixController.ID;
}
private _handleLightBulbSelect(e: { x: number, y: number, state: CodeActionsState.Triggered }): void {
this._codeActionContextMenu.show(e.state.actions, e);
}
public triggerFromEditorSelection(filter?: CodeActionFilter, autoApply?: CodeActionAutoApply): Promise<CodeActionSet | undefined> {
return this._model.trigger({ type: 'manual', filter, autoApply });
}
private _updateLightBulbTitle(): void {
const kb = this._keybindingService.lookupKeybinding(QuickFixAction.Id);
let title: string;
if (kb) {
title = nls.localize('quickFixWithKb', "Show Fixes ({0})", kb.getLabel());
} else {
title = nls.localize('quickFix', "Show Fixes");
public manualTriggerAtCurrentPosition(
notAvailableMessage: string,
filter?: CodeActionFilter,
autoApply?: CodeActionAutoApply
): void {
if (!this._editor.hasModel()) {
return;
}
this._lightBulbWidget.title = title;
MessageController.get(this._editor).closeMessage();
const triggerPosition = this._editor.getPosition();
this._trigger({ type: 'manual', filter, autoApply, context: { notAvailableMessage, position: triggerPosition } });
}
private _onApplyCodeAction(action: CodeAction): Promise<void> {
private _trigger(trigger: CodeActionTrigger) {
return this._model.trigger(trigger);
}
private _applyCodeAction(action: CodeAction): Promise<void> {
return applyCodeAction(action, this._bulkEditService, this._commandService, this._editor);
}
}
@@ -161,28 +125,18 @@ export async function applyCodeAction(
}
}
function showCodeActionsForEditorSelection(
function triggerCodeActionsForEditorSelection(
editor: ICodeEditor,
notAvailableMessage: string,
filter?: CodeActionFilter,
autoApply?: CodeActionAutoApply
) {
if (!editor.hasModel()) {
return;
}
const controller = QuickFixController.get(editor);
if (!controller) {
return;
}
MessageController.get(editor).closeMessage();
const pos = editor.getPosition();
controller.triggerFromEditorSelection(filter, autoApply).then(codeActions => {
if (!codeActions || !codeActions.actions.length) {
MessageController.get(editor).showMessage(notAvailableMessage, pos);
filter: CodeActionFilter | undefined,
autoApply: CodeActionAutoApply | undefined
): void {
if (editor.hasModel()) {
const controller = QuickFixController.get(editor);
if (controller) {
controller.manualTriggerAtCurrentPosition(notAvailableMessage, filter, autoApply);
}
});
}
}
export class QuickFixAction extends EditorAction {
@@ -193,7 +147,7 @@ export class QuickFixAction extends EditorAction {
super({
id: QuickFixAction.Id,
label: nls.localize('quickfix.trigger.label', "Quick Fix..."),
alias: 'Quick Fix',
alias: 'Quick Fix...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
kbOpts: {
kbExpr: EditorContextKeys.editorTextFocus,
@@ -204,7 +158,7 @@ export class QuickFixAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"));
return triggerCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"), undefined, undefined);
}
}
@@ -284,7 +238,7 @@ export class CodeActionCommand extends EditorCommand {
kind: CodeActionKind.Empty,
apply: CodeActionAutoApply.IfSingle,
});
return showCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"),
return triggerCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"),
{
kind: args.kind,
includeSourceActions: true,
@@ -303,7 +257,7 @@ export class RefactorAction extends EditorAction {
super({
id: RefactorAction.Id,
label: nls.localize('refactor.label', "Refactor..."),
alias: 'Refactor',
alias: 'Refactor...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
kbOpts: {
kbExpr: EditorContextKeys.editorTextFocus,
@@ -347,7 +301,7 @@ export class RefactorAction extends EditorAction {
kind: CodeActionKind.Refactor,
apply: CodeActionAutoApply.Never
});
return showCodeActionsForEditorSelection(editor,
return triggerCodeActionsForEditorSelection(editor,
nls.localize('editor.action.refactor.noneMessage', "No refactorings available"),
{
kind: CodeActionKind.Refactor.contains(args.kind) ? args.kind : CodeActionKind.Empty,
@@ -366,7 +320,7 @@ export class SourceAction extends EditorAction {
super({
id: SourceAction.Id,
label: nls.localize('source.label', "Source Action..."),
alias: 'Source Action',
alias: 'Source Action...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
menuOpts: {
group: '1_modification',
@@ -402,7 +356,7 @@ export class SourceAction extends EditorAction {
kind: CodeActionKind.Source,
apply: CodeActionAutoApply.Never
});
return showCodeActionsForEditorSelection(editor,
return triggerCodeActionsForEditorSelection(editor,
nls.localize('editor.action.source.noneMessage', "No source actions available"),
{
kind: CodeActionKind.Source.contains(args.kind) ? args.kind : CodeActionKind.Empty,
@@ -434,7 +388,7 @@ export class OrganizeImportsAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor,
return triggerCodeActionsForEditorSelection(editor,
nls.localize('editor.action.organize.noneMessage', "No organize imports action available"),
{ kind: CodeActionKind.SourceOrganizeImports, includeSourceActions: true },
CodeActionAutoApply.IfSingle);
@@ -457,7 +411,7 @@ export class FixAllAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor,
return triggerCodeActionsForEditorSelection(editor,
nls.localize('fixAll.noneMessage', "No fix all action available"),
{ kind: CodeActionKind.SourceFixAll, includeSourceActions: true },
CodeActionAutoApply.IfSingle);
@@ -472,7 +426,7 @@ export class AutoFixAction extends EditorAction {
super({
id: AutoFixAction.Id,
label: nls.localize('autoFix.label', "Auto Fix..."),
alias: 'Auto Fix',
alias: 'Auto Fix...',
precondition: ContextKeyExpr.and(
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.QuickFix)),
@@ -488,7 +442,7 @@ export class AutoFixAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor,
return triggerCodeActionsForEditorSelection(editor,
nls.localize('editor.action.autoFix.noneMessage', "No auto fixes available"),
{
kind: CodeActionKind.QuickFix,

View File

@@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/
import { CancelablePromise, createCancelablePromise, TimeoutTimer } from 'vs/base/common/async';
import { Emitter, Event } from 'vs/base/common/event';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { Emitter } from 'vs/base/common/event';
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
@@ -14,36 +14,34 @@ import { Selection } from 'vs/editor/common/core/selection';
import { CodeActionProviderRegistry } from 'vs/editor/common/modes';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
import { getCodeActions, CodeActionSet } from './codeAction';
import { CodeActionTrigger } from './codeActionTrigger';
export const SUPPORTED_CODE_ACTIONS = new RawContextKey<string>('supportedCodeAction', '');
export class CodeActionOracle {
export type TriggeredCodeAction = undefined | {
readonly selection: Selection;
readonly trigger: CodeActionTrigger;
readonly position: Position;
};
private _disposables: IDisposable[] = [];
private readonly _autoTriggerTimer = new TimeoutTimer();
class CodeActionOracle extends Disposable {
private readonly _autoTriggerTimer = this._register(new TimeoutTimer());
constructor(
private readonly _editor: ICodeEditor,
private readonly _markerService: IMarkerService,
private readonly _signalChange: (newState: CodeActionsState.State) => void,
private readonly _signalChange: (triggered: TriggeredCodeAction) => void,
private readonly _delay: number = 250,
private readonly _progressService?: IProgressService,
) {
this._disposables.push(
this._markerService.onMarkerChanged(e => this._onMarkerChanges(e)),
this._editor.onDidChangeCursorPosition(() => this._onCursorChange()),
);
super();
this._register(this._markerService.onMarkerChanged(e => this._onMarkerChanges(e)));
this._register(this._editor.onDidChangeCursorPosition(() => this._onCursorChange()));
}
dispose(): void {
this._disposables = dispose(this._disposables);
this._autoTriggerTimer.cancel();
}
trigger(trigger: CodeActionTrigger) {
public trigger(trigger: CodeActionTrigger): TriggeredCodeAction {
const selection = this._getRangeOfSelectionUnlessWhitespaceEnclosed(trigger);
return this._createEventAndSignalChange(trigger, selection);
}
@@ -109,38 +107,27 @@ export class CodeActionOracle {
}
}
}
return selection ? selection : undefined;
return selection;
}
private _createEventAndSignalChange(trigger: CodeActionTrigger, selection: Selection | undefined): Promise<CodeActionSet | undefined> {
if (!selection) {
private _createEventAndSignalChange(trigger: CodeActionTrigger, selection: Selection | undefined): TriggeredCodeAction {
const model = this._editor.getModel();
if (!selection || !model) {
// cancel
this._signalChange(CodeActionsState.Empty);
return Promise.resolve(undefined);
} else {
const model = this._editor.getModel();
if (!model) {
// cancel
this._signalChange(CodeActionsState.Empty);
return Promise.resolve(undefined);
}
const markerRange = this._getRangeOfMarker(selection);
const position = markerRange ? markerRange.getStartPosition() : selection.getStartPosition();
const actions = createCancelablePromise(token => getCodeActions(model, selection, trigger, token));
if (this._progressService && trigger.type === 'manual') {
this._progressService.showWhile(actions, 250);
}
this._signalChange(new CodeActionsState.Triggered(
trigger,
selection,
position,
actions
));
return actions;
this._signalChange(undefined);
return undefined;
}
const markerRange = this._getRangeOfMarker(selection);
const position = markerRange ? markerRange.getStartPosition() : selection.getStartPosition();
const e: TriggeredCodeAction = {
trigger,
selection,
position
};
this._signalChange(e);
return e;
}
}
@@ -167,47 +154,39 @@ export namespace CodeActionsState {
export type State = typeof Empty | Triggered;
}
export class CodeActionModel {
export class CodeActionModel extends Disposable {
private _codeActionOracle?: CodeActionOracle;
private readonly _codeActionOracle = this._register(new MutableDisposable<CodeActionOracle>());
private _state: CodeActionsState.State = CodeActionsState.Empty;
private _onDidChangeState = new Emitter<CodeActionsState.State>();
private _disposables: IDisposable[] = [];
private readonly _supportedCodeActions: IContextKey<string>;
private readonly _onDidChangeState = this._register(new Emitter<CodeActionsState.State>());
public readonly onDidChangeState = this._onDidChangeState.event;
constructor(
private readonly _editor: ICodeEditor,
private readonly _markerService: IMarkerService,
contextKeyService: IContextKeyService,
private readonly _progressService: IProgressService
private readonly _progressService?: IEditorProgressService
) {
super();
this._supportedCodeActions = SUPPORTED_CODE_ACTIONS.bindTo(contextKeyService);
this._disposables.push(this._editor.onDidChangeModel(() => this._update()));
this._disposables.push(this._editor.onDidChangeModelLanguage(() => this._update()));
this._disposables.push(CodeActionProviderRegistry.onDidChange(() => this._update()));
this._register(this._editor.onDidChangeModel(() => this._update()));
this._register(this._editor.onDidChangeModelLanguage(() => this._update()));
this._register(CodeActionProviderRegistry.onDidChange(() => this._update()));
this._update();
}
dispose(): void {
this._disposables = dispose(this._disposables);
dispose(this._codeActionOracle);
}
get onDidChangeState(): Event<CodeActionsState.State> {
return this._onDidChangeState.event;
super.dispose();
this.setState(CodeActionsState.Empty, true);
}
private _update(): void {
if (this._codeActionOracle) {
this._codeActionOracle.dispose();
this._codeActionOracle = undefined;
}
this._codeActionOracle.value = undefined;
if (this._state.type === CodeActionsState.Type.Triggered) {
this._state.actions.cancel();
}
this.setState(CodeActionsState.Empty);
const model = this._editor.getModel();
@@ -224,25 +203,46 @@ export class CodeActionModel {
this._supportedCodeActions.set(supportedActions.join(' '));
this._codeActionOracle = new CodeActionOracle(this._editor, this._markerService, newState => this.setState(newState), undefined, this._progressService);
this._codeActionOracle.trigger({ type: 'auto' });
this._codeActionOracle.value = new CodeActionOracle(this._editor, this._markerService, trigger => {
if (!trigger) {
this.setState(CodeActionsState.Empty);
return;
}
const actions = createCancelablePromise(token => getCodeActions(model, trigger.selection, trigger.trigger, token));
if (this._progressService && trigger.trigger.type === 'manual') {
this._progressService.showWhile(actions, 250);
}
this.setState(new CodeActionsState.Triggered(trigger.trigger, trigger.selection, trigger.position, actions));
}, undefined);
this._codeActionOracle.value.trigger({ type: 'auto' });
} else {
this._supportedCodeActions.reset();
}
}
public trigger(trigger: CodeActionTrigger): Promise<CodeActionSet | undefined> {
if (this._codeActionOracle) {
return this._codeActionOracle.trigger(trigger);
public trigger(trigger: CodeActionTrigger) {
if (this._codeActionOracle.value) {
this._codeActionOracle.value.trigger(trigger);
}
return Promise.resolve(undefined);
}
private setState(newState: CodeActionsState.State) {
private setState(newState: CodeActionsState.State, skipNotify?: boolean) {
if (newState === this._state) {
return;
}
// Cancel old request
if (this._state.type === CodeActionsState.Type.Triggered) {
this._state.actions.cancel();
}
this._state = newState;
this._onDidChangeState.fire(newState);
if (!skipNotify) {
this._onDidChangeState.fire(newState);
}
}
}

View File

@@ -5,6 +5,7 @@
import { startsWith } from 'vs/base/common/strings';
import { CodeAction } from 'vs/editor/common/modes';
import { Position } from 'vs/editor/common/core/position';
export class CodeActionKind {
private static readonly sep = '.';
@@ -90,4 +91,8 @@ export interface CodeActionTrigger {
readonly type: 'auto' | 'manual';
readonly filter?: CodeActionFilter;
readonly autoApply?: CodeActionAutoApply;
readonly context?: {
readonly notAvailableMessage: string;
readonly position: Position;
};
}

View File

@@ -0,0 +1,105 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { onUnexpectedError } from 'vs/base/common/errors';
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { CodeAction } from 'vs/editor/common/modes';
import { CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
import { MessageController } from 'vs/editor/contrib/message/messageController';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { CodeActionsState } from './codeActionModel';
import { CodeActionAutoApply } from './codeActionTrigger';
import { CodeActionWidget } from './codeActionWidget';
import { LightBulbWidget } from './lightBulbWidget';
import { IPosition } from 'vs/editor/common/core/position';
import { IAnchor } from 'vs/base/browser/ui/contextview/contextview';
export class CodeActionUi extends Disposable {
private readonly _codeActionWidget: CodeActionWidget;
private readonly _lightBulbWidget: LightBulbWidget;
private readonly _activeCodeActions = this._register(new MutableDisposable<CodeActionSet>());
constructor(
private readonly _editor: ICodeEditor,
quickFixActionId: string,
private readonly delegate: {
applyCodeAction: (action: CodeAction, regtriggerAfterApply: boolean) => void
},
@IContextMenuService contextMenuService: IContextMenuService,
@IKeybindingService keybindingService: IKeybindingService,
) {
super();
this._codeActionWidget = this._register(new CodeActionWidget(this._editor, contextMenuService, {
onSelectCodeAction: async (action) => {
this.delegate.applyCodeAction(action, /* retrigger */ true);
}
}));
this._lightBulbWidget = this._register(new LightBulbWidget(this._editor, quickFixActionId, keybindingService));
this._register(this._lightBulbWidget.onClick(this._handleLightBulbSelect, this));
}
public async update(newState: CodeActionsState.State): Promise<void> {
if (newState.type !== CodeActionsState.Type.Triggered) {
this._lightBulbWidget.hide();
return;
}
let actions: CodeActionSet;
try {
actions = await newState.actions;
} catch (e) {
onUnexpectedError(e);
return;
}
this._lightBulbWidget.update(actions, newState.position);
if (!actions.actions.length && newState.trigger.context) {
MessageController.get(this._editor).showMessage(newState.trigger.context.notAvailableMessage, newState.trigger.context.position);
this._activeCodeActions.value = actions;
return;
}
if (newState.trigger.type === 'manual') {
if (newState.trigger.filter && newState.trigger.filter.kind) {
// Triggered for specific scope
if (actions.actions.length > 0) {
// Apply if we only have one action or requested autoApply
if (newState.trigger.autoApply === CodeActionAutoApply.First || (newState.trigger.autoApply === CodeActionAutoApply.IfSingle && actions.actions.length === 1)) {
try {
await this.delegate.applyCodeAction(actions.actions[0], false);
} finally {
actions.dispose();
}
return;
}
}
}
this._activeCodeActions.value = actions;
this._codeActionWidget.show(actions, newState.position);
} else {
// auto magically triggered
if (this._codeActionWidget.isVisible) {
// TODO: Figure out if we should update the showing menu?
actions.dispose();
} else {
this._activeCodeActions.value = actions;
}
}
}
public async showCodeActionList(actions: CodeActionSet, at?: IAnchor | IPosition): Promise<void> {
this._codeActionWidget.show(actions, at);
}
private _handleLightBulbSelect(e: { x: number, y: number, actions: CodeActionSet }): void {
this._codeActionWidget.show(e.actions, e);
}
}

View File

@@ -6,35 +6,47 @@
import { getDomNodePagePosition } from 'vs/base/browser/dom';
import { Action } from 'vs/base/common/actions';
import { canceled } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
import { Position, IPosition } from 'vs/editor/common/core/position';
import { ScrollType } from 'vs/editor/common/editorCommon';
import { CodeAction } from 'vs/editor/common/modes';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IAnchor } from 'vs/base/browser/ui/contextview/contextview';
export class CodeActionContextMenu {
interface CodeActionWidgetDelegate {
onSelectCodeAction: (action: CodeAction) => Promise<any>;
}
export class CodeActionWidget extends Disposable {
private _visible: boolean;
private readonly _onDidExecuteCodeAction = new Emitter<void>();
public readonly onDidExecuteCodeAction: Event<void> = this._onDidExecuteCodeAction.event;
private readonly _showingActions = this._register(new MutableDisposable<CodeActionSet>());
constructor(
private readonly _editor: ICodeEditor,
private readonly _contextMenuService: IContextMenuService,
private readonly _onApplyCodeAction: (action: CodeAction) => Promise<any>
) { }
private readonly _delegate: CodeActionWidgetDelegate,
) {
super();
}
async show(actionsToShow: Promise<CodeActionSet>, at?: { x: number; y: number } | Position): Promise<void> {
const codeActions = await actionsToShow;
public async show(codeActions: CodeActionSet, at?: IAnchor | IPosition): Promise<void> {
if (!codeActions.actions.length) {
this._visible = false;
return;
}
if (!this._editor.getDomNode()) {
// cancel when editor went off-dom
this._visible = false;
return Promise.reject(canceled());
}
this._visible = true;
const actions = codeActions.actions.map(action => this.codeActionToAction(action));
this._showingActions.value = codeActions;
this._contextMenuService.showContextMenu({
getAnchor: () => {
if (Position.isIPosition(at)) {
@@ -54,16 +66,14 @@ export class CodeActionContextMenu {
private codeActionToAction(action: CodeAction): Action {
const id = action.command ? action.command.id : action.title;
const title = action.title;
return new Action(id, title, undefined, true, () =>
this._onApplyCodeAction(action)
.finally(() => this._onDidExecuteCodeAction.fire(undefined)));
return new Action(id, title, undefined, true, () => this._delegate.onSelectCodeAction(action));
}
get isVisible(): boolean {
return this._visible;
}
private _toCoords(position: Position): { x: number, y: number } {
private _toCoords(position: IPosition): { x: number, y: number } {
if (!this._editor.hasModel()) {
return { x: 0, y: 0 };
}

View File

@@ -18,11 +18,11 @@
}
.monaco-editor.vs .lightbulb-glyph {
background: url('lightbulb.svg') center center no-repeat;
background: url('lightbulb-light.svg') center center no-repeat;
}
.monaco-editor.vs .lightbulb-glyph.autofixable {
background: url('lightbulb-autofix.svg') center center no-repeat;
background: url('lightbulb-autofix-light.svg') center center no-repeat;
}
.monaco-editor.vs-dark .lightbulb-glyph,

View File

@@ -5,67 +5,90 @@
import * as dom from 'vs/base/browser/dom';
import { GlobalMouseMoveMonitor, IStandardMouseMoveEventData, standardMouseMoveMerger } from 'vs/base/browser/globalMouseMoveMonitor';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import 'vs/css!./lightBulbWidget';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { IPosition } from 'vs/editor/common/core/position';
import { TextModel } from 'vs/editor/common/model/textModel';
import { CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
import { CodeActionsState } from './codeActionModel';
import * as nls from 'vs/nls';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
namespace LightBulbState {
export const enum Type {
Hidden,
Showing,
}
export const Hidden = new class { readonly type = Type.Hidden; };
export class Showing {
readonly type = Type.Showing;
constructor(
public readonly actions: CodeActionSet,
public readonly editorPosition: IPosition,
public readonly widgetPosition: IContentWidgetPosition,
) { }
}
export type State = typeof Hidden | Showing;
}
export class LightBulbWidget extends Disposable implements IContentWidget {
private static readonly _posPref = [ContentWidgetPositionPreference.EXACT];
private readonly _domNode: HTMLDivElement;
private readonly _editor: ICodeEditor;
private readonly _onClick = this._register(new Emitter<{ x: number; y: number; state: CodeActionsState.Triggered }>());
private readonly _onClick = this._register(new Emitter<{ x: number; y: number; actions: CodeActionSet }>());
public readonly onClick = this._onClick.event;
private _position: IContentWidgetPosition | null;
private _state: CodeActionsState.State = CodeActionsState.Empty;
private _futureFixes = new CancellationTokenSource();
private _state: LightBulbState.State = LightBulbState.Hidden;
constructor(editor: ICodeEditor) {
constructor(
private readonly _editor: ICodeEditor,
private readonly _quickFixActionId: string,
@IKeybindingService private readonly _keybindingService: IKeybindingService
) {
super();
this._domNode = document.createElement('div');
this._domNode.className = 'lightbulb-glyph';
this._editor = editor;
this._editor.addContentWidget(this);
this._register(this._editor.onDidChangeModel(_ => this._futureFixes.cancel()));
this._register(this._editor.onDidChangeModelLanguage(_ => this._futureFixes.cancel()));
this._register(this._editor.onDidChangeModelContent(_ => {
// cancel when the line in question has been removed
const editorModel = this._editor.getModel();
if (this._state.type !== CodeActionsState.Type.Triggered || !editorModel || this._state.position.lineNumber >= editorModel.getLineCount()) {
this._futureFixes.cancel();
if (this._state.type !== LightBulbState.Type.Showing || !editorModel || this._state.editorPosition.lineNumber >= editorModel.getLineCount()) {
this.hide();
}
}));
this._register(dom.addStandardDisposableListener(this._domNode, 'click', e => {
if (this._state.type !== CodeActionsState.Type.Triggered) {
this._register(dom.addStandardDisposableListener(this._domNode, 'mousedown', e => {
if (this._state.type !== LightBulbState.Type.Showing) {
return;
}
// Make sure that focus / cursor location is not lost when clicking widget icon
this._editor.focus();
dom.EventHelper.stop(e, true);
// a bit of extra work to make sure the menu
// doesn't cover the line-text
const { top, height } = dom.getDomNodePagePosition(this._domNode);
const { lineHeight } = this._editor.getConfiguration();
let pad = Math.floor(lineHeight / 3);
if (this._position && this._position.position !== null && this._position.position.lineNumber < this._state.position.lineNumber) {
if (this._state.widgetPosition.position !== null && this._state.widgetPosition.position.lineNumber < this._state.editorPosition.lineNumber) {
pad += lineHeight;
}
this._onClick.fire({
x: e.posx,
y: top + height + pad,
state: this._state
actions: this._state.actions
});
}));
this._register(dom.addDisposableListener(this._domNode, 'mouseenter', (e: MouseEvent) => {
@@ -87,6 +110,9 @@ export class LightBulbWidget extends Disposable implements IContentWidget {
this.hide();
}
}));
this._updateLightBulbTitle();
this._register(this._keybindingService.onDidUpdateKeybindings(this._updateLightBulbTitle, this));
}
dispose(): void {
@@ -103,60 +129,23 @@ export class LightBulbWidget extends Disposable implements IContentWidget {
}
getPosition(): IContentWidgetPosition | null {
return this._position;
return this._state.type === LightBulbState.Type.Showing ? this._state.widgetPosition : null;
}
tryShow(newState: CodeActionsState.State) {
if (newState.type !== CodeActionsState.Type.Triggered || this._position && (!newState.position || this._position.position && this._position.position.lineNumber !== newState.position.lineNumber)) {
// hide when getting a 'hide'-request or when currently
// showing on another line
this.hide();
} else if (this._futureFixes) {
// cancel pending show request in any case
this._futureFixes.cancel();
public update(actions: CodeActionSet, atPosition: IPosition) {
if (actions.actions.length <= 0) {
return this.hide();
}
this._futureFixes = new CancellationTokenSource();
const { token } = this._futureFixes;
this._state = newState;
if (this._state.type === CodeActionsState.Empty.type) {
return;
}
const selection = this._state.rangeOrSelection;
this._state.actions.then(fixes => {
if (!token.isCancellationRequested && fixes.actions.length > 0 && selection) {
this._show(fixes);
} else {
this.hide();
}
}).catch(() => {
this.hide();
});
}
set title(value: string) {
this._domNode.title = value;
}
get title(): string {
return this._domNode.title;
}
private _show(codeActions: CodeActionSet): void {
const config = this._editor.getConfiguration();
if (!config.contribInfo.lightbulbEnabled) {
return;
return this.hide();
}
if (this._state.type !== CodeActionsState.Type.Triggered) {
return;
}
const { lineNumber, column } = this._state.position;
const { lineNumber, column } = atPosition;
const model = this._editor.getModel();
if (!model) {
return;
return this.hide();
}
const tabSize = model.getOptions().tabSize;
@@ -176,23 +165,35 @@ export class LightBulbWidget extends Disposable implements IContentWidget {
} else if (column * config.fontInfo.spaceWidth < 22) {
// cannot show lightbulb above/below and showing
// it inline would overlay the cursor...
this.hide();
return;
return this.hide();
}
}
this._position = {
this._state = new LightBulbState.Showing(actions, atPosition, {
position: { lineNumber: effectiveLineNumber, column: 1 },
preference: LightBulbWidget._posPref
};
dom.toggleClass(this._domNode, 'autofixable', codeActions.hasAutoFix);
});
dom.toggleClass(this._domNode, 'autofixable', actions.hasAutoFix);
this._editor.layoutContentWidget(this);
}
hide(): void {
this._position = null;
this._state = CodeActionsState.Empty;
this._futureFixes.cancel();
private set title(value: string) {
this._domNode.title = value;
}
public hide(): void {
this._state = LightBulbState.Hidden;
this._editor.layoutContentWidget(this);
}
private _updateLightBulbTitle(): void {
const kb = this._keybindingService.lookupKeybinding(this._quickFixActionId);
let title: string;
if (kb) {
title = nls.localize('quickFixWithKb', "Show Fixes ({0})", kb.getLabel());
} else {
title = nls.localize('quickFix', "Show Fixes");
}
this.title = title;
}
}

View File

@@ -1,10 +1,4 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M13.001 4.2C12.601 2.1 10.301 0 8.801 0H6.201C5.801 0 5.601 0.2 5.601 0.2C3.501 0.8 2.001 2.7 2.001 4.9C2.001 5.4 1.901 7.2 3.701 8.7C4.201 9.2 4.901 10.7 5.001 11.1V14.4L6.601 16H8.601L10.101 14.4V11C10.201 10.6 10.901 9.1 11.401 8.7C12.501 7.8 12.901 6.8 13.001 6V4.2Z" fill="#1E1E1E"/>
<path d="M6.00098 12H9.00098V13H6.00098V12ZM7.00098 15H8.10098L9.00098 14H6.00098L7.00098 15Z" fill="#C5C5C5"/>
<path d="M12.1011 4.9999C12.1011 2.6999 10.3011 0.899902 8.00107 0.899902C7.90107 0.899902 6.60107 0.999902 6.60107 0.999902C4.50107 1.2999 2.90107 2.9999 2.90107 4.9999C2.90107 5.0999 2.70107 6.5999 4.30107 7.9999C5.00107 8.6999 5.80107 10.3999 5.90107 10.8999L6.00107 10.9999H9.00107L9.10107 10.7999C9.20107 10.2999 10.0011 8.5999 10.7011 7.8999C12.3011 6.5999 12.1011 5.0999 12.1011 4.9999V4.9999ZM9.10107 5.9999L8.60107 8.9999H8.00107V5.9999C9.10107 5.9999 8.90107 4.9999 8.90107 4.9999H6.00107V5.0999C6.00107 5.2999 6.10107 5.9999 7.00107 5.9999V8.9999H6.50107L6.30107 8.2999L6.00107 5.9999C5.30107 5.9999 5.10107 5.5999 5.00107 5.2999V4.8999C5.00107 4.0999 5.90107 3.9999 5.90107 3.9999H9.00107C9.00107 3.9999 10.0011 4.0999 10.0011 4.9999C10.0011 4.9999 10.1011 5.9999 9.10107 5.9999Z" fill="#DDB204"/>
<path d="M10.001 5C10.001 4.1 9.00098 4 9.00098 4H5.90098C5.90098 4 5.00098 4.1 5.00098 4.9V5.3C5.00098 5.6 5.30098 6 5.90098 6L6.30098 8.3L6.50098 9H7.00098V6C6.00098 6 6.00098 5.3 6.00098 5.1V5H9.00098C9.00098 5 9.10098 6 8.10098 6V9H8.70098L9.20098 6C10.101 6 10.001 5 10.001 5Z" fill="#252526"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M8 12C8 9.77386 9.77386 8 12 8C14.2261 8 16 9.77386 16 12C16 14.2261 14.2261 16 12 16C9.77386 16 8 14.2261 8 12Z" fill="#1E1E1E"/>
<path d="M12.3192 12.3031L13.3495 13.3334L13.3334 13.3495L12.3031 12.3192L12 12.0162L11.697 12.3192L10.6667 13.3495L10.6506 13.3334L11.6809 12.3031L11.9839 12L11.6809 11.697L10.6506 10.6667L10.6667 10.6506L11.697 11.6809L12 11.9839L12.3031 11.6809L13.3334 10.6506L13.3495 10.6667L12.3192 11.697L12.0162 12L12.3192 12.3031ZM12 8.46034C10.03 8.46034 8.46034 10.03 8.46034 12C8.46034 13.9701 10.03 15.5397 12 15.5397C13.9701 15.5397 15.5397 13.9701 15.5397 12C15.5397 10.03 13.9701 8.46034 12 8.46034Z" fill="#007ACC" stroke="#1E1E1E" stroke-width="0.857143"/>
<path d="M12.6225 12.0002L13.9558 13.3336L13.3336 13.9558L12.0002 12.6225L10.6669 13.9558L10.0447 13.3336L11.378 12.0002L10.0447 10.6669L10.6669 10.0447L12.0002 11.378L13.3336 10.0447L13.9558 10.6669L12.6225 12.0002Z" fill="#007ACC"/>
<path d="M10.704 14L11.2028 12.4712L10 11.6394H11.4732L12 10L12.5361 11.6394H14L12.7972 12.4712L13.3054 14L12 13.024L10.704 14Z" fill="white"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 9C10.3304 9 9 10.3304 9 12C9 13.6696 10.3304 15 12 15C13.6696 15 15 13.6696 15 12C15 10.3304 13.6696 9 12 9ZM11.2028 12.4712L10.704 14L12 13.024L13.3054 14L12.7972 12.4712L14 11.6394H12.5361L12 10L11.4732 11.6394H10L11.2028 12.4712Z" fill="#75BEFF"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.1708 8.08474C9.85081 8.35911 8.77687 9.27684 8.28696 10.5H6.40867V12.7012C6.40867 12.7823 6.4372 12.8512 6.49888 12.9127C6.56058 12.9741 6.63007 13.0028 6.71205 13.0028H8.12487C8.21364 13.3513 8.34773 13.6809 8.52059 13.9851C8.45462 13.9951 8.38715 14 8.31823 14H6.71205C6.53223 14 6.36223 13.9663 6.20306 13.8984C6.04564 13.8311 5.90753 13.7388 5.78961 13.6213C5.67168 13.5038 5.57895 13.3661 5.51141 13.2091C5.44311 13.0503 5.40927 12.8807 5.40927 12.7012V11.1009C5.40927 10.622 5.31772 10.1795 5.13553 9.77209C4.95683 9.36336 4.69832 8.99156 4.35953 8.65806C3.92468 8.22903 3.58896 7.75003 3.35361 7.22134C3.11756 6.69107 3 6.11672 3 5.49953C3 5.08664 3.05342 4.68802 3.16048 4.30397C3.26728 3.92089 3.41907 3.56286 3.61595 3.23018C3.81257 2.89377 4.04777 2.58911 4.32146 2.31641C4.59503 2.04383 4.89858 1.80953 5.23195 1.61364C5.56979 1.41764 5.93146 1.2662 6.31578 1.15983C6.70106 1.0532 7.10094 1 7.51514 1C7.92934 1 8.32923 1.0532 8.71451 1.15983C9.09883 1.2662 9.45803 1.41739 9.79183 1.61351C10.1294 1.80938 10.4351 2.0437 10.7088 2.31641C10.9825 2.5891 11.2177 2.89376 11.4143 3.23016C11.6112 3.56285 11.763 3.92088 11.8698 4.30397C11.9769 4.68802 12.0303 5.08664 12.0303 5.49953C12.0303 6.11672 11.9127 6.69107 11.6767 7.22134C11.5412 7.52562 11.3725 7.81344 11.1708 8.08474Z" fill="#75BEFF"/>
</svg>

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -0,0 +1,4 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 9C10.3304 9 9 10.3304 9 12C9 13.6696 10.3304 15 12 15C13.6696 15 15 13.6696 15 12C15 10.3304 13.6696 9 12 9ZM11.2028 12.4712L10.704 14L12 13.024L13.3054 14L12.7972 12.4712L14 11.6394H12.5361L12 10L11.4732 11.6394H10L11.2028 12.4712Z" fill="#007ACC"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.1708 8.08474C9.85081 8.35911 8.77687 9.27684 8.28696 10.5H6.40867V12.7012C6.40867 12.7823 6.4372 12.8512 6.49888 12.9127C6.56058 12.9741 6.63007 13.0028 6.71205 13.0028H8.12487C8.21364 13.3513 8.34773 13.6809 8.52059 13.9851C8.45462 13.9951 8.38715 14 8.31823 14H6.71205C6.53223 14 6.36223 13.9663 6.20306 13.8984C6.04564 13.8311 5.90753 13.7388 5.78961 13.6213C5.67168 13.5038 5.57895 13.3661 5.51141 13.2091C5.44311 13.0503 5.40927 12.8807 5.40927 12.7012V11.1009C5.40927 10.622 5.31772 10.1795 5.13553 9.77209C4.95683 9.36336 4.69832 8.99156 4.35953 8.65806C3.92468 8.22903 3.58896 7.75003 3.35361 7.22134C3.11756 6.69107 3 6.11672 3 5.49953C3 5.08664 3.05342 4.68802 3.16048 4.30397C3.26728 3.92089 3.41907 3.56286 3.61595 3.23018C3.81257 2.89377 4.04777 2.58911 4.32146 2.31641C4.59503 2.04383 4.89858 1.80953 5.23195 1.61364C5.56979 1.41764 5.93146 1.2662 6.31578 1.15983C6.70106 1.0532 7.10094 1 7.51514 1C7.92934 1 8.32923 1.0532 8.71451 1.15983C9.09883 1.2662 9.45803 1.41739 9.79183 1.61351C10.1294 1.80938 10.4351 2.0437 10.7088 2.31641C10.9825 2.5891 11.2177 2.89376 11.4143 3.23016C11.6112 3.56285 11.763 3.92088 11.8698 4.30397C11.9769 4.68802 12.0303 5.08664 12.0303 5.49953C12.0303 6.11672 11.9127 6.69107 11.6767 7.22134C11.5412 7.52562 11.3725 7.81344 11.1708 8.08474Z" fill="#007ACC"/>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -1,10 +0,0 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M13.001 4.2C12.601 2.1 10.301 0 8.801 0H6.201C5.801 0 5.601 0.2 5.601 0.2C3.501 0.8 2.001 2.7 2.001 4.9C2.001 5.4 1.901 7.2 3.701 8.7C4.201 9.2 4.901 10.7 5.001 11.1V14.4L6.601 16H8.601L10.101 14.4V11C10.201 10.6 10.901 9.1 11.401 8.7C12.501 7.8 12.901 6.8 13.001 6V4.2Z" fill="#F6F6F6"/>
<path d="M6.00098 12H9.00098V13H6.00098V12ZM7.00098 15H8.10098L9.00098 14H6.00098L7.00098 15Z" fill="#848484"/>
<path d="M12.1011 4.9999C12.1011 2.6999 10.3011 0.899902 8.00107 0.899902C7.90107 0.899902 6.60107 0.999902 6.60107 0.999902C4.50107 1.2999 2.90107 2.9999 2.90107 4.9999C2.90107 5.0999 2.70107 6.5999 4.30107 7.9999C5.00107 8.6999 5.80107 10.3999 5.90107 10.8999L6.00107 10.9999H9.00107L9.10107 10.7999C9.20107 10.2999 10.0011 8.5999 10.7011 7.8999C12.3011 6.5999 12.1011 5.0999 12.1011 4.9999V4.9999ZM9.10107 5.9999L8.60107 8.9999H8.00107V5.9999C9.10107 5.9999 8.90107 4.9999 8.90107 4.9999H6.00107V5.0999C6.00107 5.2999 6.10107 5.9999 7.00107 5.9999V8.9999H6.50107L6.30107 8.2999L6.00107 5.9999C5.30107 5.9999 5.10107 5.5999 5.00107 5.2999V4.8999C5.00107 4.0999 5.90107 3.9999 5.90107 3.9999H9.00107C9.00107 3.9999 10.0011 4.0999 10.0011 4.9999C10.0011 4.9999 10.1011 5.9999 9.10107 5.9999Z" fill="#FFCC00"/>
<path d="M10.001 5C10.001 4.1 9.00098 4 9.00098 4H5.90098C5.90098 4 5.00098 4.1 5.00098 4.9V5.3C5.00098 5.6 5.30098 6 5.90098 6L6.30098 8.3L6.50098 9H7.00098V6C6.00098 6 6.00098 5.3 6.00098 5.1V5H9.00098C9.00098 5 9.10098 6 8.10098 6V9H8.70098L9.20098 6C10.101 6 10.001 5 10.001 5Z" fill="#F0EFF1"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M8 12C8 9.77386 9.77386 8 12 8C14.2261 8 16 9.77386 16 12C16 14.2261 14.2261 16 12 16C9.77386 16 8 14.2261 8 12Z" fill="#F6F6F6"/>
<path d="M12.3192 12.3031L13.3495 13.3334L13.3334 13.3495L12.3031 12.3192L12 12.0162L11.697 12.3192L10.6667 13.3495L10.6506 13.3334L11.6809 12.3031L11.9839 12L11.6809 11.697L10.6506 10.6667L10.6667 10.6506L11.697 11.6809L12 11.9839L12.3031 11.6809L13.3334 10.6506L13.3495 10.6667L12.3192 11.697L12.0162 12L12.3192 12.3031ZM12 8.46034C10.03 8.46034 8.46034 10.03 8.46034 12C8.46034 13.9701 10.03 15.5397 12 15.5397C13.9701 15.5397 15.5397 13.9701 15.5397 12C15.5397 10.03 13.9701 8.46034 12 8.46034Z" fill="#007ACC" stroke="#F6F6F6" stroke-width="0.857143"/>
<path d="M12.6225 12.0002L13.9558 13.3336L13.3336 13.9558L12.0002 12.6225L10.6669 13.9558L10.0447 13.3336L11.378 12.0002L10.0447 10.6669L10.6669 10.0447L12.0002 11.378L13.3336 10.0447L13.9558 10.6669L12.6225 12.0002Z" fill="#007ACC"/>
<path d="M10.704 14L11.2028 12.4712L10 11.6394H11.4732L12 10L12.5361 11.6394H14L12.7972 12.4712L13.3054 14L12 13.024L10.704 14Z" fill="white"/>
</svg>

Before

Width:  |  Height:  |  Size: 2.7 KiB

View File

@@ -1 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" enable-background="new 0 0 16 16" height="16" width="16"><path fill="#1E1E1E" d="M13.5 4.2C13.1 2.1 10.8 0 9.3 0H6.7c-.4 0-.6.2-.6.2C4 .8 2.5 2.7 2.5 4.9c0 .5-.1 2.3 1.7 3.8.5.5 1.2 2 1.3 2.4v3.3L7.1 16h2l1.5-1.6V11c.1-.4.8-1.9 1.3-2.3 1.1-.9 1.5-1.9 1.6-2.7V4.2z"/><g><g fill="#C5C5C5"><path d="M6.5 12h3v1h-3zM7.5 15h1.1l.9-1h-3z"/></g><path fill="#DDB204" d="M12.6 5c0-2.3-1.8-4.1-4.1-4.1-.1 0-1.4.1-1.4.1-2.1.3-3.7 2-3.7 4 0 .1-.2 1.6 1.4 3 .7.7 1.5 2.4 1.6 2.9l.1.1h3l.1-.2c.1-.5.9-2.2 1.6-2.9 1.6-1.3 1.4-2.8 1.4-2.9zm-3 1l-.5 3h-.6V6c1.1 0 .9-1 .9-1H6.5v.1c0 .2.1.9 1 .9v3H7l-.2-.7L6.5 6c-.7 0-.9-.4-1-.7v-.4c0-.8.9-.9.9-.9h3.1s1 .1 1 1c0 0 .1 1-.9 1z"/></g><path fill="#252526" d="M10.5 5c0-.9-1-1-1-1H6.4s-.9.1-.9.9v.4c0 .3.3.7.9.7l.4 2.3.2.7h.5V6c-1 0-1-.7-1-.9V5h3s.1 1-.9 1v3h.6l.5-3c.9 0 .8-1 .8-1z"/></svg>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.6708 8.65806C11.3319 8.9916 11.0716 9.36278 10.8886 9.77172C10.7105 10.1792 10.621 10.6219 10.621 11.1009V12.7012C10.621 12.8807 10.5872 13.0503 10.5189 13.2091C10.4513 13.3661 10.3586 13.5038 10.2407 13.6213C10.1228 13.7388 9.98464 13.8311 9.82723 13.8984C9.66806 13.9663 9.49806 14 9.31823 14H7.71205C7.53223 14 7.36223 13.9663 7.20306 13.8984C7.04564 13.8311 6.90753 13.7388 6.78961 13.6213C6.67168 13.5038 6.57895 13.3661 6.51141 13.2091C6.44311 13.0503 6.40927 12.8807 6.40927 12.7012V11.1009C6.40927 10.622 6.31772 10.1795 6.13553 9.77209C5.95683 9.36336 5.69832 8.99156 5.35953 8.65806C4.92468 8.22903 4.58896 7.75003 4.35361 7.22134C4.11756 6.69107 4 6.11672 4 5.49953C4 5.08664 4.05342 4.68802 4.16048 4.30397C4.26728 3.92089 4.41907 3.56286 4.61595 3.23018C4.81257 2.89377 5.04777 2.58911 5.32146 2.31641C5.59503 2.04383 5.89858 1.80953 6.23195 1.61364C6.56979 1.41764 6.93146 1.2662 7.31578 1.15983C7.70106 1.0532 8.10094 1 8.51514 1C8.92934 1 9.32923 1.0532 9.71451 1.15983C10.0988 1.2662 10.458 1.41739 10.7918 1.61351C11.1294 1.80938 11.4351 2.0437 11.7088 2.31641C11.9825 2.5891 12.2177 2.89376 12.4143 3.23016C12.6112 3.56285 12.763 3.92088 12.8698 4.30397C12.9769 4.68802 13.0303 5.08664 13.0303 5.49953C13.0303 6.11672 12.9127 6.69107 12.6767 7.22134C12.4413 7.75003 12.1056 8.22903 11.6708 8.65806ZM9.62162 10.5H7.40867V12.7012C7.40867 12.7823 7.4372 12.8512 7.49888 12.9127C7.56058 12.9741 7.63007 13.0028 7.71205 13.0028H9.31823C9.40022 13.0028 9.46971 12.9741 9.5314 12.9127C9.59309 12.8512 9.62162 12.7823 9.62162 12.7012V10.5Z" fill="#FFCC00"/>
</svg>

Before

Width:  |  Height:  |  Size: 880 B

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -0,0 +1,4 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.6708 8.65806C11.3319 8.9916 11.0716 9.36278 10.8886 9.77172C10.7105 10.1792 10.621 10.6219 10.621 11.1009V12.7012C10.621 12.8807 10.5872 13.0503 10.5189 13.2091C10.4513 13.3661 10.3586 13.5038 10.2407 13.6213C10.1228 13.7388 9.98464 13.8311 9.82723 13.8984C9.66806 13.9663 9.49806 14 9.31823 14H7.71205C7.53223 14 7.36223 13.9663 7.20306 13.8984C7.04564 13.8311 6.90753 13.7388 6.78961 13.6213C6.67168 13.5038 6.57895 13.3661 6.51141 13.2091C6.44311 13.0503 6.40927 12.8807 6.40927 12.7012V11.1009C6.40927 10.622 6.31772 10.1795 6.13553 9.77209C5.95683 9.36336 5.69832 8.99156 5.35953 8.65806C4.92468 8.22903 4.58896 7.75003 4.35361 7.22134C4.11756 6.69107 4 6.11672 4 5.49953C4 5.08664 4.05342 4.68802 4.16048 4.30397C4.26728 3.92089 4.41907 3.56286 4.61595 3.23018C4.81257 2.89377 5.04777 2.58911 5.32146 2.31641C5.59503 2.04383 5.89858 1.80953 6.23195 1.61364C6.56979 1.41764 6.93146 1.2662 7.31578 1.15983C7.70106 1.0532 8.10094 1 8.51514 1C8.92934 1 9.32923 1.0532 9.71451 1.15983C10.0988 1.2662 10.458 1.41739 10.7918 1.61351C11.1294 1.80938 11.4351 2.0437 11.7088 2.31641C11.9825 2.5891 12.2177 2.89376 12.4143 3.23016C12.6112 3.56285 12.763 3.92088 12.8698 4.30397C12.9769 4.68802 13.0303 5.08664 13.0303 5.49953C13.0303 6.11672 12.9127 6.69107 12.6767 7.22134C12.4413 7.75003 12.1056 8.22903 11.6708 8.65806ZM9.62162 10.5H7.40867V12.7012C7.40867 12.7823 7.4372 12.8512 7.49888 12.9127C7.56058 12.9741 7.63007 13.0028 7.71205 13.0028H9.31823C9.40022 13.0028 9.46971 12.9741 9.5314 12.9127C9.59309 12.8512 9.62162 12.7823 9.62162 12.7012V10.5Z" fill="#FFCC00"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.6708 8.65806C11.3319 8.9916 11.0716 9.36278 10.8886 9.77172C10.7105 10.1792 10.621 10.6219 10.621 11.1009V12.7012C10.621 12.8807 10.5872 13.0503 10.5189 13.2091C10.4513 13.3661 10.3586 13.5038 10.2407 13.6213C10.1228 13.7388 9.98464 13.8311 9.82723 13.8984C9.66806 13.9663 9.49806 14 9.31823 14H7.71205C7.53223 14 7.36223 13.9663 7.20306 13.8984C7.04564 13.8311 6.90753 13.7388 6.78961 13.6213C6.67168 13.5038 6.57895 13.3661 6.51141 13.2091C6.44311 13.0503 6.40927 12.8807 6.40927 12.7012V11.1009C6.40927 10.622 6.31772 10.1795 6.13553 9.77209C5.95683 9.36336 5.69832 8.99156 5.35953 8.65806C4.92468 8.22903 4.58896 7.75003 4.35361 7.22134C4.11756 6.69107 4 6.11672 4 5.49953C4 5.08664 4.05342 4.68802 4.16048 4.30397C4.26728 3.92089 4.41907 3.56286 4.61595 3.23018C4.81257 2.89377 5.04777 2.58911 5.32146 2.31641C5.59503 2.04383 5.89858 1.80953 6.23195 1.61364C6.56979 1.41764 6.93146 1.2662 7.31578 1.15983C7.70106 1.0532 8.10094 1 8.51514 1C8.92934 1 9.32923 1.0532 9.71451 1.15983C10.0988 1.2662 10.458 1.41739 10.7918 1.61351C11.1294 1.80938 11.4351 2.0437 11.7088 2.31641C11.9825 2.5891 12.2177 2.89376 12.4143 3.23016C12.6112 3.56285 12.763 3.92088 12.8698 4.30397C12.9769 4.68802 13.0303 5.08664 13.0303 5.49953C13.0303 6.11672 12.9127 6.69107 12.6767 7.22134C12.4413 7.75003 12.1056 8.22903 11.6708 8.65806ZM9.62162 10.5H7.40867V12.7012C7.40867 12.7823 7.4372 12.8512 7.49888 12.9127C7.56058 12.9741 7.63007 13.0028 7.71205 13.0028H9.31823C9.40022 13.0028 9.46971 12.9741 9.5314 12.9127C9.59309 12.8512 9.62162 12.7823 9.62162 12.7012V10.5Z" fill="#DDB100"/>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" enable-background="new 0 0 16 16" height="16" width="16"><path fill="#F6F6F6" d="M13.5 4.2C13.1 2.1 10.8 0 9.3 0H6.7c-.4 0-.6.2-.6.2C4 .8 2.5 2.7 2.5 4.9c0 .5-.1 2.3 1.7 3.8.5.5 1.2 2 1.3 2.4v3.3L7.1 16h2l1.5-1.6V11c.1-.4.8-1.9 1.3-2.3 1.1-.9 1.5-1.9 1.6-2.7V4.2z"/><g><g fill="#848484"><path d="M6.5 12h3v1h-3zM7.5 15h1.1l.9-1h-3z"/></g><path fill="#fc0" d="M12.6 5c0-2.3-1.8-4.1-4.1-4.1-.1 0-1.4.1-1.4.1-2.1.3-3.7 2-3.7 4 0 .1-.2 1.6 1.4 3 .7.7 1.5 2.4 1.6 2.9l.1.1h3l.1-.2c.1-.5.9-2.2 1.6-2.9 1.6-1.3 1.4-2.8 1.4-2.9zm-3 1l-.5 3h-.6V6c1.1 0 .9-1 .9-1H6.5v.1c0 .2.1.9 1 .9v3H7l-.2-.7L6.5 6c-.7 0-.9-.4-1-.7v-.4c0-.8.9-.9.9-.9h3.1s1 .1 1 1c0 0 .1 1-.9 1z"/></g><path fill="#F0EFF1" d="M10.5 5c0-.9-1-1-1-1H6.4s-.9.1-.9.9v.4c0 .3.3.7.9.7l.4 2.3.2.7h.5V6c-1 0-1-.7-1-.9V5h3s.1 1-.9 1v3h.6l.5-3c.9 0 .8-1 .8-1z"/></svg>

Before

Width:  |  Height:  |  Size: 877 B

View File

@@ -3,22 +3,34 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { Range } from 'vs/editor/common/core/range';
import { TextModel } from 'vs/editor/common/model/textModel';
import { CodeAction, CodeActionContext, CodeActionProvider, CodeActionProviderRegistry, Command, LanguageIdentifier, ResourceTextEdit, WorkspaceEdit } from 'vs/editor/common/modes';
import * as modes from 'vs/editor/common/modes';
import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction';
import { CodeActionKind } from 'vs/editor/contrib/codeAction/codeActionTrigger';
import { IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
import { CancellationToken } from 'vs/base/common/cancellation';
function staticCodeActionProvider(...actions: modes.CodeAction[]): modes.CodeActionProvider {
return new class implements modes.CodeActionProvider {
provideCodeActions(): modes.CodeActionList {
return {
actions: actions,
dispose: () => { }
};
}
};
}
suite('CodeAction', () => {
let langId = new LanguageIdentifier('fooLang', 17);
let langId = new modes.LanguageIdentifier('fooLang', 17);
let uri = URI.parse('untitled:path');
let model: TextModel;
let disposables: IDisposable[] = [];
const disposables = new DisposableStore();
let testData = {
diagnostics: {
abc: {
@@ -46,7 +58,7 @@ suite('CodeAction', () => {
},
command: {
abc: {
command: new class implements Command {
command: new class implements modes.Command {
id: '1';
title: 'abc';
},
@@ -56,8 +68,8 @@ suite('CodeAction', () => {
spelling: {
bcd: {
diagnostics: <IMarkerData[]>[],
edit: new class implements WorkspaceEdit {
edits: ResourceTextEdit[];
edit: new class implements modes.WorkspaceEdit {
edits: modes.ResourceTextEdit[];
},
title: 'abc'
}
@@ -79,30 +91,27 @@ suite('CodeAction', () => {
};
setup(function () {
disposables.clear();
model = TextModel.createFromString('test1\ntest2\ntest3', undefined, langId, uri);
disposables = [model];
disposables.add(model);
});
teardown(function () {
dispose(disposables);
disposables.clear();
});
test('CodeActions are sorted by type, #38623', async function () {
const provider = new class implements CodeActionProvider {
provideCodeActions() {
return [
testData.command.abc,
testData.diagnostics.bcd,
testData.spelling.bcd,
testData.tsLint.bcd,
testData.tsLint.abc,
testData.diagnostics.abc
];
}
};
const provider = staticCodeActionProvider(
testData.command.abc,
testData.diagnostics.bcd,
testData.spelling.bcd,
testData.tsLint.bcd,
testData.tsLint.abc,
testData.diagnostics.abc
);
disposables.push(CodeActionProviderRegistry.register('fooLang', provider));
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
const expected = [
// CodeActions with a diagnostics array are shown first ordered by diagnostics.message
@@ -122,17 +131,13 @@ suite('CodeAction', () => {
});
test('getCodeActions should filter by scope', async function () {
const provider = new class implements CodeActionProvider {
provideCodeActions(): CodeAction[] {
return [
{ title: 'a', kind: 'a' },
{ title: 'b', kind: 'b' },
{ title: 'a.b', kind: 'a.b' }
];
}
};
const provider = staticCodeActionProvider(
{ title: 'a', kind: 'a' },
{ title: 'b', kind: 'b' },
{ title: 'a.b', kind: 'a.b' }
);
disposables.push(CodeActionProviderRegistry.register('fooLang', provider));
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
{
const { actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { kind: new CodeActionKind('a') } }, CancellationToken.None);
@@ -154,15 +159,18 @@ suite('CodeAction', () => {
});
test('getCodeActions should forward requested scope to providers', async function () {
const provider = new class implements CodeActionProvider {
provideCodeActions(_model: any, _range: Range, context: CodeActionContext, _token: any): CodeAction[] {
return [
{ title: context.only || '', kind: context.only }
];
const provider = new class implements modes.CodeActionProvider {
provideCodeActions(_model: any, _range: Range, context: modes.CodeActionContext, _token: any): modes.CodeActionList {
return {
actions: [
{ title: context.only || '', kind: context.only }
],
dispose: () => { }
};
}
};
disposables.push(CodeActionProviderRegistry.register('fooLang', provider));
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
const { actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { kind: new CodeActionKind('a') } }, CancellationToken.None);
assert.equal(actions.length, 1);
@@ -170,16 +178,12 @@ suite('CodeAction', () => {
});
test('getCodeActions should not return source code action by default', async function () {
const provider = new class implements CodeActionProvider {
provideCodeActions(): CodeAction[] {
return [
{ title: 'a', kind: CodeActionKind.Source.value },
{ title: 'b', kind: 'b' }
];
}
};
const provider = staticCodeActionProvider(
{ title: 'a', kind: CodeActionKind.Source.value },
{ title: 'b', kind: 'b' }
);
disposables.push(CodeActionProviderRegistry.register('fooLang', provider));
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
{
const { actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto' }, CancellationToken.None);
@@ -196,16 +200,16 @@ suite('CodeAction', () => {
test('getCodeActions should not invoke code action providers filtered out by providedCodeActionKinds', async function () {
let wasInvoked = false;
const provider = new class implements CodeActionProvider {
provideCodeActions() {
const provider = new class implements modes.CodeActionProvider {
provideCodeActions(): modes.CodeActionList {
wasInvoked = true;
return [];
return { actions: [], dispose: () => { } };
}
providedCodeActionKinds = [CodeActionKind.Refactor.value];
};
disposables.push(CodeActionProviderRegistry.register('fooLang', provider));
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
const { actions } = await getCodeActions(model, new Range(1, 1, 2, 1), {
type: 'auto',

View File

@@ -4,32 +4,38 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Selection } from 'vs/editor/common/core/selection';
import { TextModel } from 'vs/editor/common/model/textModel';
import { CodeActionProviderRegistry, LanguageIdentifier } from 'vs/editor/common/modes';
import { CodeActionOracle, CodeActionsState } from 'vs/editor/contrib/codeAction/codeActionModel';
import * as modes from 'vs/editor/common/modes';
import { CodeActionModel, CodeActionsState } from 'vs/editor/contrib/codeAction/codeActionModel';
import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
import { MarkerService } from 'vs/platform/markers/common/markerService';
const testProvider = {
provideCodeActions() {
return [{ id: 'test-command', title: 'test', arguments: [] }];
provideCodeActions(): modes.CodeActionList {
return {
actions: [
{ title: 'test', command: { id: 'test-command', title: 'test', arguments: [] } }
],
dispose() { /* noop*/ }
};
}
};
suite('CodeAction', () => {
suite('CodeActionModel', () => {
const languageIdentifier = new LanguageIdentifier('foo-lang', 3);
const languageIdentifier = new modes.LanguageIdentifier('foo-lang', 3);
let uri = URI.parse('untitled:path');
let model: TextModel;
let markerService: MarkerService;
let editor: ICodeEditor;
let disposables: IDisposable[];
const disposables = new DisposableStore();
setup(() => {
disposables = [];
disposables.clear();
markerService = new MarkerService();
model = TextModel.createFromString('foobar foo bar\nfarboo far boo', undefined, languageIdentifier, uri);
editor = createTestCodeEditor({ model: model });
@@ -37,26 +43,28 @@ suite('CodeAction', () => {
});
teardown(() => {
dispose(disposables);
disposables.clear();
editor.dispose();
model.dispose();
markerService.dispose();
});
test('Orcale -> marker added', done => {
const reg = CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.push(reg);
const reg = modes.CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.add(reg);
const oracle = new CodeActionOracle(editor, markerService, (e: CodeActionsState.Triggered) => {
const contextKeys = new MockContextKeyService();
const model = disposables.add(new CodeActionModel(editor, markerService, contextKeys, undefined));
disposables.add(model.onDidChangeState((e: CodeActionsState.Triggered) => {
assert.equal(e.trigger.type, 'auto');
assert.ok(e.actions);
e.actions.then(fixes => {
oracle.dispose();
model.dispose();
assert.equal(fixes.actions.length, 1);
done();
}, done);
});
}));
// start here
markerService.changeOne('fake', uri, [{
@@ -70,8 +78,8 @@ suite('CodeAction', () => {
});
test('Orcale -> position changed', () => {
const reg = CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.push(reg);
const reg = modes.CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.add(reg);
markerService.changeOne('fake', uri, [{
startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 6,
@@ -84,28 +92,29 @@ suite('CodeAction', () => {
editor.setPosition({ lineNumber: 2, column: 1 });
return new Promise((resolve, reject) => {
const oracle = new CodeActionOracle(editor, markerService, (e: CodeActionsState.Triggered) => {
const contextKeys = new MockContextKeyService();
const model = disposables.add(new CodeActionModel(editor, markerService, contextKeys, undefined));
disposables.add(model.onDidChangeState((e: CodeActionsState.Triggered) => {
assert.equal(e.trigger.type, 'auto');
assert.ok(e.actions);
e.actions.then(fixes => {
oracle.dispose();
model.dispose();
assert.equal(fixes.actions.length, 1);
resolve(undefined);
}, reject);
});
}));
// start here
editor.setPosition({ lineNumber: 1, column: 1 });
});
});
test('Lightbulb is in the wrong place, #29933', async function () {
const reg = CodeActionProviderRegistry.register(languageIdentifier.language, {
provideCodeActions(_doc, _range) {
return [];
const reg = modes.CodeActionProviderRegistry.register(languageIdentifier.language, {
provideCodeActions(_doc, _range): modes.CodeActionList {
return { actions: [], dispose() { /* noop*/ } };
}
});
disposables.push(reg);
disposables.add(reg);
editor.getModel()!.setValue('// @ts-check\n2\ncon\n');
@@ -119,8 +128,9 @@ suite('CodeAction', () => {
// case 1 - drag selection over multiple lines -> range of enclosed marker, position or marker
await new Promise(resolve => {
let oracle = new CodeActionOracle(editor, markerService, (e: CodeActionsState.Triggered) => {
const contextKeys = new MockContextKeyService();
const model = disposables.add(new CodeActionModel(editor, markerService, contextKeys, undefined));
disposables.add(model.onDidChangeState((e: CodeActionsState.Triggered) => {
assert.equal(e.trigger.type, 'auto');
const selection = <Selection>e.rangeOrSelection;
assert.deepEqual(selection.selectionStartLineNumber, 1);
@@ -128,31 +138,32 @@ suite('CodeAction', () => {
assert.deepEqual(selection.endLineNumber, 4);
assert.deepEqual(selection.endColumn, 1);
assert.deepEqual(e.position, { lineNumber: 3, column: 1 });
oracle.dispose();
model.dispose();
resolve(undefined);
}, 5);
}, 5));
editor.setSelection({ startLineNumber: 1, startColumn: 1, endLineNumber: 4, endColumn: 1 });
});
});
test('Orcale -> should only auto trigger once for cursor and marker update right after each other', done => {
const reg = CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.push(reg);
const reg = modes.CodeActionProviderRegistry.register(languageIdentifier.language, testProvider);
disposables.add(reg);
let triggerCount = 0;
const oracle = new CodeActionOracle(editor, markerService, (e: CodeActionsState.Triggered) => {
const contextKeys = new MockContextKeyService();
const model = disposables.add(new CodeActionModel(editor, markerService, contextKeys, undefined));
disposables.add(model.onDidChangeState((e: CodeActionsState.Triggered) => {
assert.equal(e.trigger.type, 'auto');
++triggerCount;
// give time for second trigger before completing test
setTimeout(() => {
oracle.dispose();
model.dispose();
assert.strictEqual(triggerCount, 1);
done();
}, 50);
}, 5 /*delay*/);
}, 5 /*delay*/));
markerService.changeOne('fake', uri, [{
startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 6,