Path: blob/main/src/vs/editor/contrib/suggest/browser/suggestWidgetDetails.ts
4797 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as dom from '../../../../base/browser/dom.js';6import { ResizableHTMLElement } from '../../../../base/browser/ui/resizable/resizable.js';7import { DomScrollableElement } from '../../../../base/browser/ui/scrollbar/scrollableElement.js';8import { Codicon } from '../../../../base/common/codicons.js';9import { Emitter, Event } from '../../../../base/common/event.js';10import { MarkdownString } from '../../../../base/common/htmlContent.js';11import { DisposableStore } from '../../../../base/common/lifecycle.js';12import { ThemeIcon } from '../../../../base/common/themables.js';13import * as nls from '../../../../nls.js';14import { isHighContrast } from '../../../../platform/theme/common/theme.js';15import { IThemeService } from '../../../../platform/theme/common/themeService.js';16import { ICodeEditor, IOverlayWidget, IOverlayWidgetPosition } from '../../../browser/editorBrowser.js';17import { IMarkdownRendererService } from '../../../../platform/markdown/browser/markdownRenderer.js';18import { EditorOption } from '../../../common/config/editorOptions.js';19import { CompletionItem } from './suggest.js';2021export function canExpandCompletionItem(item: CompletionItem | undefined): boolean {22return !!item && Boolean(item.completion.documentation || item.completion.detail && item.completion.detail !== item.completion.label);23}2425export class SuggestDetailsWidget {2627readonly domNode: HTMLDivElement;2829private readonly _onDidClose = new Emitter<void>();30readonly onDidClose: Event<void> = this._onDidClose.event;3132private readonly _onDidChangeContents = new Emitter<this>();33readonly onDidChangeContents: Event<this> = this._onDidChangeContents.event;3435private readonly _close: HTMLElement;36private readonly _scrollbar: DomScrollableElement;37private readonly _body: HTMLElement;38private readonly _header: HTMLElement;39private readonly _type: HTMLElement;40private readonly _docs: HTMLElement;41private readonly _disposables = new DisposableStore();4243private readonly _renderDisposeable = new DisposableStore();44private _size = new dom.Dimension(330, 0);4546constructor(47private readonly _editor: ICodeEditor,48@IThemeService private readonly _themeService: IThemeService,49@IMarkdownRendererService private readonly _markdownRendererService: IMarkdownRendererService,50) {51this.domNode = dom.$('.suggest-details');52this.domNode.classList.add('no-docs');535455this._body = dom.$('.body');5657this._scrollbar = new DomScrollableElement(this._body, {58alwaysConsumeMouseWheel: true,59});60dom.append(this.domNode, this._scrollbar.getDomNode());61this._disposables.add(this._scrollbar);6263this._header = dom.append(this._body, dom.$('.header'));64this._close = dom.append(this._header, dom.$('span' + ThemeIcon.asCSSSelector(Codicon.close)));65this._close.title = nls.localize('details.close', "Close");66this._close.role = 'button';67this._close.tabIndex = -1;68this._type = dom.append(this._header, dom.$('p.type'));6970this._docs = dom.append(this._body, dom.$('p.docs'));7172this._configureFont();7374this._disposables.add(this._editor.onDidChangeConfiguration(e => {75if (e.hasChanged(EditorOption.fontInfo)) {76this._configureFont();77}78}));79}8081dispose(): void {82this._disposables.dispose();83this._renderDisposeable.dispose();84}8586private _configureFont(): void {87const options = this._editor.getOptions();88const fontInfo = options.get(EditorOption.fontInfo);89const fontFamily = fontInfo.getMassagedFontFamily();90const fontSize = options.get(EditorOption.suggestFontSize) || fontInfo.fontSize;91const lineHeight = options.get(EditorOption.suggestLineHeight) || fontInfo.lineHeight;92const fontWeight = fontInfo.fontWeight;93const fontSizePx = `${fontSize}px`;94const lineHeightPx = `${lineHeight}px`;9596this.domNode.style.fontSize = fontSizePx;97this.domNode.style.lineHeight = `${lineHeight / fontSize}`;98this.domNode.style.fontWeight = fontWeight;99this.domNode.style.fontFeatureSettings = fontInfo.fontFeatureSettings;100this._type.style.fontFamily = fontFamily;101this._close.style.height = lineHeightPx;102this._close.style.width = lineHeightPx;103}104105getLayoutInfo() {106const lineHeight = this._editor.getOption(EditorOption.suggestLineHeight) || this._editor.getOption(EditorOption.fontInfo).lineHeight;107const borderWidth = isHighContrast(this._themeService.getColorTheme().type) ? 2 : 1;108const borderHeight = borderWidth * 2;109return {110lineHeight,111borderWidth,112borderHeight,113verticalPadding: 22,114horizontalPadding: 14115};116}117118119renderLoading(): void {120this._type.textContent = nls.localize('loading', "Loading...");121this._docs.textContent = '';122this.domNode.classList.remove('no-docs', 'no-type');123this.layout(this.size.width, this.getLayoutInfo().lineHeight * 2);124this._onDidChangeContents.fire(this);125}126127renderItem(item: CompletionItem, explainMode: boolean): void {128this._renderDisposeable.clear();129130let { detail, documentation } = item.completion;131132if (explainMode) {133let md = '';134md += `score: ${item.score[0]}\n`;135md += `prefix: ${item.word ?? '(no prefix)'}\n`;136md += `word: ${item.completion.filterText ? item.completion.filterText + ' (filterText)' : item.textLabel}\n`;137md += `distance: ${item.distance} (localityBonus-setting)\n`;138md += `index: ${item.idx}, based on ${item.completion.sortText && `sortText: "${item.completion.sortText}"` || 'label'}\n`;139md += `commit_chars: ${item.completion.commitCharacters?.join('')}\n`;140documentation = new MarkdownString().appendCodeblock('empty', md);141detail = `Provider: ${item.provider._debugDisplayName}`;142}143144if (!explainMode && !canExpandCompletionItem(item)) {145this.clearContents();146return;147}148149this.domNode.classList.remove('no-docs', 'no-type');150151// --- details152153if (detail) {154const cappedDetail = detail.length > 100000 ? `${detail.substr(0, 100000)}…` : detail;155this._type.textContent = cappedDetail;156this._type.title = cappedDetail;157dom.show(this._type);158this._type.classList.toggle('auto-wrap', !/\r?\n^\s+/gmi.test(cappedDetail));159} else {160dom.clearNode(this._type);161this._type.title = '';162dom.hide(this._type);163this.domNode.classList.add('no-type');164}165166// --- documentation167dom.clearNode(this._docs);168if (typeof documentation === 'string') {169this._docs.classList.remove('markdown-docs');170this._docs.textContent = documentation;171172} else if (documentation) {173this._docs.classList.add('markdown-docs');174dom.clearNode(this._docs);175const renderedContents = this._markdownRendererService.render(documentation, {176context: this._editor,177asyncRenderCallback: () => {178this.layout(this._size.width, this._type.clientHeight + this._docs.clientHeight);179this._onDidChangeContents.fire(this);180}181});182this._docs.appendChild(renderedContents.element);183this._renderDisposeable.add(renderedContents);184}185186this.domNode.classList.toggle('detail-and-doc', !!detail && !!documentation);187188this.domNode.style.userSelect = 'text';189this.domNode.tabIndex = -1;190191this._close.onmousedown = e => {192e.preventDefault();193e.stopPropagation();194};195this._close.onclick = e => {196e.preventDefault();197e.stopPropagation();198this._onDidClose.fire();199};200201this._body.scrollTop = 0;202203this.layout(this._size.width, this._type.clientHeight + this._docs.clientHeight);204this._onDidChangeContents.fire(this);205}206207clearContents() {208this.domNode.classList.add('no-docs');209this._type.textContent = '';210this._docs.textContent = '';211}212213get isEmpty(): boolean {214return this.domNode.classList.contains('no-docs');215}216217get size() {218return this._size;219}220221layout(width: number, height: number): void {222const newSize = new dom.Dimension(width, height);223if (!dom.Dimension.equals(newSize, this._size)) {224this._size = newSize;225dom.size(this.domNode, width, height);226}227this._scrollbar.scanDomNode();228}229230scrollDown(much = 8): void {231this._body.scrollTop += much;232}233234scrollUp(much = 8): void {235this._body.scrollTop -= much;236}237238scrollTop(): void {239this._body.scrollTop = 0;240}241242scrollBottom(): void {243this._body.scrollTop = this._body.scrollHeight;244}245246pageDown(): void {247this.scrollDown(80);248}249250pageUp(): void {251this.scrollUp(80);252}253254focus() {255this.domNode.focus();256}257}258259interface TopLeftPosition {260top: number;261left: number;262}263264export class SuggestDetailsOverlay implements IOverlayWidget {265266readonly allowEditorOverflow = true;267268private readonly _disposables = new DisposableStore();269private readonly _resizable: ResizableHTMLElement;270271private _added: boolean = false;272private _anchorBox?: dom.IDomNodePagePosition;273private _preferAlignAtTop: boolean = true;274private _userSize?: dom.Dimension;275private _topLeft?: TopLeftPosition;276277constructor(278readonly widget: SuggestDetailsWidget,279private readonly _editor: ICodeEditor280) {281282this._resizable = new ResizableHTMLElement();283this._resizable.domNode.classList.add('suggest-details-container');284this._resizable.domNode.appendChild(widget.domNode);285this._resizable.enableSashes(false, true, true, false);286287let topLeftNow: TopLeftPosition | undefined;288let sizeNow: dom.Dimension | undefined;289let deltaTop: number = 0;290let deltaLeft: number = 0;291this._disposables.add(this._resizable.onDidWillResize(() => {292topLeftNow = this._topLeft;293sizeNow = this._resizable.size;294}));295296this._disposables.add(this._resizable.onDidResize(e => {297if (topLeftNow && sizeNow) {298this.widget.layout(e.dimension.width, e.dimension.height);299300let updateTopLeft = false;301if (e.west) {302deltaLeft = sizeNow.width - e.dimension.width;303updateTopLeft = true;304}305if (e.north) {306deltaTop = sizeNow.height - e.dimension.height;307updateTopLeft = true;308}309if (updateTopLeft) {310this._applyTopLeft({311top: topLeftNow.top + deltaTop,312left: topLeftNow.left + deltaLeft,313});314}315}316if (e.done) {317topLeftNow = undefined;318sizeNow = undefined;319deltaTop = 0;320deltaLeft = 0;321this._userSize = e.dimension;322}323}));324325this._disposables.add(this.widget.onDidChangeContents(() => {326if (this._anchorBox) {327this._placeAtAnchor(this._anchorBox, this._userSize ?? this.widget.size, this._preferAlignAtTop);328}329}));330}331332dispose(): void {333this._resizable.dispose();334this._disposables.dispose();335this.hide();336}337338getId(): string {339return 'suggest.details';340}341342getDomNode(): HTMLElement {343return this._resizable.domNode;344}345346getPosition(): IOverlayWidgetPosition | null {347return this._topLeft ? { preference: this._topLeft } : null;348}349350show(): void {351if (!this._added) {352this._editor.addOverlayWidget(this);353this._added = true;354}355}356357hide(sessionEnded: boolean = false): void {358this._resizable.clearSashHoverState();359360if (this._added) {361this._editor.removeOverlayWidget(this);362this._added = false;363this._anchorBox = undefined;364this._topLeft = undefined;365}366if (sessionEnded) {367this._userSize = undefined;368this.widget.clearContents();369}370}371372placeAtAnchor(anchor: HTMLElement, preferAlignAtTop: boolean) {373const anchorBox = anchor.getBoundingClientRect();374this._anchorBox = anchorBox;375this._preferAlignAtTop = preferAlignAtTop;376this._placeAtAnchor(this._anchorBox, this._userSize ?? this.widget.size, preferAlignAtTop);377}378379_placeAtAnchor(anchorBox: dom.IDomNodePagePosition, size: dom.Dimension, preferAlignAtTop: boolean) {380const bodyBox = dom.getClientArea(this.getDomNode().ownerDocument.body);381382const info = this.widget.getLayoutInfo();383384const defaultMinSize = new dom.Dimension(220, 2 * info.lineHeight);385const defaultTop = anchorBox.top;386387type Placement = { top: number; left: number; fit: number; maxSizeTop: dom.Dimension; maxSizeBottom: dom.Dimension; minSize: dom.Dimension };388389// EAST390const eastPlacement: Placement = (function () {391const width = bodyBox.width - (anchorBox.left + anchorBox.width + info.borderWidth + info.horizontalPadding);392const left = -info.borderWidth + anchorBox.left + anchorBox.width;393const maxSizeTop = new dom.Dimension(width, bodyBox.height - anchorBox.top - info.borderHeight - info.verticalPadding);394const maxSizeBottom = maxSizeTop.with(undefined, anchorBox.top + anchorBox.height - info.borderHeight - info.verticalPadding);395return { top: defaultTop, left, fit: width - size.width, maxSizeTop, maxSizeBottom, minSize: defaultMinSize.with(Math.min(width, defaultMinSize.width)) };396})();397398// WEST399const westPlacement: Placement = (function () {400const width = anchorBox.left - info.borderWidth - info.horizontalPadding;401const left = Math.max(info.horizontalPadding, anchorBox.left - size.width - info.borderWidth);402const maxSizeTop = new dom.Dimension(width, bodyBox.height - anchorBox.top - info.borderHeight - info.verticalPadding);403const maxSizeBottom = maxSizeTop.with(undefined, anchorBox.top + anchorBox.height - info.borderHeight - info.verticalPadding);404return { top: defaultTop, left, fit: width - size.width, maxSizeTop, maxSizeBottom, minSize: defaultMinSize.with(Math.min(width, defaultMinSize.width)) };405})();406407// SOUTH408const southPlacement: Placement = (function () {409const left = anchorBox.left;410const top = -info.borderWidth + anchorBox.top + anchorBox.height;411const maxSizeBottom = new dom.Dimension(anchorBox.width - info.borderHeight, bodyBox.height - anchorBox.top - anchorBox.height - info.verticalPadding);412return { top, left, fit: maxSizeBottom.height - size.height, maxSizeBottom, maxSizeTop: maxSizeBottom, minSize: defaultMinSize.with(maxSizeBottom.width) };413})();414415// NORTH416const northPlacement: Placement = (function () {417const left = anchorBox.left;418const maxSizeTop = new dom.Dimension(anchorBox.width - info.borderHeight, anchorBox.top - info.verticalPadding);419const top = Math.max(info.verticalPadding, anchorBox.top - size.height);420return { top, left, fit: maxSizeTop.height - size.height, maxSizeTop, maxSizeBottom: maxSizeTop, minSize: defaultMinSize.with(maxSizeTop.width) };421})();422423// take first placement that fits or the first with "least bad" fit424// when the suggest widget is rendering above the cursor (preferAlignAtTop=false), prefer NORTH over SOUTH425const verticalPlacement = preferAlignAtTop ? southPlacement : northPlacement;426const placements = [eastPlacement, westPlacement, verticalPlacement];427const placement = placements.find(p => p.fit >= 0) ?? placements.sort((a, b) => b.fit - a.fit)[0];428429// top/bottom placement430const bottom = anchorBox.top + anchorBox.height - info.borderHeight;431let alignAtTop: boolean;432let height = size.height;433const maxHeight = Math.max(placement.maxSizeTop.height, placement.maxSizeBottom.height);434if (height > maxHeight) {435height = maxHeight;436}437let maxSize: dom.Dimension;438if (preferAlignAtTop) {439if (height <= placement.maxSizeTop.height) {440alignAtTop = true;441maxSize = placement.maxSizeTop;442} else {443alignAtTop = false;444maxSize = placement.maxSizeBottom;445}446} else {447if (height <= placement.maxSizeBottom.height) {448alignAtTop = false;449maxSize = placement.maxSizeBottom;450} else {451alignAtTop = true;452maxSize = placement.maxSizeTop;453}454}455456let { top, left } = placement;457if (placement === northPlacement) {458// For NORTH placement, position the details above the anchor459top = anchorBox.top - height + info.borderWidth;460} else if (!alignAtTop && height > anchorBox.height) {461top = bottom - height;462}463const editorDomNode = this._editor.getDomNode();464if (editorDomNode) {465// get bounding rectangle of the suggest widget relative to the editor466const editorBoundingBox = editorDomNode.getBoundingClientRect();467top -= editorBoundingBox.top;468left -= editorBoundingBox.left;469}470this._applyTopLeft({ left, top });471472// enableSashes(north, east, south, west)473// For NORTH placement: enable north sash (resize upward from top), disable south (can't resize into the anchor)474// Also enable west sash for horizontal resizing, consistent with SOUTH placement475// For SOUTH placement and EAST/WEST placements: use existing logic based on alignAtTop476if (placement === northPlacement) {477this._resizable.enableSashes(true, false, false, true);478} else {479this._resizable.enableSashes(!alignAtTop, placement === eastPlacement, alignAtTop, placement !== eastPlacement);480}481482this._resizable.minSize = placement.minSize;483this._resizable.maxSize = maxSize;484this._resizable.layout(height, Math.min(maxSize.width, size.width));485this.widget.layout(this._resizable.size.width, this._resizable.size.height);486}487488private _applyTopLeft(topLeft: TopLeftPosition): void {489this._topLeft = topLeft;490this._editor.layoutOverlayWidget(this);491}492}493494495