labels.ts 7.6 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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';
import paths = require('vs/base/common/paths');
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';
J
Johannes Rieken 已提交
15 16 17 18 19 20
import { getPathLabel } from 'vs/base/common/labels';
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 23 24 25 26 27 28 29 30 31 32 33

export interface IEditorLabel {
	name: string;
	description?: string;
	resource?: uri;
}

export interface IResourceLabelOptions extends IIconLabelOptions {
	isFolder?: boolean;
}

export class ResourceLabel extends IconLabel {
34
	private toDispose: IDisposable[];
B
Benjamin Pasero 已提交
35 36
	private label: IEditorLabel;
	private options: IResourceLabelOptions;
37 38
	private computedIconClasses: string[];
	private lastKnownConfiguredLangId: string;
B
Benjamin Pasero 已提交
39 40 41

	constructor(
		container: HTMLElement,
B
Benjamin Pasero 已提交
42
		options: IIconLabelCreationOptions,
B
Benjamin Pasero 已提交
43 44
		@IExtensionService private extensionService: IExtensionService,
		@IWorkspaceContextService protected contextService: IWorkspaceContextService,
45
		@IConfigurationService private configurationService: IConfigurationService,
46
		@IModeService private modeService: IModeService,
47 48
		@IModelService private modelService: IModelService,
		@IEnvironmentService protected environmentService: IEnvironmentService
B
Benjamin Pasero 已提交
49
	) {
B
Benjamin Pasero 已提交
50
		super(container, options);
B
Benjamin Pasero 已提交
51

52 53 54 55 56 57
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners(): void {
58 59
		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 已提交
60 61 62
	}

	public setLabel(label: IEditorLabel, options?: IResourceLabelOptions): void {
63
		const hasResourceChanged = this.hasResourceChanged(label, options);
64

B
Benjamin Pasero 已提交
65 66 67
		this.label = label;
		this.options = options;

68 69 70
		this.render(hasResourceChanged);
	}

71
	private hasResourceChanged(label: IEditorLabel, options: IResourceLabelOptions): boolean {
72 73 74
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

75 76 77 78 79 80 81
		const newIsFolder = options ? options.isFolder : false;
		const oldIsFolder = this.options ? this.options.isFolder : false;

		if (newIsFolder !== oldIsFolder) {
			return true; // same resource but different kind (file, folder)
		}

82
		if (newResource && oldResource) {
83
			return newResource.toString() !== oldResource.toString();
84 85 86 87 88 89 90
		}

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

		return true;
B
Benjamin Pasero 已提交
91 92 93 94 95
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
96 97
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
98 99 100 101

		this.setValue();
	}

102 103 104 105 106 107 108 109 110 111 112 113 114
	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 已提交
115 116 117 118 119 120 121
		if (!this.label) {
			return;
		}

		const resource = this.label.resource;

		let title = '';
122
		if (this.options && typeof this.options.title === 'string') {
B
Benjamin Pasero 已提交
123 124
			title = this.options.title;
		} else if (resource) {
125
			title = getPathLabel(resource.fsPath, void 0, this.environmentService);
B
Benjamin Pasero 已提交
126 127
		}

128 129 130 131 132
		if (!this.computedIconClasses) {
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.isFolder);
		}

		let extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
133 134 135 136 137
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
138
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
139

B
Benjamin Pasero 已提交
140
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
141 142
	}

143
	public dispose(): void {
B
Benjamin Pasero 已提交
144 145
		super.dispose();

146 147 148
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
149 150
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
151
	}
B
Benjamin Pasero 已提交
152 153 154 155 156 157
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
158
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
159 160 161 162 163 164 165
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
166
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
167 168 169 170 171 172 173 174
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
175
			name: !options.hideLabel ? paths.basename(resource.fsPath) : void 0,
176
			description: !options.hidePath ? getPathLabel(paths.dirname(resource.fsPath), this.contextService, this.environmentService) : void 0
B
Benjamin Pasero 已提交
177 178
		}, options);
	}
B
Benjamin Pasero 已提交
179 180
}

181
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, isFolder?: boolean): string[] {
182 183 184 185

	// we always set these base classes even if we do not have a path
	const classes = isFolder ? ['folder-icon'] : ['file-icon'];

B
Benjamin Pasero 已提交
186
	let path: string;
187 188
	if (resource) {
		path = resource.fsPath;
B
Benjamin Pasero 已提交
189 190 191
	}

	if (path) {
M
Martin Aeschlimann 已提交
192
		const basename = cssEscape(paths.basename(path).toLowerCase());
B
Benjamin Pasero 已提交
193

194 195
		// Folders
		if (isFolder) {
M
Martin Aeschlimann 已提交
196
			classes.push(`${basename}-name-folder-icon`);
B
Benjamin Pasero 已提交
197 198
		}

199 200
		// Files
		else {
201 202

			// Name
M
Martin Aeschlimann 已提交
203
			classes.push(`${basename}-name-file-icon`);
B
Benjamin Pasero 已提交
204

205
			// Extension(s)
M
Martin Aeschlimann 已提交
206 207 208
			const dotSegments = basename.split('.');
			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
209 210
			}

211
			// Configured Language
212
			let configuredLangId = getConfiguredLangId(modelService, resource);
213 214 215
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(path);
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
216
			}
B
Benjamin Pasero 已提交
217 218 219 220 221
		}
	}
	return classes;
}

222 223 224 225 226
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
227
			const modeId = model.getLanguageIdentifier().language;
228 229 230 231 232 233 234 235 236
			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 已提交
237 238
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
B
Benjamin Pasero 已提交
239
}