labels.ts 12.5 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import { URI as uri } from 'vs/base/common/uri';
9
import * as resources from 'vs/base/common/resources';
10
import { IconLabel, IIconLabelValueOptions, IIconLabelCreationOptions } from 'vs/base/browser/ui/iconLabel/iconLabel';
11
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
J
Johannes Rieken 已提交
12
import { IModeService } from 'vs/editor/common/services/modeService';
13
import { toResource, IEditorInput } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
14 15 16 17
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IModelService } from 'vs/editor/common/services/modelService';
B
Benjamin Pasero 已提交
18
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
19
import { IDecorationsService, IResourceDecorationChangeEvent, IDecorationData } from 'vs/workbench/services/decorations/browser/decorations';
B
Benjamin Pasero 已提交
20
import { Schemas } from 'vs/base/common/network';
21
import { FileKind, FILES_ASSOCIATIONS_CONFIG } from 'vs/platform/files/common/files';
A
Alex Dima 已提交
22
import { ITextModel } from 'vs/editor/common/model';
23
import { IThemeService } from 'vs/platform/theme/common/themeService';
M
Matt Bierner 已提交
24
import { Event, Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
25
import { DataUri } from 'vs/workbench/common/resources';
I
isidor 已提交
26
import { ILabelService } from 'vs/platform/label/common/label';
B
Benjamin Pasero 已提交
27

B
Benjamin Pasero 已提交
28
export interface IResourceLabel {
B
Benjamin Pasero 已提交
29 30 31 32 33
	name: string;
	description?: string;
	resource?: uri;
}

34
export interface IResourceLabelOptions extends IIconLabelValueOptions {
35
	fileKind?: FileKind;
36
	fileDecorations?: { colors: boolean, badges: boolean, data?: IDecorationData };
B
Benjamin Pasero 已提交
37 38 39
}

export class ResourceLabel extends IconLabel {
40

B
Benjamin Pasero 已提交
41 42 43
	private _onDidRender = this._register(new Emitter<void>());
	get onDidRender(): Event<void> { return this._onDidRender.event; }

B
Benjamin Pasero 已提交
44
	private label: IResourceLabel;
B
Benjamin Pasero 已提交
45
	private options: IResourceLabelOptions;
46 47
	private computedIconClasses: string[];
	private lastKnownConfiguredLangId: string;
B
Benjamin Pasero 已提交
48
	private computedPathLabel: string;
B
Benjamin Pasero 已提交
49 50 51

	constructor(
		container: HTMLElement,
B
Benjamin Pasero 已提交
52
		options: IIconLabelCreationOptions,
B
Benjamin Pasero 已提交
53
		@IExtensionService private extensionService: IExtensionService,
54
		@IConfigurationService private configurationService: IConfigurationService,
55
		@IModeService private modeService: IModeService,
56
		@IModelService private modelService: IModelService,
J
Johannes Rieken 已提交
57
		@IDecorationsService protected decorationsService: IDecorationsService,
I
isidor 已提交
58
		@IThemeService private themeService: IThemeService,
I
isidor 已提交
59
		@ILabelService protected labelService: ILabelService
B
Benjamin Pasero 已提交
60
	) {
B
Benjamin Pasero 已提交
61
		super(container, options);
B
Benjamin Pasero 已提交
62

63 64 65 66
		this.registerListeners();
	}

	private registerListeners(): void {
67

68
		// update when extensions are registered with potentially new languages
B
Benjamin Pasero 已提交
69
		this._register(this.extensionService.onDidRegisterExtensions(() => this.render(true /* clear cache */)));
70 71

		// react to model mode changes
B
Benjamin Pasero 已提交
72
		this._register(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e)));
73 74

		// react to file decoration changes
B
Benjamin Pasero 已提交
75
		this._register(this.decorationsService.onDidChangeDecorations(this.onFileDecorationsChanges, this));
76 77

		// react to theme changes
B
Benjamin Pasero 已提交
78
		this._register(this.themeService.onThemeChange(() => this.render(false)));
79 80

		// react to files.associations changes
B
Benjamin Pasero 已提交
81
		this._register(this.configurationService.onDidChangeConfiguration(e => {
82 83 84 85
			if (e.affectsConfiguration(FILES_ASSOCIATIONS_CONFIG)) {
				this.render(true /* clear cache */);
			}
		}));
86 87
	}

A
Alex Dima 已提交
88
	private onModelModeChanged(e: { model: ITextModel; oldModeId: string; }): void {
89 90 91 92 93 94 95 96
		if (!this.label || !this.label.resource) {
			return; // only update if label exists
		}

		if (!e.model.uri) {
			return; // we need the resource to compare
		}

97
		if (e.model.uri.scheme === Schemas.file && e.oldModeId === PLAINTEXT_MODE_ID) { // todo@remote does this apply?
B
Benjamin Pasero 已提交
98
			return; // ignore transitions in files from no mode to specific mode because this happens each time a model is created
99 100 101 102
		}

		if (e.model.uri.toString() === this.label.resource.toString()) {
			if (this.lastKnownConfiguredLangId !== e.model.getLanguageIdentifier().language) {
B
Benjamin Pasero 已提交
103
				this.render(true); // update if the language id of the model has changed from our last known state
104 105
			}
		}
B
Benjamin Pasero 已提交
106 107
	}

108 109 110 111
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
B
fix npe  
Benjamin Pasero 已提交
112

J
Johannes Rieken 已提交
113
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
114 115 116 117
			this.render(false);
		}
	}

B
Benjamin Pasero 已提交
118
	setLabel(label: IResourceLabel, options?: IResourceLabelOptions): void {
119
		const hasResourceChanged = this.hasResourceChanged(label, options);
120

B
Benjamin Pasero 已提交
121 122 123
		this.label = label;
		this.options = options;

B
Benjamin Pasero 已提交
124 125 126 127
		if (hasResourceChanged) {
			this.computedPathLabel = void 0; // reset path label due to resource change
		}

128 129 130
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
131
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
132 133 134
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

135 136
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
137

138
		if (newFileKind !== oldFileKind) {
139 140 141
			return true; // same resource but different kind (file, folder)
		}

142 143 144 145
		if (newResource && this.computedPathLabel !== this.labelService.getUriLabel(newResource)) {
			return true;
		}

146
		if (newResource && oldResource) {
147
			return newResource.toString() !== oldResource.toString();
148 149 150 151 152 153 154
		}

		if (!newResource && !oldResource) {
			return false;
		}

		return true;
B
Benjamin Pasero 已提交
155 156
	}

B
Benjamin Pasero 已提交
157
	clear(): void {
B
Benjamin Pasero 已提交
158 159
		this.label = void 0;
		this.options = void 0;
160 161
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
162
		this.computedPathLabel = void 0;
B
Benjamin Pasero 已提交
163 164 165 166

		this.setValue();
	}

167 168 169 170 171 172 173 174 175 176 177 178 179
	private render(clearIconCache: boolean): void {
		if (this.label) {
			const configuredLangId = getConfiguredLangId(this.modelService, this.label.resource);
			if (this.lastKnownConfiguredLangId !== configuredLangId) {
				clearIconCache = true;
				this.lastKnownConfiguredLangId = configuredLangId;
			}
		}

		if (clearIconCache) {
			this.computedIconClasses = void 0;
		}

B
Benjamin Pasero 已提交
180 181 182 183
		if (!this.label) {
			return;
		}

184
		const iconLabelOptions: IIconLabelValueOptions = {
185 186 187
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
S
Sandeep Somavarapu 已提交
188
			extraClasses: []
189 190
		};

B
Benjamin Pasero 已提交
191
		const resource = this.label.resource;
B
fix npe  
Benjamin Pasero 已提交
192
		const label = this.label.name;
193

194
		if (this.options && typeof this.options.title === 'string') {
195
			iconLabelOptions.title = this.options.title;
196
		} else if (resource && resource.scheme !== Schemas.data /* do not accidentally inline Data URIs */) {
B
Benjamin Pasero 已提交
197
			if (!this.computedPathLabel) {
I
isidor 已提交
198
				this.computedPathLabel = this.labelService.getUriLabel(resource);
B
Benjamin Pasero 已提交
199 200 201
			}

			iconLabelOptions.title = this.computedPathLabel;
B
Benjamin Pasero 已提交
202 203
		}

204 205 206 207 208
		if (this.options && !this.options.hideIcon) {
			if (!this.computedIconClasses) {
				this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
			}
			iconLabelOptions.extraClasses = this.computedIconClasses.slice(0);
209
		}
B
Benjamin Pasero 已提交
210
		if (this.options && this.options.extraClasses) {
211
			iconLabelOptions.extraClasses.push(...this.options.extraClasses);
B
Benjamin Pasero 已提交
212 213
		}

B
fix npe  
Benjamin Pasero 已提交
214
		if (this.options && this.options.fileDecorations && resource) {
B
Benjamin Pasero 已提交
215
			const deco = this.decorationsService.getDecoration(
J
Johannes Rieken 已提交
216
				resource,
217 218
				this.options.fileKind !== FileKind.FILE,
				this.options.fileDecorations.data
J
Johannes Rieken 已提交
219
			);
B
fix npe  
Benjamin Pasero 已提交
220

221
			if (deco) {
222
				if (deco.tooltip) {
223
					iconLabelOptions.title = `${iconLabelOptions.title}${deco.tooltip}`;
224
				}
B
Benjamin Pasero 已提交
225

226 227 228
				if (this.options.fileDecorations.colors) {
					iconLabelOptions.extraClasses.push(deco.labelClassName);
				}
B
Benjamin Pasero 已提交
229

230 231 232
				if (this.options.fileDecorations.badges) {
					iconLabelOptions.extraClasses.push(deco.badgeClassName);
				}
J
Johannes Rieken 已提交
233 234
			}
		}
B
Benjamin Pasero 已提交
235

236
		this.setValue(label, this.label.description, iconLabelOptions);
B
Benjamin Pasero 已提交
237

238
		this._onDidRender.fire();
B
Benjamin Pasero 已提交
239 240
	}

B
Benjamin Pasero 已提交
241
	dispose(): void {
B
Benjamin Pasero 已提交
242 243
		super.dispose();

244 245
		this.label = void 0;
		this.options = void 0;
246 247
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
248
		this.computedPathLabel = void 0;
249
	}
B
Benjamin Pasero 已提交
250 251 252 253
}

export class EditorLabel extends ResourceLabel {

B
Benjamin Pasero 已提交
254
	setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
B
Benjamin Pasero 已提交
255
		this.setLabel({
256
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
257 258 259 260 261 262 263
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
264
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
265 266 267 268 269
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

270 271 272 273
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
I
isidor 已提交
274
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
275 276 277
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
J
Johannes Rieken 已提交
278
		@IDecorationsService decorationsService: IDecorationsService,
279 280
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
I
isidor 已提交
281
		@ILabelService labelService: ILabelService
282
	) {
I
isidor 已提交
283
		super(container, options, extensionService, configurationService, modeService, modelService, decorationsService, themeService, labelService);
284 285
	}

B
Benjamin Pasero 已提交
286
	setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
287
		const hideLabel = options && options.hideLabel;
288
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
289 290 291 292 293 294 295 296 297
		if (!hideLabel) {
			if (options && options.fileKind === FileKind.ROOT_FOLDER) {
				const workspaceFolder = this.contextService.getWorkspaceFolder(resource);
				if (workspaceFolder) {
					name = workspaceFolder.name;
				}
			}

			if (!name) {
B
Benjamin Pasero 已提交
298
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
299
			}
300 301
		}

B
Benjamin Pasero 已提交
302
		let description: string;
B
Benjamin Pasero 已提交
303
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
304
		if (!hidePath) {
305
			description = this.labelService.getUriLabel(resources.dirname(resource), { relative: true });
J
Johannes Rieken 已提交
306
		}
B
Benjamin Pasero 已提交
307

B
Benjamin Pasero 已提交
308
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
309
	}
B
Benjamin Pasero 已提交
310 311
}

312
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
313 314

	// we always set these base classes even if we do not have a path
315
	const classes = fileKind === FileKind.ROOT_FOLDER ? ['rootfolder-icon'] : fileKind === FileKind.FOLDER ? ['folder-icon'] : ['file-icon'];
316

I
isidor 已提交
317
	if (resource) {
B
Benjamin Pasero 已提交
318

B
Benjamin Pasero 已提交
319
		// Get the path and name of the resource. For data-URIs, we need to parse specially
B
Benjamin Pasero 已提交
320
		let name: string;
B
Benjamin Pasero 已提交
321
		let path: string;
B
Benjamin Pasero 已提交
322 323 324
		if (resource.scheme === Schemas.data) {
			const metadata = DataUri.parseMetaData(resource);
			name = metadata.get(DataUri.META_DATA_LABEL);
B
Benjamin Pasero 已提交
325
			path = name;
B
Benjamin Pasero 已提交
326 327
		} else {
			name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
328
			path = resource.path.toLowerCase();
B
Benjamin Pasero 已提交
329
		}
B
Benjamin Pasero 已提交
330

331
		// Folders
332
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
333
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
334 335
		}

336 337
		// Files
		else {
338

B
Benjamin Pasero 已提交
339 340 341
			// Name & Extension(s)
			if (name) {
				classes.push(`${name}-name-file-icon`);
B
Benjamin Pasero 已提交
342

B
Benjamin Pasero 已提交
343 344 345 346 347 348
				const dotSegments = name.split('.');
				for (let i = 1; i < dotSegments.length; i++) {
					classes.push(`${dotSegments.slice(i).join('.')}-ext-file-icon`); // add each combination of all found extensions if more than one
				}

				classes.push(`ext-file-icon`); // extra segment to increase file-ext score
349 350
			}

351
			// Configured Language
352
			let configuredLangId = getConfiguredLangId(modelService, resource);
B
Benjamin Pasero 已提交
353
			configuredLangId = configuredLangId || modeService.getModeIdByFilepathOrFirstLine(path);
354 355
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
356
			}
B
Benjamin Pasero 已提交
357 358
		}
	}
B
fix npe  
Benjamin Pasero 已提交
359

B
Benjamin Pasero 已提交
360 361 362
	return classes;
}

363 364 365 366 367
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
368
			const modeId = model.getLanguageIdentifier().language;
369 370 371 372 373 374 375 376 377
			if (modeId && modeId !== PLAINTEXT_MODE_ID) {
				configuredLangId = modeId; // only take if the mode is specific (aka no just plain text)
			}
		}
	}

	return configuredLangId;
}

B
Benjamin Pasero 已提交
378 379
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
380
}