naive first cut of getPathLabels()

This commit is contained in:
Benjamin Pasero 2016-08-12 11:16:31 +02:00
parent ef2e222905
commit 6df36e50f2
2 changed files with 84 additions and 7 deletions

View file

@ -4,11 +4,12 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import uri from 'vs/base/common/uri';
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');
import {LinkedMap} from 'vs/base/common/map';
export interface ILabelProvider {
@ -20,23 +21,23 @@ export interface ILabelProvider {
export interface IWorkspaceProvider {
getWorkspace(): {
resource: uri;
resource: URI;
};
}
export class PathLabelProvider implements ILabelProvider {
private root: string;
constructor(arg1?: uri|string|IWorkspaceProvider) {
constructor(arg1?: URI | string | IWorkspaceProvider) {
this.root = arg1 && getPath(arg1);
}
public getLabel(arg1: uri|string|IWorkspaceProvider): string {
public getLabel(arg1: URI | string | IWorkspaceProvider): string {
return getPathLabel(getPath(arg1), this.root);
}
}
export function getPathLabel(arg1: uri|string, arg2?: uri|string|IWorkspaceProvider): string {
export function getPathLabel(arg1: URI | string, arg2?: URI | string | IWorkspaceProvider): string {
let basepath = arg2 && getPath(arg2);
let absolutePath = getPath(arg1);
@ -55,7 +56,7 @@ export function getPathLabel(arg1: uri|string, arg2?: uri|string|IWorkspaceProvi
return paths.normalize(absolutePath, true);
}
function getPath(arg1: uri|string|IWorkspaceProvider): string {
function getPath(arg1: URI | string | IWorkspaceProvider): string {
if (!arg1) {
return null;
}
@ -69,5 +70,35 @@ function getPath(arg1: uri|string|IWorkspaceProvider): string {
return ws ? ws.resource.fsPath : void 0;
}
return (<uri>arg1).fsPath;
return (<URI>arg1).fsPath;
}
export interface IPathLabel {
resource: URI;
label: string;
meta?: string;
}
export function getPathLabels(resources: URI[], provider?: IWorkspaceProvider): LinkedMap<URI, IPathLabel> {
const labels = new LinkedMap<URI, IPathLabel>();
const mapLabelToDuplicates = new LinkedMap<string, IPathLabel[]>();
resources.forEach(resource => {
const item = { resource, label: paths.basename(resource.fsPath) };
labels.set(resource, item);
const duplicates = mapLabelToDuplicates.getOrSet(item.label, []);
duplicates.push(item);
});
const duplicates = mapLabelToDuplicates.values();
duplicates.forEach(duplicates => {
if (duplicates.length > 1) {
duplicates.forEach(duplicate => {
duplicate.meta = getPathLabel(paths.dirname(duplicate.resource.fsPath), provider);
});
}
});
return labels;
}

View file

@ -0,0 +1,46 @@
/*---------------------------------------------------------------------------------------------
* 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 * as assert from 'assert';
import {getPathLabels} from 'vs/base/common/labels';
import uri from 'vs/base/common/uri';
suite('Labels', () => {
test('getPathLabels - no collisions', () => {
const uris = [
uri.file('/parentA/childA.txt'),
uri.file('/parentA/childB.txt'),
uri.file('/parentA/other/childC.txt')
];
const res = getPathLabels(uris).values();
assert.equal(res.length, 3);
assert.equal(res[0].label, 'childA.txt');
assert.ok(!res[0].meta);
assert.equal(res[1].label, 'childB.txt');
assert.ok(!res[1].meta);
assert.equal(res[2].label, 'childC.txt');
assert.ok(!res[2].meta);
});
test('getPathLabels - collisions', () => {
const uris = [
uri.file('/parentA/childA.txt'),
uri.file('/parentB/childA.txt'),
uri.file('/parentC/other/childA.txt')
];
const res = getPathLabels(uris).values();
assert.equal(res.length, 3);
assert.equal(res[0].label, 'childA.txt');
assert.equal(res[0].meta, '/parentA');
assert.equal(res[1].label, 'childA.txt');
assert.equal(res[1].meta, '/parentB');
assert.equal(res[2].label, 'childA.txt');
assert.equal(res[2].meta, '/parentC/other');
});
});