Highlight CUDA files as C++ in the TUI (#34570)

## What changed

- Map `.cu` and `.cuh` extensions to the C++ syntax definition so CUDA source
  and header files receive syntax highlighting.
- Extend syntax lookup and rendered-diff tests to cover both extensions.

GitOrigin-RevId: 5484ea9e30f9e4df788b0079596ec0239e8ef8d5
This commit is contained in:
Eric Traut
2026-07-21 15:53:33 +00:00
committed by copyberry
parent 0b175e6439
commit 726689564a
3 changed files with 42 additions and 29 deletions
+30 -27
View File
@@ -2222,34 +2222,37 @@ mod tests {
#[test]
fn cpp_module_extensions_use_cpp_highlighting() {
let highlighted_tokens = ["cpp", "cppm", "CPPM", "cxxm", "CxXm", "ixx", "IXX"]
.into_iter()
.map(|extension| {
let mut changes: HashMap<PathBuf, FileChange> = HashMap::new();
changes.insert(
PathBuf::from(format!("math.{extension}")),
FileChange::Add {
content:
"export module math;\nexport int sum(int a, int b) { return a + b; }\n"
.to_string(),
},
);
let highlighted_tokens = [
"cpp",
// CUDA source and header extensions use C++ highlighting as a fallback.
"cu", "cuh", "cppm", "CPPM", "cxxm", "CxXm", "ixx", "IXX",
]
.into_iter()
.map(|extension| {
let mut changes: HashMap<PathBuf, FileChange> = HashMap::new();
changes.insert(
PathBuf::from(format!("math.{extension}")),
FileChange::Add {
content:
"export module math;\nexport int sum(int a, int b) { return a + b; }\n"
.to_string(),
},
);
let lines =
create_diff_summary(&changes, &PathBuf::from("/"), /*wrap_cols*/ 80);
let rgb_tokens = lines
.iter()
.flat_map(|line| &line.spans)
.filter(|span| matches!(span.style.fg, Some(ratatui::style::Color::Rgb(..))))
.map(|span| span.content.to_string())
.collect::<Vec<_>>();
assert!(
!rgb_tokens.is_empty(),
"add diff for .{extension} file should produce syntax-highlighted (RGB) spans"
);
(extension, rgb_tokens.join("|"))
})
.collect::<Vec<_>>();
let lines = create_diff_summary(&changes, &PathBuf::from("/"), /*wrap_cols*/ 80);
let rgb_tokens = lines
.iter()
.flat_map(|line| &line.spans)
.filter(|span| matches!(span.style.fg, Some(ratatui::style::Color::Rgb(..))))
.map(|span| span.content.to_string())
.collect::<Vec<_>>();
assert!(
!rgb_tokens.is_empty(),
"add diff for .{extension} file should produce syntax-highlighted (RGB) spans"
);
(extension, rgb_tokens.join("|"))
})
.collect::<Vec<_>>();
assert_debug_snapshot!("cpp_module_extension_highlighting", highlighted_tokens);
}
+4 -2
View File
@@ -539,6 +539,8 @@ fn find_syntax(lang: &str) -> Option<&'static SyntaxReference> {
let normalized = lang.to_ascii_lowercase();
let patched = match normalized.as_str() {
"csharp" | "c-sharp" => "c#",
// CUDA source (.cu) and header (.cuh) files use C++ highlighting as a fallback.
"cu" | "cuh" => "cpp",
"cppm" | "cxxm" | "ixx" => "cpp",
"golang" => "go",
"python3" => "python",
@@ -1216,8 +1218,8 @@ mod tests {
}
// Patched aliases that two-face cannot resolve on its own.
for alias in [
"csharp", "c-sharp", "cppm", "CPPM", "cxxm", "CxXm", "ixx", "IXX", "golang", "python3",
"shell",
"csharp", "c-sharp", "cu", "cuh", "cppm", "CPPM", "cxxm", "CxXm", "ixx", "IXX",
"golang", "python3", "shell",
] {
assert!(
find_syntax(alias).is_some(),
@@ -7,6 +7,14 @@ expression: highlighted_tokens
"cpp",
"export| module math|;|export| |int| |sum|(|int| |a|,| |int| |b|)| |{| |return| a |+| b|;| |}",
),
(
"cu",
"export| module math|;|export| |int| |sum|(|int| |a|,| |int| |b|)| |{| |return| a |+| b|;| |}",
),
(
"cuh",
"export| module math|;|export| |int| |sum|(|int| |a|,| |int| |b|)| |{| |return| a |+| b|;| |}",
),
(
"cppm",
"export| module math|;|export| |int| |sum|(|int| |a|,| |int| |b|)| |{| |return| a |+| b|;| |}",