forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathreplCommands.ts
More file actions
133 lines (124 loc) · 4.93 KB
/
replCommands.ts
File metadata and controls
133 lines (124 loc) · 4.93 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
import { commands, Uri, window } from 'vscode';
import { Disposable } from 'vscode-jsonrpc';
import { ICommandManager } from '../common/application/types';
import { Commands } from '../common/constants';
import { noop } from '../common/utils/misc';
import { IInterpreterService } from '../interpreter/contracts';
import { ICodeExecutionHelper } from '../terminals/types';
import { getNativeRepl } from './nativeRepl';
import {
executeInTerminal,
getActiveInterpreter,
getSelectedTextToExecute,
getSendToNativeREPLSetting,
insertNewLineToREPLInput,
isMultiLineText,
} from './replUtils';
import { registerCommand } from '../common/vscodeApis/commandApis';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { ReplType } from './types';
/**
* Register Start Native REPL command in the command palette
*/
export async function registerStartNativeReplCommand(
disposables: Disposable[],
interpreterService: IInterpreterService,
): Promise<void> {
disposables.push(
registerCommand(Commands.Start_Native_REPL, async (uri: Uri) => {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Native' });
const interpreter = await getActiveInterpreter(uri, interpreterService);
if (interpreter) {
const nativeRepl = await getNativeRepl(interpreter, disposables);
await nativeRepl.sendToNativeRepl(undefined, false);
}
}),
);
}
/**
* Registers REPL command for shift+enter if sendToNativeREPL setting is enabled.
*/
export async function registerReplCommands(
disposables: Disposable[],
interpreterService: IInterpreterService,
executionHelper: ICodeExecutionHelper,
commandManager: ICommandManager,
): Promise<void> {
disposables.push(
commandManager.registerCommand(Commands.Exec_In_REPL, async (uri: Uri) => {
const nativeREPLSetting = getSendToNativeREPLSetting();
if (!nativeREPLSetting) {
await executeInTerminal();
return;
}
const interpreter = await getActiveInterpreter(uri, interpreterService);
if (interpreter) {
const nativeRepl = await getNativeRepl(interpreter, disposables);
const activeEditor = window.activeTextEditor;
if (activeEditor) {
const code = await getSelectedTextToExecute(activeEditor);
if (code) {
// Smart Send
let wholeFileContent = '';
if (activeEditor && activeEditor.document) {
wholeFileContent = activeEditor.document.getText();
}
const normalizedCode = await executionHelper.normalizeLines(
code!,
ReplType.native,
wholeFileContent,
);
await nativeRepl.sendToNativeRepl(normalizedCode);
}
}
}
}),
);
}
/**
* Command triggered for 'Enter': Conditionally call interactive.execute OR insert \n in text input box.
*/
export async function registerReplExecuteOnEnter(
disposables: Disposable[],
interpreterService: IInterpreterService,
commandManager: ICommandManager,
): Promise<void> {
disposables.push(
commandManager.registerCommand(Commands.Exec_In_REPL_Enter, async (uri: Uri) => {
await onInputEnter(uri, 'repl.execute', interpreterService, disposables);
}),
);
disposables.push(
commandManager.registerCommand(Commands.Exec_In_IW_Enter, async (uri: Uri) => {
await onInputEnter(uri, 'interactive.execute', interpreterService, disposables);
}),
);
}
async function onInputEnter(
uri: Uri,
commandName: string,
interpreterService: IInterpreterService,
disposables: Disposable[],
): Promise<void> {
const interpreter = await interpreterService.getActiveInterpreter(uri);
if (!interpreter) {
commands.executeCommand(Commands.TriggerEnvironmentSelection, uri).then(noop, noop);
return;
}
const nativeRepl = await getNativeRepl(interpreter, disposables);
const completeCode = await nativeRepl?.checkUserInputCompleteCode(window.activeTextEditor);
const editor = window.activeTextEditor;
if (editor) {
// Execute right away when complete code and Not multi-line
if (completeCode && !isMultiLineText(editor)) {
await commands.executeCommand(commandName);
} else {
insertNewLineToREPLInput(editor);
// Handle case when user enters on blank line, just trigger interactive.execute
if (editor && editor.document.lineAt(editor.selection.active.line).text === '') {
await commands.executeCommand(commandName);
}
}
}
}