cf3cbf48f6d005429b57094e317f9895931821302bd28528833765afe78992913004c3fc5653a63760ee026fb869316a6ecb86430f4307d31577acc26b096d 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. var __asyncValues = (this && this.__asyncValues) || function (o) {
  15. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  16. var m = o[Symbol.asyncIterator], i;
  17. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  18. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  19. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  20. };
  21. import { createCancelableAsyncIterable, RunOnceScheduler } from '../../../../base/common/async.js';
  22. import { onUnexpectedError } from '../../../../base/common/errors.js';
  23. import { Emitter } from '../../../../base/common/event.js';
  24. import { Disposable } from '../../../../base/common/lifecycle.js';
  25. export class HoverResult {
  26. constructor(value, isComplete, hasLoadingMessage) {
  27. this.value = value;
  28. this.isComplete = isComplete;
  29. this.hasLoadingMessage = hasLoadingMessage;
  30. }
  31. }
  32. /**
  33. * Computing the hover is very fine tuned.
  34. *
  35. * Suppose the hover delay is 300ms (the default). Then, when resting the mouse at an anchor:
  36. * - at 150ms, the async computation is triggered (i.e. semantic hover)
  37. * - if async results already come in, they are not rendered yet.
  38. * - at 300ms, the sync computation is triggered (i.e. decorations, markers)
  39. * - if there are sync or async results, they are rendered.
  40. * - at 900ms, if the async computation hasn't finished, a "Loading..." result is added.
  41. */
  42. export class HoverOperation extends Disposable {
  43. constructor(_editor, _computer) {
  44. super();
  45. this._editor = _editor;
  46. this._computer = _computer;
  47. this._onResult = this._register(new Emitter());
  48. this.onResult = this._onResult.event;
  49. this._firstWaitScheduler = this._register(new RunOnceScheduler(() => this._triggerAsyncComputation(), 0));
  50. this._secondWaitScheduler = this._register(new RunOnceScheduler(() => this._triggerSyncComputation(), 0));
  51. this._loadingMessageScheduler = this._register(new RunOnceScheduler(() => this._triggerLoadingMessage(), 0));
  52. this._state = 0 /* HoverOperationState.Idle */;
  53. this._asyncIterable = null;
  54. this._asyncIterableDone = false;
  55. this._result = [];
  56. }
  57. dispose() {
  58. if (this._asyncIterable) {
  59. this._asyncIterable.cancel();
  60. this._asyncIterable = null;
  61. }
  62. super.dispose();
  63. }
  64. get _hoverTime() {
  65. return this._editor.getOption(55 /* EditorOption.hover */).delay;
  66. }
  67. get _firstWaitTime() {
  68. return this._hoverTime / 2;
  69. }
  70. get _secondWaitTime() {
  71. return this._hoverTime - this._firstWaitTime;
  72. }
  73. get _loadingMessageTime() {
  74. return 3 * this._hoverTime;
  75. }
  76. _setState(state, fireResult = true) {
  77. this._state = state;
  78. if (fireResult) {
  79. this._fireResult();
  80. }
  81. }
  82. _triggerAsyncComputation() {
  83. this._setState(2 /* HoverOperationState.SecondWait */);
  84. this._secondWaitScheduler.schedule(this._secondWaitTime);
  85. if (this._computer.computeAsync) {
  86. this._asyncIterableDone = false;
  87. this._asyncIterable = createCancelableAsyncIterable(token => this._computer.computeAsync(token));
  88. (() => __awaiter(this, void 0, void 0, function* () {
  89. var e_1, _a;
  90. try {
  91. try {
  92. for (var _b = __asyncValues(this._asyncIterable), _c; _c = yield _b.next(), !_c.done;) {
  93. const item = _c.value;
  94. if (item) {
  95. this._result.push(item);
  96. this._fireResult();
  97. }
  98. }
  99. }
  100. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  101. finally {
  102. try {
  103. if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
  104. }
  105. finally { if (e_1) throw e_1.error; }
  106. }
  107. this._asyncIterableDone = true;
  108. if (this._state === 3 /* HoverOperationState.WaitingForAsync */ || this._state === 4 /* HoverOperationState.WaitingForAsyncShowingLoading */) {
  109. this._setState(0 /* HoverOperationState.Idle */);
  110. }
  111. }
  112. catch (e) {
  113. onUnexpectedError(e);
  114. }
  115. }))();
  116. }
  117. else {
  118. this._asyncIterableDone = true;
  119. }
  120. }
  121. _triggerSyncComputation() {
  122. if (this._computer.computeSync) {
  123. this._result = this._result.concat(this._computer.computeSync());
  124. }
  125. this._setState(this._asyncIterableDone ? 0 /* HoverOperationState.Idle */ : 3 /* HoverOperationState.WaitingForAsync */);
  126. }
  127. _triggerLoadingMessage() {
  128. if (this._state === 3 /* HoverOperationState.WaitingForAsync */) {
  129. this._setState(4 /* HoverOperationState.WaitingForAsyncShowingLoading */);
  130. }
  131. }
  132. _fireResult() {
  133. if (this._state === 1 /* HoverOperationState.FirstWait */ || this._state === 2 /* HoverOperationState.SecondWait */) {
  134. // Do not send out results before the hover time
  135. return;
  136. }
  137. const isComplete = (this._state === 0 /* HoverOperationState.Idle */);
  138. const hasLoadingMessage = (this._state === 4 /* HoverOperationState.WaitingForAsyncShowingLoading */);
  139. this._onResult.fire(new HoverResult(this._result.slice(0), isComplete, hasLoadingMessage));
  140. }
  141. start(mode) {
  142. if (mode === 0 /* HoverStartMode.Delayed */) {
  143. if (this._state === 0 /* HoverOperationState.Idle */) {
  144. this._setState(1 /* HoverOperationState.FirstWait */);
  145. this._firstWaitScheduler.schedule(this._firstWaitTime);
  146. this._loadingMessageScheduler.schedule(this._loadingMessageTime);
  147. }
  148. }
  149. else {
  150. switch (this._state) {
  151. case 0 /* HoverOperationState.Idle */:
  152. this._triggerAsyncComputation();
  153. this._secondWaitScheduler.cancel();
  154. this._triggerSyncComputation();
  155. break;
  156. case 2 /* HoverOperationState.SecondWait */:
  157. this._secondWaitScheduler.cancel();
  158. this._triggerSyncComputation();
  159. break;
  160. }
  161. }
  162. }
  163. cancel() {
  164. this._firstWaitScheduler.cancel();
  165. this._secondWaitScheduler.cancel();
  166. this._loadingMessageScheduler.cancel();
  167. if (this._asyncIterable) {
  168. this._asyncIterable.cancel();
  169. this._asyncIterable = null;
  170. }
  171. this._result = [];
  172. this._setState(0 /* HoverOperationState.Idle */, false);
  173. }
  174. }