forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcodeExecutionManager.ts
More file actions
232 lines (210 loc) · 11.1 KB
/
codeExecutionManager.ts
File metadata and controls
232 lines (210 loc) · 11.1 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Disposable, EventEmitter, Terminal, Uri } from 'vscode';
import * as path from 'path';
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';
import { ReplType } from '../../repl/types';
import { runInDedicatedTerminal, runInTerminal, useEnvExtension } from '../../envExt/api.internal';
@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 trigger = cmd === Commands.Exec_In_Terminal ? 'command' : 'icon';
const newTerminalPerFile = cmd === Commands.Exec_In_Separate_Terminal;
if (useEnvExtension()) {
try {
await this.executeUsingExtension(file, cmd === Commands.Exec_In_Separate_Terminal);
} catch (ex) {
traceError('Failed to execute file in terminal', ex);
}
sendTelemetryEvent(EventName.ENVIRONMENT_CHECK_TRIGGER, undefined, {
trigger: 'run-in-terminal',
});
sendTelemetryEvent(EventName.EXECUTION_CODE, undefined, {
scope: 'file',
trigger,
newTerminalPerFile,
});
return;
}
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);
await this.executeFileInTerminal(file, trigger, {
newTerminalPerFile,
})
.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 executeUsingExtension(file: Resource, dedicated: boolean): Promise<void> {
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;
}
// Check on setting terminal.executeInFileDir
const pythonSettings = this.configSettings.getSettings(file);
let cwd = pythonSettings.terminal.executeInFileDir ? path.dirname(fileToExecute.fsPath) : undefined;
// Check on setting terminal.launchArgs
const launchArgs = pythonSettings.terminal.launchArgs;
const totalArgs = [...launchArgs, fileToExecute.fsPath.fileToCommandArgumentForPythonExt()];
const show = this.shouldTerminalFocusOnStart(fileToExecute);
let terminal: Terminal | undefined;
if (dedicated) {
terminal = await runInDedicatedTerminal(fileToExecute, totalArgs, cwd, show);
} else {
terminal = await runInTerminal(fileToExecute, totalArgs, cwd, show);
}
if (terminal) {
terminal.show();
}
}
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!,
ReplType.terminal,
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;
}
}