Skip to content

Commit aefe1ca

Browse files
committed
restart jupyter adapter if python path changes
1 parent 74f69af commit aefe1ca

3 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/client/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ export function activate(context: vscode.ExtensionContext) {
8080
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
8181

8282

83-
jupMain = new jup.Jupyter(lintingOutChannel);
83+
jupMain = new jup.Jupyter(lintingOutChannel, vscode.workspace.rootPath);
8484
const documentHasJupyterCodeCells = jupMain.hasCodeCells.bind(jupMain);
85-
jupMain.activate(vscode.workspace.rootPath);
85+
jupMain.activate();
8686
context.subscriptions.push(jupMain);
8787

8888
context.subscriptions.push(new LintProvider(context, lintingOutChannel, vscode.workspace.rootPath, documentHasJupyterCodeCells));

src/client/jupyter/kernel-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class KernelManagerImpl extends EventEmitter {
2929
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.StartKernelForKernelSpeck, this.startKernel.bind(this)));
3030
}
3131
public dispose() {
32+
this.removeAllListeners();
3233
this._runningKernels.forEach(kernel => {
3334
kernel.dispose();
3435
});

src/client/jupyter/main.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,53 @@ import * as telemetryHelper from '../common/telemetry';
1212
import * as telemetryContracts from '../common/telemetryContracts';
1313
import * as main from './jupyter_client/main';
1414
import { KernelRestartedError, KernelShutdownError } from './common/errors';
15+
import { PythonSettings } from '../common/configSettings';
1516

16-
// Todo: Refactor the error handling and displaying of messages
17+
const pythonSettings = PythonSettings.getInstance();
1718

19+
// Todo: Refactor the error handling and displaying of messages
1820
export class Jupyter extends vscode.Disposable {
1921
public kernelManager: KernelManagerImpl;
2022
public kernel: Kernel = null;
2123
private status: KernelStatus;
2224
private disposables: vscode.Disposable[];
2325
private display: JupyterDisplay;
2426
private codeLensProvider: JupyterCodeLensProvider;
25-
constructor(private outputChannel: vscode.OutputChannel) {
27+
private lastUsedPythonPath: string;
28+
constructor(private outputChannel: vscode.OutputChannel, private rootPath: string) {
2629
super(() => { });
2730
this.disposables = [];
2831
this.registerCommands();
2932
this.registerKernelCommands();
33+
34+
pythonSettings.on('change', this.onConfigurationChanged.bind(this));
35+
this.lastUsedPythonPath = pythonSettings.pythonPath;
36+
}
37+
private onConfigurationChanged() {
38+
if (this.lastUsedPythonPath === pythonSettings.pythonPath) {
39+
return;
40+
}
41+
this.kernelManager.dispose();
42+
this.createKernelManager();
43+
}
44+
public dispose() {
45+
this.kernelManager.dispose();
46+
this.disposables.forEach(d => d.dispose());
3047
}
31-
activate(rootPath: string) {
32-
const jupyterClient = new main.JupyterClientAdapter(this.outputChannel, rootPath);
48+
private createKernelManager() {
49+
const jupyterClient = new main.JupyterClientAdapter(this.outputChannel, this.rootPath);
3350
this.kernelManager = new KernelManagerImpl(this.outputChannel, jupyterClient);
34-
this.disposables.push(this.kernelManager);
51+
52+
// This happend when user changes it from status bar
53+
this.kernelManager.on('kernelChanged', (kernel: Kernel, language: string) => {
54+
if (this.kernel !== kernel && (this.kernel && this.kernel.kernelSpec.language === kernel.kernelSpec.language)) {
55+
this.onKernelChanged(kernel);
56+
}
57+
});
58+
}
59+
activate() {
60+
this.createKernelManager();
61+
3562
this.disposables.push(vscode.window.onDidChangeActiveTextEditor(this.onEditorChanged.bind(this)));
3663
this.codeLensProvider = new JupyterCodeLensProvider();
3764
this.disposables.push(vscode.languages.registerCodeLensProvider(PythonLanguage, this.codeLensProvider));
@@ -40,13 +67,6 @@ export class Jupyter extends vscode.Disposable {
4067
this.disposables.push(this.status);
4168
this.display = new JupyterDisplay(this.codeLensProvider);
4269
this.disposables.push(this.display);
43-
44-
// This happend when user changes it from status bar
45-
this.kernelManager.on('kernelChanged', (kernel: Kernel, language: string) => {
46-
if (this.kernel !== kernel && (this.kernel && this.kernel.kernelSpec.language === kernel.kernelSpec.language)) {
47-
this.onKernelChanged(kernel);
48-
}
49-
});
5070
}
5171
public hasCodeCells(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<boolean> {
5272
return new Promise<boolean>(resolve => {
@@ -59,9 +79,6 @@ export class Jupyter extends vscode.Disposable {
5979
});
6080
});
6181
}
62-
public dispose() {
63-
this.disposables.forEach(d => d.dispose());
64-
}
6582
private onEditorChanged(editor: vscode.TextEditor) {
6683
if (!editor || !editor.document) {
6784
return;

0 commit comments

Comments
 (0)