labels.ts 13.0 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';
S
Sandeep Somavarapu 已提交
14
import { getPathLabel, IWorkspaceFolderProvider } from 'vs/base/common/labels';
J
Johannes Rieken 已提交
15 16 17 18
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';
19
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
20
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
21
import { IDecorationsService, IResourceDecorationChangeEvent, IDecorationData } from 'vs/workbench/services/decorations/browser/decorations';
B
Benjamin Pasero 已提交
22
import { Schemas } from 'vs/base/common/network';
23
import { FileKind, FILES_ASSOCIATIONS_CONFIG } from 'vs/platform/files/common/files';
A
Alex Dima 已提交
24
import { ITextModel } from 'vs/editor/common/model';
25
import { IThemeService } from 'vs/platform/theme/common/themeService';
M
Matt Bierner 已提交
26
import { Event, Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
27
import { DataUri } from 'vs/workbench/common/resources';
B
Benjamin Pasero 已提交
28

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

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

export class ResourceLabel extends IconLabel {
41

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

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

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

65 66 67 68
		this.registerListeners();
	}

	private registerListeners(): void {
69

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

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

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

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
123 124 125
		this.label = label;
		this.options = options;

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

130 131 132
		this.render(hasResourceChanged);
	}

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

137 138
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
139

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

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

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

		return true;
B
Benjamin Pasero 已提交
153 154
	}

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

		this.setValue();
	}

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

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

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

192
		if (this.options && typeof this.options.title === 'string') {
193
			iconLabelOptions.title = this.options.title;
194
		} else if (resource && resource.scheme !== Schemas.data /* do not accidentally inline Data URIs */) {
B
Benjamin Pasero 已提交
195
			if (!this.computedPathLabel) {
196 197
				const rootProvider = resource.scheme !== Schemas.file ? this.contextService : undefined;
				this.computedPathLabel = getPathLabel(resource, this.environmentService, rootProvider);
B
Benjamin Pasero 已提交
198 199 200
			}

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

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

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

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

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

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

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

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

270 271 272 273 274 275 276 277 278
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IEnvironmentService environmentService: IEnvironmentService,
J
Johannes Rieken 已提交
279
		@IDecorationsService decorationsService: IDecorationsService,
280 281
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
282
	) {
283
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
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) {
B
Benjamin Pasero 已提交
305
			let rootProvider: IWorkspaceFolderProvider;
306 307 308 309 310 311 312 313
			if (options && options.root) {
				rootProvider = {
					getWorkspaceFolder(): { uri } { return { uri: options.root }; },
					getWorkspace(): { folders: { uri: uri }[]; } { return { folders: [{ uri: options.root }] }; },
				};
			} else {
				rootProvider = this.contextService;
			}
B
Benjamin Pasero 已提交
314

315
			description = getPathLabel(resources.dirname(resource), this.environmentService, rootProvider);
J
Johannes Rieken 已提交
316
		}
B
Benjamin Pasero 已提交
317

B
Benjamin Pasero 已提交
318
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
319
	}
B
Benjamin Pasero 已提交
320 321
}

322
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
323 324

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

I
isidor 已提交
327
	if (resource) {
B
Benjamin Pasero 已提交
328 329 330 331 332 333 334 335 336

		// 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 已提交
337

338
		// Folders
339
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
340
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
341 342
		}

343 344
		// Files
		else {
345

B
Benjamin Pasero 已提交
346 347 348
			// Name & Extension(s)
			if (name) {
				classes.push(`${name}-name-file-icon`);
B
Benjamin Pasero 已提交
349

B
Benjamin Pasero 已提交
350 351 352 353 354 355
				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
356 357
			}

358
			// Configured Language
359
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
360
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
361 362
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
363
			}
B
Benjamin Pasero 已提交
364 365
		}
	}
B
fix npe  
Benjamin Pasero 已提交
366

B
Benjamin Pasero 已提交
367 368 369
	return classes;
}

370 371 372 373 374
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
375
			const modeId = model.getLanguageIdentifier().language;
376 377 378 379 380 381 382 383 384
			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 已提交
385 386
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
387
}