labels.ts 12.1 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 { IDecorationsService, IResourceDecorationChangeEvent, IDecorationData } from 'vs/workbench/services/decorations/browser/decorations';
B
Benjamin Pasero 已提交
24
import { Schemas } from 'vs/base/common/network';
25
import { FileKind, FILES_ASSOCIATIONS_CONFIG } 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, data?: IDecorationData };
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 68 69 70 71 72 73 74 75 76 77

		// update when extensions are loaded with potentially new languages
		this.extensionService.onReady().then(() => this.render(true /* clear cache */));

		// react to model mode changes
		this.toDispose.push(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e)));

		// react to file decoration changes
		this.toDispose.push(this.decorationsService.onDidChangeDecorations(this.onFileDecorationsChanges, this));

		// react to theme changes
J
Johannes Rieken 已提交
78
		this.toDispose.push(this.themeService.onThemeChange(() => this.render(false)));
79 80 81 82 83 84 85

		// react to files.associations changes
		this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration(FILES_ASSOCIATIONS_CONFIG)) {
				this.render(true /* clear cache */);
			}
		}));
86 87 88 89 90 91 92 93 94 95 96
	}

	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 已提交
97 98
		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
99 100 101 102
		}

		if (e.model.uri.toString() === this.label.resource.toString()) {
			if (this.lastKnownConfiguredLangId !== e.model.getLanguageIdentifier().language) {
B
Benjamin Pasero 已提交
103
				this.render(true); // update if the language id of the model has changed from our last known state
104 105
			}
		}
B
Benjamin Pasero 已提交
106 107
	}

108 109 110 111
	private onFileDecorationsChanges(e: IResourceDecorationChangeEvent): void {
		if (!this.options || !this.label || !this.label.resource) {
			return;
		}
B
fix npe  
Benjamin Pasero 已提交
112

J
Johannes Rieken 已提交
113
		if (this.options.fileDecorations && e.affectsResource(this.label.resource)) {
114 115 116 117
			this.render(false);
		}
	}

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

B
Benjamin Pasero 已提交
121 122 123
		this.label = label;
		this.options = options;

124 125 126
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
127
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
128 129 130
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

131 132
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
133

134
		if (newFileKind !== oldFileKind) {
135 136 137
			return true; // same resource but different kind (file, folder)
		}

138
		if (newResource && oldResource) {
139
			return newResource.toString() !== oldResource.toString();
140 141 142 143 144 145 146
		}

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

		return true;
B
Benjamin Pasero 已提交
147 148 149 150 151
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
152 153
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
154 155 156 157

		this.setValue();
	}

158 159 160 161 162 163 164 165 166 167 168 169 170
	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 已提交
171 172 173 174
		if (!this.label) {
			return;
		}

175 176 177 178 179 180
		const iconLabelOptions: IIconLabelOptions = {
			title: '',
			italic: this.options && this.options.italic,
			matches: this.options && this.options.matches,
		};

B
Benjamin Pasero 已提交
181
		const resource = this.label.resource;
B
fix npe  
Benjamin Pasero 已提交
182
		const label = this.label.name;
183

184
		if (this.options && typeof this.options.title === 'string') {
185
			iconLabelOptions.title = this.options.title;
B
Benjamin Pasero 已提交
186
		} else if (resource) {
187
			iconLabelOptions.title = getPathLabel(resource, void 0, this.environmentService);
B
Benjamin Pasero 已提交
188 189
		}

190
		if (!this.computedIconClasses) {
191
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
192 193
		}

194
		iconLabelOptions.extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
195
		if (this.options && this.options.extraClasses) {
196
			iconLabelOptions.extraClasses.push(...this.options.extraClasses);
B
Benjamin Pasero 已提交
197 198
		}

B
fix npe  
Benjamin Pasero 已提交
199
		if (this.options && this.options.fileDecorations && resource) {
J
Johannes Rieken 已提交
200
			let deco = this.decorationsService.getDecoration(
J
Johannes Rieken 已提交
201
				resource,
202 203
				this.options.fileKind !== FileKind.FILE,
				this.options.fileDecorations.data
J
Johannes Rieken 已提交
204
			);
B
fix npe  
Benjamin Pasero 已提交
205

206
			if (deco) {
207 208
				if (deco.tooltip) {
					iconLabelOptions.title = `${deco.tooltip}, ${iconLabelOptions.title}`;
209 210 211 212 213 214 215
				}
				if (this.options.fileDecorations.colors) {
					iconLabelOptions.extraClasses.push(deco.labelClassName);
				}
				if (this.options.fileDecorations.badges) {
					iconLabelOptions.extraClasses.push(deco.badgeClassName);
				}
J
Johannes Rieken 已提交
216 217
			}
		}
B
Benjamin Pasero 已提交
218

219
		this.setValue(label, this.label.description, iconLabelOptions);
B
Benjamin Pasero 已提交
220 221
	}

222
	public dispose(): void {
B
Benjamin Pasero 已提交
223 224
		super.dispose();

225 226 227
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
228 229
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
230
	}
B
Benjamin Pasero 已提交
231 232 233 234 235 236
}

export class EditorLabel extends ResourceLabel {

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

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
245
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
246
	hidePath?: boolean;
247
	root?: uri;
B
Benjamin Pasero 已提交
248 249 250 251
}

export class FileLabel extends ResourceLabel {

252 253 254 255 256 257 258 259 260
	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 已提交
261
		@IDecorationsService decorationsService: IDecorationsService,
262 263
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
264
	) {
265
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService, decorationsService, themeService);
266 267
	}

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

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

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

B
Benjamin Pasero 已提交
300
		this.setLabel({ resource, name, description }, options);
B
Benjamin Pasero 已提交
301
	}
B
Benjamin Pasero 已提交
302 303
}

304
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
305 306

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

I
isidor 已提交
309
	if (resource) {
I
isidor 已提交
310
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
311

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

317 318
		// Files
		else {
319 320

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

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

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

B
Benjamin Pasero 已提交
339 340 341
	return classes;
}

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