Skip to content

Commit 297a2a2

Browse files
committed
Fixes after merging
1 parent df09298 commit 297a2a2

File tree

3 files changed

+94
-59
lines changed

3 files changed

+94
-59
lines changed

src/client/datascience/jupyter/kernels/kernelSelections.ts

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,51 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { CancellationToken, QuickPickItem } from 'vscode';
7+
import { CancellationToken } from 'vscode';
88
import { IInterpreterSelector } from '../../../interpreter/configuration/types';
9-
import { PythonInterpreter } from '../../../interpreter/contracts';
109
import { IJupyterKernelSpec, IJupyterSessionManager } from '../../types';
1110
import { KernelService } from './kernelService';
11+
import { IKernelSelectionListProvider, IKernelSpecQuickPickItem } from './types';
1212

13-
export interface IKernelSpecQuickPickItem extends QuickPickItem {
14-
selection: { kernelSpec: IJupyterKernelSpec } | { interpreter: PythonInterpreter };
15-
}
13+
// Small classes, hence all put into one file.
14+
// tslint:disable: max-classes-per-file
1615

16+
/**
17+
* Given a kernel spec, this will return a quick pick item with appropriate display names and the like.
18+
*
19+
* @param {IJupyterKernelSpec} kernelSpec
20+
* @returns {IKernelSpecQuickPickItem}
21+
*/
1722
function getQuickPickFromKernelSpec(kernelSpec: IJupyterKernelSpec): IKernelSpecQuickPickItem {
1823
return {
1924
label: kernelSpec.display_name || kernelSpec.name || '',
2025
description: '',
2126
selection: { kernelSpec: kernelSpec }
2227
};
2328
}
24-
export interface IKernelSelector {
25-
selectKernelSpec(cancelToken?: CancellationToken): Promise<IJupyterKernelSpec | undefined>;
26-
}
2729

28-
// export class kernelSelector implements IKernelSelector {
29-
// public async selectKernelSpec(session?: IJupyterSessionManager, cancelToken?: CancellationToken): Promise<IJupyterKernelSpec | undefined> {
30-
// return;
31-
// }
32-
// private selectKernelSpecFromList(session?: IJupyterSessionManager, cancelToken?: CancellationToken): Promise<IJupyterKernelSpec | undefined> {}
33-
// }
34-
35-
export interface IKernelSelectionListProvider {
36-
getKernelSelections(cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]>;
37-
}
38-
39-
40-
@injectable()
41-
export class KernelSelectionProviderFactory {
42-
constructor(@inject(KernelService) private readonly kernelService: KernelService, @inject(IInterpreterSelector) private readonly interpreterSelector: IInterpreterSelector) {}
43-
public async getRemoteKernelSelectionProvider(session: IJupyterSessionManager): Promise<IKernelSelectionListProvider> {
44-
// tslint:disable: no-use-before-declare
45-
return new RemoteKernelSelectionProvider(session);
46-
}
47-
public async getLocalKernelSelectionProvider(session?: IJupyterSessionManager): Promise<IKernelSelectionListProvider> {
48-
return new LocalKernelSelectionProvider(this.kernelService, this.interpreterSelector, session);
49-
}
50-
}
51-
export class RemoteKernelSelectionProvider implements IKernelSelectionListProvider {
52-
constructor(private readonly session: IJupyterSessionManager) {}
53-
public async getKernelSelections(cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]> {
54-
return new JupyterSessionKernelSelectionnListProvider(this.session).getKernelSelections(cancelToken);
55-
}
56-
}
57-
58-
// tslint:disable: max-classes-per-file
59-
export class LocalKernelSelectionProvider implements IKernelSelectionListProvider {
60-
constructor(private readonly kernelService: KernelService, private readonly interpreterSelector: IInterpreterSelector, private readonly session?: IJupyterSessionManager) {}
61-
public async getKernelSelections(cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]> {
62-
const activeKernelsPromise = this.session ? new JupyterSessionKernelSelectionnListProvider(this.session).getKernelSelections(cancelToken) : Promise.resolve([]);
63-
const jupyterKernelsPromise = new JupyterKernelSelectionnListProvider(this.kernelService).getKernelSelections(cancelToken);
64-
const interpretersPromise = new InterpreterKernelSelectionnListProvider(this.interpreterSelector).getKernelSelections(cancelToken);
65-
const [activeKernels, jupyterKernels, interprters] = await Promise.all([activeKernelsPromise, jupyterKernelsPromise, interpretersPromise]);
66-
return [...activeKernels!, ...jupyterKernels!, ...interprters];
67-
}
68-
}
69-
70-
export class JupyterSessionKernelSelectionnListProvider implements IKernelSelectionListProvider {
30+
/**
31+
* Provider for active kernel specs in a jupyter session.
32+
*
33+
* @export
34+
* @class ActiveJupyterSessionKernelSelectionListProvider
35+
* @implements {IKernelSelectionListProvider}
36+
*/
37+
export class ActiveJupyterSessionKernelSelectionListProvider implements IKernelSelectionListProvider {
7138
constructor(private readonly session: IJupyterSessionManager) {}
7239
public async getKernelSelections(_cancelToken?: CancellationToken | undefined): Promise<IKernelSpecQuickPickItem[]> {
7340
const items = await this.session.getActiveKernelSpecs();
7441
return items.filter(item => item.display_name || item.name).map(getQuickPickFromKernelSpec);
7542
}
7643
}
7744

45+
/**
46+
* Provider for kernel specs in a jupyter process (`python -m jupyter kernelspec list`).
47+
*
48+
* @export
49+
* @class JupyterKernelSelectionnListProvider
50+
* @implements {IKernelSelectionListProvider}
51+
*/
7852
export class JupyterKernelSelectionnListProvider implements IKernelSelectionListProvider {
7953
constructor(private readonly kernelService: KernelService) {}
8054
public async getKernelSelections(cancelToken?: CancellationToken | undefined): Promise<IKernelSpecQuickPickItem[]> {
@@ -83,7 +57,13 @@ export class JupyterKernelSelectionnListProvider implements IKernelSelectionList
8357
}
8458
}
8559

86-
// tslint:disable: max-classes-per-file
60+
/**
61+
* Provider for interpreters to be treated as kernel specs.
62+
*
63+
* @export
64+
* @class InterpreterKernelSelectionnListProvider
65+
* @implements {IKernelSelectionListProvider}
66+
*/
8767
export class InterpreterKernelSelectionnListProvider implements IKernelSelectionListProvider {
8868
constructor(private readonly interpreterSelector: IInterpreterSelector) {}
8969
public async getKernelSelections(_cancelToken?: CancellationToken | undefined): Promise<IKernelSpecQuickPickItem[]> {
@@ -96,3 +76,40 @@ export class InterpreterKernelSelectionnListProvider implements IKernelSelection
9676
});
9777
}
9878
}
79+
80+
/**
81+
* Factory class that provides a kernel spec list provider (local or remote).
82+
*
83+
* @export
84+
* @class KernelSelectionProviderFactory
85+
*/
86+
@injectable()
87+
export class KernelSelectionProvider {
88+
constructor(@inject(KernelService) private readonly kernelService: KernelService, @inject(IInterpreterSelector) private readonly interpreterSelector: IInterpreterSelector) {}
89+
/**
90+
* Gets a selection of kernel specs from a remote session.
91+
*
92+
* @param {IJupyterSessionManager} session
93+
* @param {CancellationToken} [cancelToken]
94+
* @returns {Promise<IKernelSpecQuickPickItem[]>}
95+
* @memberof KernelSelectionProvider
96+
*/
97+
public async getKernelSelectionsForRemoteSession(session: IJupyterSessionManager, cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]> {
98+
return new ActiveJupyterSessionKernelSelectionListProvider(session).getKernelSelections(cancelToken);
99+
}
100+
/**
101+
* Gets a selection of kernel specs for a local session.
102+
*
103+
* @param {IJupyterSessionManager} [session]
104+
* @param {CancellationToken} [cancelToken]
105+
* @returns {Promise<IKernelSelectionListProvider>}
106+
* @memberof KernelSelectionProvider
107+
*/
108+
public async getLocalKernelSelectionProvider(session?: IJupyterSessionManager, cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]> {
109+
const activeKernelsPromise = session ? new ActiveJupyterSessionKernelSelectionListProvider(session).getKernelSelections(cancelToken) : Promise.resolve([]);
110+
const jupyterKernelsPromise = new JupyterKernelSelectionnListProvider(this.kernelService).getKernelSelections(cancelToken);
111+
const interpretersPromise = new InterpreterKernelSelectionnListProvider(this.interpreterSelector).getKernelSelections(cancelToken);
112+
const [activeKernels, jupyterKernels, interprters] = await Promise.all([activeKernelsPromise, jupyterKernelsPromise, interpretersPromise]);
113+
return [...activeKernels!, ...jupyterKernels!, ...interprters];
114+
}
115+
}

src/client/datascience/jupyter/kernels/kernelSelector.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import { PythonInterpreter } from '../../../interpreter/contracts';
1212
import { JupyterCommands } from '../../constants';
1313
import { IJupyterCommand, IJupyterKernelSpec, IJupyterSessionManager } from '../../types';
1414
import { JupyterCommandFinder, ModuleExistsStatus } from '../jupyterCommandFinder';
15-
import { KernelSelectionProviderFactory } from './kernelSelections';
15+
import { KernelSelectionProvider } from './kernelSelections';
1616
import { KernelService } from './kernelService';
1717

1818
@injectable()
1919
export class KernelSelector {
2020
constructor(
21-
@inject(KernelSelectionProviderFactory) private readonly providerFactory: KernelSelectionProviderFactory,
21+
@inject(KernelSelectionProvider) private readonly selectionProvider: KernelSelectionProvider,
2222
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell,
2323
@inject(JupyterCommandFinder) private readonly cmdFinder: JupyterCommandFinder,
2424
@inject(KernelService) private readonly kernelService: KernelService,
@@ -28,14 +28,12 @@ export class KernelSelector {
2828
options: { session?: IJupyterSessionManager } | { session: IJupyterSessionManager; isRemoteConnection: true },
2929
cancelToken?: CancellationToken
3030
): Promise<IJupyterKernelSpec | undefined> {
31-
const provider =
31+
const suggestions =
3232
'isRemoteConnection' in options
33-
? this.providerFactory.getRemoteKernelSelectionProvider(options.session)
34-
: this.providerFactory.getLocalKernelSelectionProvider(options.session);
33+
? this.selectionProvider.getKernelSelectionsForRemoteSession(options.session)
34+
: this.selectionProvider.getLocalKernelSelectionProvider(options.session);
3535

36-
const suggestions = (await provider).getKernelSelections(cancelToken);
3736
const selection = await this.applicationShell.showQuickPick(suggestions);
38-
3937
if (!selection) {
4038
return;
4139
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { CancellationToken, QuickPickItem } from 'vscode';
7+
import { PythonInterpreter } from '../../../interpreter/contracts';
8+
import { IJupyterKernelSpec } from '../../types';
9+
10+
export interface IKernelSpecQuickPickItem extends QuickPickItem {
11+
selection: { kernelSpec: IJupyterKernelSpec } | { interpreter: PythonInterpreter };
12+
}
13+
14+
export interface IKernelSelector {
15+
selectKernelSpec(cancelToken?: CancellationToken): Promise<IJupyterKernelSpec | undefined>;
16+
}
17+
18+
export interface IKernelSelectionListProvider {
19+
getKernelSelections(cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem[]>;
20+
}

0 commit comments

Comments
 (0)