forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernelSwitcher.ts
More file actions
44 lines (41 loc) · 1.9 KB
/
kernelSwitcher.ts
File metadata and controls
44 lines (41 loc) · 1.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { ICommandManager } from '../../common/application/types';
import { IDisposable } from '../../common/types';
import { Commands } from '../constants';
import { KernelSpecInterpreter } from '../jupyter/kernels/kernelSelector';
import { KernelSwitcher } from '../jupyter/kernels/kernelSwitcher';
import { IInteractiveWindowProvider, INotebook, INotebookEditorProvider } from '../types';
@injectable()
export class KernelSwitcherCommand implements IDisposable {
private readonly disposables: IDisposable[] = [];
constructor(
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(KernelSwitcher) private kernelSwitcher: KernelSwitcher,
@inject(INotebookEditorProvider) private notebookEditorProvider: INotebookEditorProvider,
@inject(IInteractiveWindowProvider) private interactiveWindowProvider: IInteractiveWindowProvider
) {}
public register() {
this.disposables.push(
this.commandManager.registerCommand(Commands.SwitchJupyterKernel, this.switchKernel, this)
);
}
public dispose() {
this.disposables.forEach((d) => d.dispose());
}
private async switchKernel(
notebook: INotebook | undefined,
type: 'raw' | 'jupyter'
): Promise<KernelSpecInterpreter | undefined> {
// If notebook isn't know, then user invoked this command from command palette or similar.
// We need to identify the current notebook (active native editor or interactive window).
if (!notebook) {
notebook =
this.notebookEditorProvider.activeEditor?.notebook ??
this.interactiveWindowProvider.getActive()?.notebook;
}
return this.kernelSwitcher.switchKernel(notebook, type);
}
}