labels.ts 11.9 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';
J
Johannes Rieken 已提交
23
import { IResourceDecorationsService, 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;
J
Johannes Rieken 已提交
39
	fileDecorations?: 'mine' | 'all';
B
Benjamin Pasero 已提交
40 41 42
}

export class ResourceLabel extends IconLabel {
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

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

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

		this.registerListeners();
	}

	private registerListeners(): void {
69 70
		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 已提交
71
		this.toDispose.push(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e))); // react to model mode changes
72
		this.toDispose.push(this.decorationsService.onDidChangeDecorations(this.onFileDecorationsChanges, this)); // react to file decoration changes
J
Johannes Rieken 已提交
73
		this.toDispose.push(this.themeService.onThemeChange(() => this.render(false)));
74 75 76 77 78 79 80 81 82 83 84
	}

	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 已提交
85 86
		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
87 88 89 90
		}

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

96 97 98 99
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
J
Johannes Rieken 已提交
100
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
101 102 103 104
			this.render(false);
		}
	}

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

B
Benjamin Pasero 已提交
108 109 110
		this.label = label;
		this.options = options;

111 112 113
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
114
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
115 116 117
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

118 119
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
120

121
		if (newFileKind !== oldFileKind) {
122 123 124
			return true; // same resource but different kind (file, folder)
		}

125
		if (newResource && oldResource) {
126
			return newResource.toString() !== oldResource.toString();
127 128 129 130 131 132 133
		}

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

		return true;
B
Benjamin Pasero 已提交
134 135 136 137 138
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
139 140
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
141 142 143 144

		this.setValue();
	}

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

		const resource = this.label.resource;
J
Johannes Rieken 已提交
163
		let label = this.label.name;
B
Benjamin Pasero 已提交
164 165

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

172
		if (!this.computedIconClasses) {
173
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
174 175 176
		}

		let extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
177 178 179 180
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

J
Johannes Rieken 已提交
181 182 183
		const italic = this.options && this.options.italic;
		const matches = this.options && this.options.matches;

J
Johannes Rieken 已提交
184 185


J
Johannes Rieken 已提交
186
		let color: Color;
J
Johannes Rieken 已提交
187
		let extraIcon: uri;
J
Johannes Rieken 已提交
188 189 190 191 192
		if (this.options && this.options.fileDecorations) {
			let deco = this.decorationsService.getTopDecoration(
				resource,
				this.options.fileDecorations === 'all'
			);
193

J
Johannes Rieken 已提交
194 195
			if (deco) {
				color = this.themeService.getTheme().getColor(deco.color);
J
Johannes Rieken 已提交
196 197 198 199 200

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

J
Johannes Rieken 已提交
201 202
				if (deco.icon) {
					const { type } = this.themeService.getTheme();
J
Johannes Rieken 已提交
203
					extraIcon = type === 'light' ? deco.icon.light : deco.icon.dark;
J
Johannes Rieken 已提交
204
				}
J
Johannes Rieken 已提交
205 206
			}
		}
B
Benjamin Pasero 已提交
207

J
Johannes Rieken 已提交
208
		this.setValue(label, this.label.description, {
J
Johannes Rieken 已提交
209 210 211 212
			title,
			extraClasses,
			italic,
			matches,
J
Johannes Rieken 已提交
213 214
			color,
			extraIcon
J
Johannes Rieken 已提交
215
		});
B
Benjamin Pasero 已提交
216 217
	}

218
	public dispose(): void {
B
Benjamin Pasero 已提交
219 220
		super.dispose();

221 222 223
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
224 225
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
226
	}
B
Benjamin Pasero 已提交
227 228 229 230 231 232
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
233
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
234 235 236 237 238 239 240
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
241
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
242
	hidePath?: boolean;
243
	root?: uri;
B
Benjamin Pasero 已提交
244 245 246 247
}

export class FileLabel extends ResourceLabel {

248 249 250 251 252 253 254 255 256
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IEnvironmentService environmentService: IEnvironmentService,
257 258 259
		@IResourceDecorationsService decorationsService: IResourceDecorationsService,
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
260
	) {
261
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
262 263
	}

B
Benjamin Pasero 已提交
264
	public setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
265
		const hideLabel = options && options.hideLabel;
266
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
267 268 269 270 271 272 273 274 275
		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 已提交
276
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
277
			}
278 279
		}

B
Benjamin Pasero 已提交
280
		let description: string;
B
Benjamin Pasero 已提交
281
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
282
		if (!hidePath) {
B
Benjamin Pasero 已提交
283
			let rootProvider: IWorkspaceFolderProvider;
284 285 286 287 288 289 290 291
			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 已提交
292 293

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

B
Benjamin Pasero 已提交
296
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
297
	}
B
Benjamin Pasero 已提交
298 299
}

300
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
301 302

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

B
Benjamin Pasero 已提交
305

I
isidor 已提交
306
	if (resource) {
I
isidor 已提交
307
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
308

309
		// Folders
310
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
311
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
312 313
		}

314 315
		// Files
		else {
316 317

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

320
			// Extension(s)
I
isidor 已提交
321
			const dotSegments = name.split('.');
M
Martin Aeschlimann 已提交
322 323
			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
324
			}
325
			classes.push(`ext-file-icon`); // extra segment to increase file-ext score
326

327
			// Configured Language
328
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
329
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
330 331
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
332
			}
B
Benjamin Pasero 已提交
333 334 335 336 337
		}
	}
	return classes;
}

338 339 340 341 342
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
343
			const modeId = model.getLanguageIdentifier().language;
344 345 346 347 348 349 350 351 352
			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 已提交
353 354
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
355
}