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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*---------------------------------------------------------------------------------------------
* 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 { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { Codicon } from 'vs/base/common/codicons';
import { Emitter, Event } from 'vs/base/common/event';
import { MarkdownString } from 'vs/base/common/htmlContent';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { MarkdownRenderer } from 'vs/editor/contrib/markdownRenderer/browser/markdownRenderer';
import { ICodeEditor, IOverlayWidget } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { ResizableHTMLElement } from 'vs/base/browser/ui/resizable/resizable';
import * as nls from 'vs/nls';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { CompletionItem } from './suggest';
export function canExpandCompletionItem(item: CompletionItem | undefined): boolean {
return !!item && Boolean(item.completion.documentation || item.completion.detail && item.completion.detail !== item.completion.label);
}
export class SuggestDetailsWidget {
readonly domNode: HTMLDivElement;
private readonly _onDidClose = new Emitter<void>();
readonly onDidClose: Event<void> = this._onDidClose.event;
private readonly _onDidChangeContents = new Emitter<this>();
readonly onDidChangeContents: Event<this> = this._onDidChangeContents.event;
private readonly _close: HTMLElement;
private readonly _scrollbar: DomScrollableElement;
private readonly _body: HTMLElement;
private readonly _header: HTMLElement;
private readonly _type: HTMLElement;
private readonly _docs: HTMLElement;
private readonly _disposables = new DisposableStore();
private readonly _markdownRenderer: MarkdownRenderer;
private readonly _renderDisposeable = new DisposableStore();
private _borderWidth: number = 1;
private _size = new dom.Dimension(330, 0);
constructor(
private readonly _editor: ICodeEditor,
@IInstantiationService instaService: IInstantiationService,
) {
this.domNode = dom.$('.suggest-details');
this.domNode.classList.add('no-docs');
this._markdownRenderer = instaService.createInstance(MarkdownRenderer, { editor: _editor });
this._body = dom.$('.body');
this._scrollbar = new DomScrollableElement(this._body, {
alwaysConsumeMouseWheel: true,
});
dom.append(this.domNode, this._scrollbar.getDomNode());
this._disposables.add(this._scrollbar);
this._header = dom.append(this._body, dom.$('.header'));
this._close = dom.append(this._header, dom.$('span' + Codicon.close.cssSelector));
this._close.title = nls.localize('details.close', "Close");
this._type = dom.append(this._header, dom.$('p.type'));
this._docs = dom.append(this._body, dom.$('p.docs'));
this._configureFont();
this._disposables.add(this._editor.onDidChangeConfiguration(e => {
if (e.hasChanged(EditorOption.fontInfo)) {
this._configureFont();
}
}));
}
dispose(): void {
this._disposables.dispose();
this._renderDisposeable.dispose();
}
private _configureFont(): void {
const options = this._editor.getOptions();
const fontInfo = options.get(EditorOption.fontInfo);
const fontFamily = fontInfo.getMassagedFontFamily();
const fontSize = options.get(EditorOption.suggestFontSize) || fontInfo.fontSize;
const lineHeight = options.get(EditorOption.suggestLineHeight) || fontInfo.lineHeight;
const fontWeight = fontInfo.fontWeight;
const fontSizePx = `${fontSize}px`;
const lineHeightPx = `${lineHeight}px`;
this.domNode.style.fontSize = fontSizePx;
this.domNode.style.lineHeight = `${lineHeight / fontSize}`;
this.domNode.style.fontWeight = fontWeight;
this.domNode.style.fontFeatureSettings = fontInfo.fontFeatureSettings;
this._type.style.fontFamily = fontFamily;
this._close.style.height = lineHeightPx;
this._close.style.width = lineHeightPx;
}
getLayoutInfo() {
const lineHeight = this._editor.getOption(EditorOption.suggestLineHeight) || this._editor.getOption(EditorOption.fontInfo).lineHeight;
const borderWidth = this._borderWidth;
const borderHeight = borderWidth * 2;
return {
lineHeight,
borderWidth,
borderHeight,
verticalPadding: 22,
horizontalPadding: 14
};
}
renderLoading(): void {
this._type.textContent = nls.localize('loading', "Loading...");
this._docs.textContent = '';
this.domNode.classList.remove('no-docs', 'no-type');
this.layout(this.size.width, this.getLayoutInfo().lineHeight * 2);
this._onDidChangeContents.fire(this);
}
renderItem(item: CompletionItem, explainMode: boolean): void {
this._renderDisposeable.clear();
let { detail, documentation } = item.completion;
if (explainMode) {
let md = '';
md += `score: ${item.score[0]}\n`;
md += `prefix: ${item.word ?? '(no prefix)'}\n`;
md += `word: ${item.completion.filterText ? item.completion.filterText + ' (filterText)' : item.textLabel}\n`;
md += `distance: ${item.distance} (localityBonus-setting)\n`;
md += `index: ${item.idx}, based on ${item.completion.sortText && `sortText: "${item.completion.sortText}"` || 'label'}\n`;
md += `commit_chars: ${item.completion.commitCharacters?.join('')}\n`;
documentation = new MarkdownString().appendCodeblock('empty', md);
detail = `Provider: ${item.provider._debugDisplayName}`;
}
if (!explainMode && !canExpandCompletionItem(item)) {
this.clearContents();
return;
}
this.domNode.classList.remove('no-docs', 'no-type');
// --- details
if (detail) {
const cappedDetail = detail.length > 100000 ? `${detail.substr(0, 100000)}…` : detail;
this._type.textContent = cappedDetail;
this._type.title = cappedDetail;
dom.show(this._type);
this._type.classList.toggle('auto-wrap', !/\r?\n^\s+/gmi.test(cappedDetail));
} else {
dom.clearNode(this._type);
this._type.title = '';
dom.hide(this._type);
this.domNode.classList.add('no-type');
}
// --- documentation
dom.clearNode(this._docs);
if (typeof documentation === 'string') {
this._docs.classList.remove('markdown-docs');
this._docs.textContent = documentation;
} else if (documentation) {
this._docs.classList.add('markdown-docs');
dom.clearNode(this._docs);
const renderedContents = this._markdownRenderer.render(documentation);
this._docs.appendChild(renderedContents.element);
this._renderDisposeable.add(renderedContents);
this._renderDisposeable.add(this._markdownRenderer.onDidRenderAsync(() => {
this.layout(this._size.width, this._type.clientHeight + this._docs.clientHeight);
this._onDidChangeContents.fire(this);
}));
}
this.domNode.style.userSelect = 'text';
this.domNode.tabIndex = -1;
this._close.onmousedown = e => {
e.preventDefault();
e.stopPropagation();
};
this._close.onclick = e => {
e.preventDefault();
e.stopPropagation();
this._onDidClose.fire();
};
this._body.scrollTop = 0;
this.layout(this._size.width, this._type.clientHeight + this._docs.clientHeight);
this._onDidChangeContents.fire(this);
}
clearContents() {
this.domNode.classList.add('no-docs');
this._type.textContent = '';
this._docs.textContent = '';
}
get size() {
return this._size;
}
layout(width: number, height: number): void {
const newSize = new dom.Dimension(width, height);
if (!dom.Dimension.equals(newSize, this._size)) {
this._size = newSize;
dom.size(this.domNode, width, height);
}
this._scrollbar.scanDomNode();
}
scrollDown(much = 8): void {
this._body.scrollTop += much;
}
scrollUp(much = 8): void {
this._body.scrollTop -= much;
}
scrollTop(): void {
this._body.scrollTop = 0;
}
scrollBottom(): void {
this._body.scrollTop = this._body.scrollHeight;
}
pageDown(): void {
this.scrollDown(80);
}
pageUp(): void {
this.scrollUp(80);
}
set borderWidth(width: number) {
this._borderWidth = width;
}
get borderWidth() {
return this._borderWidth;
}
}
interface TopLeftPosition {
top: number;
left: number;
}
export class SuggestDetailsOverlay implements IOverlayWidget {
private readonly _disposables = new DisposableStore();
private readonly _resizable: ResizableHTMLElement;
private _added: boolean = false;
private _anchorBox?: dom.IDomNodePagePosition;
private _preferAlignAtTop: boolean = true;
private _userSize?: dom.Dimension;
private _topLeft?: TopLeftPosition;
constructor(
readonly widget: SuggestDetailsWidget,
private readonly _editor: ICodeEditor
) {
this._resizable = new ResizableHTMLElement();
this._resizable.domNode.classList.add('suggest-details-container');
this._resizable.domNode.appendChild(widget.domNode);
this._resizable.enableSashes(false, true, true, false);
let topLeftNow: TopLeftPosition | undefined;
let sizeNow: dom.Dimension | undefined;
let deltaTop: number = 0;
let deltaLeft: number = 0;
this._disposables.add(this._resizable.onDidWillResize(() => {
topLeftNow = this._topLeft;
sizeNow = this._resizable.size;
}));
this._disposables.add(this._resizable.onDidResize(e => {
if (topLeftNow && sizeNow) {
this.widget.layout(e.dimension.width, e.dimension.height);
let updateTopLeft = false;
if (e.west) {
deltaLeft = sizeNow.width - e.dimension.width;
updateTopLeft = true;
}
if (e.north) {
deltaTop = sizeNow.height - e.dimension.height;
updateTopLeft = true;
}
if (updateTopLeft) {
this._applyTopLeft({
top: topLeftNow.top + deltaTop,
left: topLeftNow.left + deltaLeft,
});
}
}
if (e.done) {
topLeftNow = undefined;
sizeNow = undefined;
deltaTop = 0;
deltaLeft = 0;
this._userSize = e.dimension;
}
}));
this._disposables.add(this.widget.onDidChangeContents(() => {
if (this._anchorBox) {
this._placeAtAnchor(this._anchorBox, this._userSize ?? this.widget.size, this._preferAlignAtTop);
}
}));
}
dispose(): void {
this._resizable.dispose();
this._disposables.dispose();
this.hide();
}
getId(): string {
return 'suggest.details';
}
getDomNode(): HTMLElement {
return this._resizable.domNode;
}
getPosition(): null {
return null;
}
show(): void {
if (!this._added) {
this._editor.addOverlayWidget(this);
this.getDomNode().style.position = 'fixed';
this._added = true;
}
}
hide(sessionEnded: boolean = false): void {
this._resizable.clearSashHoverState();
if (this._added) {
this._editor.removeOverlayWidget(this);
this._added = false;
this._anchorBox = undefined;
this._topLeft = undefined;
}
if (sessionEnded) {
this._userSize = undefined;
this.widget.clearContents();
}
}
placeAtAnchor(anchor: HTMLElement, preferAlignAtTop: boolean) {
const anchorBox = anchor.getBoundingClientRect();
this._anchorBox = anchorBox;
this._preferAlignAtTop = preferAlignAtTop;
this._placeAtAnchor(this._anchorBox, this._userSize ?? this.widget.size, preferAlignAtTop);
}
_placeAtAnchor(anchorBox: dom.IDomNodePagePosition, size: dom.Dimension, preferAlignAtTop: boolean) {
const bodyBox = dom.getClientArea(document.body);
const info = this.widget.getLayoutInfo();
const defaultMinSize = new dom.Dimension(220, 2 * info.lineHeight);
const defaultTop = anchorBox.top;
type Placement = { top: number; left: number; fit: number; maxSizeTop: dom.Dimension; maxSizeBottom: dom.Dimension; minSize: dom.Dimension };
// EAST
const eastPlacement: Placement = (function () {
const width = bodyBox.width - (anchorBox.left + anchorBox.width + info.borderWidth + info.horizontalPadding);
const left = -info.borderWidth + anchorBox.left + anchorBox.width;
const maxSizeTop = new dom.Dimension(width, bodyBox.height - anchorBox.top - info.borderHeight - info.verticalPadding);
const maxSizeBottom = maxSizeTop.with(undefined, anchorBox.top + anchorBox.height - info.borderHeight - info.verticalPadding);
return { top: defaultTop, left, fit: width - size.width, maxSizeTop, maxSizeBottom, minSize: defaultMinSize.with(Math.min(width, defaultMinSize.width)) };
})();
// WEST
const westPlacement: Placement = (function () {
const width = anchorBox.left - info.borderWidth - info.horizontalPadding;
const left = Math.max(info.horizontalPadding, anchorBox.left - size.width - info.borderWidth);
const maxSizeTop = new dom.Dimension(width, bodyBox.height - anchorBox.top - info.borderHeight - info.verticalPadding);
const maxSizeBottom = maxSizeTop.with(undefined, anchorBox.top + anchorBox.height - info.borderHeight - info.verticalPadding);
return { top: defaultTop, left, fit: width - size.width, maxSizeTop, maxSizeBottom, minSize: defaultMinSize.with(Math.min(width, defaultMinSize.width)) };
})();
// SOUTH
const southPacement: Placement = (function () {
const left = anchorBox.left;
const top = -info.borderWidth + anchorBox.top + anchorBox.height;
const maxSizeBottom = new dom.Dimension(anchorBox.width - info.borderHeight, bodyBox.height - anchorBox.top - anchorBox.height - info.verticalPadding);
return { top, left, fit: maxSizeBottom.height - size.height, maxSizeBottom, maxSizeTop: maxSizeBottom, minSize: defaultMinSize.with(maxSizeBottom.width) };
})();
// take first placement that fits or the first with "least bad" fit
const placements = [eastPlacement, westPlacement, southPacement];
const placement = placements.find(p => p.fit >= 0) ?? placements.sort((a, b) => b.fit - a.fit)[0];
// top/bottom placement
const bottom = anchorBox.top + anchorBox.height - info.borderHeight;
let alignAtTop: boolean;
let height = size.height;
const maxHeight = Math.max(placement.maxSizeTop.height, placement.maxSizeBottom.height);
if (height > maxHeight) {
height = maxHeight;
}
let maxSize: dom.Dimension;
if (preferAlignAtTop) {
if (height <= placement.maxSizeTop.height) {
alignAtTop = true;
maxSize = placement.maxSizeTop;
} else {
alignAtTop = false;
maxSize = placement.maxSizeBottom;
}
} else {
if (height <= placement.maxSizeBottom.height) {
alignAtTop = false;
maxSize = placement.maxSizeBottom;
} else {
alignAtTop = true;
maxSize = placement.maxSizeTop;
}
}
this._applyTopLeft({ left: placement.left, top: alignAtTop ? placement.top : bottom - height });
this.getDomNode().style.position = 'fixed';
this._resizable.enableSashes(!alignAtTop, placement === eastPlacement, alignAtTop, placement !== eastPlacement);
this._resizable.minSize = placement.minSize;
this._resizable.maxSize = maxSize;
this._resizable.layout(height, Math.min(maxSize.width, size.width));
this.widget.layout(this._resizable.size.width, this._resizable.size.height);
}
private _applyTopLeft(topLeft: TopLeftPosition): void {
this._topLeft = topLeft;
this.getDomNode().style.left = `${this._topLeft.left}px`;
this.getDomNode().style.top = `${this._topLeft.top}px`;
}
}
|