diff --git a/Cargo.toml b/Cargo.toml index 45a2224..648ebe6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,14 +11,15 @@ categories = ["gui", "text-editors"] keywords = ["egui", "GUI", "editor", "syntax", "highlighting"] [dependencies] -egui = {version = "0.28", optional = true} +egui = { version = "0.28", optional = true } [lib] name = "egui_code_editor" [features] -default = ["egui"] +default = ["egui", "editor"] egui = ["dep:egui"] +editor = [] [[example]] name = "demo" diff --git a/src/highlighting.rs b/src/highlighting.rs index 685241c..5502a69 100644 --- a/src/highlighting.rs +++ b/src/highlighting.rs @@ -1,3 +1,5 @@ +use super::Editor; + use super::syntax::{Syntax, TokenType, QUOTES, SEPARATORS}; use std::mem; @@ -67,11 +69,11 @@ impl Token { #[cfg(feature = "egui")] /// Syntax highlighting - pub fn highlight(&mut self, editor: &CodeEditor, text: &str) -> LayoutJob { + pub fn highlight(&mut self, editor: &T, text: &str) -> LayoutJob { *self = Token::default(); let mut job = LayoutJob::default(); for c in text.chars() { - for token in self.automata(c, &editor.syntax) { + for token in self.automata(c, editor.syntax()) { editor.append(&mut job, &token); } } @@ -229,14 +231,12 @@ impl Token { } } -#[cfg(feature = "egui")] -use super::CodeEditor; #[cfg(feature = "egui")] use egui::text::LayoutJob; #[cfg(feature = "egui")] -impl egui::util::cache::ComputerMut<(&CodeEditor, &str), LayoutJob> for Token { - fn compute(&mut self, (cache, text): (&CodeEditor, &str)) -> LayoutJob { +impl egui::util::cache::ComputerMut<(&T, &str), LayoutJob> for Token { + fn compute(&mut self, (cache, text): (&T, &str)) -> LayoutJob { self.highlight(cache, text) } } @@ -245,13 +245,6 @@ impl egui::util::cache::ComputerMut<(&CodeEditor, &str), LayoutJob> for Token { pub type HighlightCache = egui::util::cache::FrameCache; #[cfg(feature = "egui")] -pub fn highlight(ctx: &egui::Context, cache: &CodeEditor, text: &str) -> LayoutJob { +pub fn highlight(ctx: &egui::Context, cache: &T, text: &str) -> LayoutJob { ctx.memory_mut(|mem| mem.caches.cache::().get((cache, text))) } - -#[cfg(feature = "egui")] -impl CodeEditor { - fn append(&self, job: &mut LayoutJob, token: &Token) { - job.append(token.buffer(), 0.0, self.format(token.ty())); - } -} diff --git a/src/lib.rs b/src/lib.rs index 5fe8762..569dbff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,7 @@ mod syntax; mod tests; mod themes; +use egui::text::LayoutJob; #[cfg(feature = "egui")] use egui::widgets::text_edit::TextEditOutput; #[cfg(feature = "egui")] @@ -84,6 +85,13 @@ pub use syntax::{Syntax, TokenType}; pub use themes::ColorTheme; pub use themes::DEFAULT_THEMES; +#[cfg(feature = "egui")] +pub trait Editor: Hash { + fn append(&self, job: &mut LayoutJob, token: &Token); + fn syntax(&self) -> &Syntax; +} + +#[cfg(feature = "editor")] #[derive(Clone, Debug, PartialEq)] /// CodeEditor struct which stores settings for highlighting. pub struct CodeEditor { @@ -98,6 +106,7 @@ pub struct CodeEditor { shrink: bool, } +#[cfg(feature = "editor")] impl Hash for CodeEditor { fn hash(&self, state: &mut H) { self.theme.hash(state); @@ -107,6 +116,7 @@ impl Hash for CodeEditor { } } +#[cfg(feature = "editor")] impl Default for CodeEditor { fn default() -> CodeEditor { CodeEditor { @@ -123,6 +133,7 @@ impl Default for CodeEditor { } } +#[cfg(feature = "editor")] impl CodeEditor { pub fn id_source(self, id_source: impl Into) -> Self { CodeEditor { @@ -298,3 +309,15 @@ impl CodeEditor { text_edit_output.expect("TextEditOutput should exist at this point") } } + +#[cfg(feature = "editor")] +#[cfg(feature = "egui")] +impl Editor for CodeEditor { + fn append(&self, job: &mut LayoutJob, token: &Token) { + job.append(token.buffer(), 0.0, self.format(token.ty())); + } + + fn syntax(&self) -> &Syntax { + &self.syntax + } +}