codeActionUi.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. 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;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  24. if (kind === "m") throw new TypeError("Private method is not writable");
  25. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  26. 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");
  27. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  28. };
  29. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  30. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  31. 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");
  32. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  33. };
  34. var _CodeActionUi_disposed;
  35. import { onUnexpectedError } from '../../../base/common/errors.js';
  36. import { Lazy } from '../../../base/common/lazy.js';
  37. import { Disposable, MutableDisposable } from '../../../base/common/lifecycle.js';
  38. import { MessageController } from '../message/messageController.js';
  39. import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
  40. import { CodeActionMenu } from './codeActionMenu.js';
  41. import { LightBulbWidget } from './lightBulbWidget.js';
  42. let CodeActionUi = class CodeActionUi extends Disposable {
  43. constructor(_editor, quickFixActionId, preferredFixActionId, delegate, instantiationService) {
  44. super();
  45. this._editor = _editor;
  46. this.delegate = delegate;
  47. this._activeCodeActions = this._register(new MutableDisposable());
  48. _CodeActionUi_disposed.set(this, false);
  49. this._codeActionWidget = new Lazy(() => {
  50. return this._register(instantiationService.createInstance(CodeActionMenu, this._editor, {
  51. onSelectCodeAction: (action) => __awaiter(this, void 0, void 0, function* () {
  52. this.delegate.applyCodeAction(action, /* retrigger */ true);
  53. })
  54. }));
  55. });
  56. this._lightBulbWidget = new Lazy(() => {
  57. const widget = this._register(instantiationService.createInstance(LightBulbWidget, this._editor, quickFixActionId, preferredFixActionId));
  58. this._register(widget.onClick(e => this.showCodeActionList(e.trigger, e.actions, e, { includeDisabledActions: false })));
  59. return widget;
  60. });
  61. }
  62. dispose() {
  63. __classPrivateFieldSet(this, _CodeActionUi_disposed, true, "f");
  64. super.dispose();
  65. }
  66. update(newState) {
  67. var _a, _b, _c;
  68. return __awaiter(this, void 0, void 0, function* () {
  69. if (newState.type !== 1 /* Triggered */) {
  70. (_a = this._lightBulbWidget.rawValue) === null || _a === void 0 ? void 0 : _a.hide();
  71. return;
  72. }
  73. let actions;
  74. try {
  75. actions = yield newState.actions;
  76. }
  77. catch (e) {
  78. onUnexpectedError(e);
  79. return;
  80. }
  81. if (__classPrivateFieldGet(this, _CodeActionUi_disposed, "f")) {
  82. return;
  83. }
  84. this._lightBulbWidget.getValue().update(actions, newState.trigger, newState.position);
  85. if (newState.trigger.type === 1 /* Invoke */) {
  86. if ((_b = newState.trigger.filter) === null || _b === void 0 ? void 0 : _b.include) { // Triggered for specific scope
  87. // Check to see if we want to auto apply.
  88. const validActionToApply = this.tryGetValidActionToApply(newState.trigger, actions);
  89. if (validActionToApply) {
  90. try {
  91. this._lightBulbWidget.getValue().hide();
  92. yield this.delegate.applyCodeAction(validActionToApply, false);
  93. }
  94. finally {
  95. actions.dispose();
  96. }
  97. return;
  98. }
  99. // Check to see if there is an action that we would have applied were it not invalid
  100. if (newState.trigger.context) {
  101. const invalidAction = this.getInvalidActionThatWouldHaveBeenApplied(newState.trigger, actions);
  102. if (invalidAction && invalidAction.action.disabled) {
  103. MessageController.get(this._editor).showMessage(invalidAction.action.disabled, newState.trigger.context.position);
  104. actions.dispose();
  105. return;
  106. }
  107. }
  108. }
  109. const includeDisabledActions = !!((_c = newState.trigger.filter) === null || _c === void 0 ? void 0 : _c.include);
  110. if (newState.trigger.context) {
  111. if (!actions.allActions.length || !includeDisabledActions && !actions.validActions.length) {
  112. MessageController.get(this._editor).showMessage(newState.trigger.context.notAvailableMessage, newState.trigger.context.position);
  113. this._activeCodeActions.value = actions;
  114. actions.dispose();
  115. return;
  116. }
  117. }
  118. this._activeCodeActions.value = actions;
  119. this._codeActionWidget.getValue().show(newState.trigger, actions, newState.position, { includeDisabledActions });
  120. }
  121. else {
  122. // auto magically triggered
  123. if (this._codeActionWidget.getValue().isVisible) {
  124. // TODO: Figure out if we should update the showing menu?
  125. actions.dispose();
  126. }
  127. else {
  128. this._activeCodeActions.value = actions;
  129. }
  130. }
  131. });
  132. }
  133. getInvalidActionThatWouldHaveBeenApplied(trigger, actions) {
  134. if (!actions.allActions.length) {
  135. return undefined;
  136. }
  137. if ((trigger.autoApply === "first" /* First */ && actions.validActions.length === 0)
  138. || (trigger.autoApply === "ifSingle" /* IfSingle */ && actions.allActions.length === 1)) {
  139. return actions.allActions.find(({ action }) => action.disabled);
  140. }
  141. return undefined;
  142. }
  143. tryGetValidActionToApply(trigger, actions) {
  144. if (!actions.validActions.length) {
  145. return undefined;
  146. }
  147. if ((trigger.autoApply === "first" /* First */ && actions.validActions.length > 0)
  148. || (trigger.autoApply === "ifSingle" /* IfSingle */ && actions.validActions.length === 1)) {
  149. return actions.validActions[0];
  150. }
  151. return undefined;
  152. }
  153. showCodeActionList(trigger, actions, at, options) {
  154. return __awaiter(this, void 0, void 0, function* () {
  155. this._codeActionWidget.getValue().show(trigger, actions, at, options);
  156. });
  157. }
  158. };
  159. _CodeActionUi_disposed = new WeakMap();
  160. CodeActionUi = __decorate([
  161. __param(4, IInstantiationService)
  162. ], CodeActionUi);
  163. export { CodeActionUi };