-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
parameterHintsWidget.ts
390 lines (323 loc) · 13.3 KB
/
parameterHintsWidget.ts
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { domEvent, stop } from 'vs/base/browser/event';
import * as aria from 'vs/base/browser/ui/aria/aria';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { Event } from 'vs/base/common/event';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import 'vs/css!./parameterHints';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { ConfigurationChangedEvent, EditorOption } from 'vs/editor/common/config/editorOptions';
import * as modes from 'vs/editor/common/modes';
import { IModeService } from 'vs/editor/common/services/modeService';
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
import { Context } from 'vs/editor/contrib/parameterHints/provideSignatureHelp';
import * as nls from 'vs/nls';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { editorHoverBackground, editorHoverBorder, textCodeBlockBackground, textLinkForeground, editorHoverForeground } from 'vs/platform/theme/common/colorRegistry';
import { HIGH_CONTRAST, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { ParameterHintsModel, TriggerContext } from 'vs/editor/contrib/parameterHints/parameterHintsModel';
import { pad } from 'vs/base/common/strings';
import { registerIcon, Codicon } from 'vs/base/common/codicons';
const $ = dom.$;
const parameterHintsNextIcon = registerIcon('parameter-hints-next', Codicon.chevronDown);
const parameterHintsPreviousIcon = registerIcon('parameter-hints-previous', Codicon.chevronUp);
export class ParameterHintsWidget extends Disposable implements IContentWidget {
private static readonly ID = 'editor.widget.parameterHintsWidget';
private readonly markdownRenderer: MarkdownRenderer;
private readonly renderDisposeables = this._register(new DisposableStore());
private readonly model: ParameterHintsModel;
private readonly keyVisible: IContextKey<boolean>;
private readonly keyMultipleSignatures: IContextKey<boolean>;
private domNodes?: {
readonly element: HTMLElement;
readonly signature: HTMLElement;
readonly docs: HTMLElement;
readonly overloads: HTMLElement;
readonly scrollbar: DomScrollableElement;
};
private visible: boolean = false;
private announcedLabel: string | null = null;
// Editor.IContentWidget.allowEditorOverflow
allowEditorOverflow = true;
constructor(
private readonly editor: ICodeEditor,
@IContextKeyService contextKeyService: IContextKeyService,
@IOpenerService openerService: IOpenerService,
@IModeService modeService: IModeService,
) {
super();
this.markdownRenderer = this._register(new MarkdownRenderer(editor, modeService, openerService));
this.model = this._register(new ParameterHintsModel(editor));
this.keyVisible = Context.Visible.bindTo(contextKeyService);
this.keyMultipleSignatures = Context.MultipleSignatures.bindTo(contextKeyService);
this._register(this.model.onChangedHints(newParameterHints => {
if (newParameterHints) {
this.show();
this.render(newParameterHints);
} else {
this.hide();
}
}));
}
private createParamaterHintDOMNodes() {
const element = $('.editor-widget.parameter-hints-widget');
const wrapper = dom.append(element, $('.wrapper'));
wrapper.tabIndex = -1;
const controls = dom.append(wrapper, $('.controls'));
const previous = dom.append(controls, $('.button' + parameterHintsPreviousIcon.cssSelector));
const overloads = dom.append(controls, $('.overloads'));
const next = dom.append(controls, $('.button' + parameterHintsNextIcon.cssSelector));
const onPreviousClick = stop(domEvent(previous, 'click'));
this._register(onPreviousClick(this.previous, this));
const onNextClick = stop(domEvent(next, 'click'));
this._register(onNextClick(this.next, this));
const body = $('.body');
const scrollbar = new DomScrollableElement(body, {});
this._register(scrollbar);
wrapper.appendChild(scrollbar.getDomNode());
const signature = dom.append(body, $('.signature'));
const docs = dom.append(body, $('.docs'));
element.style.userSelect = 'text';
this.domNodes = {
element,
signature,
overloads,
docs,
scrollbar,
};
this.editor.addContentWidget(this);
this.hide();
this._register(this.editor.onDidChangeCursorSelection(e => {
if (this.visible) {
this.editor.layoutContentWidget(this);
}
}));
const updateFont = () => {
if (!this.domNodes) {
return;
}
const fontInfo = this.editor.getOption(EditorOption.fontInfo);
this.domNodes.element.style.fontSize = `${fontInfo.fontSize}px`;
};
updateFont();
this._register(Event.chain<ConfigurationChangedEvent>(this.editor.onDidChangeConfiguration.bind(this.editor))
.filter(e => e.hasChanged(EditorOption.fontInfo))
.on(updateFont, null));
this._register(this.editor.onDidLayoutChange(e => this.updateMaxHeight()));
this.updateMaxHeight();
}
private show(): void {
if (this.visible) {
return;
}
if (!this.domNodes) {
this.createParamaterHintDOMNodes();
}
this.keyVisible.set(true);
this.visible = true;
setTimeout(() => {
if (this.domNodes) {
dom.addClass(this.domNodes.element, 'visible');
}
}, 100);
this.editor.layoutContentWidget(this);
}
private hide(): void {
this.renderDisposeables.clear();
if (!this.visible) {
return;
}
this.keyVisible.reset();
this.visible = false;
this.announcedLabel = null;
if (this.domNodes) {
dom.removeClass(this.domNodes.element, 'visible');
}
this.editor.layoutContentWidget(this);
}
getPosition(): IContentWidgetPosition | null {
if (this.visible) {
return {
position: this.editor.getPosition(),
preference: [ContentWidgetPositionPreference.ABOVE, ContentWidgetPositionPreference.BELOW]
};
}
return null;
}
private render(hints: modes.SignatureHelp): void {
this.renderDisposeables.clear();
if (!this.domNodes) {
return;
}
const multiple = hints.signatures.length > 1;
dom.toggleClass(this.domNodes.element, 'multiple', multiple);
this.keyMultipleSignatures.set(multiple);
this.domNodes.signature.innerHTML = '';
this.domNodes.docs.innerHTML = '';
const signature = hints.signatures[hints.activeSignature];
if (!signature) {
return;
}
const code = dom.append(this.domNodes.signature, $('.code'));
const fontInfo = this.editor.getOption(EditorOption.fontInfo);
code.style.fontSize = `${fontInfo.fontSize}px`;
code.style.fontFamily = fontInfo.fontFamily;
const hasParameters = signature.parameters.length > 0;
const activeParameterIndex = signature.activeParameter ?? hints.activeParameter;
if (!hasParameters) {
const label = dom.append(code, $('span'));
label.textContent = signature.label;
} else {
this.renderParameters(code, signature, activeParameterIndex);
}
const activeParameter: modes.ParameterInformation | undefined = signature.parameters[activeParameterIndex];
if (activeParameter?.documentation) {
const documentation = $('span.documentation');
if (typeof activeParameter.documentation === 'string') {
documentation.textContent = activeParameter.documentation;
} else {
const renderedContents = this.renderDisposeables.add(this.markdownRenderer.render(activeParameter.documentation));
dom.addClass(renderedContents.element, 'markdown-docs');
documentation.appendChild(renderedContents.element);
}
dom.append(this.domNodes.docs, $('p', {}, documentation));
}
if (signature.documentation === undefined) {
/** no op */
} else if (typeof signature.documentation === 'string') {
dom.append(this.domNodes.docs, $('p', {}, signature.documentation));
} else {
const renderedContents = this.renderDisposeables.add(this.markdownRenderer.render(signature.documentation));
dom.addClass(renderedContents.element, 'markdown-docs');
dom.append(this.domNodes.docs, renderedContents.element);
}
const hasDocs = this.hasDocs(signature, activeParameter);
dom.toggleClass(this.domNodes.signature, 'has-docs', hasDocs);
dom.toggleClass(this.domNodes.docs, 'empty', !hasDocs);
this.domNodes.overloads.textContent =
pad(hints.activeSignature + 1, hints.signatures.length.toString().length) + '/' + hints.signatures.length;
if (activeParameter) {
const labelToAnnounce = this.getParameterLabel(signature, activeParameterIndex);
// Select method gets called on every user type while parameter hints are visible.
// We do not want to spam the user with same announcements, so we only announce if the current parameter changed.
if (this.announcedLabel !== labelToAnnounce) {
aria.alert(nls.localize('hint', "{0}, hint", labelToAnnounce));
this.announcedLabel = labelToAnnounce;
}
}
this.editor.layoutContentWidget(this);
this.domNodes.scrollbar.scanDomNode();
}
private hasDocs(signature: modes.SignatureInformation, activeParameter: modes.ParameterInformation | undefined): boolean {
if (activeParameter && typeof (activeParameter.documentation) === 'string' && activeParameter.documentation.length > 0) {
return true;
}
if (activeParameter && typeof (activeParameter.documentation) === 'object' && activeParameter.documentation.value.length > 0) {
return true;
}
if (typeof (signature.documentation) === 'string' && signature.documentation.length > 0) {
return true;
}
if (typeof (signature.documentation) === 'object' && signature.documentation.value.length > 0) {
return true;
}
return false;
}
private renderParameters(parent: HTMLElement, signature: modes.SignatureInformation, activeParameterIndex: number): void {
const [start, end] = this.getParameterLabelOffsets(signature, activeParameterIndex);
const beforeSpan = document.createElement('span');
beforeSpan.textContent = signature.label.substring(0, start);
const paramSpan = document.createElement('span');
paramSpan.textContent = signature.label.substring(start, end);
paramSpan.className = 'parameter active';
const afterSpan = document.createElement('span');
afterSpan.textContent = signature.label.substring(end);
dom.append(parent, beforeSpan, paramSpan, afterSpan);
}
private getParameterLabel(signature: modes.SignatureInformation, paramIdx: number): string {
const param = signature.parameters[paramIdx];
if (typeof param.label === 'string') {
return param.label;
} else {
return signature.label.substring(param.label[0], param.label[1]);
}
}
private getParameterLabelOffsets(signature: modes.SignatureInformation, paramIdx: number): [number, number] {
const param = signature.parameters[paramIdx];
if (!param) {
return [0, 0];
} else if (Array.isArray(param.label)) {
return param.label;
} else {
const idx = signature.label.lastIndexOf(param.label);
return idx >= 0
? [idx, idx + param.label.length]
: [0, 0];
}
}
next(): void {
this.editor.focus();
this.model.next();
}
previous(): void {
this.editor.focus();
this.model.previous();
}
cancel(): void {
this.model.cancel();
}
getDomNode(): HTMLElement {
if (!this.domNodes) {
this.createParamaterHintDOMNodes();
}
return this.domNodes!.element;
}
getId(): string {
return ParameterHintsWidget.ID;
}
trigger(context: TriggerContext): void {
this.model.trigger(context, 0);
}
private updateMaxHeight(): void {
if (!this.domNodes) {
return;
}
const height = Math.max(this.editor.getLayoutInfo().height / 4, 250);
const maxHeight = `${height}px`;
this.domNodes.element.style.maxHeight = maxHeight;
const wrapper = this.domNodes.element.getElementsByClassName('wrapper') as HTMLCollectionOf<HTMLElement>;
if (wrapper.length) {
wrapper[0].style.maxHeight = maxHeight;
}
}
}
registerThemingParticipant((theme, collector) => {
const border = theme.getColor(editorHoverBorder);
if (border) {
const borderWidth = theme.type === HIGH_CONTRAST ? 2 : 1;
collector.addRule(`.monaco-editor .parameter-hints-widget { border: ${borderWidth}px solid ${border}; }`);
collector.addRule(`.monaco-editor .parameter-hints-widget.multiple .body { border-left: 1px solid ${border.transparent(0.5)}; }`);
collector.addRule(`.monaco-editor .parameter-hints-widget .signature.has-docs { border-bottom: 1px solid ${border.transparent(0.5)}; }`);
}
const background = theme.getColor(editorHoverBackground);
if (background) {
collector.addRule(`.monaco-editor .parameter-hints-widget { background-color: ${background}; }`);
}
const link = theme.getColor(textLinkForeground);
if (link) {
collector.addRule(`.monaco-editor .parameter-hints-widget a { color: ${link}; }`);
}
const foreground = theme.getColor(editorHoverForeground);
if (foreground) {
collector.addRule(`.monaco-editor .parameter-hints-widget { color: ${foreground}; }`);
}
const codeBackground = theme.getColor(textCodeBlockBackground);
if (codeBackground) {
collector.addRule(`.monaco-editor .parameter-hints-widget code { background-color: ${codeBackground}; }`);
}
});