labels.ts 12.2 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');
J
Johannes Rieken 已提交
10 11 12 13
import { IconLabel, IIconLabelOptions, IIconLabelCreationOptions } from 'vs/base/browser/ui/iconLabel/iconLabel';
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
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 { IResourceDecorationsService, IResourceDecoration, IResourceDecorationChangeEvent } from 'vs/workbench/services/decorations/browser/decorations';
B
Benjamin Pasero 已提交
24 25
import { Schemas } from 'vs/base/common/network';
import { FileKind } from 'vs/platform/files/common/files';
26
import { IModel } from 'vs/editor/common/editorCommon';
27
import { IThemeService } from 'vs/platform/theme/common/themeService';
J
Johannes Rieken 已提交
28
import { Color } from 'vs/base/common/color';
J
Johannes Rieken 已提交
29
import { localize } from 'vs/nls';
B
Benjamin Pasero 已提交
30

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

export interface IResourceLabelOptions extends IIconLabelOptions {
38
	fileKind?: FileKind;
39 40
	showDecorations?: boolean;
	showAllDecorations?: boolean;
B
Benjamin Pasero 已提交
41 42 43
}

export class ResourceLabel extends IconLabel {
44
	private toDispose: IDisposable[];
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 50 51

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

64 65 66 67 68 69
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners(): void {
70 71
		this.extensionService.onReady().then(() => this.render(true /* clear cache */)); // update when extensions are loaded with potentially new languages
		this.toDispose.push(this.configurationService.onDidUpdateConfiguration(() => this.render(true /* clear cache */))); // update when file.associations change
B
Benjamin Pasero 已提交
72
		this.toDispose.push(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e))); // react to model mode changes
73
		this.toDispose.push(this.decorationsService.onDidChangeDecorations(this.onFileDecorationsChanges, this)); // react to file decoration changes
J
Johannes Rieken 已提交
74
		this.toDispose.push(this.themeService.onThemeChange(() => this.render(false)));
75 76 77 78 79 80 81 82 83 84 85
	}

	private onModelModeChanged(e: { model: IModel; oldModeId: string; }): void {
		if (!this.label || !this.label.resource) {
			return; // only update if label exists
		}

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

B
Benjamin Pasero 已提交
86 87
		if (e.model.uri.scheme === Schemas.file && e.oldModeId === PLAINTEXT_MODE_ID) {
			return; // ignore transitions in files from no mode to specific mode because this happens each time a model is created
88 89 90 91
		}

		if (e.model.uri.toString() === this.label.resource.toString()) {
			if (this.lastKnownConfiguredLangId !== e.model.getLanguageIdentifier().language) {
B
Benjamin Pasero 已提交
92
				this.render(true); // update if the language id of the model has changed from our last known state
93 94
			}
		}
B
Benjamin Pasero 已提交
95 96
	}

97 98 99 100 101 102 103 104 105 106 107 108
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
		if (!this.options.showAllDecorations && !this.options.showDecorations) {
			return;
		}
		if (e.affectsResource(this.label.resource)) {
			this.render(false);
		}
	}

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

B
Benjamin Pasero 已提交
112 113 114
		this.label = label;
		this.options = options;

115 116 117
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
118
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
119 120 121
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

122 123
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
124

125
		if (newFileKind !== oldFileKind) {
126 127 128
			return true; // same resource but different kind (file, folder)
		}

129
		if (newResource && oldResource) {
130
			return newResource.toString() !== oldResource.toString();
131 132 133 134 135 136 137
		}

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

		return true;
B
Benjamin Pasero 已提交
138 139 140 141 142
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
143 144
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
145 146 147 148

		this.setValue();
	}

149 150 151 152 153 154 155 156 157 158 159 160 161
	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 已提交
162 163 164 165 166
		if (!this.label) {
			return;
		}

		const resource = this.label.resource;
J
Johannes Rieken 已提交
167
		let label = this.label.name;
B
Benjamin Pasero 已提交
168 169

		let title = '';
170
		if (this.options && typeof this.options.title === 'string') {
B
Benjamin Pasero 已提交
171 172
			title = this.options.title;
		} else if (resource) {
I
isidor 已提交
173
			title = getPathLabel(resource, void 0, this.environmentService);
B
Benjamin Pasero 已提交
174 175
		}

176
		if (!this.computedIconClasses) {
177
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
178 179 180
		}

		let extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
181 182 183 184
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

J
Johannes Rieken 已提交
185 186 187 188
		const italic = this.options && this.options.italic;
		const matches = this.options && this.options.matches;

		let color: Color;
189
		if (this.options) {
J
Johannes Rieken 已提交
190
			let deco: IResourceDecoration;
191 192 193 194 195 196
			if (this.options.showDecorations) {
				deco = this.decorationsService.getTopDecoration(resource, false);
			} else if (this.options.showAllDecorations) {
				deco = this.decorationsService.getTopDecoration(resource, true);
			}

J
Johannes Rieken 已提交
197 198
			if (deco) {
				color = this.themeService.getTheme().getColor(deco.color);
J
Johannes Rieken 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

				if (deco.tooltip) {
					title = localize('deco.tooltip', "{0}, {1}", title, deco.tooltip);
				}

				if (deco.prefix) {
					label += deco.prefix;
					if (matches) {
						matches.forEach(match => {
							match.start += deco.prefix.length;
							match.end += deco.prefix.length;
						});
					}
				}

				if (deco.suffix) {
					label += deco.suffix;
				}
J
Johannes Rieken 已提交
217 218
			}
		}
B
Benjamin Pasero 已提交
219

J
Johannes Rieken 已提交
220
		this.setValue(label, this.label.description, {
J
Johannes Rieken 已提交
221 222 223 224 225 226
			title,
			extraClasses,
			italic,
			matches,
			color
		});
B
Benjamin Pasero 已提交
227 228
	}

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

259 260 261 262 263 264 265 266 267
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IEnvironmentService environmentService: IEnvironmentService,
268 269 270
		@IResourceDecorationsService decorationsService: IResourceDecorationsService,
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
271
	) {
272
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
273 274
	}

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

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

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

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

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

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

B
Benjamin Pasero 已提交
316

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

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

325 326
		// Files
		else {
327 328

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

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

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

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