Skip to content

Commit 7799cbd

Browse files
authored
Use correct context key service in code action widget (microsoft#162479)
Fixes microsoft#162477 This fixes a bug where the code action widget could get stuck using the wrong context key service
1 parent 83fa50f commit 7799cbd

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CodeActionTriggerType } from 'vs/editor/common/languages';
1515
import { CodeActionItem, CodeActionSet } from 'vs/editor/contrib/codeAction/browser/codeAction';
1616
import { MessageController } from 'vs/editor/contrib/message/browser/messageController';
1717
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
18+
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1819
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1920
import { CodeActionsState } from './codeActionModel';
2021
import { CodeActionShowOptions, CodeActionWidget } from './codeActionWidget';
@@ -34,8 +35,9 @@ export class CodeActionUi extends Disposable {
3435
private readonly delegate: {
3536
applyCodeAction: (action: CodeActionItem, regtriggerAfterApply: boolean, preview: boolean) => Promise<void>;
3637
},
37-
@IInstantiationService private readonly _instantiationService: IInstantiationService,
3838
@IConfigurationService private readonly _configurationService: IConfigurationService,
39+
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
40+
@IInstantiationService private readonly _instantiationService: IInstantiationService,
3941
) {
4042
super();
4143

@@ -166,7 +168,7 @@ export class CodeActionUi extends Disposable {
166168
onHide: () => {
167169
this._editor?.focus();
168170
},
169-
});
171+
}, this._contextKeyService);
170172
}
171173

172174
private toCoords(position: IPosition): IAnchor {

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { CodeActionKind, CodeActionTrigger, CodeActionTriggerSource } from 'vs/e
2020
import 'vs/editor/contrib/symbolIcons/browser/symbolIcons'; // The codicon symbol colors are defined here and must be loaded to get colors
2121
import { localize } from 'vs/nls';
2222
import { ICommandService } from 'vs/platform/commands/common/commands';
23-
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
23+
import { IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
2424
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
2525
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2626
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
@@ -369,40 +369,44 @@ export class CodeActionWidget extends Disposable {
369369
readonly container: HTMLElement | undefined;
370370
readonly codeActions: CodeActionSet;
371371
readonly delegate: CodeActionWidgetDelegate;
372+
readonly contextKeyService: IContextKeyService;
372373
};
373374

374-
private readonly _ctxMenuWidgetVisible: IContextKey<boolean>;
375-
376375
constructor(
377376
@ICommandService private readonly _commandService: ICommandService,
378-
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
379377
@IContextViewService private readonly _contextViewService: IContextViewService,
380378
@IKeybindingService private readonly _keybindingService: IKeybindingService,
381379
@ITelemetryService private readonly _telemetryService: ITelemetryService,
382380
) {
383381
super();
384-
385-
this._ctxMenuWidgetVisible = Context.Visible.bindTo(this._contextKeyService);
386382
}
387383

388384
get isVisible(): boolean {
389385
return !!this.currentShowingContext;
390386
}
391387

392-
public async show(trigger: CodeActionTrigger, codeActions: CodeActionSet, anchor: IAnchor, container: HTMLElement | undefined, options: CodeActionShowOptions, delegate: CodeActionWidgetDelegate): Promise<void> {
388+
public async show(trigger: CodeActionTrigger, codeActions: CodeActionSet, anchor: IAnchor, container: HTMLElement | undefined, options: CodeActionShowOptions, delegate: CodeActionWidgetDelegate, contextKeyService: IContextKeyService): Promise<void> {
393389
this.currentShowingContext = undefined;
390+
const visibleContext = Context.Visible.bindTo(contextKeyService);
394391

395392
const actionsToShow = options.includeDisabledActions && (showDisabled || codeActions.validActions.length === 0) ? codeActions.allActions : codeActions.validActions;
396393
if (!actionsToShow.length) {
394+
visibleContext.reset();
397395
return;
398396
}
399397

400-
this.currentShowingContext = { trigger, codeActions, anchor, container, delegate, options };
398+
this.currentShowingContext = { trigger, codeActions, anchor, container, delegate, options, contextKeyService };
401399

402400
this._contextViewService.showContextView({
403401
getAnchor: () => anchor,
404-
render: (container: HTMLElement) => this.renderWidget(container, trigger, codeActions, options, actionsToShow, delegate),
405-
onHide: (didCancel: boolean) => this.onWidgetClosed(trigger, options, codeActions, didCancel, delegate),
402+
render: (container: HTMLElement) => {
403+
visibleContext.set(true);
404+
return this.renderWidget(container, trigger, codeActions, options, actionsToShow, delegate);
405+
},
406+
onHide: (didCancel: boolean) => {
407+
visibleContext.reset();
408+
return this.onWidgetClosed(trigger, options, codeActions, didCancel, delegate);
409+
},
406410
}, container, false);
407411
}
408412

@@ -487,8 +491,6 @@ export class CodeActionWidget extends Disposable {
487491
const focusTracker = renderDisposables.add(dom.trackFocus(element));
488492
renderDisposables.add(focusTracker.onDidBlur(() => this.hide()));
489493

490-
this._ctxMenuWidgetVisible.set(true);
491-
492494
return renderDisposables;
493495
}
494496

@@ -503,7 +505,7 @@ export class CodeActionWidget extends Disposable {
503505
showDisabled = newShowDisabled;
504506

505507
if (previousCtx) {
506-
this.show(previousCtx.trigger, previousCtx.codeActions, previousCtx.anchor, previousCtx.container, previousCtx.options, previousCtx.delegate);
508+
this.show(previousCtx.trigger, previousCtx.codeActions, previousCtx.anchor, previousCtx.container, previousCtx.options, previousCtx.delegate, previousCtx.contextKeyService);
507509
}
508510
}
509511

@@ -529,7 +531,7 @@ export class CodeActionWidget extends Disposable {
529531
});
530532

531533
this.currentShowingContext = undefined;
532-
this._ctxMenuWidgetVisible.reset();
534+
533535
delegate.onHide(cancelled);
534536
}
535537

0 commit comments

Comments
 (0)