labels.ts 7.0 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';
B
Benjamin Pasero 已提交
21 22 23 24 25 26 27 28 29 30 31 32

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

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

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

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

50 51 52 53 54 55
		this.toDispose = [];

		this.registerListeners();
	}

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

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

B
Benjamin Pasero 已提交
63 64 65
		this.label = label;
		this.options = options;

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		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) {
			return newResource.fsPath !== oldResource.fsPath;
		}

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

		return true;
B
Benjamin Pasero 已提交
82 83 84 85 86
	}

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

		this.setValue();
	}

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

		const resource = this.label.resource;

		let title = '';
		if (this.options && this.options.title) {
			title = this.options.title;
		} else if (resource) {
116
			title = getPathLabel(resource.fsPath);
B
Benjamin Pasero 已提交
117 118
		}

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

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
129
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
130

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

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

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

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

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

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

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

190 191
		// Files
		else {
192 193

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

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

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

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