diff options
author | 2023-09-17 15:29:14 +0200 | |
---|---|---|
committer | 2023-09-17 15:29:14 +0200 | |
commit | 76dc82e8e8b5201ec10f8d00d851c1decf998583 (patch) | |
tree | 18b48610e48ee90821a2c7678e9ce2f236868f01 /core/src/text | |
parent | 723111bb0df486bffaedcaed0722b1793d65bfe3 (diff) | |
download | iced-76dc82e8e8b5201ec10f8d00d851c1decf998583.tar.gz iced-76dc82e8e8b5201ec10f8d00d851c1decf998583.tar.bz2 iced-76dc82e8e8b5201ec10f8d00d851c1decf998583.zip |
Draft `Highlighter` API
Diffstat (limited to 'core/src/text')
-rw-r--r-- | core/src/text/editor.rs | 9 | ||||
-rw-r--r-- | core/src/text/highlighter.rs | 56 |
2 files changed, 65 insertions, 0 deletions
diff --git a/core/src/text/editor.rs b/core/src/text/editor.rs index 003557c1..0f439c8d 100644 --- a/core/src/text/editor.rs +++ b/core/src/text/editor.rs @@ -1,3 +1,4 @@ +use crate::text::highlighter::{self, Highlighter}; use crate::text::LineHeight; use crate::{Pixels, Point, Rectangle, Size}; @@ -29,6 +30,14 @@ pub trait Editor: Sized + Default { new_font: Self::Font, new_size: Pixels, new_line_height: LineHeight, + new_highlighter: &mut impl Highlighter, + ); + + fn highlight<H: Highlighter>( + &mut self, + font: Self::Font, + highlighter: &mut H, + format_highlight: impl Fn(&H::Highlight) -> highlighter::Format<Self::Font>, ); } diff --git a/core/src/text/highlighter.rs b/core/src/text/highlighter.rs new file mode 100644 index 00000000..1f9ac840 --- /dev/null +++ b/core/src/text/highlighter.rs @@ -0,0 +1,56 @@ +use crate::Color; + +use std::hash::Hash; +use std::ops::Range; + +pub trait Highlighter: Clone + 'static { + type Settings: Hash; + type Highlight; + + type Iterator<'a>: Iterator<Item = (Range<usize>, Self::Highlight)> + where + Self: 'a; + + fn new(settings: &Self::Settings) -> Self; + + fn change_line(&mut self, line: usize); + + fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_>; + + fn current_line(&self) -> usize; +} + +#[derive(Debug, Clone, Copy)] +pub struct Style { + pub color: Color, +} + +#[derive(Debug, Clone, Copy)] +pub struct PlainText; + +impl Highlighter for PlainText { + type Settings = (); + type Highlight = (); + + type Iterator<'a> = std::iter::Empty<(Range<usize>, Self::Highlight)>; + + fn new(_settings: &Self::Settings) -> Self { + Self + } + + fn change_line(&mut self, _line: usize) {} + + fn highlight_line(&mut self, _line: &str) -> Self::Iterator<'_> { + std::iter::empty() + } + + fn current_line(&self) -> usize { + usize::MAX + } +} + +#[derive(Debug, Clone, Copy)] +pub struct Format<Font> { + pub color: Option<Color>, + pub font: Option<Font>, +} |