From ab09f36ac8235aa64481eb11f92a46d83f695fd2 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Wed, 16 Apr 2025 12:43:05 -0700 Subject: [PATCH 1/6] fix for notebook path --- src/common/utils/pathUtils.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index 81b6152b..4ab5169e 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -1,14 +1,15 @@ -import { Uri } from 'vscode'; +import { Uri, workspace } from 'vscode'; import { isWindows } from '../../managers/common/utils'; export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined { if (scope instanceof Uri) { if (scope.scheme === 'vscode-notebook-cell') { - return Uri.from({ - scheme: 'vscode-notebook', - path: scope.path, - authority: scope.authority, + workspace.notebookDocuments.find((doc) => { + if (doc.uri.toString() === scope.toString()) { + return doc; + } }); + return scope; } } if (Array.isArray(scope)) { From 1b36b03363fd3d3bfe84be3ca326eaf230e6e4ef Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Wed, 16 Apr 2025 15:09:30 -0700 Subject: [PATCH 2/6] fix to checking uri path for notebooks --- src/common/utils/pathUtils.ts | 47 +++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index 4ab5169e..258b0cf9 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -1,25 +1,50 @@ -import { Uri, workspace } from 'vscode'; +import { NotebookCell, NotebookDocument, Uri, workspace } from 'vscode'; import { isWindows } from '../../managers/common/utils'; export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined { + if (!scope) { + return undefined; + } + + if (Array.isArray(scope)) { + return scope.map((item) => checkUri(item) as Uri); + } + if (scope instanceof Uri) { if (scope.scheme === 'vscode-notebook-cell') { - workspace.notebookDocuments.find((doc) => { - if (doc.uri.toString() === scope.toString()) { - return doc; - } + // If the scope is a cell Uri, we need to find the notebook document it belongs to. + const matchingDoc = workspace.notebookDocuments.find((doc) => { + const cell = findCell(scope, doc); + return cell !== undefined; }); - return scope; + // If we find a matching notebook document, return the Uri of the cell. + return matchingDoc ? matchingDoc.uri : scope; } } - if (Array.isArray(scope)) { - return scope.map((item) => { - return checkUri(item) as Uri; - }); - } return scope; } +/** + * Find a notebook document by cell Uri. + */ +export function findCell(cellUri: Uri, notebook: NotebookDocument): NotebookCell | undefined { + // Fragment is not unique to a notebook, hence ensure we compare the path as well. + const index = notebook + .getCells() + .findIndex( + (cell) => + isEqual(cell.document.uri, cellUri) || + (cell.document.uri.fragment === cellUri.fragment && cell.document.uri.path === cellUri.path), + ); + if (index !== -1) { + return notebook.getCells()[index]; + } +} + +function isEqual(a: Uri, b: Uri): boolean { + return a.toString() === b.toString(); +} + export function normalizePath(path: string): string { const path1 = path.replace(/\\/g, '/'); if (isWindows()) { From 1b9d83d10a6172292b9a7bf3d49fb9eceda19fe5 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Fri, 25 Apr 2025 13:48:58 -0700 Subject: [PATCH 3/6] updates --- src/common/utils/pathUtils.ts | 48 ++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index 258b0cf9..13c86d2b 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -7,16 +7,19 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u } if (Array.isArray(scope)) { - return scope.map((item) => checkUri(item) as Uri); + // if the scope is an array, all items must be Uri, check each item + return scope.map((item) => { + const s = checkUri(item); + if (s instanceof Uri) { + return s; + } + throw new Error('Invalid entry, expected Uri.'); + }); } if (scope instanceof Uri) { if (scope.scheme === 'vscode-notebook-cell') { - // If the scope is a cell Uri, we need to find the notebook document it belongs to. - const matchingDoc = workspace.notebookDocuments.find((doc) => { - const cell = findCell(scope, doc); - return cell !== undefined; - }); + const matchingDoc = workspace.notebookDocuments.find((doc) => findCell(scope, doc)); // If we find a matching notebook document, return the Uri of the cell. return matchingDoc ? matchingDoc.uri : scope; } @@ -29,20 +32,31 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u */ export function findCell(cellUri: Uri, notebook: NotebookDocument): NotebookCell | undefined { // Fragment is not unique to a notebook, hence ensure we compare the path as well. - const index = notebook - .getCells() - .findIndex( - (cell) => - isEqual(cell.document.uri, cellUri) || - (cell.document.uri.fragment === cellUri.fragment && cell.document.uri.path === cellUri.path), - ); - if (index !== -1) { - return notebook.getCells()[index]; + return notebook.getCells().find((cell) => { + return isEqual(cell.document.uri, cellUri); + }); +} +function isEqual(uri1: Uri | undefined, uri2: Uri | undefined): boolean { + if (uri1 === uri2) { + return true; + } + if (!uri1 || !uri2) { + return false; } + return getComparisonKey(uri1) === getComparisonKey(uri2); +} + +function getComparisonKey(uri: Uri): string { + return uri + .with({ + path: ignorePathCasing(uri) ? uri.path.toLowerCase() : undefined, + fragment: undefined, + }) + .toString(); } -function isEqual(a: Uri, b: Uri): boolean { - return a.toString() === b.toString(); +function ignorePathCasing(_uri: Uri): boolean { + return true; } export function normalizePath(path: string): string { From 9125191beed9c6cfa2d6028eb3ea3df0679cb249 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Fri, 25 Apr 2025 13:50:06 -0700 Subject: [PATCH 4/6] simplify --- src/common/utils/pathUtils.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index 13c86d2b..6a6e9d0b 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -49,16 +49,12 @@ function isEqual(uri1: Uri | undefined, uri2: Uri | undefined): boolean { function getComparisonKey(uri: Uri): string { return uri .with({ - path: ignorePathCasing(uri) ? uri.path.toLowerCase() : undefined, + path: uri.path.toLowerCase(), fragment: undefined, }) .toString(); } -function ignorePathCasing(_uri: Uri): boolean { - return true; -} - export function normalizePath(path: string): string { const path1 = path.replace(/\\/g, '/'); if (isWindows()) { From 1594daae08bad8428ae8cf19bc7b1d17a72b063b Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Fri, 25 Apr 2025 13:54:45 -0700 Subject: [PATCH 5/6] fix formatting --- src/common/utils/pathUtils.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index 276333a9..d566657d 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -1,11 +1,8 @@ -import { NotebookCell, NotebookDocument, Uri, workspace } from 'vscode'; -import { isWindows } from '../../managers/common/utils'; import * as os from 'os'; import * as path from 'path'; -import { Uri } from 'vscode'; +import { NotebookCell, NotebookDocument, Uri, workspace } from 'vscode'; import { isWindows } from './platformUtils'; - export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined { if (!scope) { return undefined; @@ -60,7 +57,6 @@ function getComparisonKey(uri: Uri): string { .toString(); } - export function normalizePath(fsPath: string): string { const path1 = fsPath.replace(/\\/g, '/'); if (isWindows()) { From fbb3a8ca41a3a7757276d1e1426922c553bf229d Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:03:01 -0700 Subject: [PATCH 6/6] fix casing edge case --- src/common/utils/pathUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/utils/pathUtils.ts b/src/common/utils/pathUtils.ts index d566657d..4a70f389 100644 --- a/src/common/utils/pathUtils.ts +++ b/src/common/utils/pathUtils.ts @@ -51,7 +51,7 @@ function isEqual(uri1: Uri | undefined, uri2: Uri | undefined): boolean { function getComparisonKey(uri: Uri): string { return uri .with({ - path: uri.path.toLowerCase(), + path: isWindows() ? uri.path.toLowerCase() : uri.path, fragment: undefined, }) .toString();