/
extension.ts
86 lines (72 loc) · 2.5 KB
/
extension.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
import * as vscode from "vscode";
import * as utils from "./utils";
import * as doc from "./document_parser";
import { SelectedText } from "./selected_text";
import { printConstructor } from "./print_constructor";
import { PRINT_COMMANDS, DOCUMENT_COMMANDS } from "./statements";
import { getConfig } from "./config";
import { config } from "process";
export async function executeCommand(
statement: string
): Promise<string | void> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const selectedText = new SelectedText(editor);
const text = selectedText.getText();
if (!text) {
return;
}
if (getConfig().get("prints.useDoubleQuotes")) {
statement = statement.replace(/'/g, "\"");
}
for (const match of text) {
if (selectedText.hasCodeBlock()) {
await vscode.commands.executeCommand("editor.action.jumpToBracket");
await vscode.commands.executeCommand("editor.action.jumpToBracket");
}
await vscode.commands
.executeCommand("editor.action.insertLineAfter")
.then(() => {
const stringStatement = printConstructor(statement);
const insertText = stringStatement.replace(/\{text\}/g, match);
editor.edit((editBuilder) => {
const { selection } = editor;
editBuilder.insert(
new vscode.Position(
selection.start.line,
selection.start.character
),
insertText
);
});
});
}
}
export function activate(context: vscode.ExtensionContext): void {
// Print Commands
for (const [key, statement] of Object.entries(PRINT_COMMANDS)) {
context.subscriptions.push(
vscode.commands.registerCommand(statement.command, () => {
executeCommand(statement.statement);
})
);
}
// Document parser
for (const [action, command] of Object.entries(DOCUMENT_COMMANDS)) {
context.subscriptions.push(
vscode.commands.registerCommand(command, () => {
void doc.executeCommand(action);
})
);
}
context.subscriptions.push(
vscode.commands.registerCommand(
"python-easy-print.easyPrintPy2",
() => {
void utils.initPrintPython2();
}
)
);
}