|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import { CancellationError, CancellationToken, Uri, workspace } from 'vscode'; |
| 4 | +import { CancellationError, CancellationToken, extensions, NotebookDocument, Uri, workspace } from 'vscode'; |
5 | 5 | import { IDiscoveryAPI } from '../pythonEnvironments/base/locator'; |
6 | 6 | import { PythonExtension, ResolvedEnvironment } from '../api/types'; |
7 | 7 | import { ITerminalHelper, TerminalShellType } from '../common/terminal/types'; |
8 | 8 | import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution'; |
9 | 9 | import { Conda } from '../pythonEnvironments/common/environmentManagers/conda'; |
| 10 | +import { JUPYTER_EXTENSION_ID } from '../common/constants'; |
10 | 11 |
|
11 | 12 | export function resolveFilePath(filepath?: string): Uri | undefined { |
12 | 13 | if (!filepath) { |
@@ -115,3 +116,30 @@ async function getCondaRunCommand(environment: ResolvedEnvironment) { |
115 | 116 | } |
116 | 117 | return { command: cmd[0], args: cmd.slice(1) }; |
117 | 118 | } |
| 119 | + |
| 120 | +export function throwIfNotebookUri(resource: Uri | undefined) { |
| 121 | + if (!resource) { |
| 122 | + return; |
| 123 | + } |
| 124 | + const notebook = workspace.notebookDocuments.find( |
| 125 | + (doc) => doc.uri.toString() === resource.toString() || doc.uri.path === resource.path, |
| 126 | + ); |
| 127 | + if ((notebook && isJupyterNotebook(notebook)) || resource.path.toLowerCase().endsWith('.ipynb')) { |
| 128 | + const isJupyterExtensionAvailable = extensions.getExtension(JUPYTER_EXTENSION_ID); |
| 129 | + if (isJupyterExtensionAvailable) { |
| 130 | + throw new Error( |
| 131 | + 'This tool cannot be used for Jupyter Notebooks, try using notebook specific tools instead.', |
| 132 | + ); |
| 133 | + } |
| 134 | + throw new Error( |
| 135 | + `This tool cannot be used for Jupyter Notebooks. Install the Jupyter Extension (${JUPYTER_EXTENSION_ID}) & try using notebook specific tools instead.`, |
| 136 | + ); |
| 137 | + } |
| 138 | + if (notebook) { |
| 139 | + throw new Error('This tool cannot be used for Notebooks, try using notebook specific tools instead.'); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function isJupyterNotebook(notebook: NotebookDocument) { |
| 144 | + return notebook.notebookType === 'jupyter-notebook'; |
| 145 | +} |
0 commit comments