Files
egui/crates/egui_extras/tests/syntax_highlighting.rs
T
Emil ErnerfeldtandClaude Opus 5.5 dfe33185e4 Improve the fallback syntax highlighter in egui_extras (#8708)
Improes non-`syntect` highlighter in `egui_extras::syntax_highlighting`.

* `TokenType` is now public, with new `CodeTheme::format` and
`CodeTheme::format_mut`, so apps can match the colors to their own theme
* New `TokenType::Function`: an identifier directly followed by `(`
* `/* block comments */` for C, C++, JavaScript, JSON and Rust
* Backslash escapes in strings, so `"say \"hi\""` is one string
* `'single'`, `"""triple"""` and `'''triple'''` strings in Python and
TOML, `'char'` literals in C/C++, and `` `template` `` strings in
JavaScript
* New languages: JavaScript/TypeScript, shell (bash, sh, zsh) and YAML
* `jsonc` and `json5` are now highlighted as JSON

The demo snapshots are rendered with `--all-features`, which uses
`syntect`, so they are unaffected.
The new `egui_extras/tests/syntax_highlighting.rs` snapshot test covers
the fallback highlighter, run by a new CI step without `syntect`.

* [x] I have followed the instructions in the PR template

This PR was written by Claude Code (an LLM) on behalf of @emilk, and
needs a human review before leaving draft.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 06:49:32 +00:00

50 lines
1.7 KiB
Rust

//! Snapshots of the fallback highlighter, used when the `syntect` feature is off.
#![cfg(not(feature = "syntect"))]
use egui_extras::syntax_highlighting::{CodeTheme, code_view_ui};
use egui_kittest::{Harness, SnapshotResults};
const SAMPLES: &[(&str, &str)] = &[
(
"rs",
"/* block */ fn f<'a>(s: &'a str) -> usize {\n // line\n s.len() + \"esc \\\" quote\".len()\n}",
),
(
"py",
"def f(x): # comment\n \"\"\"Doc \"string\".\"\"\"\n return 'it' if x else None",
),
(
"ts",
"const s: string = `hi ${name}`; /* block */\nconsole.log(s); // line",
),
("json", "{\"key\": [1, true, null], \"esc\": \"a\\\"b\"}"),
("sh", "if [ -f \"$f\" ]; then echo 'yes'; fi # comment"),
("sql", "SELECT name FROM t WHERE id = 'x' -- comment"),
("toml", "[package]\nname = 'egui' # comment"),
("yaml", "key: \"value\" # comment\nlist: [true, null]"),
];
#[test]
fn fallback_syntax_highlighting() {
let mut results = SnapshotResults::new();
for theme in [egui::Theme::Dark, egui::Theme::Light] {
let mut harness = Harness::builder()
.with_size(egui::vec2(480.0, 1.0))
.with_theme(theme)
.build_ui(|ui| {
let code_theme = CodeTheme::from_style(ui.style());
for (language, code) in SAMPLES {
ui.weak(*language);
code_view_ui(ui, &code_theme, code, language);
}
});
harness.run();
harness.fit_contents();
harness.snapshot(match theme {
egui::Theme::Dark => "syntax_highlighting_dark",
egui::Theme::Light => "syntax_highlighting_light",
});
results.extend_harness(&mut harness);
}
}