forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathUtils.ts
More file actions
54 lines (48 loc) · 1.72 KB
/
pathUtils.ts
File metadata and controls
54 lines (48 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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') {
// 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;
});
// If we find a matching notebook document, return the Uri of the cell.
return matchingDoc ? matchingDoc.uri : scope;
}
}
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()) {
return path1.toLowerCase();
}
return path1;
}