Skip to content

Commit 8753f0e

Browse files
authored
Merge pull request #27 from qianmoQ/dev-25.0.1
feat (language): 支持 Java 语言
2 parents 7207044 + 983bcaf commit 8753f0e

12 files changed

Lines changed: 528 additions & 212 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@babel/runtime": "^7.28.2",
1414
"@codemirror/lang-go": "^6.0.1",
15+
"@codemirror/lang-java": "^6.0.2",
1516
"@codemirror/lang-javascript": "^6.2.4",
1617
"@codemirror/lang-python": "^6.2.1",
1718
"@codemirror/state": "^6.5.2",

public/icons/java.svg

Lines changed: 7 additions & 0 deletions
Loading

src-tauri/src/plugin.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,32 @@ pub async fn get_info(
3232
debug!("获取环境 -> 插件 [ {} ] 命令 {}", language, cmd);
3333

3434
let version_output = Command::new(&cmd).args(plugin.get_version_args()).output();
35+
debug!(
36+
"获取环境 -> 插件 [ {} ] 版本, 结果 {:?}",
37+
plugin.get_language_key(),
38+
version_output
39+
);
3540
if let Ok(version_out) = version_output {
3641
if version_out.status.success() {
3742
let path_result = Command::new(&cmd)
3843
.arg("-c")
3944
.arg(plugin.get_path_command())
4045
.output();
4146

42-
let version = String::from_utf8_lossy(&version_out.stdout)
47+
let mut version = String::from_utf8_lossy(&version_out.stdout)
4348
.trim()
4449
.to_string();
4550

51+
if version.is_empty() {
52+
info!(
53+
"获取环境 -> 调用插件 [ {} ] 版本为空,通过 stderr 获取",
54+
language
55+
);
56+
version = String::from_utf8_lossy(&version_out.stderr)
57+
.trim()
58+
.to_string();
59+
}
60+
4661
let path = if let Ok(path_out) = path_result {
4762
if path_out.status.success() {
4863
String::from_utf8_lossy(&path_out.stdout).trim().to_string()

src-tauri/src/plugins/java.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::{LanguagePlugin, PluginConfig};
2+
use std::vec;
3+
4+
pub struct JavaPlugin;
5+
6+
impl LanguagePlugin for JavaPlugin {
7+
fn get_order(&self) -> i32 {
8+
5
9+
}
10+
11+
fn get_language_name(&self) -> &'static str {
12+
"Java"
13+
}
14+
15+
fn get_language_key(&self) -> &'static str {
16+
"java"
17+
}
18+
19+
fn get_file_extension(&self) -> String {
20+
self.get_config()
21+
.map(|config| config.extension.clone())
22+
.unwrap_or_else(|| "java".to_string())
23+
}
24+
25+
fn get_version_args(&self) -> Vec<&'static str> {
26+
vec!["-version"]
27+
}
28+
29+
fn get_path_command(&self) -> String {
30+
"System.out.println(System.getProperty(\"java.home\"));".to_string()
31+
}
32+
33+
fn get_default_config(&self) -> PluginConfig {
34+
PluginConfig {
35+
enabled: true,
36+
language: String::from("java"),
37+
before_compile: None,
38+
extension: String::from("java"),
39+
execute_home: None,
40+
run_command: Some(String::from("java $filename")),
41+
after_compile: Some(String::from("rm -f *.class")),
42+
template: Some(String::from(
43+
"public class Main {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}",
44+
)),
45+
timeout: Some(30),
46+
}
47+
}
48+
49+
fn get_default_command(&self) -> String {
50+
self.get_config()
51+
.and_then(|config| config.run_command.clone())
52+
.unwrap_or_else(|| "java".to_string())
53+
}
54+
}

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
LanguagePlugin, PluginConfig, go::GoPlugin, nodejs::NodeJSPlugin, python2::Python2Plugin,
33
python3::Python3Plugin,
44
};
5+
use crate::plugins::java::JavaPlugin;
56
use std::collections::HashMap;
67

78
pub struct PluginManager {
@@ -16,6 +17,7 @@ impl PluginManager {
1617
plugins.insert("python3".to_string(), Box::new(Python3Plugin));
1718
plugins.insert("nodejs".to_string(), Box::new(NodeJSPlugin));
1819
plugins.insert("go".to_string(), Box::new(GoPlugin));
20+
plugins.insert("java".to_string(), Box::new(JavaPlugin));
1921

2022
Self { plugins }
2123
}

src-tauri/src/plugins/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::get_app_config_internal;
22
use log::{debug, info};
33
use serde::{Deserialize, Serialize};
4+
use std::format;
45
use std::path::PathBuf;
56

67
// 通用结构定义
@@ -329,6 +330,7 @@ pub trait LanguagePlugin: Send + Sync {
329330

330331
// 重新导出子模块
331332
pub mod go;
333+
pub mod java;
332334
pub mod manager;
333335
pub mod nodejs;
334336
pub mod python2;

src/App.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ const {
8282
lastExecutionTime,
8383
runCode,
8484
stopCode,
85-
clearOutput
85+
clearOutput,
86+
handleRealtimeOutput,
87+
handleExecutionComplete,
88+
handleExecutionStopped,
89+
handleExecutionTimeout,
90+
handleExecutionError
8691
} = useCodeExecution(toast)
8792
8893
const {
@@ -113,7 +118,12 @@ const { initializeEventListeners, cleanupEventListeners } = useEventManager({
113118
isSuccess,
114119
lastExecutionTime,
115120
currentLanguage,
116-
toast
121+
toast,
122+
handleRealtimeOutput,
123+
handleExecutionComplete,
124+
handleExecutionStopped,
125+
handleExecutionTimeout,
126+
handleExecutionError
117127
})
118128
119129
// 禁用右键菜单

src/components/CodeEditor.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const {
2929
extensions,
3030
editorConfig,
3131
initializeEditor
32-
} = useCodeMirrorEditor(props, emit)
32+
} = useCodeMirrorEditor(props)
3333
3434
const handleInput = (value: string) => {
3535
emit('update:modelValue', value)

0 commit comments

Comments
 (0)