Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Editor Trait for Flexible Text Widget Integration and Feature Flag for CodeEditor #15

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 7 additions & 14 deletions src/highlighting.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::Editor;

use super::syntax::{Syntax, TokenType, QUOTES, SEPARATORS};
use std::mem;

Expand Down Expand Up @@ -67,11 +69,11 @@ impl Token {

#[cfg(feature = "egui")]
/// Syntax highlighting
pub fn highlight(&mut self, editor: &CodeEditor, text: &str) -> LayoutJob {
pub fn highlight<T: Editor>(&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);
}
}
Expand Down Expand Up @@ -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<T: Editor> egui::util::cache::ComputerMut<(&T, &str), LayoutJob> for Token {
fn compute(&mut self, (cache, text): (&T, &str)) -> LayoutJob {
self.highlight(cache, text)
}
}
Expand All @@ -245,13 +245,6 @@ impl egui::util::cache::ComputerMut<(&CodeEditor, &str), LayoutJob> for Token {
pub type HighlightCache = egui::util::cache::FrameCache<LayoutJob, Token>;

#[cfg(feature = "egui")]
pub fn highlight(ctx: &egui::Context, cache: &CodeEditor, text: &str) -> LayoutJob {
pub fn highlight<T: Editor>(ctx: &egui::Context, cache: &T, text: &str) -> LayoutJob {
ctx.memory_mut(|mem| mem.caches.cache::<HighlightCache>().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()));
}
}
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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 {
Expand All @@ -98,6 +106,7 @@ pub struct CodeEditor {
shrink: bool,
}

#[cfg(feature = "editor")]
impl Hash for CodeEditor {
fn hash<H: Hasher>(&self, state: &mut H) {
self.theme.hash(state);
Expand All @@ -107,6 +116,7 @@ impl Hash for CodeEditor {
}
}

#[cfg(feature = "editor")]
impl Default for CodeEditor {
fn default() -> CodeEditor {
CodeEditor {
Expand All @@ -123,6 +133,7 @@ impl Default for CodeEditor {
}
}

#[cfg(feature = "editor")]
impl CodeEditor {
pub fn id_source(self, id_source: impl Into<String>) -> Self {
CodeEditor {
Expand Down Expand Up @@ -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
}
}
Loading