labels.ts 5.9 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 14 15 16 17 18 19 20
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';
import { getResource } from 'vs/workbench/common/editor';
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 36 37 38
	private label: IEditorLabel;
	private options: IResourceLabelOptions;

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

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

		this.registerListeners();
	}

	private registerListeners(): void {
		this.extensionService.onReady().then(() => this.render()); // update when extensions are loaded with potentially new languages
		this.toDispose.push(this.configurationService.onDidUpdateConfiguration(() => this.render())); // update when file.associations change
B
Benjamin Pasero 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	}

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

		this.render();
	}

	public clear(): void {
		this.label = void 0;
		this.options = void 0;

		this.setValue();
	}

	private render(): void {
		if (!this.label) {
			return;
		}

		const resource = this.label.resource;

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

86
		const extraClasses = getIconClasses(this.modelService, this.modeService, resource, this.options && this.options.isFolder);
B
Benjamin Pasero 已提交
87 88 89 90 91
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
92
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
93

B
Benjamin Pasero 已提交
94
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
95 96
	}

97
	public dispose(): void {
B
Benjamin Pasero 已提交
98 99
		super.dispose();

100 101 102 103
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
	}
B
Benjamin Pasero 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

export class EditorLabel extends ResourceLabel {

	public setEditor(editor: IEditorInput, options?: IResourceLabelOptions): void {
		this.setLabel({
			resource: getResource(editor),
			name: editor.getName(),
			description: editor.getDescription()
		}, options);
	}
}

export interface IFileLabelOptions extends IResourceLabelOptions {
B
Benjamin Pasero 已提交
118
	hideLabel?: boolean;
B
Benjamin Pasero 已提交
119 120 121 122 123 124 125 126
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
		this.setLabel({
			resource,
B
Benjamin Pasero 已提交
127
			name: !options.hideLabel ? paths.basename(resource.fsPath) : void 0,
B
Benjamin Pasero 已提交
128 129 130
			description: !options.hidePath ? getPathLabel(paths.dirname(resource.fsPath), this.contextService) : void 0
		}, options);
	}
B
Benjamin Pasero 已提交
131 132
}

133
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, isFolder?: boolean): string[] {
B
Benjamin Pasero 已提交
134
	let path: string;
135 136 137 138 139 140 141 142 143 144
	let configuredLangId: string;
	if (resource) {
		path = resource.fsPath;
		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)
			}
		}
B
Benjamin Pasero 已提交
145 146
	}

147
	// we always set these base classes even if we do not have a path
B
Benjamin Pasero 已提交
148 149 150 151 152 153
	const classes = isFolder ? ['folder-icon'] : ['file-icon'];

	if (path) {
		const basename = paths.basename(path);
		const dotSegments = basename.split('.');

154 155 156 157 158
		// Folders
		if (isFolder) {
			if (basename) {
				classes.push(`${basename.toLowerCase()}-name-folder-icon`);
			}
B
Benjamin Pasero 已提交
159 160
		}

161 162
		// Files
		else {
163 164

			// Name
165 166 167
			const name = dotSegments[0]; // file.txt => "file", .dockerfile => "", file.some.txt => "file"
			if (name) {
				classes.push(`${cssEscape(name.toLowerCase())}-name-file-icon`);
B
Benjamin Pasero 已提交
168 169
			}

170
			// Extension(s)
171 172 173 174 175 176 177
			const extensions = dotSegments.splice(1);
			if (extensions.length > 0) {
				for (let i = 0; i < extensions.length; i++) {
					classes.push(`${cssEscape(extensions.slice(i).join('.').toLowerCase())}-ext-file-icon`); // add each combination of all found extensions if more than one
				}
			}

178 179 180 181
			// Configured Language
			configuredLangId = configuredLangId || modeService.getModeIdByFilenameOrFirstLine(path);
			if (configuredLangId) {
				classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
182
			}
B
Benjamin Pasero 已提交
183 184 185 186 187 188 189 190
		}
	}

	return classes;
}

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 已提交
191
}