e03b093b3b184468b6e8f0e7782fea5951d9ccd7b12b7354093985c2e112fad3c204d651546a1f77039a0b6e095b249a67fe8ce29501f7bd4d90bb6c8a1b32 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/browser/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. this.previewOn = false;
  49. _CodeActionUi_disposed.set(this, false);
  50. this._codeActionWidget = new Lazy(() => {
  51. return this._register(instantiationService.createInstance(CodeActionMenu, this._editor, {
  52. onSelectCodeAction: (action, trigger) => __awaiter(this, void 0, void 0, function* () {
  53. if (this.previewOn) {
  54. this.delegate.applyCodeAction(action, /* retrigger */ true, Boolean(this.previewOn));
  55. }
  56. else {
  57. this.delegate.applyCodeAction(action, /* retrigger */ true, Boolean(trigger.preview));
  58. }
  59. this.previewOn = false;
  60. })
  61. }));
  62. });
  63. this._lightBulbWidget = new Lazy(() => {
  64. const widget = this._register(instantiationService.createInstance(LightBulbWidget, this._editor, quickFixActionId, preferredFixActionId));
  65. this._register(widget.onClick(e => this.showCodeActionList(e.trigger, e.actions, e, { includeDisabledActions: false, fromLightbulb: true })));
  66. return widget;
  67. });
  68. }
  69. dispose() {
  70. __classPrivateFieldSet(this, _CodeActionUi_disposed, true, "f");
  71. super.dispose();
  72. }
  73. hideCodeActionWidget() {
  74. if (this._codeActionWidget.hasValue()) {
  75. this._codeActionWidget.getValue().hideCodeActionWidget();
  76. }
  77. }
  78. onEnter() {
  79. if (this._codeActionWidget.hasValue()) {
  80. this._codeActionWidget.getValue().onEnterSet();
  81. }
  82. }
  83. onPreviewEnter() {
  84. this.previewOn = true;
  85. this.onEnter();
  86. }
  87. navigateList(navUp) {
  88. if (this._codeActionWidget.hasValue()) {
  89. if (navUp) {
  90. this._codeActionWidget.getValue().navigateListWithKeysUp();
  91. }
  92. else {
  93. this._codeActionWidget.getValue().navigateListWithKeysDown();
  94. }
  95. }
  96. }
  97. update(newState) {
  98. var _a, _b, _c, _d, _e;
  99. return __awaiter(this, void 0, void 0, function* () {
  100. if (newState.type !== 1 /* CodeActionsState.Type.Triggered */) {
  101. (_a = this._lightBulbWidget.rawValue) === null || _a === void 0 ? void 0 : _a.hide();
  102. return;
  103. }
  104. let actions;
  105. try {
  106. actions = yield newState.actions;
  107. }
  108. catch (e) {
  109. onUnexpectedError(e);
  110. return;
  111. }
  112. if (__classPrivateFieldGet(this, _CodeActionUi_disposed, "f")) {
  113. return;
  114. }
  115. this._lightBulbWidget.getValue().update(actions, newState.trigger, newState.position);
  116. if (newState.trigger.type === 1 /* CodeActionTriggerType.Invoke */) {
  117. if ((_b = newState.trigger.filter) === null || _b === void 0 ? void 0 : _b.include) { // Triggered for specific scope
  118. // Check to see if we want to auto apply.
  119. const validActionToApply = this.tryGetValidActionToApply(newState.trigger, actions);
  120. if (validActionToApply) {
  121. try {
  122. this._lightBulbWidget.getValue().hide();
  123. yield this.delegate.applyCodeAction(validActionToApply, false, false);
  124. }
  125. finally {
  126. actions.dispose();
  127. }
  128. return;
  129. }
  130. // Check to see if there is an action that we would have applied were it not invalid
  131. if (newState.trigger.context) {
  132. const invalidAction = this.getInvalidActionThatWouldHaveBeenApplied(newState.trigger, actions);
  133. if (invalidAction && invalidAction.action.disabled) {
  134. (_c = MessageController.get(this._editor)) === null || _c === void 0 ? void 0 : _c.showMessage(invalidAction.action.disabled, newState.trigger.context.position);
  135. actions.dispose();
  136. return;
  137. }
  138. }
  139. }
  140. const includeDisabledActions = !!((_d = newState.trigger.filter) === null || _d === void 0 ? void 0 : _d.include);
  141. if (newState.trigger.context) {
  142. if (!actions.allActions.length || !includeDisabledActions && !actions.validActions.length) {
  143. (_e = MessageController.get(this._editor)) === null || _e === void 0 ? void 0 : _e.showMessage(newState.trigger.context.notAvailableMessage, newState.trigger.context.position);
  144. this._activeCodeActions.value = actions;
  145. actions.dispose();
  146. return;
  147. }
  148. }
  149. this._activeCodeActions.value = actions;
  150. this._codeActionWidget.getValue().show(newState.trigger, actions, newState.position, { includeDisabledActions, fromLightbulb: false });
  151. }
  152. else {
  153. // auto magically triggered
  154. if (this._codeActionWidget.getValue().isVisible) {
  155. // TODO: Figure out if we should update the showing menu?
  156. actions.dispose();
  157. }
  158. else {
  159. this._activeCodeActions.value = actions;
  160. }
  161. }
  162. });
  163. }
  164. getInvalidActionThatWouldHaveBeenApplied(trigger, actions) {
  165. if (!actions.allActions.length) {
  166. return undefined;
  167. }
  168. if ((trigger.autoApply === "first" /* CodeActionAutoApply.First */ && actions.validActions.length === 0)
  169. || (trigger.autoApply === "ifSingle" /* CodeActionAutoApply.IfSingle */ && actions.allActions.length === 1)) {
  170. return actions.allActions.find(({ action }) => action.disabled);
  171. }
  172. return undefined;
  173. }
  174. tryGetValidActionToApply(trigger, actions) {
  175. if (!actions.validActions.length) {
  176. return undefined;
  177. }
  178. if ((trigger.autoApply === "first" /* CodeActionAutoApply.First */ && actions.validActions.length > 0)
  179. || (trigger.autoApply === "ifSingle" /* CodeActionAutoApply.IfSingle */ && actions.validActions.length === 1)) {
  180. return actions.validActions[0];
  181. }
  182. return undefined;
  183. }
  184. showCodeActionList(trigger, actions, at, options) {
  185. return __awaiter(this, void 0, void 0, function* () {
  186. this._codeActionWidget.getValue().show(trigger, actions, at, options);
  187. });
  188. }
  189. };
  190. _CodeActionUi_disposed = new WeakMap();
  191. CodeActionUi = __decorate([
  192. __param(4, IInstantiationService)
  193. ], CodeActionUi);
  194. export { CodeActionUi };