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
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 19
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';
20
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
21
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
22
import { IDecorationsService, IResourceDecorationChangeEvent, IDecorationData } from 'vs/workbench/services/decorations/browser/decorations';
B
Benjamin Pasero 已提交
23
import { Schemas } from 'vs/base/common/network';
24
import { FileKind, FILES_ASSOCIATIONS_CONFIG } from 'vs/platform/files/common/files';
A
Alex Dima 已提交
25
import { ITextModel } from 'vs/editor/common/model';
26
import { IThemeService } from 'vs/platform/theme/common/themeService';
M
Matt Bierner 已提交
27
import { Event, Emitter } from 'vs/base/common/event';
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

42
	private toDispose: IDisposable[];
B
Benjamin Pasero 已提交
43
	private label: IResourceLabel;
B
Benjamin Pasero 已提交
44
	private options: IResourceLabelOptions;
45 46
	private computedIconClasses: string[];
	private lastKnownConfiguredLangId: string;
B
Benjamin Pasero 已提交
47
	private computedPathLabel: string;
B
Benjamin Pasero 已提交
48

49 50 51
	private _onDidRender = new Emitter<void>();
	readonly onDidRender: Event<void> = this._onDidRender.event;

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

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

		this.registerListeners();
	}

	private registerListeners(): void {
72

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

		// 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 已提交
83
		this.toDispose.push(this.themeService.onThemeChange(() => this.render(false)));
84 85 86 87 88 89 90

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

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

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

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

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

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

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

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

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

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

133 134 135
		this.render(hasResourceChanged);
	}

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

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

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

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

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

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

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

		this.setValue();
	}

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

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

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 198 199 200 201
			if (!this.computedPathLabel) {
				this.computedPathLabel = getPathLabel(resource, void 0, this.environmentService);
			}

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

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

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

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

243 244 245
		this.toDispose = dispose(this.toDispose);
		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 254 255
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		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
	hidePath?: boolean;
266
	root?: uri;
B
Benjamin Pasero 已提交
267 268 269 270
}

export class FileLabel extends ResourceLabel {

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

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

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

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

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

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

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

I
isidor 已提交
328
	if (resource) {
I
isidor 已提交
329
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
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 339

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

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

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

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

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