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';
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';
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 38
	showDecorations?: boolean;
	showAllDecorations?: boolean;
B
Benjamin Pasero 已提交
39 40 41
}

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

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

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

		this.registerListeners();
	}

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

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

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

95 96 97 98 99 100 101 102 103 104 105 106
	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 已提交
107
	public setLabel(label: IResourceLabel, options?: IResourceLabelOptions): void {
108
		const hasResourceChanged = this.hasResourceChanged(label, options);
109

B
Benjamin Pasero 已提交
110 111 112
		this.label = label;
		this.options = options;

113 114 115
		this.render(hasResourceChanged);
	}

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

120 121
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
122

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

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

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

		return true;
B
Benjamin Pasero 已提交
136 137 138 139 140
	}

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

		this.setValue();
	}

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

		const resource = this.label.resource;

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

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

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

182 183 184 185 186 187 188 189 190 191 192 193 194
		let deco: IResourceDecoration;
		if (this.options) {
			if (this.options.showDecorations) {
				deco = this.decorationsService.getTopDecoration(resource, false);
			} else if (this.options.showAllDecorations) {
				deco = this.decorationsService.getTopDecoration(resource, true);
			}
		}

		// set/unset color from decoration
		const color = deco && this.themeService.getTheme().getColor(deco.color, true);
		this.element.style.color = color ? color.toString() : '';

B
Benjamin Pasero 已提交
195
		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
196
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
197

B
Benjamin Pasero 已提交
198
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
199 200
	}

201
	public dispose(): void {
B
Benjamin Pasero 已提交
202 203
		super.dispose();

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

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

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

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

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

B
Benjamin Pasero 已提交
279
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
280
	}
B
Benjamin Pasero 已提交
281 282
}

283
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
284 285

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

B
Benjamin Pasero 已提交
288

I
isidor 已提交
289
	if (resource) {
I
isidor 已提交
290
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
291

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

297 298
		// Files
		else {
299 300

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

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

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

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