Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/suggest/browser/suggestWidgetRenderer.ts
4797 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { $, append, hide, show } from '../../../../base/browser/dom.js';
7
import { IconLabel, IIconLabelValueOptions } from '../../../../base/browser/ui/iconLabel/iconLabel.js';
8
import { IListRenderer } from '../../../../base/browser/ui/list/list.js';
9
import { Codicon } from '../../../../base/common/codicons.js';
10
import { ThemeIcon } from '../../../../base/common/themables.js';
11
import { Emitter, Event } from '../../../../base/common/event.js';
12
import { createMatches } from '../../../../base/common/filters.js';
13
import { DisposableStore } from '../../../../base/common/lifecycle.js';
14
import { URI } from '../../../../base/common/uri.js';
15
import { ICodeEditor } from '../../../browser/editorBrowser.js';
16
import { EditorOption } from '../../../common/config/editorOptions.js';
17
import { CompletionItemKind, CompletionItemKinds, CompletionItemTag } from '../../../common/languages.js';
18
import { getIconClasses } from '../../../common/services/getIconClasses.js';
19
import { IModelService } from '../../../common/services/model.js';
20
import { ILanguageService } from '../../../common/languages/language.js';
21
import * as nls from '../../../../nls.js';
22
import { FileKind } from '../../../../platform/files/common/files.js';
23
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
24
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
25
import { CompletionItem } from './suggest.js';
26
import { canExpandCompletionItem } from './suggestWidgetDetails.js';
27
28
const suggestMoreInfoIcon = registerIcon('suggest-more-info', Codicon.chevronRight, nls.localize('suggestMoreInfoIcon', 'Icon for more information in the suggest widget.'));
29
30
const _completionItemColor = new class ColorExtractor {
31
32
private 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*\))/;
33
private static _regexStrict = new RegExp(`^${ColorExtractor._regexRelaxed.source}$`, 'i');
34
35
extract(item: CompletionItem, out: string[]): boolean {
36
if (item.textLabel.match(ColorExtractor._regexStrict)) {
37
out[0] = item.textLabel;
38
return true;
39
}
40
if (item.completion.detail && item.completion.detail.match(ColorExtractor._regexStrict)) {
41
out[0] = item.completion.detail;
42
return true;
43
}
44
45
if (item.completion.documentation) {
46
const value = typeof item.completion.documentation === 'string'
47
? item.completion.documentation
48
: item.completion.documentation.value;
49
50
const match = ColorExtractor._regexRelaxed.exec(value);
51
if (match && (match.index === 0 || match.index + match[0].length === value.length)) {
52
out[0] = match[0];
53
return true;
54
}
55
}
56
return false;
57
}
58
};
59
60
61
export interface ISuggestionTemplateData {
62
readonly root: HTMLElement;
63
64
/**
65
* Flexbox
66
* < ------------- left ------------ > < --- right -- >
67
* <icon><label><signature><qualifier> <type><readmore>
68
*/
69
readonly left: HTMLElement;
70
readonly right: HTMLElement;
71
72
readonly icon: HTMLElement;
73
readonly colorspan: HTMLElement;
74
readonly iconLabel: IconLabel;
75
readonly iconContainer: HTMLElement;
76
readonly parametersLabel: HTMLElement;
77
readonly qualifierLabel: HTMLElement;
78
/**
79
* Showing either `CompletionItem#details` or `CompletionItemLabel#type`
80
*/
81
readonly detailsLabel: HTMLElement;
82
readonly readMore: HTMLElement;
83
readonly disposables: DisposableStore;
84
85
readonly configureFont: () => void;
86
}
87
88
export class ItemRenderer implements IListRenderer<CompletionItem, ISuggestionTemplateData> {
89
90
private readonly _onDidToggleDetails = new Emitter<void>();
91
readonly onDidToggleDetails: Event<void> = this._onDidToggleDetails.event;
92
93
readonly templateId = 'suggestion';
94
95
constructor(
96
private readonly _editor: ICodeEditor,
97
@IModelService private readonly _modelService: IModelService,
98
@ILanguageService private readonly _languageService: ILanguageService,
99
@IThemeService private readonly _themeService: IThemeService
100
) { }
101
102
dispose(): void {
103
this._onDidToggleDetails.dispose();
104
}
105
106
renderTemplate(container: HTMLElement): ISuggestionTemplateData {
107
const disposables = new DisposableStore();
108
109
const root = container;
110
root.classList.add('show-file-icons');
111
112
const icon = append(container, $('.icon'));
113
const colorspan = append(icon, $('span.colorspan'));
114
115
const text = append(container, $('.contents'));
116
const main = append(text, $('.main'));
117
118
const iconContainer = append(main, $('.icon-label.codicon'));
119
const left = append(main, $('span.left'));
120
const right = append(main, $('span.right'));
121
122
const iconLabel = new IconLabel(left, { supportHighlights: true, supportIcons: true });
123
disposables.add(iconLabel);
124
125
const parametersLabel = append(left, $('span.signature-label'));
126
const qualifierLabel = append(left, $('span.qualifier-label'));
127
const detailsLabel = append(right, $('span.details-label'));
128
129
const readMore = append(right, $('span.readMore' + ThemeIcon.asCSSSelector(suggestMoreInfoIcon)));
130
readMore.title = nls.localize('readMore', "Read More");
131
132
const configureFont = () => {
133
const options = this._editor.getOptions();
134
const fontInfo = options.get(EditorOption.fontInfo);
135
const fontFamily = fontInfo.getMassagedFontFamily();
136
const fontFeatureSettings = fontInfo.fontFeatureSettings;
137
const fontVariationSettings = fontInfo.fontVariationSettings;
138
const fontSize = options.get(EditorOption.suggestFontSize) || fontInfo.fontSize;
139
const lineHeight = options.get(EditorOption.suggestLineHeight) || fontInfo.lineHeight;
140
const fontWeight = fontInfo.fontWeight;
141
const letterSpacing = fontInfo.letterSpacing;
142
const fontSizePx = `${fontSize}px`;
143
const lineHeightPx = `${lineHeight}px`;
144
const letterSpacingPx = `${letterSpacing}px`;
145
146
root.style.fontSize = fontSizePx;
147
root.style.fontWeight = fontWeight;
148
root.style.letterSpacing = letterSpacingPx;
149
main.style.fontFamily = fontFamily;
150
main.style.fontFeatureSettings = fontFeatureSettings;
151
main.style.fontVariationSettings = fontVariationSettings;
152
main.style.lineHeight = lineHeightPx;
153
icon.style.height = lineHeightPx;
154
icon.style.width = lineHeightPx;
155
readMore.style.height = lineHeightPx;
156
readMore.style.width = lineHeightPx;
157
};
158
159
return { root, left, right, icon, colorspan, iconLabel, iconContainer, parametersLabel, qualifierLabel, detailsLabel, readMore, disposables, configureFont };
160
}
161
162
renderElement(element: CompletionItem, index: number, data: ISuggestionTemplateData): void {
163
164
165
data.configureFont();
166
167
const { completion } = element;
168
data.colorspan.style.backgroundColor = '';
169
170
const labelOptions: IIconLabelValueOptions = {
171
labelEscapeNewLines: true,
172
matches: createMatches(element.score)
173
};
174
175
const color: string[] = [];
176
if (completion.kind === CompletionItemKind.Color && _completionItemColor.extract(element, color)) {
177
// special logic for 'color' completion items
178
data.icon.className = 'icon customcolor';
179
data.iconContainer.className = 'icon hide';
180
data.colorspan.style.backgroundColor = color[0];
181
182
} else if (completion.kind === CompletionItemKind.File && this._themeService.getFileIconTheme().hasFileIcons) {
183
// special logic for 'file' completion items
184
data.icon.className = 'icon hide';
185
data.iconContainer.className = 'icon hide';
186
const labelClasses = getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: element.textLabel }), FileKind.FILE);
187
const detailClasses = getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: completion.detail }), FileKind.FILE);
188
labelOptions.extraClasses = labelClasses.length > detailClasses.length ? labelClasses : detailClasses;
189
190
} else if (completion.kind === CompletionItemKind.Folder && this._themeService.getFileIconTheme().hasFolderIcons) {
191
// special logic for 'folder' completion items
192
data.icon.className = 'icon hide';
193
data.iconContainer.className = 'icon hide';
194
labelOptions.extraClasses = [
195
getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: element.textLabel }), FileKind.FOLDER),
196
getIconClasses(this._modelService, this._languageService, URI.from({ scheme: 'fake', path: completion.detail }), FileKind.FOLDER)
197
].flat();
198
} else {
199
// normal icon
200
data.icon.className = 'icon hide';
201
data.iconContainer.className = '';
202
data.iconContainer.classList.add('suggest-icon', ...ThemeIcon.asClassNameArray(CompletionItemKinds.toIcon(completion.kind)));
203
}
204
205
if (completion.tags && completion.tags.indexOf(CompletionItemTag.Deprecated) >= 0) {
206
labelOptions.extraClasses = (labelOptions.extraClasses || []).concat(['deprecated']);
207
labelOptions.matches = [];
208
}
209
210
data.iconLabel.setLabel(element.textLabel, undefined, labelOptions);
211
if (typeof completion.label === 'string') {
212
data.parametersLabel.textContent = '';
213
data.detailsLabel.textContent = stripNewLines(completion.detail || '');
214
data.root.classList.add('string-label');
215
} else {
216
data.parametersLabel.textContent = stripNewLines(completion.label.detail || '');
217
data.detailsLabel.textContent = stripNewLines(completion.label.description || '');
218
data.root.classList.remove('string-label');
219
}
220
221
if (this._editor.getOption(EditorOption.suggest).showInlineDetails) {
222
show(data.detailsLabel);
223
} else {
224
hide(data.detailsLabel);
225
}
226
227
if (canExpandCompletionItem(element)) {
228
data.right.classList.add('can-expand-details');
229
show(data.readMore);
230
data.readMore.onmousedown = e => {
231
e.stopPropagation();
232
e.preventDefault();
233
};
234
data.readMore.onclick = e => {
235
e.stopPropagation();
236
e.preventDefault();
237
this._onDidToggleDetails.fire();
238
};
239
} else {
240
data.right.classList.remove('can-expand-details');
241
hide(data.readMore);
242
data.readMore.onmousedown = null;
243
data.readMore.onclick = null;
244
}
245
}
246
247
disposeTemplate(templateData: ISuggestionTemplateData): void {
248
templateData.disposables.dispose();
249
}
250
}
251
252
function stripNewLines(str: string): string {
253
return str.replace(/\r\n|\r|\n/g, '');
254
}
255
256