提交 a0e9d6a3 编写于 作者: J Johannes Rieken

make ResourceMap accept a key-function, https://github.com/microsoft/vscode/issues/93368

This allows to use the ResourceMap with case insensitive path logic etc
上级 2f6770df
......@@ -481,14 +481,40 @@ export class TernarySearchTree<K, V> {
}
}
interface ResourceMapKeyFn {
(resource: URI): string;
}
export class ResourceMap<T> implements Map<URI, T> {
readonly [Symbol.toStringTag] = 'ResourceMap';
private static readonly defaultToKey = (resource: URI) => resource.toString();
protected readonly map: Map<string, T>;
readonly [Symbol.toStringTag] = 'ResourceMap';
constructor(other?: ResourceMap<T>) {
this.map = other ? new Map(other.map) : new Map();
private readonly map: Map<string, T>;
private readonly toKey: ResourceMapKeyFn;
/**
*
* @param toKey Custom uri identity function, e.g use an existing `IExtUri#getComparison`-util
*/
constructor(toKey?: ResourceMapKeyFn);
/**
*
* @param other Another resource which this maps is created from
* @param toKey Custom uri identity function, e.g use an existing `IExtUri#getComparison`-util
*/
constructor(other?: ResourceMap<T>, toKey?: ResourceMapKeyFn);
constructor(mapOrKeyFn?: ResourceMap<T> | ResourceMapKeyFn, toKey?: ResourceMapKeyFn) {
if (mapOrKeyFn instanceof ResourceMap) {
this.map = new Map(mapOrKeyFn.map);
this.toKey = toKey ?? ResourceMap.defaultToKey;
} else {
this.map = new Map();
this.toKey = mapOrKeyFn ?? ResourceMap.defaultToKey;
}
}
set(resource: URI, value: T): this {
......@@ -546,10 +572,6 @@ export class ResourceMap<T> implements Map<URI, T> {
yield [URI.parse(item[0]), item[1]];
}
}
private toKey(resource: URI): string {
return resource.toString();
}
}
interface Item<K, V> {
......
......@@ -6,6 +6,7 @@
import { ResourceMap, TernarySearchTree, PathIterator, StringIterator, LinkedMap, Touch, LRUCache, UriIterator } from 'vs/base/common/map';
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { extUriIgnorePathCase } from 'vs/base/common/resources';
suite('Map', () => {
......@@ -811,32 +812,32 @@ suite('Map', () => {
assert.equal(map.get(uncFile), 'true');
});
// test('ResourceMap - files (ignorecase)', function () {
// const map = new ResourceMap<any>(true);
test('ResourceMap - files (ignorecase)', function () {
const map = new ResourceMap<any>(uri => extUriIgnorePathCase.getComparisonKey(uri));
// const fileA = URI.parse('file://some/filea');
// const fileB = URI.parse('some://some/other/fileb');
// const fileAUpper = URI.parse('file://SOME/FILEA');
const fileA = URI.parse('file://some/filea');
const fileB = URI.parse('some://some/other/fileb');
const fileAUpper = URI.parse('file://SOME/FILEA');
// map.set(fileA, 'true');
// assert.equal(map.get(fileA), 'true');
map.set(fileA, 'true');
assert.equal(map.get(fileA), 'true');
// assert.equal(map.get(fileAUpper), 'true');
assert.equal(map.get(fileAUpper), 'true');
// assert.ok(!map.get(fileB));
assert.ok(!map.get(fileB));
// map.set(fileAUpper, 'false');
// assert.equal(map.get(fileAUpper), 'false');
map.set(fileAUpper, 'false');
assert.equal(map.get(fileAUpper), 'false');
// assert.equal(map.get(fileA), 'false');
assert.equal(map.get(fileA), 'false');
// const windowsFile = URI.file('c:\\test with %25\\c#code');
// const uncFile = URI.file('\\\\shäres\\path\\c#\\plugin.json');
const windowsFile = URI.file('c:\\test with %25\\c#code');
const uncFile = URI.file('\\\\shäres\\path\\c#\\plugin.json');
// map.set(windowsFile, 'true');
// map.set(uncFile, 'true');
map.set(windowsFile, 'true');
map.set(uncFile, 'true');
// assert.equal(map.get(windowsFile), 'true');
// assert.equal(map.get(uncFile), 'true');
// });
assert.equal(map.get(windowsFile), 'true');
assert.equal(map.get(uncFile), 'true');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册