labels.ts 5.0 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');
B
Benjamin Pasero 已提交
10
import {IconLabel, IIconLabelOptions, IIconLabelCreationOptions} from 'vs/base/browser/ui/iconLabel/iconLabel';
B
Benjamin Pasero 已提交
11 12 13 14 15 16
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 {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
17 18
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
19 20 21 22 23 24 25 26 27 28 29 30

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

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

export class ResourceLabel extends IconLabel {
31
	private toDispose: IDisposable[];
B
Benjamin Pasero 已提交
32 33 34 35 36
	private label: IEditorLabel;
	private options: IResourceLabelOptions;

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

45 46 47 48 49 50 51 52
		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 已提交
53 54 55 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 83 84 85 86 87 88
	}

	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) {
			title = resource.fsPath;
		}

		const extraClasses = this.getIconClasses(resource);
		if (this.options && this.options.extraClasses) {
			extraClasses.push(...this.options.extraClasses);
		}

		const italic = this.options && this.options.italic;
B
Benjamin Pasero 已提交
89
		const matches = this.options && this.options.matches;
B
Benjamin Pasero 已提交
90

B
Benjamin Pasero 已提交
91
		this.setValue(this.label.name, this.label.description, { title, extraClasses, italic, matches });
B
Benjamin Pasero 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	}

	protected getIconClasses(arg1?: uri | string): string[] {
		let path: string;
		if (typeof arg1 === 'string') {
			path = arg1;
		} else if (arg1) {
			path = arg1.fsPath;
		}

		const classes = (this.options && this.options.isFolder) ? ['folder-icon'] : ['file-icon'];

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

			const name = dotSegments[0]; // file.txt => "file", .dockerfile => "", file.some.txt => "file"
			if (name) {
				classes.push(`${this.cssEscape(name.toLowerCase())}-name-file-icon`);
			}

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

			const langId = this.modeService.getModeIdByFilenameOrFirstLine(path);
			if (langId) {
				classes.push(`${this.cssEscape(langId)}-lang-file-icon`);
			}
		}

		return classes;
	}

	private cssEscape(val: string): string {
		return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
	}
132 133

	public dispose(): void {
B
Benjamin Pasero 已提交
134 135
		super.dispose();

136 137 138 139
		this.toDispose = dispose(this.toDispose);
		this.label = void 0;
		this.options = void 0;
	}
B
Benjamin Pasero 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

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 {
	hidePath?: boolean;
}

export class FileLabel extends ResourceLabel {

	public setFile(resource: uri, options: IFileLabelOptions = Object.create(null)): void {
		this.setLabel({
			resource,
			name: paths.basename(resource.fsPath),
			description: !options.hidePath ? getPathLabel(paths.dirname(resource.fsPath), this.contextService) : void 0
		}, options);
	}
}