diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 876ec73..9fc1e39 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -1,6 +1,6 @@ -use crate::plugins::PluginConfig; // 全局配置管理器 use crate::plugin::PluginManagerState; +use crate::plugins::PluginConfig; use log::{info, warn}; use serde::{Deserialize, Serialize}; use std::fs; @@ -72,11 +72,8 @@ impl ConfigManager { Ok(mut config) => { println!("读取配置 -> 成功加载配置文件: {:?}", config_path); - // 检查 plugins 是否为 null,如果是则加载默认配置 - if config.plugins.is_none() { - println!("读取配置 -> plugins 为 null,加载默认插件配置"); - config.plugins = Self::get_default_plugins_config(app_handle); - } + // 合并插件配置(现有配置 + 默认配置中缺失的插件) + config.plugins = Self::merge_plugins_config(config.plugins, app_handle); Ok(config) } @@ -96,6 +93,59 @@ impl ConfigManager { } } + // 合并插件配置(现有配置 + 默认配置中缺失的插件) + fn merge_plugins_config( + existing_plugins: Option>, + app_handle: Option<&AppHandle>, + ) -> Option> { + // 获取所有默认插件配置 + let default_plugins = Self::get_default_plugins_config(app_handle).unwrap_or_default(); + + if let Some(existing) = existing_plugins { + let mut merged_plugins = Vec::new(); + + // 遍历所有默认插件 + for default_plugin in &default_plugins { + // 检查现有配置中是否已存在该插件 + if let Some(existing_plugin) = existing + .iter() + .find(|p| p.language == default_plugin.language) + { + // 如果存在,使用现有配置 + merged_plugins.push(existing_plugin.clone()); + println!("读取配置 -> 使用现有插件配置: {}", existing_plugin.language); + } else { + // 如果不存在,使用默认配置 + merged_plugins.push(default_plugin.clone()); + println!( + "读取配置 -> 添加缺失的默认插件配置: {}", + default_plugin.language + ); + } + } + + // 添加现有配置中有但默认配置中没有的插件(用户自定义的插件) + for existing_plugin in existing { + if !default_plugins + .iter() + .any(|p| p.language == existing_plugin.language) + { + merged_plugins.push(existing_plugin.clone()); + println!( + "读取配置 -> 保留用户自定义插件配置: {}", + existing_plugin.language + ); + } + } + + Some(merged_plugins) + } else { + // 如果现有配置中没有 plugins,直接使用默认配置 + println!("读取配置 -> plugins 为 null,使用默认插件配置"); + Some(default_plugins) + } + } + fn get_default_plugins_config(app_handle: Option<&AppHandle>) -> Option> { if let Some(handle) = app_handle { // 从 Tauri 状态中获取 PluginManager diff --git a/src-tauri/src/plugins/mod.rs b/src-tauri/src/plugins/mod.rs index 304944e..c6c0fbf 100644 --- a/src-tauri/src/plugins/mod.rs +++ b/src-tauri/src/plugins/mod.rs @@ -56,9 +56,7 @@ pub trait LanguagePlugin: Send + Sync { fn get_language_key(&self) -> &'static str; // 获取插件支持的文件扩展名 - fn get_file_extension(&self) -> String { - self.get_config().unwrap().extension.clone() - } + fn get_file_extension(&self) -> String; // 获取执行目录 fn get_execute_home(&self) -> Option { diff --git a/src-tauri/src/plugins/python2.rs b/src-tauri/src/plugins/python2.rs index 86fa37e..8be3214 100644 --- a/src-tauri/src/plugins/python2.rs +++ b/src-tauri/src/plugins/python2.rs @@ -16,6 +16,12 @@ impl LanguagePlugin for Python2Plugin { "python2" } + fn get_file_extension(&self) -> String { + self.get_config() + .map(|config| config.extension.clone()) + .unwrap_or_else(|| "py".to_string()) + } + fn get_version_args(&self) -> Vec<&'static str> { vec!["--version"] } @@ -39,6 +45,8 @@ impl LanguagePlugin for Python2Plugin { } fn get_default_command(&self) -> String { - self.get_config().unwrap().run_command.unwrap() + self.get_config() + .and_then(|config| config.run_command) + .unwrap_or_else(|| "python2".to_string()) } } diff --git a/src-tauri/src/plugins/python3.rs b/src-tauri/src/plugins/python3.rs index 3bb8dfe..e1cc200 100644 --- a/src-tauri/src/plugins/python3.rs +++ b/src-tauri/src/plugins/python3.rs @@ -15,6 +15,12 @@ impl LanguagePlugin for Python3Plugin { "python3" } + fn get_file_extension(&self) -> String { + self.get_config() + .map(|config| config.extension.clone()) + .unwrap_or_else(|| "py".to_string()) + } + fn get_version_args(&self) -> Vec<&'static str> { vec!["--version"] } @@ -38,6 +44,8 @@ impl LanguagePlugin for Python3Plugin { } fn get_default_command(&self) -> String { - self.get_config().unwrap().run_command.unwrap() + self.get_config() + .and_then(|config| config.run_command) + .unwrap_or_else(|| "python3".to_string()) } } diff --git a/src/App.vue b/src/App.vue index 927f772..5dd671f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -251,9 +251,7 @@ const handleLanguageChange = async (newLanguage: string) => { // 更新代码模板 code.value = codeTemplates[newLanguage] || `# ${ getLanguageDisplayName(newLanguage) } Code -# Write your code here... - -print("Hello from ${ getLanguageDisplayName(newLanguage) }!")` +# Write your code here...` // 清空输出 clearOutput()