parameterHintsModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. import { createCancelablePromise, Delayer } from '../../../base/common/async.js';
  15. import { onUnexpectedError } from '../../../base/common/errors.js';
  16. import { Emitter } from '../../../base/common/event.js';
  17. import { Disposable, MutableDisposable } from '../../../base/common/lifecycle.js';
  18. import { CharacterSet } from '../../common/core/characterClassifier.js';
  19. import * as modes from '../../common/modes.js';
  20. import { provideSignatureHelp } from './provideSignatureHelp.js';
  21. var ParameterHintState;
  22. (function (ParameterHintState) {
  23. ParameterHintState.Default = { type: 0 /* Default */ };
  24. class Pending {
  25. constructor(request, previouslyActiveHints) {
  26. this.request = request;
  27. this.previouslyActiveHints = previouslyActiveHints;
  28. this.type = 2 /* Pending */;
  29. }
  30. }
  31. ParameterHintState.Pending = Pending;
  32. class Active {
  33. constructor(hints) {
  34. this.hints = hints;
  35. this.type = 1 /* Active */;
  36. }
  37. }
  38. ParameterHintState.Active = Active;
  39. })(ParameterHintState || (ParameterHintState = {}));
  40. export class ParameterHintsModel extends Disposable {
  41. constructor(editor, delay = ParameterHintsModel.DEFAULT_DELAY) {
  42. super();
  43. this._onChangedHints = this._register(new Emitter());
  44. this.onChangedHints = this._onChangedHints.event;
  45. this.triggerOnType = false;
  46. this._state = ParameterHintState.Default;
  47. this._pendingTriggers = [];
  48. this._lastSignatureHelpResult = this._register(new MutableDisposable());
  49. this.triggerChars = new CharacterSet();
  50. this.retriggerChars = new CharacterSet();
  51. this.triggerId = 0;
  52. this.editor = editor;
  53. this.throttledDelayer = new Delayer(delay);
  54. this._register(this.editor.onDidBlurEditorWidget(() => this.cancel()));
  55. this._register(this.editor.onDidChangeConfiguration(() => this.onEditorConfigurationChange()));
  56. this._register(this.editor.onDidChangeModel(e => this.onModelChanged()));
  57. this._register(this.editor.onDidChangeModelLanguage(_ => this.onModelChanged()));
  58. this._register(this.editor.onDidChangeCursorSelection(e => this.onCursorChange(e)));
  59. this._register(this.editor.onDidChangeModelContent(e => this.onModelContentChange()));
  60. this._register(modes.SignatureHelpProviderRegistry.onDidChange(this.onModelChanged, this));
  61. this._register(this.editor.onDidType(text => this.onDidType(text)));
  62. this.onEditorConfigurationChange();
  63. this.onModelChanged();
  64. }
  65. get state() { return this._state; }
  66. set state(value) {
  67. if (this._state.type === 2 /* Pending */) {
  68. this._state.request.cancel();
  69. }
  70. this._state = value;
  71. }
  72. cancel(silent = false) {
  73. this.state = ParameterHintState.Default;
  74. this.throttledDelayer.cancel();
  75. if (!silent) {
  76. this._onChangedHints.fire(undefined);
  77. }
  78. }
  79. trigger(context, delay) {
  80. const model = this.editor.getModel();
  81. if (!model || !modes.SignatureHelpProviderRegistry.has(model)) {
  82. return;
  83. }
  84. const triggerId = ++this.triggerId;
  85. this._pendingTriggers.push(context);
  86. this.throttledDelayer.trigger(() => {
  87. return this.doTrigger(triggerId);
  88. }, delay)
  89. .catch(onUnexpectedError);
  90. }
  91. next() {
  92. if (this.state.type !== 1 /* Active */) {
  93. return;
  94. }
  95. const length = this.state.hints.signatures.length;
  96. const activeSignature = this.state.hints.activeSignature;
  97. const last = (activeSignature % length) === (length - 1);
  98. const cycle = this.editor.getOption(75 /* parameterHints */).cycle;
  99. // If there is only one signature, or we're on last signature of list
  100. if ((length < 2 || last) && !cycle) {
  101. this.cancel();
  102. return;
  103. }
  104. this.updateActiveSignature(last && cycle ? 0 : activeSignature + 1);
  105. }
  106. previous() {
  107. if (this.state.type !== 1 /* Active */) {
  108. return;
  109. }
  110. const length = this.state.hints.signatures.length;
  111. const activeSignature = this.state.hints.activeSignature;
  112. const first = activeSignature === 0;
  113. const cycle = this.editor.getOption(75 /* parameterHints */).cycle;
  114. // If there is only one signature, or we're on first signature of list
  115. if ((length < 2 || first) && !cycle) {
  116. this.cancel();
  117. return;
  118. }
  119. this.updateActiveSignature(first && cycle ? length - 1 : activeSignature - 1);
  120. }
  121. updateActiveSignature(activeSignature) {
  122. if (this.state.type !== 1 /* Active */) {
  123. return;
  124. }
  125. this.state = new ParameterHintState.Active(Object.assign(Object.assign({}, this.state.hints), { activeSignature }));
  126. this._onChangedHints.fire(this.state.hints);
  127. }
  128. doTrigger(triggerId) {
  129. return __awaiter(this, void 0, void 0, function* () {
  130. const isRetrigger = this.state.type === 1 /* Active */ || this.state.type === 2 /* Pending */;
  131. const activeSignatureHelp = this.getLastActiveHints();
  132. this.cancel(true);
  133. if (this._pendingTriggers.length === 0) {
  134. return false;
  135. }
  136. const context = this._pendingTriggers.reduce(mergeTriggerContexts);
  137. this._pendingTriggers = [];
  138. const triggerContext = {
  139. triggerKind: context.triggerKind,
  140. triggerCharacter: context.triggerCharacter,
  141. isRetrigger: isRetrigger,
  142. activeSignatureHelp: activeSignatureHelp
  143. };
  144. if (!this.editor.hasModel()) {
  145. return false;
  146. }
  147. const model = this.editor.getModel();
  148. const position = this.editor.getPosition();
  149. this.state = new ParameterHintState.Pending(createCancelablePromise(token => provideSignatureHelp(model, position, triggerContext, token)), activeSignatureHelp);
  150. try {
  151. const result = yield this.state.request;
  152. // Check that we are still resolving the correct signature help
  153. if (triggerId !== this.triggerId) {
  154. result === null || result === void 0 ? void 0 : result.dispose();
  155. return false;
  156. }
  157. if (!result || !result.value.signatures || result.value.signatures.length === 0) {
  158. result === null || result === void 0 ? void 0 : result.dispose();
  159. this._lastSignatureHelpResult.clear();
  160. this.cancel();
  161. return false;
  162. }
  163. else {
  164. this.state = new ParameterHintState.Active(result.value);
  165. this._lastSignatureHelpResult.value = result;
  166. this._onChangedHints.fire(this.state.hints);
  167. return true;
  168. }
  169. }
  170. catch (error) {
  171. if (triggerId === this.triggerId) {
  172. this.state = ParameterHintState.Default;
  173. }
  174. onUnexpectedError(error);
  175. return false;
  176. }
  177. });
  178. }
  179. getLastActiveHints() {
  180. switch (this.state.type) {
  181. case 1 /* Active */: return this.state.hints;
  182. case 2 /* Pending */: return this.state.previouslyActiveHints;
  183. default: return undefined;
  184. }
  185. }
  186. get isTriggered() {
  187. return this.state.type === 1 /* Active */
  188. || this.state.type === 2 /* Pending */
  189. || this.throttledDelayer.isTriggered();
  190. }
  191. onModelChanged() {
  192. this.cancel();
  193. // Update trigger characters
  194. this.triggerChars = new CharacterSet();
  195. this.retriggerChars = new CharacterSet();
  196. const model = this.editor.getModel();
  197. if (!model) {
  198. return;
  199. }
  200. for (const support of modes.SignatureHelpProviderRegistry.ordered(model)) {
  201. for (const ch of support.signatureHelpTriggerCharacters || []) {
  202. this.triggerChars.add(ch.charCodeAt(0));
  203. // All trigger characters are also considered retrigger characters
  204. this.retriggerChars.add(ch.charCodeAt(0));
  205. }
  206. for (const ch of support.signatureHelpRetriggerCharacters || []) {
  207. this.retriggerChars.add(ch.charCodeAt(0));
  208. }
  209. }
  210. }
  211. onDidType(text) {
  212. if (!this.triggerOnType) {
  213. return;
  214. }
  215. const lastCharIndex = text.length - 1;
  216. const triggerCharCode = text.charCodeAt(lastCharIndex);
  217. if (this.triggerChars.has(triggerCharCode) || this.isTriggered && this.retriggerChars.has(triggerCharCode)) {
  218. this.trigger({
  219. triggerKind: modes.SignatureHelpTriggerKind.TriggerCharacter,
  220. triggerCharacter: text.charAt(lastCharIndex),
  221. });
  222. }
  223. }
  224. onCursorChange(e) {
  225. if (e.source === 'mouse') {
  226. this.cancel();
  227. }
  228. else if (this.isTriggered) {
  229. this.trigger({ triggerKind: modes.SignatureHelpTriggerKind.ContentChange });
  230. }
  231. }
  232. onModelContentChange() {
  233. if (this.isTriggered) {
  234. this.trigger({ triggerKind: modes.SignatureHelpTriggerKind.ContentChange });
  235. }
  236. }
  237. onEditorConfigurationChange() {
  238. this.triggerOnType = this.editor.getOption(75 /* parameterHints */).enabled;
  239. if (!this.triggerOnType) {
  240. this.cancel();
  241. }
  242. }
  243. dispose() {
  244. this.cancel(true);
  245. super.dispose();
  246. }
  247. }
  248. ParameterHintsModel.DEFAULT_DELAY = 120; // ms
  249. function mergeTriggerContexts(previous, current) {
  250. switch (current.triggerKind) {
  251. case modes.SignatureHelpTriggerKind.Invoke:
  252. // Invoke overrides previous triggers.
  253. return current;
  254. case modes.SignatureHelpTriggerKind.ContentChange:
  255. // Ignore content changes triggers
  256. return previous;
  257. case modes.SignatureHelpTriggerKind.TriggerCharacter:
  258. default:
  259. return current;
  260. }
  261. }