Skip to content

Commit 6b23cd3

Browse files
authored
Merge pull request #21 from qianmoQ/dev-25.0.1
fix (language): 修复默认插件未设置值的问题
2 parents 47be7e9 + f34de54 commit 6b23cd3

5 files changed

Lines changed: 76 additions & 14 deletions

File tree

src-tauri/src/config.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::plugins::PluginConfig;
21
// 全局配置管理器
32
use crate::plugin::PluginManagerState;
3+
use crate::plugins::PluginConfig;
44
use log::{info, warn};
55
use serde::{Deserialize, Serialize};
66
use std::fs;
@@ -72,11 +72,8 @@ impl ConfigManager {
7272
Ok(mut config) => {
7373
println!("读取配置 -> 成功加载配置文件: {:?}", config_path);
7474

75-
// 检查 plugins 是否为 null,如果是则加载默认配置
76-
if config.plugins.is_none() {
77-
println!("读取配置 -> plugins 为 null,加载默认插件配置");
78-
config.plugins = Self::get_default_plugins_config(app_handle);
79-
}
75+
// 合并插件配置(现有配置 + 默认配置中缺失的插件)
76+
config.plugins = Self::merge_plugins_config(config.plugins, app_handle);
8077

8178
Ok(config)
8279
}
@@ -96,6 +93,59 @@ impl ConfigManager {
9693
}
9794
}
9895

96+
// 合并插件配置(现有配置 + 默认配置中缺失的插件)
97+
fn merge_plugins_config(
98+
existing_plugins: Option<Vec<PluginConfig>>,
99+
app_handle: Option<&AppHandle>,
100+
) -> Option<Vec<PluginConfig>> {
101+
// 获取所有默认插件配置
102+
let default_plugins = Self::get_default_plugins_config(app_handle).unwrap_or_default();
103+
104+
if let Some(existing) = existing_plugins {
105+
let mut merged_plugins = Vec::new();
106+
107+
// 遍历所有默认插件
108+
for default_plugin in &default_plugins {
109+
// 检查现有配置中是否已存在该插件
110+
if let Some(existing_plugin) = existing
111+
.iter()
112+
.find(|p| p.language == default_plugin.language)
113+
{
114+
// 如果存在,使用现有配置
115+
merged_plugins.push(existing_plugin.clone());
116+
println!("读取配置 -> 使用现有插件配置: {}", existing_plugin.language);
117+
} else {
118+
// 如果不存在,使用默认配置
119+
merged_plugins.push(default_plugin.clone());
120+
println!(
121+
"读取配置 -> 添加缺失的默认插件配置: {}",
122+
default_plugin.language
123+
);
124+
}
125+
}
126+
127+
// 添加现有配置中有但默认配置中没有的插件(用户自定义的插件)
128+
for existing_plugin in existing {
129+
if !default_plugins
130+
.iter()
131+
.any(|p| p.language == existing_plugin.language)
132+
{
133+
merged_plugins.push(existing_plugin.clone());
134+
println!(
135+
"读取配置 -> 保留用户自定义插件配置: {}",
136+
existing_plugin.language
137+
);
138+
}
139+
}
140+
141+
Some(merged_plugins)
142+
} else {
143+
// 如果现有配置中没有 plugins,直接使用默认配置
144+
println!("读取配置 -> plugins 为 null,使用默认插件配置");
145+
Some(default_plugins)
146+
}
147+
}
148+
99149
fn get_default_plugins_config(app_handle: Option<&AppHandle>) -> Option<Vec<PluginConfig>> {
100150
if let Some(handle) = app_handle {
101151
// 从 Tauri 状态中获取 PluginManager

src-tauri/src/plugins/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ pub trait LanguagePlugin: Send + Sync {
5656
fn get_language_key(&self) -> &'static str;
5757

5858
// 获取插件支持的文件扩展名
59-
fn get_file_extension(&self) -> String {
60-
self.get_config().unwrap().extension.clone()
61-
}
59+
fn get_file_extension(&self) -> String;
6260

6361
// 获取执行目录
6462
fn get_execute_home(&self) -> Option<PathBuf> {

src-tauri/src/plugins/python2.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ impl LanguagePlugin for Python2Plugin {
1616
"python2"
1717
}
1818

19+
fn get_file_extension(&self) -> String {
20+
self.get_config()
21+
.map(|config| config.extension.clone())
22+
.unwrap_or_else(|| "py".to_string())
23+
}
24+
1925
fn get_version_args(&self) -> Vec<&'static str> {
2026
vec!["--version"]
2127
}
@@ -39,6 +45,8 @@ impl LanguagePlugin for Python2Plugin {
3945
}
4046

4147
fn get_default_command(&self) -> String {
42-
self.get_config().unwrap().run_command.unwrap()
48+
self.get_config()
49+
.and_then(|config| config.run_command)
50+
.unwrap_or_else(|| "python2".to_string())
4351
}
4452
}

src-tauri/src/plugins/python3.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ impl LanguagePlugin for Python3Plugin {
1515
"python3"
1616
}
1717

18+
fn get_file_extension(&self) -> String {
19+
self.get_config()
20+
.map(|config| config.extension.clone())
21+
.unwrap_or_else(|| "py".to_string())
22+
}
23+
1824
fn get_version_args(&self) -> Vec<&'static str> {
1925
vec!["--version"]
2026
}
@@ -38,6 +44,8 @@ impl LanguagePlugin for Python3Plugin {
3844
}
3945

4046
fn get_default_command(&self) -> String {
41-
self.get_config().unwrap().run_command.unwrap()
47+
self.get_config()
48+
.and_then(|config| config.run_command)
49+
.unwrap_or_else(|| "python3".to_string())
4250
}
4351
}

src/App.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ const handleLanguageChange = async (newLanguage: string) => {
251251
252252
// 更新代码模板
253253
code.value = codeTemplates[newLanguage] || `# ${ getLanguageDisplayName(newLanguage) } Code
254-
# Write your code here...
255-
256-
print("Hello from ${ getLanguageDisplayName(newLanguage) }!")`
254+
# Write your code here...`
257255
258256
// 清空输出
259257
clearOutput()

0 commit comments

Comments
 (0)