From b7afd12538ed7f5318690c4616b38332c44c7b01 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 16 Jun 2026 16:20:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(highlight):=20=E5=A4=A7=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E4=B8=8D=E6=95=8F=E6=84=9F=E7=9A=84=E8=AF=AD=E6=B3=95=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E5=8C=B9=E9=85=8D=EF=BC=8C=E4=BF=AE=E5=A4=8D=20haskel?= =?UTF-8?q?l=20=E7=AD=89=E9=AB=98=E4=BA=AE=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/highlight.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index a2243ae..03b2887 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -48,15 +48,20 @@ pub mod server { if let Some(s) = ss.find_syntax_by_name(lang) { return s; } - // 小写后再匹配一次 + // 小写扩展名再匹配一次(部分语言的扩展名习惯小写) let lower = lang.to_lowercase(); if lower != lang { if let Some(s) = ss.find_syntax_by_extension(&lower) { return s; } - if let Some(s) = ss.find_syntax_by_name(&lower) { - return s; - } + } + // 大小写不敏感的语法名称匹配(syntect 的语法名通常首字母大写,如 Haskell) + if let Some(s) = ss + .syntaxes() + .iter() + .find(|s| s.name.eq_ignore_ascii_case(lang)) + { + return s; } // 常用语言别名映射表 let aliases: &[(&str, &str)] = &[ @@ -163,6 +168,23 @@ mod tests { assert!(result.contains(r#"1"#)); } + #[test] + fn highlight_code_haskell_by_full_name() { + // Haskell 语法名首字母大写,扩展名为 hs;直接写 "haskell" 应能匹配。 + let code = "factorial :: Integer -> Integer\nfactorial 0 = 1"; + let result = highlight_code(code, Some("haskell")); + assert!( + !result.contains(r#""#), + "Haskell 不应回退到纯文本: {}", + result + ); + assert!( + result.contains("source haskell"), + "Haskell 应输出 source haskell: {}", + result + ); + } + #[test] fn highlight_code_uppercase_language_falls_back_via_lowercase() { // 大写语言标识应通过小写回退路径匹配到对应语法。