mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 23:00:04 -04:00
Add syntax highlighing feature to egui_extras (#3333)
* Add syntax highlighing feature to egui_extras Enable "syntect" feature for great syntax highlighting of any language. If not a simple fallback is used that works fine for C++, Rust, Python * Check --no-default-features of egui_extras on CI * spelling * Fix building egui_extras without additional features
This commit is contained in:
@@ -23,7 +23,7 @@ image_viewer = ["image", "egui_extras/all-loaders", "rfd"]
|
||||
persistence = ["eframe/persistence", "egui/persistence", "serde"]
|
||||
web_screen_reader = ["eframe/web_screen_reader"] # experimental
|
||||
serde = ["dep:serde", "egui_demo_lib/serde", "egui/serde"]
|
||||
syntax_highlighting = ["egui_demo_lib/syntax_highlighting"]
|
||||
syntect = ["egui_demo_lib/syntect"]
|
||||
|
||||
glow = ["eframe/glow"]
|
||||
wgpu = ["eframe/wgpu", "bytemuck"]
|
||||
|
||||
@@ -223,25 +223,19 @@ fn selectable_text(ui: &mut egui::Ui, mut text: &str) {
|
||||
// ----------------------------------------------------------------------------
|
||||
// Syntax highlighting:
|
||||
|
||||
#[cfg(feature = "syntect")]
|
||||
fn syntax_highlighting(
|
||||
ctx: &egui::Context,
|
||||
response: &ehttp::Response,
|
||||
text: &str,
|
||||
) -> Option<ColoredText> {
|
||||
let extension_and_rest: Vec<&str> = response.url.rsplitn(2, '.').collect();
|
||||
let extension = extension_and_rest.get(0)?;
|
||||
let theme = crate::syntax_highlighting::CodeTheme::from_style(&ctx.style());
|
||||
Some(ColoredText(crate::syntax_highlighting::highlight(
|
||||
let extension = extension_and_rest.first()?;
|
||||
let theme = egui_extras::syntax_highlighting::CodeTheme::from_style(&ctx.style());
|
||||
Some(ColoredText(egui_extras::syntax_highlighting::highlight(
|
||||
ctx, &theme, text, extension,
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "syntect"))]
|
||||
fn syntax_highlighting(_ctx: &egui::Context, _: &ehttp::Response, _: &str) -> Option<ColoredText> {
|
||||
None
|
||||
}
|
||||
|
||||
struct ColoredText(egui::text::LayoutJob);
|
||||
|
||||
impl ColoredText {
|
||||
|
||||
Reference in New Issue
Block a user