labels.ts 8.5 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';
22 23
import { IUntitledEditorService } from "vs/workbench/services/untitled/common/untitledEditorService";
import { Schemas } from "vs/base/common/network";
B
Benjamin Pasero 已提交
24 25 26 27 28 29 30 31 32 33 34 35

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

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

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

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

54 55 56 57 58 59
		this.toDispose = [];

		this.registerListeners();
	}

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

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

B
Benjamin Pasero 已提交
67 68 69
		this.label = label;
		this.options = options;

70 71 72
		this.render(hasResourceChanged);
	}

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

77 78 79 80 81 82 83
		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)
		}

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

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

		return true;
B
Benjamin Pasero 已提交
93 94 95 96 97
	}

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

		this.setValue();
	}

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

		const resource = this.label.resource;

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

130 131 132 133 134
		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 已提交
135 136 137 138 139
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
140
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
141

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

174 175 176 177 178 179 180 181 182 183 184 185 186 187
	constructor(
		container: HTMLElement,
		options: IIconLabelCreationOptions,
		@IExtensionService extensionService: IExtensionService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IConfigurationService configurationService: IConfigurationService,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IEnvironmentService environmentService: IEnvironmentService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService
	) {
		super(container, options, extensionService, contextService, configurationService, modeService, modelService, environmentService);
	}

B
Benjamin Pasero 已提交
188
	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
189 190
		const hidePath = options.hidePath || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));

B
Benjamin Pasero 已提交
191 192
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
193
			name: !options.hideLabel ? paths.basename(resource.fsPath) : void 0,
194
			description: !hidePath ? getPathLabel(paths.dirname(resource.fsPath), this.contextService, this.environmentService) : void 0
B
Benjamin Pasero 已提交
195 196
		}, options);
	}
B
Benjamin Pasero 已提交
197 198
}

199
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, isFolder?: boolean): string[] {
200 201 202 203

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

B
Benjamin Pasero 已提交
204
	let path: string;
205 206
	if (resource) {
		path = resource.fsPath;
B
Benjamin Pasero 已提交
207 208 209
	}

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

212 213
		// Folders
		if (isFolder) {
M
Martin Aeschlimann 已提交
214
			classes.push(`${basename}-name-folder-icon`);
B
Benjamin Pasero 已提交
215 216
		}

217 218
		// Files
		else {
219 220

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

223
			// Extension(s)
M
Martin Aeschlimann 已提交
224 225 226
			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
227 228
			}

229
			// Configured Language
230
			let configuredLangId = getConfiguredLangId(modelService, resource);
231 232 233
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(path);
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
234
			}
B
Benjamin Pasero 已提交
235 236 237 238 239
		}
	}
	return classes;
}

240 241 242 243 244
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
245
			const modeId = model.getLanguageIdentifier().language;
246 247 248 249 250 251 252 253 254
			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 已提交
255 256
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 已提交
257
}