Skip to content

Commit 9bc1a06

Browse files
authored
Add API to get notebook URI connected to a text document URI (#10865)
1 parent ab48e72 commit 9bc1a06

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/platform/api/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,21 @@ export type PythonApi = {
125125
interpreter?: PythonEnvironment_PythonApi
126126
): Promise<string[] | undefined>;
127127

128+
/**
129+
* Call to provide a function that the Python extension can call to request the Python
130+
* path to use for a particular notebook.
131+
* @param func : The function that Python should call when requesting the Python path.
132+
*/
128133
registerJupyterPythonPathFunction(func: (uri: Uri) => Promise<string | undefined>): void;
134+
135+
/**
136+
* Call to provide a function that the Python extension can call to request the notebook
137+
* document URI related to a particular text document URI, or undefined if there is no
138+
* associated notebook.
139+
* @param func : The function that Python should call when requesting the notebook URI.
140+
*/
141+
registerGetNotebookUriForTextDocumentUriFunction(func: (textDocumentUri: Uri) => Uri | undefined): void;
142+
129143
/**
130144
* This API will re-trigger environment discovery. Extensions can wait on the returned
131145
* promise to get the updated interpreters list. If there is a refresh already going on

src/standalone/intellisense/notebookPythonPathService.node.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export class NotebookPythonPathService implements IExtensionSingleActivationServ
4747
if (api.registerJupyterPythonPathFunction !== undefined) {
4848
api.registerJupyterPythonPathFunction((uri) => this._jupyterPythonPathFunction(uri));
4949
}
50+
if (api.registerGetNotebookUriForTextDocumentUriFunction !== undefined) {
51+
api.registerGetNotebookUriForTextDocumentUriFunction((uri) =>
52+
this._getNotebookUriForTextDocumentUri(uri)
53+
);
54+
}
5055
});
5156
}
5257

@@ -138,4 +143,14 @@ export class NotebookPythonPathService implements IExtensionSingleActivationServ
138143

139144
return pythonPath;
140145
}
146+
147+
private _getNotebookUriForTextDocumentUri(textDocumentUri: Uri): Uri | undefined {
148+
if (textDocumentUri.scheme !== 'vscode-interactive-input') {
149+
return undefined;
150+
}
151+
152+
const notebookPath = `${textDocumentUri.fsPath.replace('\\InteractiveInput-', 'Interactive-')}.interactive`;
153+
const notebookUri = textDocumentUri.with({ scheme: 'vscode-interactive', path: notebookPath });
154+
return notebookUri;
155+
}
141156
}

0 commit comments

Comments
 (0)