Skip to content

Commit

Permalink
Draft Highlighter API
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Sep 17, 2023
1 parent 723111b commit 76dc82e
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 19 deletions.
11 changes: 11 additions & 0 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
) {
}
}
2 changes: 2 additions & 0 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions core/src/text/editor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::text::highlighter::{self, Highlighter};
use crate::text::LineHeight;
use crate::{Pixels, Point, Rectangle, Size};

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

Expand Down
56 changes: 56 additions & 0 deletions core/src/text/highlighter.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
8 changes: 7 additions & 1 deletion graphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use cosmic_text;

use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::Size;
use crate::core::{Color, Size};

use once_cell::sync::OnceCell;
use std::borrow::Cow;
Expand Down Expand Up @@ -129,3 +129,9 @@ pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
Shaping::Advanced => cosmic_text::Shaping::Advanced,
}
}

pub fn to_color(color: Color) -> cosmic_text::Color {
let [r, g, b, a] = color.into_rgba8();

cosmic_text::Color::rgba(r, g, b, a)
}
67 changes: 67 additions & 0 deletions graphics/src/text/editor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::text::editor::{self, Action, Cursor, Direction, Motion};
use crate::core::text::highlighter::{self, Highlighter};
use crate::core::text::LineHeight;
use crate::core::{Font, Pixels, Point, Rectangle, Size};
use crate::text;
Expand All @@ -15,6 +16,7 @@ struct Internal {
editor: cosmic_text::Editor,
font: Font,
bounds: Size,
topmost_line_changed: Option<usize>,
version: text::Version,
}

Expand Down Expand Up @@ -433,6 +435,7 @@ impl editor::Editor for Editor {
new_font: Font,
new_size: Pixels,
new_line_height: LineHeight,
new_highlighter: &mut impl Highlighter,
) {
let editor =
self.0.take().expect("editor should always be initialized");
Expand Down Expand Up @@ -479,6 +482,69 @@ impl editor::Editor for Editor {
internal.bounds = new_bounds;
}

if let Some(topmost_line_changed) = internal.topmost_line_changed.take()
{
new_highlighter.change_line(topmost_line_changed);
}

self.0 = Some(Arc::new(internal));
}

fn highlight<H: Highlighter>(
&mut self,
font: Self::Font,
highlighter: &mut H,
format_highlight: impl Fn(&H::Highlight) -> highlighter::Format<Self::Font>,
) {
let internal = self.internal();

let scroll = internal.editor.buffer().scroll();
let visible_lines = internal.editor.buffer().visible_lines();
let last_visible_line = (scroll + visible_lines - 1) as usize;

let current_line = highlighter.current_line();

if current_line > last_visible_line {
return;
}

let editor =
self.0.take().expect("editor should always be initialized");

let mut internal = Arc::try_unwrap(editor)
.expect("Editor cannot have multiple strong references");

let mut font_system =
text::font_system().write().expect("Write font system");

let attributes = text::to_attributes(font);

for line in &mut internal.editor.buffer_mut().lines
[current_line..=last_visible_line]
{
let mut list = cosmic_text::AttrsList::new(attributes);

for (range, highlight) in highlighter.highlight_line(line.text()) {
let format = format_highlight(&highlight);

list.add_span(
range,
cosmic_text::Attrs {
color_opt: format.color.map(text::to_color),
..if let Some(font) = format.font {
text::to_attributes(font)
} else {
attributes
}
},
);
}

let _ = line.set_attrs_list(list);
}

internal.editor.shape_as_needed(font_system.raw());

self.0 = Some(Arc::new(internal));
}
}
Expand Down Expand Up @@ -508,6 +574,7 @@ impl Default for Internal {
)),
font: Font::default(),
bounds: Size::ZERO,
topmost_line_changed: None,
version: text::Version::default(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion style/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
clippy::needless_borrow,
clippy::new_without_default,
clippy::useless_conversion,
missing_docs,
// missing_docs,
unused_results,
rustdoc::broken_intra_doc_links
)]
Expand Down
16 changes: 15 additions & 1 deletion style/src/text_editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Change the appearance of a text editor.
use iced_core::{Background, BorderRadius, Color};
use crate::core::text::highlighter;
use crate::core::{self, Background, BorderRadius, Color};

/// The appearance of a text input.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -45,3 +46,16 @@ pub trait StyleSheet {
/// Produces the style of a disabled text input.
fn disabled(&self, style: &Self::Style) -> Appearance;
}

pub trait Highlight<Font = core::Font, Theme = crate::Theme> {
fn format(&self, theme: &Theme) -> highlighter::Format<Font>;
}

impl<Font, Theme> Highlight<Font, Theme> for () {
fn format(&self, _theme: &Theme) -> highlighter::Format<Font> {
highlighter::Format {
color: None,
font: None,
}
}
}
2 changes: 1 addition & 1 deletion widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ where
/// [`TextEditor`]: crate::TextEditor
pub fn text_editor<Message, Renderer>(
content: &text_editor::Content<Renderer>,
) -> TextEditor<'_, Message, Renderer>
) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Renderer>
where
Message: Clone,
Renderer: core::text::Renderer,
Expand Down
Loading

0 comments on commit 76dc82e

Please sign in to comment.