1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancelablePromise, createCancelablePromise, Delayer } from 'vs/base/common/async';
import { onUnexpectedError } from 'vs/base/common/errors';
import { Emitter } from 'vs/base/common/event';
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { ICursorSelectionChangedEvent } from 'vs/editor/common/cursorEvents';
import { CharacterSet } from 'vs/editor/common/core/characterClassifier';
import * as languages from 'vs/editor/common/languages';
import { provideSignatureHelp } from 'vs/editor/contrib/parameterHints/browser/provideSignatureHelp';
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
export interface TriggerContext {
readonly triggerKind: languages.SignatureHelpTriggerKind;
readonly triggerCharacter?: string;
}
namespace ParameterHintState {
export const enum Type {
Default,
Active,
Pending,
}
export const Default = { type: Type.Default } as const;
export class Pending {
readonly type = Type.Pending;
constructor(
readonly request: CancelablePromise<languages.SignatureHelpResult | undefined | null>,
readonly previouslyActiveHints: languages.SignatureHelp | undefined,
) { }
}
export class Active {
readonly type = Type.Active;
constructor(
readonly hints: languages.SignatureHelp
) { }
}
export type State = typeof Default | Pending | Active;
}
export class ParameterHintsModel extends Disposable {
private static readonly DEFAULT_DELAY = 120; // ms
private readonly _onChangedHints = this._register(new Emitter<languages.SignatureHelp | undefined>());
public readonly onChangedHints = this._onChangedHints.event;
private readonly editor: ICodeEditor;
private readonly providers: LanguageFeatureRegistry<languages.SignatureHelpProvider>;
private triggerOnType = false;
private _state: ParameterHintState.State = ParameterHintState.Default;
private _pendingTriggers: TriggerContext[] = [];
private readonly _lastSignatureHelpResult = this._register(new MutableDisposable<languages.SignatureHelpResult>());
private triggerChars = new CharacterSet();
private retriggerChars = new CharacterSet();
private readonly throttledDelayer: Delayer<boolean>;
private triggerId = 0;
constructor(
editor: ICodeEditor,
providers: LanguageFeatureRegistry<languages.SignatureHelpProvider>,
delay: number = ParameterHintsModel.DEFAULT_DELAY
) {
super();
this.editor = editor;
this.providers = providers;
this.throttledDelayer = new Delayer(delay);
this._register(this.editor.onDidBlurEditorWidget(() => this.cancel()));
this._register(this.editor.onDidChangeConfiguration(() => this.onEditorConfigurationChange()));
this._register(this.editor.onDidChangeModel(e => this.onModelChanged()));
this._register(this.editor.onDidChangeModelLanguage(_ => this.onModelChanged()));
this._register(this.editor.onDidChangeCursorSelection(e => this.onCursorChange(e)));
this._register(this.editor.onDidChangeModelContent(e => this.onModelContentChange()));
this._register(this.providers.onDidChange(this.onModelChanged, this));
this._register(this.editor.onDidType(text => this.onDidType(text)));
this.onEditorConfigurationChange();
this.onModelChanged();
}
private get state() { return this._state; }
private set state(value: ParameterHintState.State) {
if (this._state.type === ParameterHintState.Type.Pending) {
this._state.request.cancel();
}
this._state = value;
}
cancel(silent: boolean = false): void {
this.state = ParameterHintState.Default;
this.throttledDelayer.cancel();
if (!silent) {
this._onChangedHints.fire(undefined);
}
}
trigger(context: TriggerContext, delay?: number): void {
const model = this.editor.getModel();
if (!model || !this.providers.has(model)) {
return;
}
const triggerId = ++this.triggerId;
this._pendingTriggers.push(context);
this.throttledDelayer.trigger(() => {
return this.doTrigger(triggerId);
}, delay)
.catch(onUnexpectedError);
}
public next(): void {
if (this.state.type !== ParameterHintState.Type.Active) {
return;
}
const length = this.state.hints.signatures.length;
const activeSignature = this.state.hints.activeSignature;
const last = (activeSignature % length) === (length - 1);
const cycle = this.editor.getOption(EditorOption.parameterHints).cycle;
// If there is only one signature, or we're on last signature of list
if ((length < 2 || last) && !cycle) {
this.cancel();
return;
}
this.updateActiveSignature(last && cycle ? 0 : activeSignature + 1);
}
public previous(): void {
if (this.state.type !== ParameterHintState.Type.Active) {
return;
}
const length = this.state.hints.signatures.length;
const activeSignature = this.state.hints.activeSignature;
const first = activeSignature === 0;
const cycle = this.editor.getOption(EditorOption.parameterHints).cycle;
// If there is only one signature, or we're on first signature of list
if ((length < 2 || first) && !cycle) {
this.cancel();
return;
}
this.updateActiveSignature(first && cycle ? length - 1 : activeSignature - 1);
}
private updateActiveSignature(activeSignature: number) {
if (this.state.type !== ParameterHintState.Type.Active) {
return;
}
this.state = new ParameterHintState.Active({ ...this.state.hints, activeSignature });
this._onChangedHints.fire(this.state.hints);
}
private async doTrigger(triggerId: number): Promise<boolean> {
const isRetrigger = this.state.type === ParameterHintState.Type.Active || this.state.type === ParameterHintState.Type.Pending;
const activeSignatureHelp = this.getLastActiveHints();
this.cancel(true);
if (this._pendingTriggers.length === 0) {
return false;
}
const context: TriggerContext = this._pendingTriggers.reduce(mergeTriggerContexts);
this._pendingTriggers = [];
const triggerContext = {
triggerKind: context.triggerKind,
triggerCharacter: context.triggerCharacter,
isRetrigger: isRetrigger,
activeSignatureHelp: activeSignatureHelp
};
if (!this.editor.hasModel()) {
return false;
}
const model = this.editor.getModel();
const position = this.editor.getPosition();
this.state = new ParameterHintState.Pending(
createCancelablePromise(token => provideSignatureHelp(this.providers, model, position, triggerContext, token)),
activeSignatureHelp);
try {
const result = await this.state.request;
// Check that we are still resolving the correct signature help
if (triggerId !== this.triggerId) {
result?.dispose();
return false;
}
if (!result || !result.value.signatures || result.value.signatures.length === 0) {
result?.dispose();
this._lastSignatureHelpResult.clear();
this.cancel();
return false;
} else {
this.state = new ParameterHintState.Active(result.value);
this._lastSignatureHelpResult.value = result;
this._onChangedHints.fire(this.state.hints);
return true;
}
} catch (error) {
if (triggerId === this.triggerId) {
this.state = ParameterHintState.Default;
}
onUnexpectedError(error);
return false;
}
}
private getLastActiveHints(): languages.SignatureHelp | undefined {
switch (this.state.type) {
case ParameterHintState.Type.Active: return this.state.hints;
case ParameterHintState.Type.Pending: return this.state.previouslyActiveHints;
default: return undefined;
}
}
private get isTriggered(): boolean {
return this.state.type === ParameterHintState.Type.Active
|| this.state.type === ParameterHintState.Type.Pending
|| this.throttledDelayer.isTriggered();
}
private onModelChanged(): void {
this.cancel();
// Update trigger characters
this.triggerChars = new CharacterSet();
this.retriggerChars = new CharacterSet();
const model = this.editor.getModel();
if (!model) {
return;
}
for (const support of this.providers.ordered(model)) {
for (const ch of support.signatureHelpTriggerCharacters || []) {
this.triggerChars.add(ch.charCodeAt(0));
// All trigger characters are also considered retrigger characters
this.retriggerChars.add(ch.charCodeAt(0));
}
for (const ch of support.signatureHelpRetriggerCharacters || []) {
this.retriggerChars.add(ch.charCodeAt(0));
}
}
}
private onDidType(text: string) {
if (!this.triggerOnType) {
return;
}
const lastCharIndex = text.length - 1;
const triggerCharCode = text.charCodeAt(lastCharIndex);
if (this.triggerChars.has(triggerCharCode) || this.isTriggered && this.retriggerChars.has(triggerCharCode)) {
this.trigger({
triggerKind: languages.SignatureHelpTriggerKind.TriggerCharacter,
triggerCharacter: text.charAt(lastCharIndex),
});
}
}
private onCursorChange(e: ICursorSelectionChangedEvent): void {
if (e.source === 'mouse') {
this.cancel();
} else if (this.isTriggered) {
this.trigger({ triggerKind: languages.SignatureHelpTriggerKind.ContentChange });
}
}
private onModelContentChange(): void {
if (this.isTriggered) {
this.trigger({ triggerKind: languages.SignatureHelpTriggerKind.ContentChange });
}
}
private onEditorConfigurationChange(): void {
this.triggerOnType = this.editor.getOption(EditorOption.parameterHints).enabled;
if (!this.triggerOnType) {
this.cancel();
}
}
override dispose(): void {
this.cancel(true);
super.dispose();
}
}
function mergeTriggerContexts(previous: TriggerContext, current: TriggerContext) {
switch (current.triggerKind) {
case languages.SignatureHelpTriggerKind.Invoke:
// Invoke overrides previous triggers.
return current;
case languages.SignatureHelpTriggerKind.ContentChange:
// Ignore content changes triggers
return previous;
case languages.SignatureHelpTriggerKind.TriggerCharacter:
default:
return current;
}
}
|