Allow Editor to use reference or Arc of Buffer

This commit is contained in:
Jeremy Soller 2023-12-19 11:04:33 -07:00
parent ae030e9885
commit cbbf6f0d8f
9 changed files with 1160 additions and 1021 deletions

View file

@ -86,7 +86,7 @@ pub struct Window {
path_opt: Option<PathBuf>, path_opt: Option<PathBuf>,
attrs: Attrs<'static>, attrs: Attrs<'static>,
font_size: FontSize, font_size: FontSize,
editor: Mutex<cosmic_text::ViEditor<'static>>, editor: Mutex<cosmic_text::ViEditor<'static, 'static>>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -185,10 +185,12 @@ impl Application for Window {
if let Some(path) = &self.path_opt { if let Some(path) = &self.path_opt {
let editor = self.editor.lock().unwrap(); let editor = self.editor.lock().unwrap();
let mut text = String::new(); let mut text = String::new();
for line in editor.buffer().lines.iter() { editor.with_buffer(|buffer| {
text.push_str(line.text()); for line in buffer.lines.iter() {
text.push('\n'); text.push_str(line.text());
} text.push('\n');
}
});
match fs::write(path, text) { match fs::write(path, text) {
Ok(()) => { Ok(()) => {
log::info!("saved '{}'", path.display()); log::info!("saved '{}'", path.display());
@ -234,15 +236,13 @@ impl Application for Window {
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
editor editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap()) .borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut() .with_buffer_mut(|buffer| buffer.set_metrics(font_size.to_metrics()));
.set_metrics(font_size.to_metrics());
} }
Message::WrapChanged(wrap) => { Message::WrapChanged(wrap) => {
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
editor editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap()) .borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut() .with_buffer_mut(|buffer| buffer.set_wrap(wrap));
.set_wrap(wrap);
} }
Message::AlignmentChanged(align) => { Message::AlignmentChanged(align) => {
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
@ -304,7 +304,7 @@ impl Application for Window {
let editor = self.editor.lock().unwrap(); let editor = self.editor.lock().unwrap();
pick_list( pick_list(
WRAP_MODE, WRAP_MODE,
Some(editor.buffer().wrap()), Some(editor.with_buffer(|buffer| buffer.wrap())),
Message::WrapChanged, Message::WrapChanged,
) )
}; };
@ -373,20 +373,25 @@ impl Application for Window {
} }
fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) { fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
editor.buffer_mut().lines.iter_mut().for_each(|line| { editor.with_buffer_mut(|buffer| {
line.set_attrs_list(AttrsList::new(attrs)); buffer.lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
});
}); });
} }
fn update_alignment<T: Edit>(editor: &mut T, align: Align) { fn update_alignment<T: Edit>(editor: &mut T, align: Align) {
let current_line = editor.cursor().line; let current_line = editor.cursor().line;
if let Some((start, end)) = editor.selection_bounds() { let selection_bounds_opt = editor.selection_bounds();
if let Some(lines) = editor.buffer_mut().lines.get_mut(start.line..=end.line) { editor.with_buffer_mut(|buffer| {
for line in lines.iter_mut() { if let Some((start, end)) = selection_bounds_opt {
line.set_align(Some(align)); if let Some(lines) = buffer.lines.get_mut(start.line..=end.line) {
for line in lines.iter_mut() {
line.set_align(Some(align));
}
} }
} else if let Some(line) = buffer.lines.get_mut(current_line) {
line.set_align(Some(align));
} }
} else if let Some(line) = editor.buffer_mut().lines.get_mut(current_line) { });
line.set_align(Some(align));
}
} }

View file

@ -31,13 +31,13 @@ impl StyleSheet for Theme {
} }
} }
pub struct TextBox<'a, 'editor> { pub struct TextBox<'a, 'editor, 'buffer> {
editor: &'a Mutex<ViEditor<'editor>>, editor: &'a Mutex<ViEditor<'editor, 'buffer>>,
padding: Padding, padding: Padding,
} }
impl<'a, 'editor> TextBox<'a, 'editor> { impl<'a, 'editor, 'buffer> TextBox<'a, 'editor, 'buffer> {
pub fn new(editor: &'a Mutex<ViEditor<'editor>>) -> Self { pub fn new(editor: &'a Mutex<ViEditor<'editor, 'buffer>>) -> Self {
Self { Self {
editor, editor,
padding: Padding::new(0.), padding: Padding::new(0.),
@ -50,7 +50,9 @@ impl<'a, 'editor> TextBox<'a, 'editor> {
} }
} }
pub fn text_box<'a, 'editor>(editor: &'a Mutex<ViEditor<'editor>>) -> TextBox<'a, 'editor> { pub fn text_box<'a, 'editor, 'buffer>(
editor: &'a Mutex<ViEditor<'editor, 'buffer>>,
) -> TextBox<'a, 'editor, 'buffer> {
TextBox::new(editor) TextBox::new(editor)
} }
@ -103,7 +105,8 @@ fn draw_pixel(
buffer[offset + 3] = (current >> 24) as u8; buffer[offset + 3] = (current >> 24) as u8;
} }
impl<'a, 'editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, 'editor> impl<'a, 'editor, 'buffer, Message, Renderer> Widget<Message, Renderer>
for TextBox<'a, 'editor, 'buffer>
where where
Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>, Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
@ -133,14 +136,17 @@ where
.shape_as_needed(true); .shape_as_needed(true);
let mut layout_lines = 0; let mut layout_lines = 0;
for line in editor.buffer().lines.iter() { editor.with_buffer(|buffer| {
match line.layout_opt() { for line in buffer.lines.iter() {
Some(layout) => layout_lines += layout.len(), match line.layout_opt() {
None => (), Some(layout) => layout_lines += layout.len(),
None => (),
}
} }
} });
let height = layout_lines as f32 * editor.buffer().metrics().line_height; let height =
layout_lines as f32 * editor.with_buffer(|buffer| buffer.metrics().line_height);
let size = Size::new(limits.max().width, height); let size = Size::new(limits.max().width, height);
layout::Node::new(limits.resolve(size)) layout::Node::new(limits.resolve(size))
@ -205,13 +211,11 @@ where
let mut editor = editor.borrow_with(&mut font_system); let mut editor = editor.borrow_with(&mut font_system);
// Scale metrics // Scale metrics
let metrics = editor.buffer().metrics(); let metrics = editor.with_buffer(|buffer| buffer.metrics());
editor editor.with_buffer_mut(|buffer| buffer.set_metrics(metrics.scale(SCALE_FACTOR as f32)));
.buffer_mut()
.set_metrics(metrics.scale(SCALE_FACTOR as f32));
// Set size // Set size
editor.buffer_mut().set_size(image_w as f32, image_h as f32); editor.with_buffer_mut(|buffer| buffer.set_size(image_w as f32, image_h as f32));
// Shape and layout // Shape and layout
editor.shape_as_needed(true); editor.shape_as_needed(true);
@ -228,7 +232,7 @@ where
}); });
// Restore original metrics // Restore original metrics
editor.buffer_mut().set_metrics(metrics); editor.with_buffer_mut(|buffer| buffer.set_metrics(metrics));
let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels); let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
image::Renderer::draw( image::Renderer::draw(
@ -355,12 +359,13 @@ where
} }
} }
impl<'a, 'editor, Message, Renderer> From<TextBox<'a, 'editor>> for Element<'a, Message, Renderer> impl<'a, 'editor, 'buffer, Message, Renderer> From<TextBox<'a, 'editor, 'buffer>>
for Element<'a, Message, Renderer>
where where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>, Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
fn from(text_box: TextBox<'a, 'editor>) -> Self { fn from(text_box: TextBox<'a, 'editor, 'buffer>) -> Self {
Self::new(text_box) Self::new(text_box)
} }
} }

View file

@ -69,9 +69,9 @@ fn main() {
let mut editor = editor.borrow_with(&mut font_system); let mut editor = editor.borrow_with(&mut font_system);
editor editor.with_buffer_mut(|buffer| {
.buffer_mut() buffer.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32)
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32); });
let attrs = Attrs::new().family(Family::Monospace); let attrs = Attrs::new().family(Family::Monospace);
match editor.load_text(&path, attrs) { match editor.load_text(&path, attrs) {
@ -89,7 +89,7 @@ fn main() {
let mut mouse_left = false; let mut mouse_left = false;
loop { loop {
editor.shape_as_needed(true); editor.shape_as_needed(true);
if editor.buffer().redraw() { if editor.redraw() {
let instant = Instant::now(); let instant = Instant::now();
let bg = editor.background_color(); let bg = editor.background_color();
@ -109,15 +109,17 @@ fn main() {
{ {
let mut start_line_opt = None; let mut start_line_opt = None;
let mut end_line = 0; let mut end_line = 0;
for run in editor.buffer().layout_runs() { editor.with_buffer(|buffer| {
end_line = run.line_i; for run in buffer.layout_runs() {
if start_line_opt.is_none() { end_line = run.line_i;
start_line_opt = Some(end_line); if start_line_opt.is_none() {
start_line_opt = Some(end_line);
}
} }
} });
let start_line = start_line_opt.unwrap_or(end_line); let start_line = start_line_opt.unwrap_or(end_line);
let lines = editor.buffer().lines.len(); let lines = editor.with_buffer(|buffer| buffer.lines.len());
let start_y = (start_line * window.height() as usize) / lines; let start_y = (start_line * window.height() as usize) / lines;
let end_y = (end_line * window.height() as usize) / lines; let end_y = (end_line * window.height() as usize) / lines;
if end_y > start_y { if end_y > start_y {
@ -133,7 +135,7 @@ fn main() {
window.sync(); window.sync();
editor.buffer_mut().set_redraw(false); editor.set_redraw(false);
log::debug!("redraw: {:?}", instant.elapsed()); log::debug!("redraw: {:?}", instant.elapsed());
} }
@ -172,18 +174,23 @@ fn main() {
orbclient::K_DEL if event.pressed => editor.action(Action::Delete), orbclient::K_DEL if event.pressed => editor.action(Action::Delete),
orbclient::K_0 if event.pressed && ctrl_pressed => { orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default; font_size_i = font_size_default;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]); editor
.with_buffer_mut(|buffer| buffer.set_metrics(font_sizes[font_size_i]));
} }
orbclient::K_MINUS if event.pressed && ctrl_pressed => { orbclient::K_MINUS if event.pressed && ctrl_pressed => {
if font_size_i > 0 { if font_size_i > 0 {
font_size_i -= 1; font_size_i -= 1;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]); editor.with_buffer_mut(|buffer| {
buffer.set_metrics(font_sizes[font_size_i])
});
} }
} }
orbclient::K_EQUALS if event.pressed && ctrl_pressed => { orbclient::K_EQUALS if event.pressed && ctrl_pressed => {
if font_size_i + 1 < font_sizes.len() { if font_size_i + 1 < font_sizes.len() {
font_size_i += 1; font_size_i += 1;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]); editor.with_buffer_mut(|buffer| {
buffer.set_metrics(font_sizes[font_size_i])
});
} }
} }
_ => (), _ => (),
@ -224,9 +231,9 @@ fn main() {
} }
} }
EventOption::Resize(event) => { EventOption::Resize(event) => {
editor editor.with_buffer_mut(|buffer| {
.buffer_mut() buffer.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
.set_size(event.width as f32 - line_x * 2.0, event.height as f32); });
} }
EventOption::Scroll(event) => { EventOption::Scroll(event) => {
editor.action(Action::Scroll { editor.action(Action::Scroll {

View file

@ -19,7 +19,7 @@ fn redraw(
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33); let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
editor.shape_as_needed(true); editor.shape_as_needed(true);
if editor.buffer().redraw() { if editor.redraw() {
let instant = Instant::now(); let instant = Instant::now();
window.set(bg_color); window.set(bg_color);
@ -36,7 +36,7 @@ fn redraw(
window.sync(); window.sync();
editor.buffer_mut().set_redraw(false); editor.set_redraw(false);
let duration = instant.elapsed(); let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration); log::debug!("redraw: {:?}", duration);
@ -159,13 +159,15 @@ fn main() {
log::info!("Test completed in {:?}", test_elapsed); log::info!("Test completed in {:?}", test_elapsed);
let mut wrong = 0; let mut wrong = 0;
for (line_i, line) in text.lines().enumerate() { editor.with_buffer(|buffer| {
let buffer_line = &editor.buffer().lines[line_i]; for (line_i, line) in text.lines().enumerate() {
if buffer_line.text() != line { let buffer_line = &buffer.lines[line_i];
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line); if buffer_line.text() != line {
wrong += 1; log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
wrong += 1;
}
} }
} });
if wrong == 0 { if wrong == 0 {
log::info!("All lines matched!"); log::info!("All lines matched!");
process::exit(0); process::exit(0);

View file

@ -42,9 +42,7 @@ fn main() {
let mut editor = editor.borrow_with(&mut font_system); let mut editor = editor.borrow_with(&mut font_system);
editor editor.with_buffer_mut(|buffer| buffer.set_size(window.width() as f32, window.height() as f32));
.buffer_mut()
.set_size(window.width() as f32, window.height() as f32);
let attrs = Attrs::new(); let attrs = Attrs::new();
let serif_attrs = attrs.family(Family::Serif); let serif_attrs = attrs.family(Family::Serif);
@ -117,9 +115,9 @@ fn main() {
), ),
]; ];
editor editor.with_buffer_mut(|buffer| {
.buffer_mut() buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced)
.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced); });
let mut swash_cache = SwashCache::new(); let mut swash_cache = SwashCache::new();
@ -134,7 +132,7 @@ fn main() {
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33); let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
editor.shape_as_needed(true); editor.shape_as_needed(true);
if editor.buffer().redraw() { if editor.redraw() {
let instant = Instant::now(); let instant = Instant::now();
window.set(bg_color); window.set(bg_color);
@ -151,7 +149,7 @@ fn main() {
window.sync(); window.sync();
editor.buffer_mut().set_redraw(false); editor.set_redraw(false);
let duration = instant.elapsed(); let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration); log::debug!("redraw: {:?}", duration);
@ -206,9 +204,9 @@ fn main() {
} }
} }
EventOption::Resize(resize) => { EventOption::Resize(resize) => {
editor editor.with_buffer_mut(|buffer| {
.buffer_mut() buffer.set_size(resize.width as f32, resize.height as f32)
.set_size(resize.width as f32, resize.height as f32); });
} }
EventOption::Quit(_) => process::exit(0), EventOption::Quit(_) => process::exit(0),
_ => (), _ => (),

File diff suppressed because it is too large Load diff

View file

@ -131,10 +131,20 @@ pub trait Edit {
} }
/// Get the internal [`Buffer`] /// Get the internal [`Buffer`]
fn buffer(&self) -> &Buffer; fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T;
/// Get the internal [`Buffer`], mutably /// Get the internal [`Buffer`], mutably
fn buffer_mut(&mut self) -> &mut Buffer; fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T;
/// Get the [`Buffer`] redraw flag
fn redraw(&self) -> bool {
self.with_buffer(|buffer| buffer.redraw())
}
/// Set the [`Buffer`] redraw flag
fn set_redraw(&mut self, redraw: bool) {
self.with_buffer_mut(|buffer| buffer.set_redraw(redraw))
}
/// Get the current cursor /// Get the current cursor
fn cursor(&self) -> Cursor; fn cursor(&self) -> Cursor;
@ -151,69 +161,71 @@ pub trait Edit {
/// Get the bounds of the current selection /// Get the bounds of the current selection
//TODO: will not work with Block select //TODO: will not work with Block select
fn selection_bounds(&self) -> Option<(Cursor, Cursor)> { fn selection_bounds(&self) -> Option<(Cursor, Cursor)> {
let cursor = self.cursor(); self.with_buffer(|buffer| {
match self.selection() { let cursor = self.cursor();
Selection::None => None, match self.selection() {
Selection::Normal(select) => match select.line.cmp(&cursor.line) { Selection::None => None,
cmp::Ordering::Greater => Some((cursor, select)), Selection::Normal(select) => match select.line.cmp(&cursor.line) {
cmp::Ordering::Less => Some((select, cursor)), cmp::Ordering::Greater => Some((cursor, select)),
cmp::Ordering::Equal => { cmp::Ordering::Less => Some((select, cursor)),
/* select.line == cursor.line */
if select.index < cursor.index {
Some((select, cursor))
} else {
/* select.index >= cursor.index */
Some((cursor, select))
}
}
},
Selection::Line(select) => {
let start_line = cmp::min(select.line, cursor.line);
let end_line = cmp::max(select.line, cursor.line);
let end_index = self.buffer().lines[end_line].text().len();
Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
}
Selection::Word(select) => {
let (mut start, mut end) = match select.line.cmp(&cursor.line) {
cmp::Ordering::Greater => (cursor, select),
cmp::Ordering::Less => (select, cursor),
cmp::Ordering::Equal => { cmp::Ordering::Equal => {
/* select.line == cursor.line */ /* select.line == cursor.line */
if select.index < cursor.index { if select.index < cursor.index {
(select, cursor) Some((select, cursor))
} else { } else {
/* select.index >= cursor.index */ /* select.index >= cursor.index */
(cursor, select) Some((cursor, select))
} }
} }
}; },
Selection::Line(select) => {
// Move start to beginning of word let start_line = cmp::min(select.line, cursor.line);
{ let end_line = cmp::max(select.line, cursor.line);
let line = &self.buffer().lines[start.line]; let end_index = buffer.lines[end_line].text().len();
start.index = line Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
.text()
.unicode_word_indices()
.rev()
.map(|(i, _)| i)
.find(|&i| i < start.index)
.unwrap_or(0);
} }
Selection::Word(select) => {
let (mut start, mut end) = match select.line.cmp(&cursor.line) {
cmp::Ordering::Greater => (cursor, select),
cmp::Ordering::Less => (select, cursor),
cmp::Ordering::Equal => {
/* select.line == cursor.line */
if select.index < cursor.index {
(select, cursor)
} else {
/* select.index >= cursor.index */
(cursor, select)
}
}
};
// Move end to end of word // Move start to beginning of word
{ {
let line = &self.buffer().lines[end.line]; let line = &buffer.lines[start.line];
end.index = line start.index = line
.text() .text()
.unicode_word_indices() .unicode_word_indices()
.map(|(i, word)| i + word.len()) .rev()
.find(|&i| i > end.index) .map(|(i, _)| i)
.unwrap_or(line.text().len()); .find(|&i| i < start.index)
.unwrap_or(0);
}
// Move end to end of word
{
let line = &buffer.lines[end.line];
end.index = line
.text()
.unicode_word_indices()
.map(|(i, word)| i + word.len())
.find(|&i| i > end.index)
.unwrap_or(line.text().len());
}
Some((start, end))
} }
Some((start, end))
} }
} })
} }
/// Get the current automatic indentation setting /// Get the current automatic indentation setting
@ -265,13 +277,19 @@ pub trait Edit {
fn action(&mut self, font_system: &mut FontSystem, action: Action); fn action(&mut self, font_system: &mut FontSystem, action: Action);
} }
impl<'a, T: Edit> BorrowedWithFontSystem<'a, T> { impl<'a, E: Edit> BorrowedWithFontSystem<'a, E> {
/// Get the internal [`Buffer`], mutably /// Get the internal [`Buffer`], mutably
pub fn buffer_mut(&mut self) -> BorrowedWithFontSystem<Buffer> { pub fn with_buffer_mut<F: FnOnce(&mut BorrowedWithFontSystem<Buffer>) -> T, T>(
BorrowedWithFontSystem { &mut self,
inner: self.inner.buffer_mut(), f: F,
font_system: self.font_system, ) -> T {
} self.inner.with_buffer_mut(|buffer| {
let mut borrowed = BorrowedWithFontSystem {
inner: buffer,
font_system: self.font_system,
};
f(&mut borrowed)
})
} }
/// Shape lines until scroll, after adjusting scroll if the cursor moved /// Shape lines until scroll, after adjusting scroll if the cursor moved

View file

@ -8,8 +8,8 @@ use syntect::highlighting::{
use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet}; use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};
use crate::{ use crate::{
Action, AttrsList, BorrowedWithFontSystem, Buffer, Change, Color, Cursor, Edit, Editor, Action, AttrsList, BorrowedWithFontSystem, Buffer, BufferRef, Change, Color, Cursor, Edit,
FontSystem, Selection, Shaping, Style, Weight, Editor, FontSystem, Selection, Shaping, Style, Weight,
}; };
pub use syntect::highlighting::Theme as SyntaxTheme; pub use syntect::highlighting::Theme as SyntaxTheme;
@ -33,8 +33,8 @@ impl SyntaxSystem {
/// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`] /// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`]
#[derive(Debug)] #[derive(Debug)]
pub struct SyntaxEditor<'a> { pub struct SyntaxEditor<'a, 'b> {
editor: Editor, editor: Editor<'b>,
syntax_system: &'a SyntaxSystem, syntax_system: &'a SyntaxSystem,
syntax: &'a SyntaxReference, syntax: &'a SyntaxReference,
theme: &'a SyntaxTheme, theme: &'a SyntaxTheme,
@ -42,13 +42,17 @@ pub struct SyntaxEditor<'a> {
syntax_cache: Vec<(ParseState, ScopeStack)>, syntax_cache: Vec<(ParseState, ScopeStack)>,
} }
impl<'a> SyntaxEditor<'a> { impl<'a, 'b> SyntaxEditor<'a, 'b> {
/// Create a new [`SyntaxEditor`] with the provided [`Buffer`], [`SyntaxSystem`], and theme name. /// Create a new [`SyntaxEditor`] with the provided [`Buffer`], [`SyntaxSystem`], and theme name.
/// ///
/// A good default theme name is "base16-eighties.dark". /// A good default theme name is "base16-eighties.dark".
/// ///
/// Returns None if theme not found /// Returns None if theme not found
pub fn new(buffer: Buffer, syntax_system: &'a SyntaxSystem, theme_name: &str) -> Option<Self> { pub fn new(
buffer: impl Into<BufferRef<'b>>,
syntax_system: &'a SyntaxSystem,
theme_name: &str,
) -> Option<Self> {
let editor = Editor::new(buffer); let editor = Editor::new(buffer);
let syntax = syntax_system.syntax_set.find_syntax_plain_text(); let syntax = syntax_system.syntax_set.find_syntax_plain_text();
let theme = syntax_system.theme_set.themes.get(theme_name)?; let theme = syntax_system.theme_set.themes.get(theme_name)?;
@ -73,18 +77,20 @@ impl<'a> SyntaxEditor<'a> {
self.syntax_cache.clear(); self.syntax_cache.clear();
// Reset attrs to match default foreground and no highlighting // Reset attrs to match default foreground and no highlighting
for line in self.editor.buffer_mut().lines.iter_mut() { self.with_buffer_mut(|buffer| {
let mut attrs = line.attrs_list().defaults(); for line in buffer.lines.iter_mut() {
if let Some(foreground) = self.theme.settings.foreground { let mut attrs = line.attrs_list().defaults();
attrs = attrs.color(Color::rgba( if let Some(foreground) = self.theme.settings.foreground {
foreground.r, attrs = attrs.color(Color::rgba(
foreground.g, foreground.r,
foreground.b, foreground.g,
foreground.a, foreground.b,
)); foreground.a,
));
}
line.set_attrs_list(AttrsList::new(attrs));
} }
line.set_attrs_list(AttrsList::new(attrs)); });
}
} }
true true
@ -118,9 +124,9 @@ impl<'a> SyntaxEditor<'a> {
} }
let text = fs::read_to_string(path)?; let text = fs::read_to_string(path)?;
self.editor self.editor.with_buffer_mut(|buffer| {
.buffer_mut() buffer.set_text(font_system, &text, attrs, Shaping::Advanced)
.set_text(font_system, &text, attrs, Shaping::Advanced); });
//TODO: re-use text //TODO: re-use text
self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) { self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) {
@ -194,7 +200,7 @@ impl<'a> SyntaxEditor<'a> {
where where
F: FnMut(i32, i32, u32, u32, Color), F: FnMut(i32, i32, u32, u32, Color),
{ {
let size = self.buffer().size(); let size = self.with_buffer(|buffer| buffer.size());
f(0, 0, size.0 as u32, size.1 as u32, self.background_color()); f(0, 0, size.0 as u32, size.1 as u32, self.background_color());
self.editor.draw( self.editor.draw(
font_system, font_system,
@ -207,13 +213,13 @@ impl<'a> SyntaxEditor<'a> {
} }
} }
impl<'a> Edit for SyntaxEditor<'a> { impl<'a, 'b> Edit for SyntaxEditor<'a, 'b> {
fn buffer(&self) -> &Buffer { fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
self.editor.buffer() self.editor.with_buffer(f)
} }
fn buffer_mut(&mut self) -> &mut Buffer { fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
self.editor.buffer_mut() self.editor.with_buffer_mut(f)
} }
fn cursor(&self) -> Cursor { fn cursor(&self) -> Cursor {
@ -253,23 +259,84 @@ impl<'a> Edit for SyntaxEditor<'a> {
let now = std::time::Instant::now(); let now = std::time::Instant::now();
let cursor = self.cursor(); let cursor = self.cursor();
let buffer = self.editor.buffer_mut(); self.editor.with_buffer_mut(|buffer| {
let visible_lines = buffer.visible_lines(); let visible_lines = buffer.visible_lines();
let scroll = buffer.scroll(); let scroll = buffer.scroll();
let scroll_end = scroll.layout + visible_lines; let scroll_end = scroll.layout + visible_lines;
let mut total_layout = 0; let mut total_layout = 0;
let mut highlighted = 0; let mut highlighted = 0;
for line_i in 0..buffer.lines.len() { for line_i in 0..buffer.lines.len() {
// Break out if we have reached the end of scroll and are past the cursor // Break out if we have reached the end of scroll and are past the cursor
if total_layout >= scroll_end && line_i > cursor.line { if total_layout >= scroll_end && line_i > cursor.line {
break; break;
} }
let line = &mut buffer.lines[line_i]; let line = &mut buffer.lines[line_i];
if line.metadata().is_some() && line_i < self.syntax_cache.len() { if line.metadata().is_some() && line_i < self.syntax_cache.len() {
//TODO: duplicated code! //TODO: duplicated code!
if line_i >= scroll.line && total_layout < scroll_end {
// Perform shaping and layout of this line in order to count if we have reached scroll
match buffer.line_layout(font_system, line_i) {
Some(layout_lines) => {
total_layout += layout_lines.len() as i32;
}
None => {
//TODO: should this be possible?
}
}
}
continue;
}
highlighted += 1;
let (mut parse_state, scope_stack) =
if line_i > 0 && line_i <= self.syntax_cache.len() {
self.syntax_cache[line_i - 1].clone()
} else {
(ParseState::new(self.syntax), ScopeStack::new())
};
let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
let ops = parse_state
.parse_line(line.text(), &self.syntax_system.syntax_set)
.expect("failed to parse syntax");
let ranges = RangedHighlightIterator::new(
&mut highlight_state,
&ops,
line.text(),
&self.highlighter,
);
let attrs = line.attrs_list().defaults();
let mut attrs_list = AttrsList::new(attrs);
for (style, _, range) in ranges {
let span_attrs = attrs
.color(Color::rgba(
style.foreground.r,
style.foreground.g,
style.foreground.b,
style.foreground.a,
))
//TODO: background
.style(if style.font_style.contains(FontStyle::ITALIC) {
Style::Italic
} else {
Style::Normal
})
.weight(if style.font_style.contains(FontStyle::BOLD) {
Weight::BOLD
} else {
Weight::NORMAL
}); //TODO: underline
if span_attrs != attrs {
attrs_list.add_span(range, span_attrs);
}
}
// Update line attributes. This operation only resets if the line changes
line.set_attrs_list(attrs_list);
// Perform shaping and layout of this line in order to count if we have reached scroll
if line_i >= scroll.line && total_layout < scroll_end { if line_i >= scroll.line && total_layout < scroll_end {
// Perform shaping and layout of this line in order to count if we have reached scroll
match buffer.line_layout(font_system, line_i) { match buffer.line_layout(font_system, line_i) {
Some(layout_lines) => { Some(layout_lines) => {
total_layout += layout_lines.len() as i32; total_layout += layout_lines.len() as i32;
@ -279,91 +346,31 @@ impl<'a> Edit for SyntaxEditor<'a> {
} }
} }
} }
continue;
}
highlighted += 1;
let (mut parse_state, scope_stack) = if line_i > 0 && line_i <= self.syntax_cache.len() let cache_item = (parse_state.clone(), highlight_state.path.clone());
{ if line_i < self.syntax_cache.len() {
self.syntax_cache[line_i - 1].clone() if self.syntax_cache[line_i] != cache_item {
} else { self.syntax_cache[line_i] = cache_item;
(ParseState::new(self.syntax), ScopeStack::new()) if line_i + 1 < buffer.lines.len() {
}; buffer.lines[line_i + 1].reset();
let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack); }
let ops = parse_state }
.parse_line(line.text(), &self.syntax_system.syntax_set) } else {
.expect("failed to parse syntax"); buffer.lines[line_i].set_metadata(self.syntax_cache.len());
let ranges = RangedHighlightIterator::new( self.syntax_cache.push(cache_item);
&mut highlight_state,
&ops,
line.text(),
&self.highlighter,
);
let attrs = line.attrs_list().defaults();
let mut attrs_list = AttrsList::new(attrs);
for (style, _, range) in ranges {
let span_attrs = attrs
.color(Color::rgba(
style.foreground.r,
style.foreground.g,
style.foreground.b,
style.foreground.a,
))
//TODO: background
.style(if style.font_style.contains(FontStyle::ITALIC) {
Style::Italic
} else {
Style::Normal
})
.weight(if style.font_style.contains(FontStyle::BOLD) {
Weight::BOLD
} else {
Weight::NORMAL
}); //TODO: underline
if span_attrs != attrs {
attrs_list.add_span(range, span_attrs);
} }
} }
// Update line attributes. This operation only resets if the line changes if highlighted > 0 {
line.set_attrs_list(attrs_list); buffer.set_redraw(true);
#[cfg(feature = "std")]
// Perform shaping and layout of this line in order to count if we have reached scroll log::debug!(
if line_i >= scroll.line && total_layout < scroll_end { "Syntax highlighted {} lines in {:?}",
match buffer.line_layout(font_system, line_i) { highlighted,
Some(layout_lines) => { now.elapsed()
total_layout += layout_lines.len() as i32; );
}
None => {
//TODO: should this be possible?
}
}
} }
});
let cache_item = (parse_state.clone(), highlight_state.path.clone());
if line_i < self.syntax_cache.len() {
if self.syntax_cache[line_i] != cache_item {
self.syntax_cache[line_i] = cache_item;
if line_i + 1 < buffer.lines.len() {
buffer.lines[line_i + 1].reset();
}
}
} else {
buffer.lines[line_i].set_metadata(self.syntax_cache.len());
self.syntax_cache.push(cache_item);
}
}
if highlighted > 0 {
buffer.set_redraw(true);
#[cfg(feature = "std")]
log::debug!(
"Syntax highlighted {} lines in {:?}",
highlighted,
now.elapsed()
);
}
self.editor.shape_as_needed(font_system, prune); self.editor.shape_as_needed(font_system, prune);
} }
@ -401,7 +408,7 @@ impl<'a> Edit for SyntaxEditor<'a> {
} }
} }
impl<'a, 'b> BorrowedWithFontSystem<'b, SyntaxEditor<'a>> { impl<'a, 'b, 'c> BorrowedWithFontSystem<'c, SyntaxEditor<'a, 'b>> {
/// Load text from a file, and also set syntax to the best option /// Load text from a file, and also set syntax to the best option
/// ///
/// ## Errors /// ## Errors

File diff suppressed because it is too large Load diff