Skip to content

Commit 1b9d83d

Browse files
committed
updates
1 parent 88880b7 commit 1b9d83d

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/common/utils/pathUtils.ts

+31-17
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u
77
}
88

99
if (Array.isArray(scope)) {
10-
return scope.map((item) => checkUri(item) as Uri);
10+
// if the scope is an array, all items must be Uri, check each item
11+
return scope.map((item) => {
12+
const s = checkUri(item);
13+
if (s instanceof Uri) {
14+
return s;
15+
}
16+
throw new Error('Invalid entry, expected Uri.');
17+
});
1118
}
1219

1320
if (scope instanceof Uri) {
1421
if (scope.scheme === 'vscode-notebook-cell') {
15-
// If the scope is a cell Uri, we need to find the notebook document it belongs to.
16-
const matchingDoc = workspace.notebookDocuments.find((doc) => {
17-
const cell = findCell(scope, doc);
18-
return cell !== undefined;
19-
});
22+
const matchingDoc = workspace.notebookDocuments.find((doc) => findCell(scope, doc));
2023
// If we find a matching notebook document, return the Uri of the cell.
2124
return matchingDoc ? matchingDoc.uri : scope;
2225
}
@@ -29,20 +32,31 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u
2932
*/
3033
export function findCell(cellUri: Uri, notebook: NotebookDocument): NotebookCell | undefined {
3134
// Fragment is not unique to a notebook, hence ensure we compare the path as well.
32-
const index = notebook
33-
.getCells()
34-
.findIndex(
35-
(cell) =>
36-
isEqual(cell.document.uri, cellUri) ||
37-
(cell.document.uri.fragment === cellUri.fragment && cell.document.uri.path === cellUri.path),
38-
);
39-
if (index !== -1) {
40-
return notebook.getCells()[index];
35+
return notebook.getCells().find((cell) => {
36+
return isEqual(cell.document.uri, cellUri);
37+
});
38+
}
39+
function isEqual(uri1: Uri | undefined, uri2: Uri | undefined): boolean {
40+
if (uri1 === uri2) {
41+
return true;
42+
}
43+
if (!uri1 || !uri2) {
44+
return false;
4145
}
46+
return getComparisonKey(uri1) === getComparisonKey(uri2);
47+
}
48+
49+
function getComparisonKey(uri: Uri): string {
50+
return uri
51+
.with({
52+
path: ignorePathCasing(uri) ? uri.path.toLowerCase() : undefined,
53+
fragment: undefined,
54+
})
55+
.toString();
4256
}
4357

44-
function isEqual(a: Uri, b: Uri): boolean {
45-
return a.toString() === b.toString();
58+
function ignorePathCasing(_uri: Uri): boolean {
59+
return true;
4660
}
4761

4862
export function normalizePath(path: string): string {

0 commit comments

Comments
 (0)