labels.ts 9.7 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';
15
import { getPathLabel, IRootProvider } 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 23 24
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { Schemas } from 'vs/base/common/network';
import { FileKind } from 'vs/platform/files/common/files';
25
import { IModel } from 'vs/editor/common/editorCommon';
B
Benjamin Pasero 已提交
26 27 28 29 30 31 32 33

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

export interface IResourceLabelOptions extends IIconLabelOptions {
34
	fileKind?: FileKind;
B
Benjamin Pasero 已提交
35 36 37
}

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

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

56 57 58 59 60 61
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners(): void {
62 63
		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 已提交
64
		this.toDispose.push(this.modelService.onModelModeChanged(e => this.onModelModeChanged(e))); // react to model mode changes
65 66 67 68 69 70 71 72 73 74 75
	}

	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 已提交
76 77
		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
78 79 80 81
		}

		if (e.model.uri.toString() === this.label.resource.toString()) {
			if (this.lastKnownConfiguredLangId !== e.model.getLanguageIdentifier().language) {
B
Benjamin Pasero 已提交
82
				this.render(true); // update if the language id of the model has changed from our last known state
83 84
			}
		}
B
Benjamin Pasero 已提交
85 86 87
	}

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

B
Benjamin Pasero 已提交
90 91 92
		this.label = label;
		this.options = options;

93 94 95
		this.render(hasResourceChanged);
	}

96
	private hasResourceChanged(label: IEditorLabel, options: IResourceLabelOptions): boolean {
97 98 99
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

100 101
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
102

103
		if (newFileKind !== oldFileKind) {
104 105 106
			return true; // same resource but different kind (file, folder)
		}

107
		if (newResource && oldResource) {
108
			return newResource.toString() !== oldResource.toString();
109 110 111 112 113 114 115
		}

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

		return true;
B
Benjamin Pasero 已提交
116 117 118 119 120
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;
121 122
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
B
Benjamin Pasero 已提交
123 124 125 126

		this.setValue();
	}

127 128 129 130 131 132 133 134 135 136 137 138 139
	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 已提交
140 141 142 143 144 145 146
		if (!this.label) {
			return;
		}

		const resource = this.label.resource;

		let title = '';
147
		if (this.options && typeof this.options.title === 'string') {
B
Benjamin Pasero 已提交
148 149
			title = this.options.title;
		} else if (resource) {
150
			title = getPathLabel(resource.fsPath, void 0, this.environmentService);
B
Benjamin Pasero 已提交
151 152
		}

153
		if (!this.computedIconClasses) {
154
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
155 156 157
		}

		let extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
158 159 160 161 162
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
163
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
164

B
Benjamin Pasero 已提交
165
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
166 167
	}

168
	public dispose(): void {
B
Benjamin Pasero 已提交
169 170
		super.dispose();

171 172 173
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
174 175
		this.lastKnownConfiguredLangId = void 0;
		this.computedIconClasses = void 0;
176
	}
B
Benjamin Pasero 已提交
177 178 179 180 181 182
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
183
			resource: toResource(editor, { supportSideBySide: true }),
B
Benjamin Pasero 已提交
184 185 186 187 188 189 190
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
191
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
192
	hidePath?: boolean;
193
	root?: uri;
B
Benjamin Pasero 已提交
194 195 196 197
}

export class FileLabel extends ResourceLabel {

198 199 200 201 202 203 204 205 206 207 208 209 210 211
	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 已提交
212 213 214
	public setFile(resource: uri, options?: IFileLabelOptions): void {
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
		const rootProvider: IRootProvider = (options && options.root) ? {
215 216 217
			getRoot(): uri { return options.root; },
			getWorkspace(): { roots: uri[]; } { return { roots: [options.root] }; },
		} : this.contextService;
B
Benjamin Pasero 已提交
218

B
Benjamin Pasero 已提交
219 220
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
221
			name: (options && options.hideLabel) ? void 0 : paths.basename(resource.fsPath),
222
			description: !hidePath ? getPathLabel(paths.dirname(resource.fsPath), rootProvider, this.environmentService) : void 0
B
Benjamin Pasero 已提交
223 224
		}, options);
	}
B
Benjamin Pasero 已提交
225 226
}

227
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
228 229

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

B
Benjamin Pasero 已提交
232
	let path: string;
233 234
	if (resource) {
		path = resource.fsPath;
B
Benjamin Pasero 已提交
235 236 237
	}

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

240
		// Folders
241
		if (fileKind === FileKind.FOLDER) {
M
Martin Aeschlimann 已提交
242
			classes.push(`${basename}-name-folder-icon`);
B
Benjamin Pasero 已提交
243 244
		}

245 246
		// Files
		else {
247 248

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

251
			// Extension(s)
M
Martin Aeschlimann 已提交
252 253 254
			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
255 256
			}

257
			// Configured Language
258
			let configuredLangId = getConfiguredLangId(modelService, resource);
259 260 261
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(path);
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
262
			}
B
Benjamin Pasero 已提交
263 264 265 266 267
		}
	}
	return classes;
}

268 269 270 271 272
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
273
			const modeId = model.getLanguageIdentifier().language;
274 275 276 277 278 279 280 281 282
			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 已提交
283 284
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 已提交
285
}