Skip to content

Commit 35691ae

Browse files
committed
Replace highlight::Text with Cache (removes highlighter)
1 parent e02b89d commit 35691ae

File tree

3 files changed

+26
-61
lines changed

3 files changed

+26
-61
lines changed

crates/kas-widgets/src/edit/editor.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct Editor {
3737
id: Id,
3838
read_only: bool,
3939
display: ConfiguredDisplay,
40+
highlight: highlight::Cache,
4041
text: String,
4142
colors: SchemeColors,
4243
selection: SelectionHelper,
@@ -56,6 +57,7 @@ impl Default for Editor {
5657
id: Id::default(),
5758
read_only: false,
5859
display: ConfiguredDisplay::new(TextClass::Editor, false),
60+
highlight: Default::default(),
5961
text: Default::default(),
6062
colors: SchemeColors::default(),
6163
selection: Default::default(),
@@ -99,8 +101,8 @@ impl<S: ToString> From<S> for Editor {
99101
/// support scrolling of text content. Since this component is not a widget it
100102
/// cannot implement [`Viewport`] directly, but it does provide the following
101103
/// methods: [`Self::content_size`], [`Self::draw_with_offset`].
102-
#[autoimpl(Debug where H: trait)]
103-
pub struct Component<H: Highlighter>(pub Editor, highlight::Text<H>);
104+
#[derive(Debug, Default)]
105+
pub struct Component<H: Highlighter>(pub Editor, H);
104106

105107
impl<H: Highlighter> Deref for Component<H> {
106108
type Target = ConfiguredDisplay;
@@ -141,30 +143,23 @@ impl<H: Highlighter> Layout for Component<H> {
141143
}
142144
}
143145

144-
impl<H: Default + Highlighter> Default for Component<H> {
145-
#[inline]
146-
fn default() -> Self {
147-
Component(Editor::default(), Default::default())
148-
}
149-
}
150-
151-
impl<H: Default + Highlighter, S: ToString> From<S> for Component<H> {
146+
impl<H: Highlighter + Default, S: ToString> From<S> for Component<H> {
152147
#[inline]
153148
fn from(text: S) -> Self {
154-
Component(Editor::from(text), Default::default())
149+
Component(Editor::from(text), H::default())
155150
}
156151
}
157152

158153
impl<H: Highlighter> Component<H> {
159154
/// Replace the highlighter
160155
#[inline]
161156
pub fn with_highlighter<H2: Highlighter>(self, highlighter: H2) -> Component<H2> {
162-
Component(self.0, highlight::Text::new(highlighter))
157+
Component(self.0, highlighter)
163158
}
164159

165160
/// Set a new highlighter of the same type
166161
pub fn set_highlighter(&mut self, highlighter: H) {
167-
self.1 = highlight::Text::new(highlighter);
162+
self.1 = highlighter;
168163
}
169164

170165
/// Get the background color
@@ -210,11 +205,12 @@ impl<H: Highlighter> Component<H> {
210205
#[inline]
211206
fn prepare_runs(&mut self) {
212207
fn inner<H: Highlighter>(this: &mut Component<H>) {
213-
this.1.highlight(&this.0.text);
208+
this.0.highlight.highlight(&this.0.text, &mut this.1);
214209
let (dpem, font) = (this.0.display.font_size(), this.0.display.font());
215-
this.0
216-
.display
217-
.prepare_runs(this.0.text.as_str(), this.1.font_tokens(dpem, font));
210+
this.0.display.prepare_runs(
211+
this.0.text.as_str(),
212+
this.0.highlight.font_tokens(dpem, font),
213+
);
218214
}
219215

220216
if self.0.display.status() < Status::LevelRuns {
@@ -307,7 +303,7 @@ impl<H: Highlighter> Component<H> {
307303
let pos = self.rect().pos - offset;
308304
let range: Range<u32> = self.0.selection.range().cast();
309305

310-
let color_tokens = self.1.color_tokens();
306+
let color_tokens = self.0.highlight.color_tokens();
311307
let default_colors = format::Colors {
312308
foreground: self.0.colors.foreground,
313309
background: None,
@@ -391,7 +387,7 @@ impl<H: Highlighter> Component<H> {
391387
};
392388
draw.text(pos, rect, display, tokens);
393389

394-
let decorations = self.1.decorations();
390+
let decorations = self.0.highlight.decorations();
395391
if !decorations.is_empty() {
396392
draw.decorate_text(pos, rect, display, decorations);
397393
}

crates/kas-widgets/src/edit/highlight.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
//! Supporting elements for syntax highlighting
77
8+
mod cache;
89
#[cfg(feature = "syntect")] mod syntect;
9-
mod text;
1010

11+
pub(crate) use cache::Cache;
1112
use kas::impl_scope;
1213
#[cfg(feature = "syntect")]
1314
pub use syntect::{
1415
SyntaxReference as SyntectSyntax, SyntaxSet as SyntectSyntaxSet, SyntectHighlighter,
1516
};
16-
pub(crate) use text::Text;
1717

1818
use kas::event::ConfigCx;
1919
use kas::text::fonts::{FontStyle, FontWeight};

crates/kas-widgets/src/edit/highlight/text.rs renamed to crates/kas-widgets/src/edit/highlight/cache.rs

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,27 @@ struct Fmt {
1818
}
1919

2020
/// A highlighted text
21-
///
22-
/// Two `Text` objects compare equal if their formatted text is equal regardless
23-
/// of the embedded highlighter.
24-
#[derive(Clone, Debug)]
25-
#[kas::autoimpl(PartialEq ignore self.highlighter)]
26-
pub(crate) struct Text<H: Highlighter> {
27-
highlighter: H,
21+
#[derive(Clone, Debug, PartialEq)]
22+
pub(crate) struct Cache {
2823
fonts: Vec<Fmt>,
2924
colors: Vec<(u32, Colors)>,
3025
decorations: Vec<(u32, Decoration)>,
3126
}
3227

33-
impl<H: Highlighter + Default> Default for Text<H> {
34-
fn default() -> Self {
35-
Self::new(H::default())
36-
}
37-
}
38-
39-
impl<H: Highlighter> Text<H> {
40-
/// Construct a new instance
28+
impl Default for Cache {
4129
#[inline]
42-
pub fn new(highlighter: H) -> Self {
43-
Text {
44-
highlighter,
30+
fn default() -> Self {
31+
Cache {
4532
fonts: vec![Fmt::default()],
4633
colors: vec![],
4734
decorations: vec![],
4835
}
4936
}
37+
}
5038

51-
/// Configure the highlighter
52-
///
53-
/// This is called when the widget is configured. It may be used to set the
54-
/// theme / color scheme.
55-
///
56-
/// Returns `true` when the highlighter must be re-run.
57-
#[inline]
58-
#[must_use]
59-
pub fn configure(&mut self, cx: &mut ConfigCx) -> bool {
60-
self.highlighter.configure(cx)
61-
}
62-
63-
/// Get scheme colors
64-
///
65-
/// This method allows usage of the highlighter's colors by the editor.
66-
#[inline]
67-
pub fn scheme_colors(&self) -> SchemeColors {
68-
self.highlighter.scheme_colors()
69-
}
70-
39+
impl Cache {
7140
/// Highlight the text (from scratch)
72-
pub fn highlight(&mut self, text: &str) {
41+
pub fn highlight<H: Highlighter>(&mut self, text: &str, highlighter: &mut H) {
7342
self.fonts.clear();
7443
self.fonts.push(Fmt::default());
7544
self.colors.clear();
@@ -110,7 +79,7 @@ impl<H: Highlighter> Text<H> {
11079
state = token;
11180
};
11281

113-
if let Err(err) = self.highlighter.highlight_text(text, &mut push_token) {
82+
if let Err(err) = highlighter.highlight_text(text, &mut push_token) {
11483
log::error!("Highlighting failed: {err}");
11584
debug_assert!(false, "Highlighter: {err}");
11685
}

0 commit comments

Comments
 (0)