|
| 1 | +use super::{LanguagePlugin, PluginConfig}; |
| 2 | +use std::vec; |
| 3 | + |
| 4 | +pub struct HaskellPlugin; |
| 5 | + |
| 6 | +impl LanguagePlugin for HaskellPlugin { |
| 7 | + fn get_order(&self) -> i32 { |
| 8 | + 25 |
| 9 | + } |
| 10 | + |
| 11 | + fn get_language_name(&self) -> &'static str { |
| 12 | + "Haskell" |
| 13 | + } |
| 14 | + |
| 15 | + fn get_language_key(&self) -> &'static str { |
| 16 | + "haskell" |
| 17 | + } |
| 18 | + |
| 19 | + fn get_file_extension(&self) -> String { |
| 20 | + self.get_config() |
| 21 | + .map(|config| config.extension.clone()) |
| 22 | + .unwrap_or_else(|| "hs".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 ghc".to_string() |
| 31 | + } |
| 32 | + |
| 33 | + fn get_command( |
| 34 | + &self, |
| 35 | + _file_path: Option<&str>, |
| 36 | + _is_version: bool, |
| 37 | + _file_name: Option<String>, |
| 38 | + ) -> String { |
| 39 | + if _is_version { |
| 40 | + let ghc_command = if self.get_execute_home().is_some() { |
| 41 | + "./ghc" |
| 42 | + } else { |
| 43 | + "ghc" |
| 44 | + }; |
| 45 | + |
| 46 | + return ghc_command.to_string(); |
| 47 | + } |
| 48 | + |
| 49 | + // 执行代码时 |
| 50 | + if let Some(config) = self.get_config() { |
| 51 | + if let Some(run_cmd) = &config.run_command { |
| 52 | + return if let Some(file_name) = _file_name { |
| 53 | + run_cmd.replace("$filename", &file_name) |
| 54 | + } else { |
| 55 | + // 执行代码但没有文件名时,返回原始命令让框架处理 $filename 替换 |
| 56 | + run_cmd.clone() |
| 57 | + }; |
| 58 | + } |
| 59 | + } |
| 60 | + self.get_default_command() |
| 61 | + } |
| 62 | + |
| 63 | + fn get_execute_args(&self, file_path: &str) -> Vec<String> { |
| 64 | + let ghc_command = if self.get_execute_home().is_some() { |
| 65 | + "./ghc" |
| 66 | + } else { |
| 67 | + "ghc" |
| 68 | + }; |
| 69 | + |
| 70 | + // 对于 Haskell,通常先编译再运行 |
| 71 | + // 这里假设编译后的可执行文件名为 main |
| 72 | + let cmd = format!("{} {} -o main && ./main", ghc_command, file_path); |
| 73 | + |
| 74 | + vec!["-c".to_string(), cmd] |
| 75 | + } |
| 76 | + |
| 77 | + fn get_default_config(&self) -> PluginConfig { |
| 78 | + PluginConfig { |
| 79 | + enabled: true, |
| 80 | + language: String::from("haskell"), |
| 81 | + before_compile: Some(String::from("ghc $filename -o /tmp/main")), |
| 82 | + extension: String::from("hs"), |
| 83 | + execute_home: None, |
| 84 | + run_command: Some(String::from("/tmp/main")), |
| 85 | + after_compile: Some(String::from("rm -f /tmp/main /tmp/main.hi /tmp/main.o")), |
| 86 | + template: Some(String::from( |
| 87 | + "-- 在这里输入 Haskell 代码\n-- Haskell - 纯函数式编程语言\n", |
| 88 | + )), |
| 89 | + timeout: Some(30), |
| 90 | + console_type: Some(String::from("console")), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fn get_default_command(&self) -> String { |
| 95 | + self.get_config() |
| 96 | + .and_then(|config| config.run_command.clone()) |
| 97 | + .unwrap_or_else(|| "./main".to_string()) |
| 98 | + } |
| 99 | +} |
0 commit comments