Skip to content

Commit 2a1d111

Browse files
authored
Merge pull request #28 from qianmoQ/dev-25.0.1
feat (language): 支持 Shell 语言
2 parents 8753f0e + 33c636a commit 2a1d111

8 files changed

Lines changed: 80 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ CodeForge 是一款轻量级、高性能的桌面代码执行器,专为开发
2020

2121
- **Python 2**
2222
- **Python 3**
23+
- **Node.js**
24+
- **Go**
25+
- **Java**
2326
- **...更多语言敬请期待**
2427

2528
## 安装

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"@codemirror/lang-java": "^6.0.2",
1616
"@codemirror/lang-javascript": "^6.2.4",
1717
"@codemirror/lang-python": "^6.2.1",
18+
"@codemirror/language": "^6.11.2",
19+
"@codemirror/legacy-modes": "^6.5.1",
1820
"@codemirror/state": "^6.5.2",
1921
"@codemirror/view": "^6.38.1",
2022
"@tauri-apps/api": "^2",

public/icons/shell.svg

Lines changed: 13 additions & 0 deletions
Loading

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
python3::Python3Plugin,
44
};
55
use crate::plugins::java::JavaPlugin;
6+
use crate::plugins::shell::ShellPlugin;
67
use std::collections::HashMap;
78

89
pub struct PluginManager {
@@ -18,6 +19,7 @@ impl PluginManager {
1819
plugins.insert("nodejs".to_string(), Box::new(NodeJSPlugin));
1920
plugins.insert("go".to_string(), Box::new(GoPlugin));
2021
plugins.insert("java".to_string(), Box::new(JavaPlugin));
22+
plugins.insert("shell".to_string(), Box::new(ShellPlugin));
2123

2224
Self { plugins }
2325
}

src-tauri/src/plugins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,6 @@ pub mod manager;
335335
pub mod nodejs;
336336
pub mod python2;
337337
pub mod python3;
338+
pub mod shell;
338339

339340
pub use manager::PluginManager;

src-tauri/src/plugins/shell.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 ShellPlugin;
5+
6+
impl LanguagePlugin for ShellPlugin {
7+
fn get_order(&self) -> i32 {
8+
6
9+
}
10+
11+
fn get_language_name(&self) -> &'static str {
12+
"Shell"
13+
}
14+
15+
fn get_language_key(&self) -> &'static str {
16+
"shell"
17+
}
18+
19+
fn get_file_extension(&self) -> String {
20+
self.get_config()
21+
.map(|config| config.extension.clone())
22+
.unwrap_or_else(|| "sh".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+
"which bash || which sh".to_string()
31+
}
32+
33+
fn get_default_config(&self) -> PluginConfig {
34+
PluginConfig {
35+
enabled: true,
36+
language: String::from("shell"),
37+
before_compile: None,
38+
extension: String::from("sh"),
39+
execute_home: None,
40+
run_command: Some(String::from("bash $filename")),
41+
after_compile: None,
42+
template: Some(String::from(
43+
"#!/bin/bash\n# Shell 示例代码 - CodeForge 代码执行环境\n\necho \"🎉 欢迎使用 CodeForge!\"\necho \"Welcome to CodeForge!\"\necho \"\"\n\necho \"=========================================\"\necho \" CodeForge Shell \"\necho \"=========================================\"\necho \"\"\n\n# 基本输出示例\necho \"✅ Shell 运行成功! (Shell is working!)\"\necho \"🐚 这是 Shell 脚本 (This is Shell script)\"\necho \"\"\n\n# 变量操作\nname=\"CodeForge\"\nversion=\"Shell\"\nnumber1=10\nnumber2=20\nresult=$((number1 + number2))\n\necho \"🔢 简单计算 (Simple calculation):\"\necho \"$number1 + $number2 = $result\"\necho \"\"\n\n# 字符串操作\necho \"📝 字符串操作 (String operations):\"\necho \"平台名称 (Platform): $name\"\necho \"语言版本 (Language): $version\"\necho \"完整信息 (Full info): $name - $version\"\necho \"\"\n\n# 循环示例\necho \"🔄 循环输出 (Loop output):\"\nfor i in {1..5}; do\n echo \"第 $i 次输出 (Output #$i): Hello from CodeForge!\"\ndone\necho \"\"\n\n# 数组操作\nfruits=(\"苹果\" \"香蕉\" \"橙子\" \"葡萄\")\necho \"🍎 水果列表 (Fruit list):\"\nfor i in \"${!fruits[@]}\"; do\n echo \"$((i + 1)). ${fruits[i]}\"\ndone\necho \"\"\n\n# 条件判断\nscore=85\necho \"📊 成绩评估 (Score evaluation):\"\nif [ $score -ge 90 ]; then\n echo \"优秀! (Excellent!)\"\nelif [ $score -ge 80 ]; then\n echo \"良好! (Good!)\"\nelif [ $score -ge 60 ]; then\n echo \"及格 (Pass)\"\nelse\n echo \"需要努力 (Need improvement)\"\nfi\n\necho \"\"\necho \"🎯 CodeForge Shell 代码执行完成!\"\necho \"🎯 CodeForge Shell execution completed!\"\necho \"\"\necho \"感谢使用 CodeForge 代码执行环境! 🚀\"\necho \"Thank you for using CodeForge! 🚀\"",
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(|| "bash".to_string())
53+
}
54+
}

src/components/setting/Language.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<template #template>
9999
<div class="w-[98%]">
100100
<Codemirror v-if="isEditorReady && pluginConfig.template !== undefined"
101-
style="width: 100%; height: 380px"
101+
style="width: 102%; height: 380px"
102102
v-model="pluginConfig.template"
103103
:extensions="currentExtensions"
104104
class="flex-1 border border-gray-300 dark:border-gray-600 rounded-md overflow-hidden"/>

src/composables/useCodeMirrorEditor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { python } from '@codemirror/lang-python'
33
import { javascript } from '@codemirror/lang-javascript'
44
import { go } from '@codemirror/lang-go'
55
import { java } from '@codemirror/lang-java'
6+
import { shell } from '@codemirror/legacy-modes/mode/shell'
67
import {
78
abcdef,
89
abyss,
@@ -53,6 +54,7 @@ import {
5354
} from '@uiw/codemirror-themes-all'
5455
import { invoke } from '@tauri-apps/api/core'
5556
import { useToast } from '../plugins/toast'
57+
import { StreamLanguage } from '@codemirror/language'
5658

5759
interface EditorConfig
5860
{
@@ -153,6 +155,8 @@ export function useCodeMirrorEditor(props: Props)
153155
return go()
154156
case 'java':
155157
return java()
158+
case 'shell':
159+
return StreamLanguage.define(shell)
156160
default:
157161
return null
158162
}

0 commit comments

Comments
 (0)