Draft Highlighter API

This commit is contained in:
Héctor Ramón Jiménez 2023-09-17 15:29:14 +02:00
parent 723111bb0d
commit 76dc82e8e8
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
10 changed files with 218 additions and 19 deletions

View file

@ -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>,
);
}

View file

@ -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>,
}