Skip to content

Commit c3c5474

Browse files
committed
Convert CodeActionTrigger type enum type
1 parent 560f922 commit c3c5474

11 files changed

Lines changed: 60 additions & 54 deletions

File tree

src/vs/editor/contrib/codeAction/codeAction.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
import { equals, flatten, isNonEmptyArray, mergeSort } from 'vs/base/common/arrays';
77
import { CancellationToken } from 'vs/base/common/cancellation';
88
import { illegalArgument, isPromiseCanceledError, onUnexpectedExternalError } from 'vs/base/common/errors';
9+
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
910
import { URI } from 'vs/base/common/uri';
11+
import { TextModelCancellationTokenSource } from 'vs/editor/browser/core/editorState';
1012
import { registerLanguageCommand } from 'vs/editor/browser/editorExtensions';
1113
import { Range } from 'vs/editor/common/core/range';
1214
import { Selection } from 'vs/editor/common/core/selection';
1315
import { ITextModel } from 'vs/editor/common/model';
14-
import { CodeAction, CodeActionContext, CodeActionProviderRegistry, CodeActionTrigger as CodeActionTriggerKind } from 'vs/editor/common/modes';
16+
import * as modes from 'vs/editor/common/modes';
1517
import { IModelService } from 'vs/editor/common/services/modelService';
16-
import { CodeActionFilter, CodeActionKind, CodeActionTrigger, filtersAction, mayIncludeActionsOfKind } from './types';
17-
import { TextModelCancellationTokenSource } from 'vs/editor/browser/core/editorState';
18-
import { DisposableStore, IDisposable, Disposable } from 'vs/base/common/lifecycle';
18+
import { CodeActionFilter, CodeActionKind, CodeActionTrigger, CodeActionTriggerType, filtersAction, mayIncludeActionsOfKind } from './types';
1919

2020
export const codeActionCommandId = 'editor.action.codeAction';
2121
export const refactorCommandId = 'editor.action.refactor';
@@ -24,14 +24,14 @@ export const organizeImportsCommandId = 'editor.action.organizeImports';
2424
export const fixAllCommandId = 'editor.action.fixAll';
2525

2626
export interface CodeActionSet extends IDisposable {
27-
readonly validActions: readonly CodeAction[];
28-
readonly allActions: readonly CodeAction[];
27+
readonly validActions: readonly modes.CodeAction[];
28+
readonly allActions: readonly modes.CodeAction[];
2929
readonly hasAutoFix: boolean;
3030
}
3131

3232
class ManagedCodeActionSet extends Disposable implements CodeActionSet {
3333

34-
private static codeActionsComparator(a: CodeAction, b: CodeAction): number {
34+
private static codeActionsComparator(a: modes.CodeAction, b: modes.CodeAction): number {
3535
if (isNonEmptyArray(a.diagnostics)) {
3636
if (isNonEmptyArray(b.diagnostics)) {
3737
return a.diagnostics[0].message.localeCompare(b.diagnostics[0].message);
@@ -45,10 +45,10 @@ class ManagedCodeActionSet extends Disposable implements CodeActionSet {
4545
}
4646
}
4747

48-
public readonly validActions: readonly CodeAction[];
49-
public readonly allActions: readonly CodeAction[];
48+
public readonly validActions: readonly modes.CodeAction[];
49+
public readonly allActions: readonly modes.CodeAction[];
5050

51-
public constructor(actions: readonly CodeAction[], disposables: DisposableStore) {
51+
public constructor(actions: readonly modes.CodeAction[], disposables: DisposableStore) {
5252
super();
5353
this._register(disposables);
5454
this.allActions = mergeSort([...actions], ManagedCodeActionSet.codeActionsComparator);
@@ -68,9 +68,9 @@ export function getCodeActions(
6868
): Promise<CodeActionSet> {
6969
const filter = trigger.filter || {};
7070

71-
const codeActionContext: CodeActionContext = {
71+
const codeActionContext: modes.CodeActionContext = {
7272
only: filter.include?.value,
73-
trigger: trigger.type === 'manual' ? CodeActionTriggerKind.Manual : CodeActionTriggerKind.Automatic
73+
trigger: trigger.type === CodeActionTriggerType.Manual ? modes.CodeActionTrigger.Manual : modes.CodeActionTrigger.Automatic
7474
};
7575

7676
const cts = new TextModelCancellationTokenSource(model, token);
@@ -94,8 +94,8 @@ export function getCodeActions(
9494
}
9595
});
9696

97-
const listener = CodeActionProviderRegistry.onDidChange(() => {
98-
const newProviders = CodeActionProviderRegistry.all(model);
97+
const listener = modes.CodeActionProviderRegistry.onDidChange(() => {
98+
const newProviders = modes.CodeActionProviderRegistry.all(model);
9999
if (!equals(newProviders, providers)) {
100100
cts.cancel();
101101
}
@@ -114,7 +114,7 @@ function getCodeActionProviders(
114114
model: ITextModel,
115115
filter: CodeActionFilter
116116
) {
117-
return CodeActionProviderRegistry.all(model)
117+
return modes.CodeActionProviderRegistry.all(model)
118118
// Don't include providers that we know will not return code actions of interest
119119
.filter(provider => {
120120
if (!provider.providedCodeActionKinds) {
@@ -125,7 +125,7 @@ function getCodeActionProviders(
125125
});
126126
}
127127

128-
registerLanguageCommand('_executeCodeActionProvider', async function (accessor, args): Promise<ReadonlyArray<CodeAction>> {
128+
registerLanguageCommand('_executeCodeActionProvider', async function (accessor, args): Promise<ReadonlyArray<modes.CodeAction>> {
129129
const { resource, rangeOrSelection, kind } = args;
130130
if (!(resource instanceof URI)) {
131131
throw illegalArgument();
@@ -149,7 +149,7 @@ registerLanguageCommand('_executeCodeActionProvider', async function (accessor,
149149
const codeActionSet = await getCodeActions(
150150
model,
151151
validatedRangeOrSelection,
152-
{ type: 'manual', filter: { includeSourceActions: true, include: kind && kind.value ? new CodeActionKind(kind.value) : undefined } },
152+
{ type: CodeActionTriggerType.Manual, filter: { includeSourceActions: true, include: kind && kind.value ? new CodeActionKind(kind.value) : undefined } },
153153
CancellationToken.None);
154154

155155
setTimeout(() => codeActionSet.dispose(), 100);

src/vs/editor/contrib/codeAction/codeActionCommands.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { INotificationService } from 'vs/platform/notification/common/notificati
2929
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
3030
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
3131
import { CodeActionModel, CodeActionsState, SUPPORTED_CODE_ACTIONS } from './codeActionModel';
32-
import { CodeActionAutoApply, CodeActionCommandArgs, CodeActionFilter, CodeActionKind, CodeActionTrigger } from './types';
32+
import { CodeActionAutoApply, CodeActionCommandArgs, CodeActionFilter, CodeActionKind, CodeActionTrigger, CodeActionTriggerType } from './types';
3333

3434
function contextKeyForSupportedActions(kind: CodeActionKind) {
3535
return ContextKeyExpr.regex(
@@ -97,7 +97,7 @@ export class QuickFixController extends Disposable implements IEditorContributio
9797
await this._applyCodeAction(action);
9898
} finally {
9999
if (retrigger) {
100-
this._trigger({ type: 'auto', filter: {} });
100+
this._trigger({ type: CodeActionTriggerType.Auto, filter: {} });
101101
}
102102
}
103103
}
@@ -124,7 +124,7 @@ export class QuickFixController extends Disposable implements IEditorContributio
124124

125125
MessageController.get(this._editor).closeMessage();
126126
const triggerPosition = this._editor.getPosition();
127-
this._trigger({ type: 'manual', filter, autoApply, context: { notAvailableMessage, position: triggerPosition } });
127+
this._trigger({ type: CodeActionTriggerType.Manual, filter, autoApply, context: { notAvailableMessage, position: triggerPosition } });
128128
}
129129

130130
private _trigger(trigger: CodeActionTrigger) {
@@ -176,7 +176,6 @@ export async function applyCodeAction(
176176
typeof message === 'string'
177177
? message
178178
: nls.localize('applyCodeActionFailed', "An unknown error occurred while applying the code action"));
179-
180179
}
181180
}
182181
}

src/vs/editor/contrib/codeAction/codeActionModel.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/cont
1616
import { IMarkerService } from 'vs/platform/markers/common/markers';
1717
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
1818
import { getCodeActions, CodeActionSet } from './codeAction';
19-
import { CodeActionTrigger } from './types';
19+
import { CodeActionTrigger, CodeActionTriggerType } from './types';
2020
import { EditorOption } from 'vs/editor/common/config/editorOptions';
2121
import { isEqual } from 'vs/base/common/resources';
2222

@@ -56,14 +56,14 @@ class CodeActionOracle extends Disposable {
5656

5757
if (resources.some(resource => isEqual(resource, model.uri))) {
5858
this._autoTriggerTimer.cancelAndSet(() => {
59-
this.trigger({ type: 'auto' });
59+
this.trigger({ type: CodeActionTriggerType.Auto });
6060
}, this._delay);
6161
}
6262
}
6363

6464
private _onCursorChange(): void {
6565
this._autoTriggerTimer.cancelAndSet(() => {
66-
this.trigger({ type: 'auto' });
66+
this.trigger({ type: CodeActionTriggerType.Auto });
6767
}, this._delay);
6868
}
6969

@@ -88,7 +88,7 @@ class CodeActionOracle extends Disposable {
8888
}
8989
const model = this._editor.getModel();
9090
const selection = this._editor.getSelection();
91-
if (selection.isEmpty() && trigger.type === 'auto') {
91+
if (selection.isEmpty() && trigger.type === CodeActionTriggerType.Auto) {
9292
const { lineNumber, column } = selection.getPosition();
9393
const line = model.getLineContent(lineNumber);
9494
if (line.length === 0) {
@@ -214,14 +214,14 @@ export class CodeActionModel extends Disposable {
214214
}
215215

216216
const actions = createCancelablePromise(token => getCodeActions(model, trigger.selection, trigger.trigger, token));
217-
if (this._progressService && trigger.trigger.type === 'manual') {
217+
if (this._progressService && trigger.trigger.type === CodeActionTriggerType.Manual) {
218218
this._progressService.showWhile(actions, 250);
219219
}
220220

221221
this.setState(new CodeActionsState.Triggered(trigger.trigger, trigger.selection, trigger.position, actions));
222222

223223
}, undefined);
224-
this._codeActionOracle.value.trigger({ type: 'auto' });
224+
this._codeActionOracle.value.trigger({ type: CodeActionTriggerType.Auto });
225225
} else {
226226
this._supportedCodeActions.reset();
227227
}

src/vs/editor/contrib/codeAction/codeActionUi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { MessageController } from 'vs/editor/contrib/message/messageController';
1616
import { CodeActionsState } from './codeActionModel';
1717
import { CodeActionMenu, CodeActionShowOptions } from './codeActionMenu';
1818
import { LightBulbWidget } from './lightBulbWidget';
19-
import { CodeActionAutoApply, CodeActionTrigger } from './types';
19+
import { CodeActionAutoApply, CodeActionTrigger, CodeActionTriggerType } from './types';
2020
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2121

2222
export class CodeActionUi extends Disposable {
@@ -67,7 +67,7 @@ export class CodeActionUi extends Disposable {
6767

6868
this._lightBulbWidget.getValue().update(actions, newState.position);
6969

70-
if (newState.trigger.type === 'manual') {
70+
if (newState.trigger.type === CodeActionTriggerType.Manual) {
7171
if (newState.trigger.filter?.include) { // Triggered for specific scope
7272
// Check to see if we want to auto apply.
7373

src/vs/editor/contrib/codeAction/test/codeAction.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Range } from 'vs/editor/common/core/range';
99
import { TextModel } from 'vs/editor/common/model/textModel';
1010
import * as modes from 'vs/editor/common/modes';
1111
import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction';
12-
import { CodeActionKind } from 'vs/editor/contrib/codeAction/types';
12+
import { CodeActionKind, CodeActionTriggerType } from 'vs/editor/contrib/codeAction/types';
1313
import { IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
1414
import { CancellationToken } from 'vs/base/common/cancellation';
1515

@@ -125,7 +125,7 @@ suite('CodeAction', () => {
125125
testData.tsLint.abc
126126
];
127127

128-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'manual' }, CancellationToken.None);
128+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Manual }, CancellationToken.None);
129129
assert.equal(actions.length, 6);
130130
assert.deepEqual(actions, expected);
131131
});
@@ -140,20 +140,20 @@ suite('CodeAction', () => {
140140
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
141141

142142
{
143-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { include: new CodeActionKind('a') } }, CancellationToken.None);
143+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto, filter: { include: new CodeActionKind('a') } }, CancellationToken.None);
144144
assert.equal(actions.length, 2);
145145
assert.strictEqual(actions[0].title, 'a');
146146
assert.strictEqual(actions[1].title, 'a.b');
147147
}
148148

149149
{
150-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { include: new CodeActionKind('a.b') } }, CancellationToken.None);
150+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto, filter: { include: new CodeActionKind('a.b') } }, CancellationToken.None);
151151
assert.equal(actions.length, 1);
152152
assert.strictEqual(actions[0].title, 'a.b');
153153
}
154154

155155
{
156-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { include: new CodeActionKind('a.b.c') } }, CancellationToken.None);
156+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto, filter: { include: new CodeActionKind('a.b.c') } }, CancellationToken.None);
157157
assert.equal(actions.length, 0);
158158
}
159159
});
@@ -172,7 +172,7 @@ suite('CodeAction', () => {
172172

173173
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
174174

175-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { include: new CodeActionKind('a') } }, CancellationToken.None);
175+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto, filter: { include: new CodeActionKind('a') } }, CancellationToken.None);
176176
assert.equal(actions.length, 1);
177177
assert.strictEqual(actions[0].title, 'a');
178178
});
@@ -186,13 +186,13 @@ suite('CodeAction', () => {
186186
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
187187

188188
{
189-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto' }, CancellationToken.None);
189+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto }, CancellationToken.None);
190190
assert.equal(actions.length, 1);
191191
assert.strictEqual(actions[0].title, 'b');
192192
}
193193

194194
{
195-
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: 'auto', filter: { include: CodeActionKind.Source, includeSourceActions: true } }, CancellationToken.None);
195+
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), { type: CodeActionTriggerType.Auto, filter: { include: CodeActionKind.Source, includeSourceActions: true } }, CancellationToken.None);
196196
assert.equal(actions.length, 1);
197197
assert.strictEqual(actions[0].title, 'a');
198198
}
@@ -209,7 +209,7 @@ suite('CodeAction', () => {
209209

210210
{
211211
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), {
212-
type: 'auto', filter: {
212+
type: CodeActionTriggerType.Auto, filter: {
213213
include: CodeActionKind.Source.append('test'),
214214
excludes: [CodeActionKind.Source],
215215
includeSourceActions: true,
@@ -234,7 +234,7 @@ suite('CodeAction', () => {
234234
disposables.add(modes.CodeActionProviderRegistry.register('fooLang', provider));
235235

236236
const { validActions: actions } = await getCodeActions(model, new Range(1, 1, 2, 1), {
237-
type: 'auto',
237+
type: CodeActionTriggerType.Auto,
238238
filter: {
239239
include: CodeActionKind.QuickFix
240240
}

src/vs/editor/contrib/codeAction/test/codeActionModel.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CodeActionModel, CodeActionsState } from 'vs/editor/contrib/codeAction/
1515
import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
1616
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
1717
import { MarkerService } from 'vs/platform/markers/common/markerService';
18+
import { CodeActionTriggerType } from 'vs/editor/contrib/codeAction/types';
1819

1920
const testProvider = {
2021
provideCodeActions(): modes.CodeActionList {
@@ -59,7 +60,7 @@ suite('CodeActionModel', () => {
5960
disposables.add(model.onDidChangeState((e: CodeActionsState.State) => {
6061
assertType(e.type === CodeActionsState.Type.Triggered);
6162

62-
assert.strictEqual(e.trigger.type, 'auto');
63+
assert.strictEqual(e.trigger.type, CodeActionTriggerType.Auto);
6364
assert.ok(e.actions);
6465

6566
e.actions.then(fixes => {
@@ -100,7 +101,7 @@ suite('CodeActionModel', () => {
100101
disposables.add(model.onDidChangeState((e: CodeActionsState.State) => {
101102
assertType(e.type === CodeActionsState.Type.Triggered);
102103

103-
assert.equal(e.trigger.type, 'auto');
104+
assert.equal(e.trigger.type, CodeActionTriggerType.Auto);
104105
assert.ok(e.actions);
105106
e.actions.then(fixes => {
106107
model.dispose();
@@ -138,7 +139,7 @@ suite('CodeActionModel', () => {
138139
disposables.add(model.onDidChangeState((e: CodeActionsState.State) => {
139140
assertType(e.type === CodeActionsState.Type.Triggered);
140141

141-
assert.equal(e.trigger.type, 'auto');
142+
assert.equal(e.trigger.type, CodeActionTriggerType.Auto);
142143
const selection = <Selection>e.rangeOrSelection;
143144
assert.deepEqual(selection.selectionStartLineNumber, 1);
144145
assert.deepEqual(selection.selectionStartColumn, 1);
@@ -163,7 +164,7 @@ suite('CodeActionModel', () => {
163164
disposables.add(model.onDidChangeState((e: CodeActionsState.State) => {
164165
assertType(e.type === CodeActionsState.Type.Triggered);
165166

166-
assert.equal(e.trigger.type, 'auto');
167+
assert.equal(e.trigger.type, CodeActionTriggerType.Auto);
167168
++triggerCount;
168169

169170
// give time for second trigger before completing test

src/vs/editor/contrib/codeAction/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ export function filtersAction(filter: CodeActionFilter, action: CodeAction): boo
102102
return true;
103103
}
104104

105+
export const enum CodeActionTriggerType {
106+
Auto,
107+
Manual
108+
}
109+
105110
export interface CodeActionTrigger {
106-
readonly type: 'auto' | 'manual';
111+
readonly type: CodeActionTriggerType;
107112
readonly filter?: CodeActionFilter;
108113
readonly autoApply?: CodeActionAutoApply;
109114
readonly context?: {

src/vs/editor/contrib/hover/modesContentHover.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
3434
import { CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
3535
import { getCodeActions, CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
3636
import { QuickFixAction, QuickFixController } from 'vs/editor/contrib/codeAction/codeActionCommands';
37-
import { CodeActionKind } from 'vs/editor/contrib/codeAction/types';
37+
import { CodeActionKind, CodeActionTriggerType } from 'vs/editor/contrib/codeAction/types';
3838
import { IModeService } from 'vs/editor/common/services/modeService';
3939
import { IIdentifiedSingleEditOperation } from 'vs/editor/common/model';
4040
import { EditorOption } from 'vs/editor/common/config/editorOptions';
@@ -592,7 +592,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
592592
return getCodeActions(
593593
this._editor.getModel()!,
594594
new Range(marker.startLineNumber, marker.startColumn, marker.endLineNumber, marker.endColumn),
595-
{ type: 'manual', filter: { include: CodeActionKind.QuickFix } },
595+
{ type: CodeActionTriggerType.Manual, filter: { include: CodeActionKind.QuickFix } },
596596
cancellationToken);
597597
});
598598
}

src/vs/workbench/api/browser/mainThreadSaveParticipant.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { CodeAction } from 'vs/editor/common/modes';
1818
import { shouldSynchronizeModel } from 'vs/editor/common/services/modelService';
1919
import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction';
2020
import { applyCodeAction } from 'vs/editor/contrib/codeAction/codeActionCommands';
21-
import { CodeActionKind } from 'vs/editor/contrib/codeAction/types';
21+
import { CodeActionKind, CodeActionTriggerType } from 'vs/editor/contrib/codeAction/types';
2222
import { formatDocumentWithSelectedProvider, FormattingMode } from 'vs/editor/contrib/format/format';
2323
import { SnippetController2 } from 'vs/editor/contrib/snippet/snippetController2';
2424
import { localize } from 'vs/nls';
@@ -311,7 +311,7 @@ class CodeActionOnSaveParticipant implements ISaveParticipantParticipant {
311311

312312
private getActionsToRun(model: ITextModel, codeActionKind: CodeActionKind, excludes: readonly CodeActionKind[], token: CancellationToken) {
313313
return getCodeActions(model, model.getFullModelRange(), {
314-
type: 'auto',
314+
type: CodeActionTriggerType.Auto,
315315
filter: { include: codeActionKind, excludes: excludes, includeSourceActions: true },
316316
}, token);
317317
}

0 commit comments

Comments
 (0)