labels.ts 7.3 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 64
		const hasResourceChanged = this.hasResourceChanged(label);

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

68 69 70 71 72 73 74 75
		this.render(hasResourceChanged);
	}

	private hasResourceChanged(label: IEditorLabel): boolean {
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

		if (newResource && oldResource) {
76
			return newResource.toString() !== oldResource.toString();
77 78 79 80 81 82 83
		}

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

		return true;
B
Benjamin Pasero 已提交
84 85 86 87 88
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
89 90
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
91 92 93 94

		this.setValue();
	}

95 96 97 98 99 100 101 102 103 104 105 106 107
	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 已提交
108 109 110 111 112 113 114
		if (!this.label) {
			return;
		}

		const resource = this.label.resource;

		let title = '';
115
		if (this.options && typeof this.options.title === 'string') {
B
Benjamin Pasero 已提交
116 117
			title = this.options.title;
		} else if (resource) {
118
			title = getPathLabel(resource.fsPath, void 0, this.environmentService);
B
Benjamin Pasero 已提交
119 120
		}

121 122 123 124 125
		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 已提交
126 127 128 129 130
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
131
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
132

B
Benjamin Pasero 已提交
133
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
134 135
	}

136
	public dispose(): void {
B
Benjamin Pasero 已提交
137 138
		super.dispose();

139 140 141
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
142 143
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
144
	}
B
Benjamin Pasero 已提交
145 146 147 148 149 150
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
151
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
152 153 154 155 156 157 158
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
159
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
160 161 162 163 164 165 166 167
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
168
			name: !options.hideLabel ? paths.basename(resource.fsPath) : void 0,
169
			description: !options.hidePath ? getPathLabel(paths.dirname(resource.fsPath), this.contextService, this.environmentService) : void 0
B
Benjamin Pasero 已提交
170 171
		}, options);
	}
B
Benjamin Pasero 已提交
172 173
}

174
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, isFolder?: boolean): string[] {
175 176 177 178

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

B
Benjamin Pasero 已提交
179
	let path: string;
180 181
	if (resource) {
		path = resource.fsPath;
B
Benjamin Pasero 已提交
182 183 184
	}

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

187 188
		// Folders
		if (isFolder) {
M
Martin Aeschlimann 已提交
189
			classes.push(`${basename}-name-folder-icon`);
B
Benjamin Pasero 已提交
190 191
		}

192 193
		// Files
		else {
194 195

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

198
			// Extension(s)
M
Martin Aeschlimann 已提交
199 200 201
			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
202 203
			}

204
			// Configured Language
205
			let configuredLangId = getConfiguredLangId(modelService, resource);
206 207 208
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(path);
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
209
			}
B
Benjamin Pasero 已提交
210 211 212 213 214
		}
	}
	return classes;
}

215 216 217 218 219
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
220
			const modeId = model.getLanguageIdentifier().language;
221 222 223 224 225 226 227 228 229
			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 已提交
230 231
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 已提交
232
}