summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-17 15:29:14 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-17 15:29:14 +0200
commit76dc82e8e8b5201ec10f8d00d851c1decf998583 (patch)
tree18b48610e48ee90821a2c7678e9ce2f236868f01 /core
parent723111bb0df486bffaedcaed0722b1793d65bfe3 (diff)
downloadiced-76dc82e8e8b5201ec10f8d00d851c1decf998583.tar.gz
iced-76dc82e8e8b5201ec10f8d00d851c1decf998583.tar.bz2
iced-76dc82e8e8b5201ec10f8d00d851c1decf998583.zip
Draft `Highlighter` API
Diffstat (limited to '')
-rw-r--r--core/src/renderer/null.rs11
-rw-r--r--core/src/text.rs2
-rw-r--r--core/src/text/editor.rs9
-rw-r--r--core/src/text/highlighter.rs56
4 files changed, 78 insertions, 0 deletions
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs
index 01a52c7a..21597c8e 100644
--- a/core/src/renderer/null.rs
+++ b/core/src/renderer/null.rs
@@ -149,6 +149,17 @@ impl text::Editor for () {
_new_font: Self::Font,
_new_size: Pixels,
_new_line_height: text::LineHeight,
+ _new_highlighter: &mut impl text::Highlighter,
+ ) {
+ }
+
+ fn highlight<H: text::Highlighter>(
+ &mut self,
+ _font: Self::Font,
+ _highlighter: &mut H,
+ _format_highlight: impl Fn(
+ &H::Highlight,
+ ) -> text::highlighter::Format<Self::Font>,
) {
}
}
diff --git a/core/src/text.rs b/core/src/text.rs
index 90581fea..9b9c753c 100644
--- a/core/src/text.rs
+++ b/core/src/text.rs
@@ -2,8 +2,10 @@
mod paragraph;
pub mod editor;
+pub mod highlighter;
pub use editor::Editor;
+pub use highlighter::Highlighter;
pub use paragraph::Paragraph;
use crate::alignment;
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>,
+}