forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplCommands.ts
More file actions
95 lines (87 loc) · 3.6 KB
/
replCommands.ts
File metadata and controls
95 lines (87 loc) · 3.6 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
import { commands, Uri, window } from 'vscode';
import { Disposable } from 'vscode-jsonrpc';
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';
/**
* Registers REPL command for shift+enter if sendToNativeREPL setting is enabled.
* @param disposables
* @param interpreterService
* @returns Promise<void>
*/
export async function registerReplCommands(
disposables: Disposable[],
interpreterService: IInterpreterService,
executionHelper: ICodeExecutionHelper,
): Promise<void> {
disposables.push(
commands.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 = 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!, wholeFileContent);
await nativeRepl.sendToNativeRepl(normalizedCode);
}
}
}
}),
);
}
/**
* Command triggered for 'Enter': Conditionally call interactive.execute OR insert \n in text input box.
* @param disposables
* @param interpreterService
*/
export async function registerReplExecuteOnEnter(
disposables: Disposable[],
interpreterService: IInterpreterService,
): Promise<void> {
disposables.push(
commands.registerCommand(Commands.Exec_In_REPL_Enter, async (uri: Uri) => {
const interpreter = await interpreterService.getActiveInterpreter(uri);
if (!interpreter) {
commands.executeCommand(Commands.TriggerEnvironmentSelection, uri).then(noop, noop);
return;
}
const nativeRepl = 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('interactive.execute');
} 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('interactive.execute');
}
}
}
}),
);
}