-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathLocalExecutionExtension.swift
84 lines (73 loc) · 2.97 KB
/
LocalExecutionExtension.swift
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
//
// LocalExecutionExtension.swift
// Code
//
// Created by Ken Chung on 19/11/2022.
//
import Foundation
private let EXTENSION_ID = "LOCAL_EXECUTION"
private let LOCAL_EXECUTION_COMMANDS = [
"py": ["python3 -u {url}"],
"js": ["node {url}"],
"c": ["clang {url}", "wasm a.out"],
"cpp": ["clang++ {url}", "wasm a.out"],
"php": ["php {url}"],
"java": [
"javac {url}",
"java -classpath \"{url_parent}\" \"{last_path_component_without_extension}\"",
],
]
class LocalExecutionExtension: CodeAppExtension {
override func onInitialize(app: MainApp, contribution: CodeAppExtension.Contribution) {
let toolbarItem = ToolbarItem(
extenionID: EXTENSION_ID,
icon: "play",
onClick: {
Task {
await self.runCodeLocally(app: app)
}
},
shortCut: .init("r", modifiers: [.command]),
panelToFocusOnTap: "TERMINAL",
shouldDisplay: {
guard let activeTextEditor = app.activeTextEditor else { return false }
return activeTextEditor.url.isFileURL
&& LOCAL_EXECUTION_COMMANDS[activeTextEditor.languageIdentifier] != nil
}
)
contribution.toolBar.registerItem(item: toolbarItem)
}
private func runCodeLocally(app: MainApp) async {
guard app.terminalInstance.executor?.state == .idle else { return }
guard let activeTextEditor = app.activeTextEditor else {
return
}
guard let commands = LOCAL_EXECUTION_COMMANDS[activeTextEditor.languageIdentifier] else {
return
}
await app.saveCurrentFile()
let sanitizedUrl = activeTextEditor.url.path.replacingOccurrences(of: " ", with: #"\ "#)
let urlParent = activeTextEditor.url.deletingLastPathComponent().path
let lastPathComponentWithoutExtension = activeTextEditor.url.deletingPathExtension()
.lastPathComponent
.replacingOccurrences(of: " ", with: #"\ "#)
let parsedCommands = commands.map {
$0
.replacingOccurrences(of: "{url}", with: sanitizedUrl)
.replacingOccurrences(of: "{url_parent}", with: urlParent)
.replacingOccurrences(
of: "{last_path_component_without_extension}",
with: lastPathComponentWithoutExtension)
}
if app.terminalOptions.value.shouldShowCompilerPath {
app.terminalInstance.executeScript(
"localEcho.println(`\(parsedCommands.joined(separator: " && "))`);readLine('');")
} else {
let commandName =
parsedCommands.first?.components(separatedBy: " ").first
?? activeTextEditor.languageIdentifier
app.terminalInstance.executeScript("localEcho.println(`\(commandName)`);readLine('');")
}
app.terminalInstance.executor?.evaluateCommands(parsedCommands)
}
}