labels.ts 2.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*---------------------------------------------------------------------------------------------
 *  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 platform = require('vs/base/common/platform');
import types = require('vs/base/common/types');
import strings = require('vs/base/common/strings');
import paths = require('vs/base/common/paths');

export interface ILabelProvider {

	/**
	 * Given an element returns a label for it to display in the UI.
	 */
	getLabel(element: any): string;
}

export interface IWorkspaceProvider {
	getWorkspace(): {
		resource: uri;
	}
}

export class PathLabelProvider implements ILabelProvider {
	private root: string;

	constructor(arg1?: uri|string|IWorkspaceProvider) {
		this.root = arg1 && getPath(arg1);
	}

	public getLabel(arg1: uri|string|IWorkspaceProvider): string {
		return getPathLabel(getPath(arg1), this.root);
	}
}

export function getPathLabel(arg1: uri|string, arg2?: uri|string|IWorkspaceProvider): string {
	var basepath = arg2 && getPath(arg2);
	var absolutePath = getPath(arg1);

	if (basepath && paths.isEqualOrParent(absolutePath, basepath)) {
		return paths.normalize(absolutePath.substr(basepath.length + 1 /* no leading slash/backslash */), platform.isNative);
	}

	if (platform.isWindows && absolutePath[1] === ':') {
		return paths.normalize(absolutePath.charAt(0).toUpperCase() + absolutePath.slice(1), platform.isNative);
	}

	return paths.normalize(absolutePath, platform.isNative);
}

function getPath(arg1: uri|string|IWorkspaceProvider): string {
	if (!arg1) {
		return null;
	}

	if (typeof arg1 === 'string') {
		return arg1;
	}

	if (types.isFunction((<IWorkspaceProvider>arg1).getWorkspace)) {
		var ws = (<IWorkspaceProvider>arg1).getWorkspace();
		return ws ? ws.resource.fsPath : void 0;
	}

	return (<uri>arg1).fsPath;
}