diff --git a/src/standalone/intellisense/notebookPythonPathService.node.ts b/src/standalone/intellisense/notebookPythonPathService.node.ts index e4720d7748a..ae2cc43cc96 100644 --- a/src/standalone/intellisense/notebookPythonPathService.node.ts +++ b/src/standalone/intellisense/notebookPythonPathService.node.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +import * as path from '../../platform/vscode-path/path'; import { inject, injectable } from 'inversify'; import { Disposable, extensions, Uri, workspace, window } from 'vscode'; import { INotebookEditorProvider } from '../../notebooks/types'; @@ -123,12 +124,11 @@ export class NotebookPythonPathService implements IExtensionSingleActivationServ } private _getNotebookUriForTextDocumentUri(textDocumentUri: Uri): Uri | undefined { - if (textDocumentUri.scheme !== 'vscode-interactive-input') { + const notebookUri = getNotebookUriFromInputBoxUri(textDocumentUri); + if (!notebookUri) { return undefined; } - const notebookPath = `${textDocumentUri.fsPath.replace('\\InteractiveInput-', 'Interactive-')}.interactive`; - const notebookUri = textDocumentUri.with({ scheme: 'vscode-interactive', path: notebookPath }); let result: string | undefined = undefined; window.tabGroups.all.find((group) => { group.tabs.find((tab) => { @@ -144,3 +144,13 @@ export class NotebookPythonPathService implements IExtensionSingleActivationServ return result; } } + +export function getNotebookUriFromInputBoxUri(textDocumentUri: Uri): Uri | undefined { + if (textDocumentUri.scheme !== 'vscode-interactive-input') { + return undefined; + } + + const inputBoxPrefix = path.sep + 'InteractiveInput-'; + const notebookPath = `${textDocumentUri.fsPath.replace(inputBoxPrefix, 'Interactive-')}.interactive`; + return textDocumentUri.with({ scheme: 'vscode-interactive', path: notebookPath }); +} diff --git a/src/test/datascience/notebook/intellisense/notebookPythonPathService.unit.test.ts b/src/test/datascience/notebook/intellisense/notebookPythonPathService.unit.test.ts new file mode 100644 index 00000000000..e80816852a4 --- /dev/null +++ b/src/test/datascience/notebook/intellisense/notebookPythonPathService.unit.test.ts @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { assert } from 'chai'; +import { getNotebookUriFromInputBoxUri } from '../../../../standalone/intellisense/notebookPythonPathService.node'; +import { Uri } from 'vscode'; + +suite(`DataScience - VSCode Intellisense Notebook - PythonPath service @lsp`, function () { + test('Not an input box', async () => { + const notebookUri = getNotebookUriFromInputBoxUri(Uri.file('/foo/bar.py')); + assert.notOk(notebookUri); + }); + test('Input box', async () => { + const notebookUri = getNotebookUriFromInputBoxUri(Uri.parse('vscode-interactive-input:/InteractiveInput-3')); + assert.ok(notebookUri); + assert.strictEqual( + notebookUri!.toString(), + Uri.from({ scheme: 'vscode-interactive', path: 'Interactive-3.interactive' }).toString() + ); + }); +});