forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatascience.ts
More file actions
105 lines (93 loc) · 4.38 KB
/
datascience.ts
File metadata and controls
105 lines (93 loc) · 4.38 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as vscode from 'vscode';
import { ICommandManager } from '../common/application/types';
import { PYTHON } from '../common/constants';
import { IDisposableRegistry, IExtensionContext } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { Commands } from './constants';
import { ICodeWatcher, IDataScience, IDataScienceCodeLensProvider, IDataScienceCommandListener } from './types';
@injectable()
export class DataScience implements IDataScience {
private readonly commandManager: ICommandManager;
private readonly disposableRegistry: IDisposableRegistry;
private readonly extensionContext: IExtensionContext;
private readonly dataScienceCodeLensProvider: IDataScienceCodeLensProvider;
private readonly commandListeners: IDataScienceCommandListener[];
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer)
{
this.commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
this.disposableRegistry = this.serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
this.extensionContext = this.serviceContainer.get<IExtensionContext>(IExtensionContext);
this.dataScienceCodeLensProvider = this.serviceContainer.get<IDataScienceCodeLensProvider>(IDataScienceCodeLensProvider);
this.commandListeners = this.serviceContainer.getAll<IDataScienceCommandListener>(IDataScienceCommandListener);
// We could potentially register different commands if jupyter isn't installed.
}
public async activate(): Promise<void> {
this.registerCommands();
this.extensionContext.subscriptions.push(
vscode.languages.registerCodeLensProvider(
PYTHON, this.dataScienceCodeLensProvider
)
);
}
public runAllCells(codeWatcher: ICodeWatcher): Promise<void> {
let activeCodeWatcher: ICodeWatcher | undefined = codeWatcher;
if (!activeCodeWatcher) {
activeCodeWatcher = this.getCurrentCodeWatcher();
}
if (activeCodeWatcher) {
return activeCodeWatcher.runAllCells();
} else {
return Promise.resolve();
}
}
public runCell(codeWatcher: ICodeWatcher, range: vscode.Range): Promise<void> {
if (codeWatcher) {
return codeWatcher.runCell(range);
} else {
return this.runCurrentCell();
}
}
public runCurrentCell(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runCurrentCell();
} else {
return Promise.resolve();
}
}
public runCurrentCellAndAdvance(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runCurrentCellAndAdvance();
} else {
return Promise.resolve();
}
}
// Get our matching code watcher for the active document
private getCurrentCodeWatcher(): ICodeWatcher | undefined {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor || !activeEditor.document)
{
return undefined;
}
// Ask our code lens provider to find the matching code watcher for the current document
return this.dataScienceCodeLensProvider.getCodeWatcher(activeEditor.document);
}
private registerCommands(): void {
let disposable = this.commandManager.registerCommand(Commands.RunAllCells, this.runAllCells, this);
this.disposableRegistry.push(disposable);
disposable = this.commandManager.registerCommand(Commands.RunCell, this.runCell, this);
this.disposableRegistry.push(disposable);
disposable = this.commandManager.registerCommand(Commands.RunCurrentCell, this.runCurrentCell, this);
this.disposableRegistry.push(disposable);
disposable = this.commandManager.registerCommand(Commands.RunCurrentCellAdvance, this.runCurrentCellAndAdvance, this);
this.disposableRegistry.push(disposable);
this.commandListeners.forEach((listener: IDataScienceCommandListener) => {
listener.register(this.commandManager);
});
}
}