labels.ts 10.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');
I
isidor 已提交
10
import resources = require('vs/base/common/resources');
J
Johannes Rieken 已提交
11 12 13 14
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';
15
import { toResource } from 'vs/workbench/common/editor';
S
Sandeep Somavarapu 已提交
16
import { getPathLabel, IWorkspaceFolderProvider } from 'vs/base/common/labels';
J
Johannes Rieken 已提交
17 18 19 20 21
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';
22
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
23 24 25
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { Schemas } from 'vs/base/common/network';
import { FileKind } from 'vs/platform/files/common/files';
26
import { IModel } from 'vs/editor/common/editorCommon';
B
Benjamin Pasero 已提交
27

B
Benjamin Pasero 已提交
28
export interface IResourceLabel {
B
Benjamin Pasero 已提交
29 30 31 32 33 34
	name: string;
	description?: string;
	resource?: uri;
}

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

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

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

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

		this.registerListeners();
	}

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

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

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

B
Benjamin Pasero 已提交
88
	public setLabel(label: IResourceLabel, options?: IResourceLabelOptions): void {
89
		const hasResourceChanged = this.hasResourceChanged(label, options);
90

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

94 95 96
		this.render(hasResourceChanged);
	}

B
Benjamin Pasero 已提交
97
	private hasResourceChanged(label: IResourceLabel, options: IResourceLabelOptions): boolean {
98 99 100
		const newResource = label ? label.resource : void 0;
		const oldResource = this.label ? this.label.resource : void 0;

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

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

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

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

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

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

		this.setValue();
	}

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

		const resource = this.label.resource;

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

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

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

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

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

199 200 201 202 203 204 205 206 207 208 209 210 211 212
	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 已提交
213
	public setFile(resource: uri, options?: IFileLabelOptions): void {
214 215 216 217 218 219 220 221 222 223
		let name: string;
		if (options && options.hideLabel) {
			name = void 0;
		} else if (options && options.fileKind === FileKind.ROOT_FOLDER) {
			const workspaceFolder = this.contextService.getWorkspaceFolder(resource);
			name = workspaceFolder.name;
		} else {
			name = paths.basename(resource.fsPath);
		}

224

B
Benjamin Pasero 已提交
225
		const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
226 227 228 229 230 231 232 233 234 235
		if (!hidePath) {
			let rootProvider: IWorkspaceFolderProvider;
			if (options && options.root) {
				rootProvider = {
					getWorkspaceFolder(): { uri } { return { uri: options.root }; },
					getWorkspace(): { folders: { uri: uri }[]; } { return { folders: [{ uri: options.root }] }; },
				};
			} else {
				rootProvider = this.contextService;
			}
B
Benjamin Pasero 已提交
236

237
			const description = resource.scheme === 'file' || resource.scheme === 'untitled' ? getPathLabel(paths.dirname(resource.fsPath), rootProvider, this.environmentService) : resource.authority;
I
isidor 已提交
238

239 240 241 242 243 244
			this.setLabel({
				resource,
				name: (options && options.hideLabel) ? void 0 : resources.basenameOrAuthority(resource),
				description: !hidePath ? description : void 0
			}, options);
		}
B
Benjamin Pasero 已提交
245
	}
B
Benjamin Pasero 已提交
246 247
}

248
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
249 250

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

B
Benjamin Pasero 已提交
253

I
isidor 已提交
254
	if (resource) {
I
isidor 已提交
255
		const name = cssEscape(resources.basenameOrAuthority(resource).toLowerCase());
B
Benjamin Pasero 已提交
256

257
		// Folders
258
		if (fileKind === FileKind.FOLDER) {
I
isidor 已提交
259
			classes.push(`${name}-name-folder-icon`);
B
Benjamin Pasero 已提交
260 261
		}

262 263
		// Files
		else {
264 265

			// Name
I
isidor 已提交
266
			classes.push(`${name}-name-file-icon`);
B
Benjamin Pasero 已提交
267

268
			// Extension(s)
I
isidor 已提交
269
			const dotSegments = name.split('.');
M
Martin Aeschlimann 已提交
270 271
			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
272 273
			}

274
			// Configured Language
275
			let configuredLangId = getConfiguredLangId(modelService, resource);
I
isidor 已提交
276
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(name);
277 278
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
279
			}
B
Benjamin Pasero 已提交
280 281 282 283 284
		}
	}
	return classes;
}

285 286 287 288 289
function getConfiguredLangId(modelService: IModelService, resource: uri): string {
	let configuredLangId: string;
	if (resource) {
		const model = modelService.getModel(resource);
		if (model) {
A
Alex Dima 已提交
290
			const modeId = model.getLanguageIdentifier().language;
291 292 293 294 295 296 297 298 299
			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 已提交
300 301
function cssEscape(val: string): string {
	return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
302
}