123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __param = (this && this.__param) || function (paramIndex, decorator) {
- return function (target, key) { decorator(target, key, paramIndex); }
- };
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
- if (kind === "m") throw new TypeError("Private method is not writable");
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
- };
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
- };
- var _CodeActionUi_disposed;
- import { onUnexpectedError } from '../../../base/common/errors.js';
- import { Lazy } from '../../../base/common/lazy.js';
- import { Disposable, MutableDisposable } from '../../../base/common/lifecycle.js';
- import { MessageController } from '../message/messageController.js';
- import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
- import { CodeActionMenu } from './codeActionMenu.js';
- import { LightBulbWidget } from './lightBulbWidget.js';
- let CodeActionUi = class CodeActionUi extends Disposable {
- constructor(_editor, quickFixActionId, preferredFixActionId, delegate, instantiationService) {
- super();
- this._editor = _editor;
- this.delegate = delegate;
- this._activeCodeActions = this._register(new MutableDisposable());
- _CodeActionUi_disposed.set(this, false);
- this._codeActionWidget = new Lazy(() => {
- return this._register(instantiationService.createInstance(CodeActionMenu, this._editor, {
- onSelectCodeAction: (action) => __awaiter(this, void 0, void 0, function* () {
- this.delegate.applyCodeAction(action, /* retrigger */ true);
- })
- }));
- });
- this._lightBulbWidget = new Lazy(() => {
- const widget = this._register(instantiationService.createInstance(LightBulbWidget, this._editor, quickFixActionId, preferredFixActionId));
- this._register(widget.onClick(e => this.showCodeActionList(e.trigger, e.actions, e, { includeDisabledActions: false })));
- return widget;
- });
- }
- dispose() {
- __classPrivateFieldSet(this, _CodeActionUi_disposed, true, "f");
- super.dispose();
- }
- update(newState) {
- var _a, _b, _c;
- return __awaiter(this, void 0, void 0, function* () {
- if (newState.type !== 1 /* Triggered */) {
- (_a = this._lightBulbWidget.rawValue) === null || _a === void 0 ? void 0 : _a.hide();
- return;
- }
- let actions;
- try {
- actions = yield newState.actions;
- }
- catch (e) {
- onUnexpectedError(e);
- return;
- }
- if (__classPrivateFieldGet(this, _CodeActionUi_disposed, "f")) {
- return;
- }
- this._lightBulbWidget.getValue().update(actions, newState.trigger, newState.position);
- if (newState.trigger.type === 1 /* Invoke */) {
- if ((_b = newState.trigger.filter) === null || _b === void 0 ? void 0 : _b.include) { // Triggered for specific scope
- // Check to see if we want to auto apply.
- const validActionToApply = this.tryGetValidActionToApply(newState.trigger, actions);
- if (validActionToApply) {
- try {
- this._lightBulbWidget.getValue().hide();
- yield this.delegate.applyCodeAction(validActionToApply, false);
- }
- finally {
- actions.dispose();
- }
- return;
- }
- // Check to see if there is an action that we would have applied were it not invalid
- if (newState.trigger.context) {
- const invalidAction = this.getInvalidActionThatWouldHaveBeenApplied(newState.trigger, actions);
- if (invalidAction && invalidAction.action.disabled) {
- MessageController.get(this._editor).showMessage(invalidAction.action.disabled, newState.trigger.context.position);
- actions.dispose();
- return;
- }
- }
- }
- const includeDisabledActions = !!((_c = newState.trigger.filter) === null || _c === void 0 ? void 0 : _c.include);
- if (newState.trigger.context) {
- if (!actions.allActions.length || !includeDisabledActions && !actions.validActions.length) {
- MessageController.get(this._editor).showMessage(newState.trigger.context.notAvailableMessage, newState.trigger.context.position);
- this._activeCodeActions.value = actions;
- actions.dispose();
- return;
- }
- }
- this._activeCodeActions.value = actions;
- this._codeActionWidget.getValue().show(newState.trigger, actions, newState.position, { includeDisabledActions });
- }
- else {
- // auto magically triggered
- if (this._codeActionWidget.getValue().isVisible) {
- // TODO: Figure out if we should update the showing menu?
- actions.dispose();
- }
- else {
- this._activeCodeActions.value = actions;
- }
- }
- });
- }
- getInvalidActionThatWouldHaveBeenApplied(trigger, actions) {
- if (!actions.allActions.length) {
- return undefined;
- }
- if ((trigger.autoApply === "first" /* First */ && actions.validActions.length === 0)
- || (trigger.autoApply === "ifSingle" /* IfSingle */ && actions.allActions.length === 1)) {
- return actions.allActions.find(({ action }) => action.disabled);
- }
- return undefined;
- }
- tryGetValidActionToApply(trigger, actions) {
- if (!actions.validActions.length) {
- return undefined;
- }
- if ((trigger.autoApply === "first" /* First */ && actions.validActions.length > 0)
- || (trigger.autoApply === "ifSingle" /* IfSingle */ && actions.validActions.length === 1)) {
- return actions.validActions[0];
- }
- return undefined;
- }
- showCodeActionList(trigger, actions, at, options) {
- return __awaiter(this, void 0, void 0, function* () {
- this._codeActionWidget.getValue().show(trigger, actions, at, options);
- });
- }
- };
- _CodeActionUi_disposed = new WeakMap();
- CodeActionUi = __decorate([
- __param(4, IInstantiationService)
- ], CodeActionUi);
- export { CodeActionUi };
|