Skip to content

Commit 31d776b

Browse files
committed
Cancel code action requests when the code action registry changes
Fixes microsoft#66322
1 parent 14aa41a commit 31d776b

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export class CodeActionModel {
172172
private _editor: ICodeEditor;
173173
private _markerService: IMarkerService;
174174
private _codeActionOracle?: CodeActionOracle;
175+
private _state: CodeActionsState.State = CodeActionsState.Empty;
175176
private _onDidChangeState = new Emitter<CodeActionsState.State>();
176177
private _disposables: IDisposable[] = [];
177178
private readonly _supportedCodeActions: IContextKey<string>;
@@ -184,7 +185,7 @@ export class CodeActionModel {
184185

185186
this._disposables.push(this._editor.onDidChangeModel(() => this._update()));
186187
this._disposables.push(this._editor.onDidChangeModelLanguage(() => this._update()));
187-
this._disposables.push(CodeActionProviderRegistry.onDidChange(this._update, this));
188+
this._disposables.push(CodeActionProviderRegistry.onDidChange(() => this._update()));
188189

189190
this._update();
190191
}
@@ -199,19 +200,21 @@ export class CodeActionModel {
199200
}
200201

201202
private _update(): void {
202-
203203
if (this._codeActionOracle) {
204204
this._codeActionOracle.dispose();
205205
this._codeActionOracle = undefined;
206-
this._onDidChangeState.fire(CodeActionsState.Empty);
207206
}
208207

208+
if (this._state.type === CodeActionsState.Type.Triggered) {
209+
this._state.actions.cancel();
210+
}
211+
this.setState(CodeActionsState.Empty);
212+
209213
const model = this._editor.getModel();
210214
if (model
211215
&& CodeActionProviderRegistry.has(model)
212216
&& !this._editor.getConfiguration().readOnly
213217
) {
214-
215218
const supportedActions: string[] = [];
216219
for (const provider of CodeActionProviderRegistry.all(model)) {
217220
if (Array.isArray(provider.providedCodeActionKinds)) {
@@ -221,17 +224,25 @@ export class CodeActionModel {
221224

222225
this._supportedCodeActions.set(supportedActions.join(' '));
223226

224-
this._codeActionOracle = new CodeActionOracle(this._editor, this._markerService, newState => this._onDidChangeState.fire(newState), undefined, this._progressService);
227+
this._codeActionOracle = new CodeActionOracle(this._editor, this._markerService, newState => this.setState(newState), undefined, this._progressService);
225228
this._codeActionOracle.trigger({ type: 'auto' });
226229
} else {
227230
this._supportedCodeActions.reset();
228231
}
229232
}
230233

231-
trigger(trigger: CodeActionTrigger): Promise<CodeAction[] | undefined> {
234+
public trigger(trigger: CodeActionTrigger): Promise<CodeAction[] | undefined> {
232235
if (this._codeActionOracle) {
233236
return this._codeActionOracle.trigger(trigger);
234237
}
235238
return Promise.resolve(undefined);
236239
}
240+
241+
private setState(newState: CodeActionsState.State) {
242+
if (newState === this._state) {
243+
return;
244+
}
245+
this._state = newState;
246+
this._onDidChangeState.fire(newState);
247+
}
237248
}

0 commit comments

Comments
 (0)