labels.ts 12.4 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';
I
isidor 已提交
9
import resources = require('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';
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

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;

129 130 131
		this.render(hasResourceChanged);
	}

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

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

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

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

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

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

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
157 158
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = 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 184 185
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
		};

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

189
		if (this.options && typeof this.options.title === 'string') {
190
			iconLabelOptions.title = this.options.title;
191
		} else if (resource && resource.scheme !== Schemas.data /* do not accidentally inline Data URIs */) {
192
			iconLabelOptions.title = getPathLabel(resource, void 0, this.environmentService);
B
Benjamin Pasero 已提交
193 194
		}

195
		if (!this.computedIconClasses) {
196
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
197 198
		}

199
		iconLabelOptions.extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
200
		if (this.options && this.options.extraClasses) {
201
			iconLabelOptions.extraClasses.push(...this.options.extraClasses);
B
Benjamin Pasero 已提交
202 203
		}

B
fix npe  
Benjamin Pasero 已提交
204
		if (this.options && this.options.fileDecorations && resource) {
J
Johannes Rieken 已提交
205
			let deco = this.decorationsService.getDecoration(
J
Johannes Rieken 已提交
206
				resource,
207 208
				this.options.fileKind !== FileKind.FILE,
				this.options.fileDecorations.data
J
Johannes Rieken 已提交
209
			);
B
fix npe  
Benjamin Pasero 已提交
210

211
			if (deco) {
212
				if (deco.tooltip) {
213
					iconLabelOptions.title = `${iconLabelOptions.title}${deco.tooltip}`;
214 215 216 217 218 219 220
				}
				if (this.options.fileDecorations.colors) {
					iconLabelOptions.extraClasses.push(deco.labelClassName);
				}
				if (this.options.fileDecorations.badges) {
					iconLabelOptions.extraClasses.push(deco.badgeClassName);
				}
J
Johannes Rieken 已提交
221 222
			}
		}
B
Benjamin Pasero 已提交
223

224
		this.setValue(label, this.label.description, iconLabelOptions);
225
		this._onDidRender.fire();
B
Benjamin Pasero 已提交
226 227
	}

228
	public dispose(): void {
B
Benjamin Pasero 已提交
229 230
		super.dispose();

231 232 233
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
234 235
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
236
	}
B
Benjamin Pasero 已提交
237 238 239 240 241 242
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
243
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
244 245 246 247 248 249 250
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
251
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
252
	hidePath?: boolean;
253
	root?: uri;
B
Benjamin Pasero 已提交
254 255 256 257
}

export class FileLabel extends ResourceLabel {

258 259 260 261 262 263 264 265 266
	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 已提交
267
		@IDecorationsService decorationsService: IDecorationsService,
268 269
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
270
	) {
271
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
272 273
	}

B
Benjamin Pasero 已提交
274
	public setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
275
		const hideLabel = options && options.hideLabel;
276
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
277 278 279 280 281 282 283 284 285
		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 已提交
286
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
287
			}
288 289
		}

B
Benjamin Pasero 已提交
290
		let description: string;
B
Benjamin Pasero 已提交
291
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
292
		if (!hidePath) {
B
Benjamin Pasero 已提交
293
			let rootProvider: IWorkspaceFolderProvider;
294 295 296 297 298 299 300 301
			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 已提交
302 303

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

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

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

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

I
isidor 已提交
315
	if (resource) {
I
isidor 已提交
316
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
317

318
		// Folders
319
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
320
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
321 322
		}

323 324
		// Files
		else {
325 326

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

329
			// Extension(s)
I
isidor 已提交
330
			const dotSegments = name.split('.');
M
Martin Aeschlimann 已提交
331 332
			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
333
			}
334
			classes.push(`ext-file-icon`); // extra segment to increase file-ext score
335

336
			// Configured Language
337
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
338
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
339 340
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
341
			}
B
Benjamin Pasero 已提交
342 343
		}
	}
B
fix npe  
Benjamin Pasero 已提交
344

B
Benjamin Pasero 已提交
345 346 347
	return classes;
}

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