From 6c9f838df80942f0a0765672803e9221fa6f9486 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Mon, 11 Aug 2025 22:37:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20(language):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=BB=98=E8=AE=A4=E5=A4=84=E7=90=86=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=A8=8B=E5=BA=8F=E4=B8=AD=E6=96=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/plugins/mod.rs | 4 +--- src-tauri/src/plugins/python2.rs | 10 +++++++++- src-tauri/src/plugins/python3.rs | 10 +++++++++- src/App.vue | 4 +--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/plugins/mod.rs b/src-tauri/src/plugins/mod.rs index 304944ef..c6c0fbf1 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 86fa37e1..8be32142 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 3bb8dfee..e1cc2004 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 927f7726..5dd671ff 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() From f34de54e9b30b9585ffab3c2a80d019519b0396a Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Mon, 11 Aug 2025 23:22:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20(language):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=8F=92=E4=BB=B6=E6=9C=AA=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/config.rs | 62 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 876ec730..9fc1e39b 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