@@ -7,16 +7,19 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u
7
7
}
8
8
9
9
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
+ } ) ;
11
18
}
12
19
13
20
if ( scope instanceof Uri ) {
14
21
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 ) ) ;
20
23
// If we find a matching notebook document, return the Uri of the cell.
21
24
return matchingDoc ? matchingDoc . uri : scope ;
22
25
}
@@ -29,20 +32,31 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u
29
32
*/
30
33
export function findCell ( cellUri : Uri , notebook : NotebookDocument ) : NotebookCell | undefined {
31
34
// 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 ;
41
45
}
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 ( ) ;
42
56
}
43
57
44
- function isEqual ( a : Uri , b : Uri ) : boolean {
45
- return a . toString ( ) === b . toString ( ) ;
58
+ function ignorePathCasing ( _uri : Uri ) : boolean {
59
+ return true ;
46
60
}
47
61
48
62
export function normalizePath ( path : string ) : string {
0 commit comments