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 { 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';
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 56 57
		@IEnvironmentService protected environmentService: IEnvironmentService,
		@IResourceDecorationsService protected decorationsService: IResourceDecorationsService,
		@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 68
		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 已提交
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;
		}
J
Johannes Rieken 已提交
98
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
99 100 101 102
			this.render(false);
		}
	}

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

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

109 110 111
		this.render(hasResourceChanged);
	}

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

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

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

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

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

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

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

		this.setValue();
	}

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

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

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

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
		}

J
Johannes Rieken 已提交
185 186 187
		if (this.options && this.options.fileDecorations) {
			let deco = this.decorationsService.getTopDecoration(
				resource,
188
				this.options.fileKind !== FileKind.FILE
J
Johannes Rieken 已提交
189
			);
190
			if (deco && this.options.fileDecorations.colors) {
191
				iconLabelOptions.extraClasses.push(deco.labelClassName);
192
			}
193
			if (deco && deco.badgeClassName && this.options.fileDecorations.badges) {
194
				iconLabelOptions.badge = {
195 196
					title: deco.tooltip,
					className: deco.badgeClassName,
197
				};
J
Johannes Rieken 已提交
198 199
			}
		}
B
Benjamin Pasero 已提交
200

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

234 235 236 237 238 239 240 241 242
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IEnvironmentService environmentService: IEnvironmentService,
243 244 245
		@IResourceDecorationsService decorationsService: IResourceDecorationsService,
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
246
	) {
247
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
248 249
	}

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

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

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

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

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

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

B
Benjamin Pasero 已提交
291

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

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

300 301
		// Files
		else {
302 303

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

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

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

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