forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeExecutionManager.ts
171 lines (158 loc) · 8.54 KB
/
codeExecutionManager.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Disposable, EventEmitter, Uri } from 'vscode';
import { ICommandManager, IDocumentManager } from '../../common/application/types';
import { Commands } from '../../common/constants';
import '../../common/extensions';
import { IDisposableRegistry, IConfigurationService, Resource } from '../../common/types';
import { noop } from '../../common/utils/misc';
import { IInterpreterService } from '../../interpreter/contracts';
import { IServiceContainer } from '../../ioc/types';
import { traceError, traceVerbose } from '../../logging';
import { captureTelemetry, sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { ICodeExecutionHelper, ICodeExecutionManager, ICodeExecutionService } from '../../terminals/types';
import {
CreateEnvironmentCheckKind,
triggerCreateEnvironmentCheckNonBlocking,
} from '../../pythonEnvironments/creation/createEnvironmentTrigger';
@injectable()
export class CodeExecutionManager implements ICodeExecutionManager {
private eventEmitter: EventEmitter<string> = new EventEmitter<string>();
constructor(
@inject(ICommandManager) private commandManager: ICommandManager,
@inject(IDocumentManager) private documentManager: IDocumentManager,
@inject(IDisposableRegistry) private disposableRegistry: Disposable[],
@inject(IConfigurationService) private readonly configSettings: IConfigurationService,
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
) {}
public registerCommands() {
[Commands.Exec_In_Terminal, Commands.Exec_In_Terminal_Icon, Commands.Exec_In_Separate_Terminal].forEach(
(cmd) => {
this.disposableRegistry.push(
this.commandManager.registerCommand(cmd as any, async (file: Resource) => {
traceVerbose(`Attempting to run Python file`, file?.fsPath);
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = await interpreterService.getActiveInterpreter(file);
if (!interpreter) {
this.commandManager
.executeCommand(Commands.TriggerEnvironmentSelection, file)
.then(noop, noop);
return;
}
sendTelemetryEvent(EventName.ENVIRONMENT_CHECK_TRIGGER, undefined, {
trigger: 'run-in-terminal',
});
triggerCreateEnvironmentCheckNonBlocking(CreateEnvironmentCheckKind.File, file);
const trigger = cmd === Commands.Exec_In_Terminal ? 'command' : 'icon';
await this.executeFileInTerminal(file, trigger, {
newTerminalPerFile: cmd === Commands.Exec_In_Separate_Terminal,
})
.then(() => {
if (this.shouldTerminalFocusOnStart(file))
this.commandManager.executeCommand('workbench.action.terminal.focus');
})
.catch((ex) => traceError('Failed to execute file in terminal', ex));
}),
);
},
);
this.disposableRegistry.push(
this.commandManager.registerCommand(Commands.Exec_Selection_In_Terminal as any, async (file: Resource) => {
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = await interpreterService.getActiveInterpreter(file);
if (!interpreter) {
this.commandManager.executeCommand(Commands.TriggerEnvironmentSelection, file).then(noop, noop);
return;
}
sendTelemetryEvent(EventName.ENVIRONMENT_CHECK_TRIGGER, undefined, { trigger: 'run-selection' });
triggerCreateEnvironmentCheckNonBlocking(CreateEnvironmentCheckKind.File, file);
await this.executeSelectionInTerminal().then(() => {
if (this.shouldTerminalFocusOnStart(file))
this.commandManager.executeCommand('workbench.action.terminal.focus');
});
}),
);
this.disposableRegistry.push(
this.commandManager.registerCommand(
Commands.Exec_Selection_In_Django_Shell as any,
async (file: Resource) => {
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = await interpreterService.getActiveInterpreter(file);
if (!interpreter) {
this.commandManager.executeCommand(Commands.TriggerEnvironmentSelection, file).then(noop, noop);
return;
}
sendTelemetryEvent(EventName.ENVIRONMENT_CHECK_TRIGGER, undefined, { trigger: 'run-selection' });
triggerCreateEnvironmentCheckNonBlocking(CreateEnvironmentCheckKind.File, file);
await this.executeSelectionInDjangoShell().then(() => {
if (this.shouldTerminalFocusOnStart(file))
this.commandManager.executeCommand('workbench.action.terminal.focus');
});
},
),
);
}
private async executeFileInTerminal(
file: Resource,
trigger: 'command' | 'icon',
options?: { newTerminalPerFile: boolean },
): Promise<void> {
sendTelemetryEvent(EventName.EXECUTION_CODE, undefined, {
scope: 'file',
trigger,
newTerminalPerFile: options?.newTerminalPerFile,
});
const codeExecutionHelper = this.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
file = file instanceof Uri ? file : undefined;
let fileToExecute = file ? file : await codeExecutionHelper.getFileToExecute();
if (!fileToExecute) {
return;
}
const fileAfterSave = await codeExecutionHelper.saveFileIfDirty(fileToExecute);
if (fileAfterSave) {
fileToExecute = fileAfterSave;
}
const executionService = this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService, 'standard');
await executionService.executeFile(fileToExecute, options);
}
@captureTelemetry(EventName.EXECUTION_CODE, { scope: 'selection' }, false)
private async executeSelectionInTerminal(): Promise<void> {
const executionService = this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService, 'standard');
await this.executeSelection(executionService);
}
@captureTelemetry(EventName.EXECUTION_DJANGO, { scope: 'selection' }, false)
private async executeSelectionInDjangoShell(): Promise<void> {
const executionService = this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService, 'djangoShell');
await this.executeSelection(executionService);
}
private async executeSelection(executionService: ICodeExecutionService): Promise<void> {
const activeEditor = this.documentManager.activeTextEditor;
if (!activeEditor) {
return;
}
const codeExecutionHelper = this.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor!);
let wholeFileContent = '';
if (activeEditor && activeEditor.document) {
wholeFileContent = activeEditor.document.getText();
}
const normalizedCode = await codeExecutionHelper.normalizeLines(codeToExecute!, wholeFileContent);
if (!normalizedCode || normalizedCode.trim().length === 0) {
return;
}
try {
this.eventEmitter.fire(normalizedCode);
} catch {
// Ignore any errors that occur for firing this event. It's only used
// for telemetry
noop();
}
await executionService.execute(normalizedCode, activeEditor!.document.uri);
}
private shouldTerminalFocusOnStart(uri: Uri | undefined): boolean {
return this.configSettings.getSettings(uri)?.terminal.focusAfterLaunch;
}
}