Path: blob/main/src/vs/editor/contrib/suggest/browser/suggestWidgetRenderer.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 { $, append, hide, show } from '../../../../base/browser/dom.js';6import { IconLabel, IIconLabelValueOptions } from '../../../../base/browser/ui/iconLabel/iconLabel.js';7import { IListRenderer } from '../../../../base/browser/ui/list/list.js';8import { Codicon } from '../../../../base/common/codicons.js';9import { ThemeIcon } from '../../../../base/common/themables.js';10import { Emitter, Event } from '../../../../base/common/event.js';11import { createMatches } from '../../../../base/common/filters.js';12import { DisposableStore } from '../../../../base/common/lifecycle.js';13import { URI } from '../../../../base/common/uri.js';14import { ICodeEditor } from '../../../browser/editorBrowser.js';15import { EditorOption } from '../../../common/config/editorOptions.js';16import { CompletionItemKind, CompletionItemKinds, CompletionItemTag } from '../../../common/languages.js';17import { getIconClasses } from '../../../common/services/getIconClasses.js';18import { IModelService } from '../../../common/services/model.js';19import { ILanguageService } from '../../../common/languages/language.js';20import * as nls from '../../../../nls.js';21import { FileKind } from '../../../../platform/files/common/files.js';22import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';23import { IThemeService } from '../../../../platform/theme/common/themeService.js';24import { CompletionItem } from './suggest.js';25import { canExpandCompletionItem } from './suggestWidgetDetails.js';2627const suggestMoreInfoIcon = registerIcon('suggest-more-info', Codicon.chevronRight, nls.localize('suggestMoreInfoIcon', 'Icon for more information in the suggest widget.'));2829const _completionItemColor = new class ColorExtractor {3031private static _regexRelaxed = /(#([\da-fA-F]{3}){1,2}|(rgb|hsl)a\(\s*(\d{1,3}%?\s*,\s*){3}(1|0?\.\d+)\)|(rgb|hsl)\(\s*\d{1,3}%?(\s*,\s*\d{1,3}%?){2}\s*\))/;32private static _regexStrict = new RegExp(`^${ColorExtractor._regexRelaxed.source}$`, 'i');3334extract(item: CompletionItem, out: string[]): boolean {35if (item.textLabel.match(ColorExtractor._regexStrict)) {36out[0] = item.textLabel;37return true;38}39if (item.completion.detail && item.completion.detail.match(ColorExtractor._regexStrict)) {40out[0] = item.completion.detail;41return true;42}4344if (item.completion.documentation) {45const value = typeof item.completion.documentation === 'string'46? item.completion.documentation47: item.completion.documentation.value;4849const match = ColorExtractor._regexRelaxed.exec(value);50if (match && (match.index === 0 || match.index + match[0].length === value.length)) {51out[0] = match[0];52return true;53}54}55return false;56}57};585960export interface ISuggestionTemplateData {61readonly root: HTMLElement;6263/**64* Flexbox65* < ------------- left ------------ > < --- right -- >66* <icon><label><signature><qualifier> <type><readmore>67*/68readonly left: HTMLElement;69readonly right: HTMLElement;7071readonly icon: HTMLElement;72readonly colorspan: HTMLElement;73readonly iconLabel: IconLabel;74readonly iconContainer: HTMLElement;75readonly parametersLabel: HTMLElement;76readonly qualifierLabel: HTMLElement;77/**78* Showing either `CompletionItem#details` or `CompletionItemLabel#type`79*/80readonly detailsLabel: HTMLElement;81readonly readMore: HTMLElement;82readonly disposables: DisposableStore;8384readonly configureFont: () => void;85}8687export class ItemRenderer implements IListRenderer<CompletionItem, ISuggestionTemplateData> {8889private readonly _onDidToggleDetails = new Emitter<void>();90readonly onDidToggleDetails: Event<void> = this._onDidToggleDetails.event;9192readonly templateId = 'suggestion';9394constructor(95private readonly _editor: ICodeEditor,96@IModelService private readonly _modelService: IModelService,97@ILanguageService private readonly _languageService: ILanguageService,98@IThemeService private readonly _themeService: IThemeService99) { }100101dispose(): void {102this._onDidToggleDetails.dispose();103}104105renderTemplate(container: HTMLElement): ISuggestionTemplateData {106const disposables = new DisposableStore();107108const root = container;109root.classList.add('show-file-icons');110111const icon = append(container, $('.icon'));112const colorspan = append(icon, $('span.colorspan'));113114const text = append(container, $('.contents'));115const main = append(text, $('.main'));116117const iconContainer = append(main, $('.icon-label.codicon'));118const left = append(main, $('span.left'));119const right = append(main, $('span.right'));120121const iconLabel = new IconLabel(left, { supportHighlights: true, supportIcons: true });122disposables.add(iconLabel);123124const parametersLabel = append(left, $('span.signature-label'));125const qualifierLabel = append(left, $('span.qualifier-label'));126const detailsLabel = append(right, $('span.details-label'));127128const readMore = append(right, $('span.readMore' + ThemeIcon.asCSSSelector(suggestMoreInfoIcon)));129readMore.title = nls.localize('readMore', "Read More");130131const configureFont = () => {132const options = this._editor.getOptions();133const fontInfo = options.get(EditorOption.fontInfo);134const fontFamily = fontInfo.getMassagedFontFamily();135const fontFeatureSettings = fontInfo.fontFeatureSettings;136const fontVariationSettings = fontInfo.fontVariationSettings;137const fontSize = options.get(EditorOption.suggestFontSize) || fontInfo.fontSize;138const lineHeight = options.get(EditorOption.suggestLineHeight) || fontInfo.lineHeight;139const fontWeight = fontInfo.fontWeight;140const letterSpacing = fontInfo.letterSpacing;141const fontSizePx = `${fontSize}px`;142const lineHeightPx = `${lineHeight}px`;143const letterSpacingPx = `${letterSpacing}px`;144145root.style.fontSize = fontSizePx;146root.style.fontWeight = fontWeight;147root.style.letterSpacing = letterSpacingPx;148main.style.fontFamily = fontFamily;149main.style.fontFeatureSettings = fontFeatureSettings;150main.style.fontVariationSettings = fontVariationSettings;151main.style.lineHeight = lineHeightPx;152icon.style.height = lineHeightPx;153icon.style.width = lineHeightPx;154readMore.style.height = lineHeightPx;155readMore.style.width = lineHeightPx;156};157158return { root, left, right, icon, colorspan, iconLabel, iconContainer, parametersLabel, qualifierLabel, detailsLabel, readMore, disposables, configureFont };159}160161renderElement(element: CompletionItem, index: number, data: ISuggestionTemplateData): void {162163164data.configureFont();165166const { completion } = element;167data.colorspan.style.backgroundColor = '';168169const labelOptions: IIconLabelValueOptions = {170labelEscapeNewLines: true,171matches: createMatches(element.score)172};173174const color: string[] = [];175if (completion.kind === CompletionItemKind.Color && _completionItemColor.extract(element, color)) {176// special logic for 'color' completion items177data.icon.className = 'icon customcolor';178data.iconContainer.className = 'icon hide';179data.colorspan.style.backgroundColor = color[0];180181} else if (completion.kind === CompletionItemKind.File && this._themeService.getFileIconTheme().hasFileIcons) {182// special logic for 'file' completion items183data.icon.className = 'icon hide';184data.iconContainer.className = 'icon hide';185const labelClasses = getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: element.textLabel }), FileKind.FILE);186const detailClasses = getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: completion.detail }), FileKind.FILE);187labelOptions.extraClasses = labelClasses.length > detailClasses.length ? labelClasses : detailClasses;188189} else if (completion.kind === CompletionItemKind.Folder && this._themeService.getFileIconTheme().hasFolderIcons) {190// special logic for 'folder' completion items191data.icon.className = 'icon hide';192data.iconContainer.className = 'icon hide';193labelOptions.extraClasses = [194getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: element.textLabel }), FileKind.FOLDER),195getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: completion.detail }), FileKind.FOLDER)196].flat();197} else {198// normal icon199data.icon.className = 'icon hide';200data.iconContainer.className = '';201data.iconContainer.classList.add('suggest-icon', ...ThemeIcon.asClassNameArray(CompletionItemKinds.toIcon(completion.kind)));202}203204if (completion.tags && completion.tags.indexOf(CompletionItemTag.Deprecated) >= 0) {205labelOptions.extraClasses = (labelOptions.extraClasses || []).concat(['deprecated']);206labelOptions.matches = [];207}208209data.iconLabel.setLabel(element.textLabel, undefined, labelOptions);210if (typeof completion.label === 'string') {211data.parametersLabel.textContent = '';212data.detailsLabel.textContent = stripNewLines(completion.detail || '');213data.root.classList.add('string-label');214} else {215data.parametersLabel.textContent = stripNewLines(completion.label.detail || '');216data.detailsLabel.textContent = stripNewLines(completion.label.description || '');217data.root.classList.remove('string-label');218}219220if (this._editor.getOption(EditorOption.suggest).showInlineDetails) {221show(data.detailsLabel);222} else {223hide(data.detailsLabel);224}225226if (canExpandCompletionItem(element)) {227data.right.classList.add('can-expand-details');228show(data.readMore);229data.readMore.onmousedown = e => {230e.stopPropagation();231e.preventDefault();232};233data.readMore.onclick = e => {234e.stopPropagation();235e.preventDefault();236this._onDidToggleDetails.fire();237};238} else {239data.right.classList.remove('can-expand-details');240hide(data.readMore);241data.readMore.onmousedown = null;242data.readMore.onclick = null;243}244}245246disposeTemplate(templateData: ISuggestionTemplateData): void {247templateData.disposables.dispose();248}249}250251function stripNewLines(str: string): string {252return str.replace(/\r\n|\r|\n/g, '');253}254255256