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

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

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

export class ResourceLabel extends IconLabel {
42

43
	private toDispose: IDisposable[];
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 52
	private _onDidRender = new Emitter<void>();
	readonly onDidRender: Event<void> = this._onDidRender.event;

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

67 68 69 70 71 72
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners(): void {
73

74 75
		// update when extensions are registered with potentially new languages
		this.toDispose.push(this.extensionService.onDidRegisterExtensions(() => this.render(true /* clear cache */)));
76 77 78 79 80 81 82 83

		// react to model mode changes
		this.toDispose.push(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e)));

		// react to file decoration changes
		this.toDispose.push(this.decorationsService.onDidChangeDecorations(this.onFileDecorationsChanges, this));

		// react to theme changes
J
Johannes Rieken 已提交
84
		this.toDispose.push(this.themeService.onThemeChange(() => this.render(false)));
85 86 87 88 89 90 91

		// react to files.associations changes
		this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration(FILES_ASSOCIATIONS_CONFIG)) {
				this.render(true /* clear cache */);
			}
		}));
92 93
	}

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

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

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

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

114 115 116 117
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
B
fix npe  
Benjamin Pasero 已提交
118

J
Johannes Rieken 已提交
119
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
120 121 122 123
			this.render(false);
		}
	}

B
Benjamin Pasero 已提交
124
	public setLabel(label: IResourceLabel, options?: IResourceLabelOptions): void {
125
		const hasResourceChanged = this.hasResourceChanged(label, options);
126

B
Benjamin Pasero 已提交
127 128 129
		this.label = label;
		this.options = options;

B
Benjamin Pasero 已提交
130 131 132 133
		if (hasResourceChanged) {
			this.computedPathLabel = void 0; // reset path label due to resource change
		}

134 135 136
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
137
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
138 139 140
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

141 142
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
143

144
		if (newFileKind !== oldFileKind) {
145 146 147
			return true; // same resource but different kind (file, folder)
		}

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

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

		return true;
B
Benjamin Pasero 已提交
157 158 159 160 161
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
162 163
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
164
		this.computedPathLabel = void 0;
B
Benjamin Pasero 已提交
165 166 167 168

		this.setValue();
	}

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

186
		const iconLabelOptions: IIconLabelValueOptions = {
187 188 189 190 191
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
		};

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

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

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

205
		if (!this.computedIconClasses) {
206
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
207 208
		}

209
		iconLabelOptions.extraClasses = this.computedIconClasses.slice(0);
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
	}

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

272 273 274 275 276 277 278 279 280
	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 已提交
281
		@IDecorationsService decorationsService: IDecorationsService,
282 283
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
284
	) {
285
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
286 287
	}

B
Benjamin Pasero 已提交
288
	public setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
289
		const hideLabel = options && options.hideLabel;
290
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
291 292 293 294 295 296 297 298 299
		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 已提交
300
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
301
			}
302 303
		}

B
Benjamin Pasero 已提交
304
		let description: string;
B
Benjamin Pasero 已提交
305
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
306
		if (!hidePath) {
B
Benjamin Pasero 已提交
307
			let rootProvider: IWorkspaceFolderProvider;
308 309 310 311 312 313 314 315
			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 已提交
316 317

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

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

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

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

I
isidor 已提交
329
	if (resource) {
I
isidor 已提交
330
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
331

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

337 338
		// Files
		else {
339 340

			// Name
I
isidor 已提交
341
			classes.push(`${name}-name-file-icon`);
B
Benjamin Pasero 已提交
342

343
			// Extension(s)
I
isidor 已提交
344
			const dotSegments = name.split('.');
M
Martin Aeschlimann 已提交
345 346
			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
347
			}
348
			classes.push(`ext-file-icon`); // extra segment to increase file-ext score
349

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

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

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