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

'use strict';

import 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
		if (newResource && oldResource) {
143
			return newResource.toString() !== oldResource.toString();
144 145 146 147 148 149 150
		}

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

		return true;
B
Benjamin Pasero 已提交
151 152
	}

B
Benjamin Pasero 已提交
153
	clear(): void {
B
Benjamin Pasero 已提交
154 155
		this.label = void 0;
		this.options = void 0;
156 157
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
158
		this.computedPathLabel = void 0;
B
Benjamin Pasero 已提交
159 160 161 162

		this.setValue();
	}

163 164 165 166 167 168 169 170 171 172 173 174 175
	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 已提交
176 177 178 179
		if (!this.label) {
			return;
		}

180
		const iconLabelOptions: IIconLabelValueOptions = {
181 182 183
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
S
Sandeep Somavarapu 已提交
184
			extraClasses: []
185 186
		};

B
Benjamin Pasero 已提交
187
		const resource = this.label.resource;
B
fix npe  
Benjamin Pasero 已提交
188
		const label = this.label.name;
189

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

			iconLabelOptions.title = this.computedPathLabel;
B
Benjamin Pasero 已提交
198 199
		}

200 201 202 203 204
		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);
205
		}
B
Benjamin Pasero 已提交
206
		if (this.options && this.options.extraClasses) {
207
			iconLabelOptions.extraClasses.push(...this.options.extraClasses);
B
Benjamin Pasero 已提交
208 209
		}

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

217
			if (deco) {
218
				if (deco.tooltip) {
219
					iconLabelOptions.title = `${iconLabelOptions.title}${deco.tooltip}`;
220
				}
B
Benjamin Pasero 已提交
221

222 223 224
				if (this.options.fileDecorations.colors) {
					iconLabelOptions.extraClasses.push(deco.labelClassName);
				}
B
Benjamin Pasero 已提交
225

226 227 228
				if (this.options.fileDecorations.badges) {
					iconLabelOptions.extraClasses.push(deco.badgeClassName);
				}
J
Johannes Rieken 已提交
229 230
			}
		}
B
Benjamin Pasero 已提交
231

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

234
		this._onDidRender.fire();
B
Benjamin Pasero 已提交
235 236
	}

B
Benjamin Pasero 已提交
237
	dispose(): void {
B
Benjamin Pasero 已提交
238 239
		super.dispose();

240 241
		this.label = void 0;
		this.options = void 0;
242 243
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
244
		this.computedPathLabel = void 0;
245
	}
B
Benjamin Pasero 已提交
246 247 248 249
}

export class EditorLabel extends ResourceLabel {

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

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
260
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
261 262 263 264 265
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

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

B
Benjamin Pasero 已提交
282
	setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
283
		const hideLabel = options && options.hideLabel;
284
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
285 286 287 288 289 290 291 292 293
		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 已提交
294
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
295
			}
296 297
		}

B
Benjamin Pasero 已提交
298
		let description: string;
B
Benjamin Pasero 已提交
299
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
300
		if (!hidePath) {
I
isidor 已提交
301
			description = this.labelService.getUriLabel(resources.dirname(resource), true);
J
Johannes Rieken 已提交
302
		}
B
Benjamin Pasero 已提交
303

B
Benjamin Pasero 已提交
304
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
305
	}
B
Benjamin Pasero 已提交
306 307
}

308
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
309 310

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

I
isidor 已提交
313
	if (resource) {
B
Benjamin Pasero 已提交
314 315 316 317 318 319 320 321 322

		// Get the name of the resource. For data-URIs, we need to parse specially
		let name: string;
		if (resource.scheme === Schemas.data) {
			const metadata = DataUri.parseMetaData(resource);
			name = metadata.get(DataUri.META_DATA_LABEL);
		} else {
			name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
		}
B
Benjamin Pasero 已提交
323

324
		// Folders
325
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
326
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
327 328
		}

329 330
		// Files
		else {
331

B
Benjamin Pasero 已提交
332 333 334
			// Name & Extension(s)
			if (name) {
				classes.push(`${name}-name-file-icon`);
B
Benjamin Pasero 已提交
335

B
Benjamin Pasero 已提交
336 337 338 339 340 341
				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
342 343
			}

344
			// Configured Language
345
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
346
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
347 348
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
349
			}
B
Benjamin Pasero 已提交
350 351
		}
	}
B
fix npe  
Benjamin Pasero 已提交
352

B
Benjamin Pasero 已提交
353 354 355
	return classes;
}

356 357 358 359 360
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
361
			const modeId = model.getLanguageIdentifier().language;
362 363 364 365 366 367 368 369 370
			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 已提交
371 372
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
373
}