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

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

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

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

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

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

		this.registerListeners();
	}

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

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

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

71 72 73
		this.render(hasResourceChanged);
	}

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

78 79
		const newFileKind = options ? options.fileKind : void 0;
		const oldFileKind = this.options ? this.options.fileKind : void 0;
80

81
		if (newFileKind !== oldFileKind) {
82 83 84
			return true; // same resource but different kind (file, folder)
		}

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

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

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

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

		this.setValue();
	}

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

		const resource = this.label.resource;

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

131
		if (!this.computedIconClasses) {
132
			this.computedIconClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.fileKind);
133 134 135
		}

		let extraClasses = this.computedIconClasses.slice(0);
B
Benjamin Pasero 已提交
136 137 138 139 140
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

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

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

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

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

export class EditorLabel extends ResourceLabel {

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

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

export class FileLabel extends ResourceLabel {

176 177 178 179 180 181 182 183 184 185 186 187 188 189
	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 已提交
190
	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
191
		const hidePath = options.hidePath || (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource));
192 193 194 195
		const rootProvider: IRootProvider = options.root ? {
			getRoot(): uri { return options.root; },
			getWorkspace(): { roots: uri[]; } { return { roots: [options.root] }; },
		} : this.contextService;
B
Benjamin Pasero 已提交
196 197
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
198
			name: !options.hideLabel ? paths.basename(resource.fsPath) : void 0,
199
			description: !hidePath ? getPathLabel(paths.dirname(resource.fsPath), rootProvider, this.environmentService) : void 0
B
Benjamin Pasero 已提交
200 201
		}, options);
	}
B
Benjamin Pasero 已提交
202 203
}

204
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
205 206

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

B
Benjamin Pasero 已提交
209
	let path: string;
210 211
	if (resource) {
		path = resource.fsPath;
B
Benjamin Pasero 已提交
212 213 214
	}

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

217
		// Folders
218
		if (fileKind === FileKind.FOLDER) {
M
Martin Aeschlimann 已提交
219
			classes.push(`${basename}-name-folder-icon`);
B
Benjamin Pasero 已提交
220 221
		}

222 223
		// Files
		else {
224 225

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

228
			// Extension(s)
M
Martin Aeschlimann 已提交
229 230 231
			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
232 233
			}

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

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