labels.ts 11.8 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 { IDecorationsService, 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';
B
Benjamin Pasero 已提交
28

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

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

export class ResourceLabel extends IconLabel {
41
	private toDispose: IDisposable[];
B
Benjamin Pasero 已提交
42
	private label: IResourceLabel;
B
Benjamin Pasero 已提交
43
	private options: IResourceLabelOptions;
44 45
	private computedIconClasses: string[];
	private lastKnownConfiguredLangId: string;
B
Benjamin Pasero 已提交
46 47 48

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

61 62 63 64 65 66
		this.toDispose = [];

		this.registerListeners();
	}

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

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

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

94 95 96 97
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
B
fix npe  
Benjamin Pasero 已提交
98

J
Johannes Rieken 已提交
99
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
100 101 102 103
			this.render(false);
		}
	}

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

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

110 111 112
		this.render(hasResourceChanged);
	}

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

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

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

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

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

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

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

		this.setValue();
	}

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

161 162 163 164 165 166
		const iconLabelOptions: IIconLabelOptions = {
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
		};

B
Benjamin Pasero 已提交
167
		const resource = this.label.resource;
B
fix npe  
Benjamin Pasero 已提交
168
		const label = this.label.name;
169

170
		if (this.options && typeof this.options.title === 'string') {
171
			iconLabelOptions.title = this.options.title;
B
Benjamin Pasero 已提交
172
		} else if (resource) {
173
			iconLabelOptions.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
		iconLabelOptions.extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
181
		if (this.options && this.options.extraClasses) {
182
			iconLabelOptions.extraClasses.push(...this.options.extraClasses);
B
Benjamin Pasero 已提交
183 184
		}

B
fix npe  
Benjamin Pasero 已提交
185
		if (this.options && this.options.fileDecorations && resource) {
J
Johannes Rieken 已提交
186
			let deco = this.decorationsService.getDecoration(
J
Johannes Rieken 已提交
187
				resource,
188
				this.options.fileKind !== FileKind.FILE
J
Johannes Rieken 已提交
189
			);
B
fix npe  
Benjamin Pasero 已提交
190

191
			if (deco && this.options.fileDecorations.colors) {
192
				iconLabelOptions.extraClasses.push(deco.labelClassName);
193
			}
B
fix npe  
Benjamin Pasero 已提交
194

195
			if (deco && deco.badgeClassName && this.options.fileDecorations.badges) {
196
				iconLabelOptions.badge = {
J
Johannes Rieken 已提交
197
					title: deco.title,
198
					className: deco.badgeClassName,
199
				};
J
Johannes Rieken 已提交
200 201
			}
		}
B
Benjamin Pasero 已提交
202

203
		this.setValue(label, this.label.description, iconLabelOptions);
B
Benjamin Pasero 已提交
204 205
	}

206
	public dispose(): void {
B
Benjamin Pasero 已提交
207 208
		super.dispose();

209 210 211
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
212 213
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
214
	}
B
Benjamin Pasero 已提交
215 216 217 218 219 220
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
221
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
222 223 224 225 226 227 228
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
229
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
230
	hidePath?: boolean;
231
	root?: uri;
B
Benjamin Pasero 已提交
232 233 234 235
}

export class FileLabel extends ResourceLabel {

236 237 238 239 240 241 242 243 244
	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 已提交
245
		@IDecorationsService decorationsService: IDecorationsService,
246 247
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
248
	) {
249
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
250 251
	}

B
Benjamin Pasero 已提交
252
	public setFile(resource: uri, options?: IFileLabelOptions): void {
B
fix npe  
Benjamin Pasero 已提交
253
		const hideLabel = options && options.hideLabel;
254
		let name: string;
B
fix npe  
Benjamin Pasero 已提交
255 256 257 258 259 260 261 262 263
		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 已提交
264
				name = resources.basenameOrAuthority(resource);
B
fix npe  
Benjamin Pasero 已提交
265
			}
266 267
		}

B
Benjamin Pasero 已提交
268
		let description: string;
B
Benjamin Pasero 已提交
269
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
270
		if (!hidePath) {
B
Benjamin Pasero 已提交
271
			let rootProvider: IWorkspaceFolderProvider;
272 273 274 275 276 277 278 279
			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 已提交
280 281

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

B
Benjamin Pasero 已提交
284
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
285
	}
B
Benjamin Pasero 已提交
286 287
}

288
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
289 290

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

I
isidor 已提交
293
	if (resource) {
I
isidor 已提交
294
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
295

296
		// Folders
297
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
298
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
299 300
		}

301 302
		// Files
		else {
303 304

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

307
			// Extension(s)
I
isidor 已提交
308
			const dotSegments = name.split('.');
M
Martin Aeschlimann 已提交
309 310
			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
311
			}
312
			classes.push(`ext-file-icon`); // extra segment to increase file-ext score
313

314
			// Configured Language
315
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
316
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
317 318
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
319
			}
B
Benjamin Pasero 已提交
320 321
		}
	}
B
fix npe  
Benjamin Pasero 已提交
322

B
Benjamin Pasero 已提交
323 324 325
	return classes;
}

326 327 328 329 330
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
331
			const modeId = model.getLanguageIdentifier().language;
332 333 334 335 336 337 338 339 340
			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 已提交
341 342
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
343
}