Skip to content

Commit 69fe7c0

Browse files
committed
isEqual, isEqualOrParent, and getComparisonKey use fragment by default, microsoft#93368
1 parent 3da22e9 commit 69fe7c0

2 files changed

Lines changed: 39 additions & 38 deletions

File tree

src/vs/base/common/resources.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,43 @@ export function originalFSPath(uri: URI): string {
1717
return uriToFsPath(uri, true);
1818
}
1919

20+
// DO NOT EXPORT, DO NOT USE
21+
function _hasToIgnoreCase(resource: URI | undefined): boolean {
22+
// A file scheme resource is in the same platform as code, so ignore case for non linux platforms
23+
// Resource can be from another platform. Lowering the case as an hack. Should come from File system provider
24+
return resource && resource.scheme === Schemas.file ? !isLinux : true;
25+
}
26+
2027
/**
2128
* Creates a key from a resource URI to be used to resource comparison and for resource maps.
22-
* URI queries are included, fragments are ignored.
29+
*
30+
* **!!! This function is not compatible with URI.toString() !!!**
2331
*/
24-
export function getComparisonKey(resource: URI, caseInsensitivePath = hasToIgnoreCase(resource)): string {
25-
let path = resource.path || '/';
32+
export function getComparisonKey(resource: URI, caseInsensitivePath = _hasToIgnoreCase(resource), ignoreFragment: boolean = false): string {
33+
let path = resource.path || '/'; // VERY bogous as it changes the uri
2634
if (caseInsensitivePath) {
2735
path = path.toLowerCase();
2836
}
29-
return resource.with({ authority: resource.authority.toLowerCase(), path: path, fragment: null }).toString();
30-
}
31-
32-
// DO NOT EXPORT, DO NOT USE
33-
function hasToIgnoreCase(resource: URI | undefined): boolean {
34-
// A file scheme resource is in the same platform as code, so ignore case for non linux platforms
35-
// Resource can be from another platform. Lowering the case as an hack. Should come from File system provider
36-
return resource && resource.scheme === Schemas.file ? !isLinux : true;
37+
return resource.with({ path, fragment: ignoreFragment ? null : undefined }).toString();
3738
}
3839

39-
export function basenameOrAuthority(resource: URI): string {
40-
return basename(resource) || resource.authority;
40+
/**
41+
* Tests whether two resources are the same.
42+
*
43+
* **!!! This function is not compatible with uriA.toString() === uriB.toString() !!!**
44+
*/
45+
export function isEqual(first: URI | undefined, second: URI | undefined, caseInsensitivePath = _hasToIgnoreCase(first), ignoreFragment: boolean = false): boolean {
46+
if (first === second) {
47+
return true;
48+
}
49+
if (!first || !second) {
50+
return false;
51+
}
52+
if (first.scheme !== second.scheme || !isEqualAuthority(first.authority, second.authority)) {
53+
return false;
54+
}
55+
const p1 = first.path || '/', p2 = second.path || '/';
56+
return (p1 === p2 || caseInsensitivePath && equalsIgnoreCase(p1, p2)) && first.query === second.query && (ignoreFragment || first.fragment === second.fragment);
4157
}
4258

4359
/**
@@ -46,45 +62,30 @@ export function basenameOrAuthority(resource: URI): string {
4662
* @param base A uri which is "longer"
4763
* @param parentCandidate A uri which is "shorter" then `base`
4864
*/
49-
export function isEqualOrParent(base: URI, parentCandidate: URI, ignoreCase = hasToIgnoreCase(base)): boolean {
65+
export function isEqualOrParent(base: URI, parentCandidate: URI, ignoreCase = _hasToIgnoreCase(base), ignoreFragment: boolean = false): boolean {
5066
if (base.scheme === parentCandidate.scheme) {
5167
if (base.scheme === Schemas.file) {
52-
return extpath.isEqualOrParent(originalFSPath(base), originalFSPath(parentCandidate), ignoreCase) && base.query === parentCandidate.query;
68+
return extpath.isEqualOrParent(originalFSPath(base), originalFSPath(parentCandidate), ignoreCase) && base.query === parentCandidate.query && (ignoreFragment || base.fragment === parentCandidate.fragment);
5369
}
5470
if (isEqualAuthority(base.authority, parentCandidate.authority)) {
55-
return extpath.isEqualOrParent(base.path || '/', parentCandidate.path || '/', ignoreCase, '/') && base.query === parentCandidate.query;
71+
return extpath.isEqualOrParent(base.path || '/', parentCandidate.path || '/', ignoreCase, '/') && base.query === parentCandidate.query && (ignoreFragment || base.fragment === parentCandidate.fragment);
5672
}
5773
}
5874
return false;
5975
}
6076

77+
78+
export function basenameOrAuthority(resource: URI): string {
79+
return basename(resource) || resource.authority;
80+
}
81+
6182
/**
6283
* Tests whether the two authorities are the same
6384
*/
6485
export function isEqualAuthority(a1: string, a2: string) {
6586
return a1 === a2 || equalsIgnoreCase(a1, a2);
6687
}
6788

68-
/**
69-
* Tests whether two resources are the same. URI queries must match, fragments are ignored unless requested.
70-
*/
71-
export function isEqual(first: URI | undefined, second: URI | undefined, caseInsensitivePath = hasToIgnoreCase(first), ignoreFragment = true): boolean {
72-
if (first === second) {
73-
return true;
74-
}
75-
76-
if (!first || !second) {
77-
return false;
78-
}
79-
80-
if (first.scheme !== second.scheme || !isEqualAuthority(first.authority, second.authority)) {
81-
return false;
82-
}
83-
84-
const p1 = first.path || '/', p2 = second.path || '/';
85-
return (p1 === p2 || caseInsensitivePath && equalsIgnoreCase(p1, p2)) && first.query === second.query && (ignoreFragment || first.fragment === second.fragment);
86-
}
87-
8889
export function basename(resource: URI): string {
8990
return paths.posix.basename(resource.path);
9091
}
@@ -214,7 +215,7 @@ export function addTrailingPathSeparator(resource: URI, sep: string = paths.sep)
214215
* Returns a relative path between two URIs. If the URIs don't have the same schema or authority, `undefined` is returned.
215216
* The returned relative path always uses forward slashes.
216217
*/
217-
export function relativePath(from: URI, to: URI, caseInsensitivePath = hasToIgnoreCase(from)): string | undefined {
218+
export function relativePath(from: URI, to: URI, caseInsensitivePath = _hasToIgnoreCase(from)): string | undefined {
218219
if (from.scheme !== to.scheme || !isEqualAuthority(from.authority, to.authority)) {
219220
return undefined;
220221
}

src/vs/base/test/common/resources.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ suite('Resources', () => {
383383
assertIsEqual(fileURI5, fileURI3, true, false);
384384
assertIsEqual(fileURI6, fileURI6, true, true);
385385
assertIsEqual(fileURI6, fileURI5, true, false);
386-
assertIsEqual(fileURI6, fileURI3, true, true);
386+
assertIsEqual(fileURI6, fileURI3, true, false);
387387
});
388388

389389
test('isEqualOrParent', () => {

0 commit comments

Comments
 (0)