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>,
attrs: Attrs<'static>,
font_size: FontSize,
editor: Mutex<cosmic_text::ViEditor<'static>>,
editor: Mutex<cosmic_text::ViEditor<'static, 'static>>,
}
#[allow(dead_code)]
@ -185,10 +185,12 @@ impl Application for Window {
if let Some(path) = &self.path_opt {
let editor = self.editor.lock().unwrap();
let mut text = String::new();
for line in editor.buffer().lines.iter() {
editor.with_buffer(|buffer| {
for line in buffer.lines.iter() {
text.push_str(line.text());
text.push('\n');
}
});
match fs::write(path, text) {
Ok(()) => {
log::info!("saved '{}'", path.display());
@ -234,15 +236,13 @@ impl Application for Window {
let mut editor = self.editor.lock().unwrap();
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut()
.set_metrics(font_size.to_metrics());
.with_buffer_mut(|buffer| buffer.set_metrics(font_size.to_metrics()));
}
Message::WrapChanged(wrap) => {
let mut editor = self.editor.lock().unwrap();
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut()
.set_wrap(wrap);
.with_buffer_mut(|buffer| buffer.set_wrap(wrap));
}
Message::AlignmentChanged(align) => {
let mut editor = self.editor.lock().unwrap();
@ -304,7 +304,7 @@ impl Application for Window {
let editor = self.editor.lock().unwrap();
pick_list(
WRAP_MODE,
Some(editor.buffer().wrap()),
Some(editor.with_buffer(|buffer| buffer.wrap())),
Message::WrapChanged,
)
};
@ -373,20 +373,25 @@ impl Application for Window {
}
fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
editor.buffer_mut().lines.iter_mut().for_each(|line| {
editor.with_buffer_mut(|buffer| {
buffer.lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
});
});
}
fn update_alignment<T: Edit>(editor: &mut T, align: Align) {
let current_line = editor.cursor().line;
if let Some((start, end)) = editor.selection_bounds() {
if let Some(lines) = editor.buffer_mut().lines.get_mut(start.line..=end.line) {
let selection_bounds_opt = editor.selection_bounds();
editor.with_buffer_mut(|buffer| {
if let Some((start, end)) = selection_bounds_opt {
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) = editor.buffer_mut().lines.get_mut(current_line) {
} else if let Some(line) = buffer.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> {
editor: &'a Mutex<ViEditor<'editor>>,
pub struct TextBox<'a, 'editor, 'buffer> {
editor: &'a Mutex<ViEditor<'editor, 'buffer>>,
padding: Padding,
}
impl<'a, 'editor> TextBox<'a, 'editor> {
pub fn new(editor: &'a Mutex<ViEditor<'editor>>) -> Self {
impl<'a, 'editor, 'buffer> TextBox<'a, 'editor, 'buffer> {
pub fn new(editor: &'a Mutex<ViEditor<'editor, 'buffer>>) -> Self {
Self {
editor,
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)
}
@ -103,7 +105,8 @@ fn draw_pixel(
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
Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
@ -133,14 +136,17 @@ where
.shape_as_needed(true);
let mut layout_lines = 0;
for line in editor.buffer().lines.iter() {
editor.with_buffer(|buffer| {
for line in buffer.lines.iter() {
match line.layout_opt() {
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);
layout::Node::new(limits.resolve(size))
@ -205,13 +211,11 @@ where
let mut editor = editor.borrow_with(&mut font_system);
// Scale metrics
let metrics = editor.buffer().metrics();
editor
.buffer_mut()
.set_metrics(metrics.scale(SCALE_FACTOR as f32));
let metrics = editor.with_buffer(|buffer| buffer.metrics());
editor.with_buffer_mut(|buffer| buffer.set_metrics(metrics.scale(SCALE_FACTOR as f32)));
// 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
editor.shape_as_needed(true);
@ -228,7 +232,7 @@ where
});
// 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);
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
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
{
fn from(text_box: TextBox<'a, 'editor>) -> Self {
fn from(text_box: TextBox<'a, 'editor, 'buffer>) -> Self {
Self::new(text_box)
}
}

View file

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

View file

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

View file

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

View file

@ -2,6 +2,7 @@
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use core::{cmp, iter::once};
use unicode_segmentation::UnicodeSegmentation;
@ -12,10 +13,35 @@ use crate::{
Edit, FontSystem, Selection, Shaping,
};
#[derive(Debug)]
pub enum BufferRef<'a> {
Owned(Buffer),
Borrowed(&'a mut Buffer),
Arc(Arc<Buffer>),
}
impl<'a> From<Buffer> for BufferRef<'a> {
fn from(buffer: Buffer) -> Self {
Self::Owned(buffer)
}
}
impl<'a> From<&'a mut Buffer> for BufferRef<'a> {
fn from(buffer: &'a mut Buffer) -> Self {
Self::Borrowed(buffer)
}
}
impl<'a> From<Arc<Buffer>> for BufferRef<'a> {
fn from(arc: Arc<Buffer>) -> Self {
Self::Arc(arc)
}
}
/// A wrapper of [`Buffer`] for easy editing
#[derive(Debug)]
pub struct Editor {
buffer: Buffer,
pub struct Editor<'a> {
buffer_ref: BufferRef<'a>,
cursor: Cursor,
cursor_x_opt: Option<i32>,
selection: Selection,
@ -25,11 +51,11 @@ pub struct Editor {
change: Option<Change>,
}
impl Editor {
impl<'a> Editor<'a> {
/// Create a new [`Editor`] with the provided [`Buffer`]
pub fn new(buffer: Buffer) -> Self {
pub fn new(buffer: impl Into<BufferRef<'a>>) -> Self {
Self {
buffer,
buffer_ref: buffer.into(),
cursor: Cursor::default(),
cursor_x_opt: None,
selection: Selection::None,
@ -53,9 +79,9 @@ impl Editor {
) where
F: FnMut(i32, i32, u32, u32, Color),
{
let line_height = self.buffer.metrics().line_height;
for run in self.buffer.layout_runs() {
self.with_buffer(|buffer| {
let line_height = buffer.metrics().line_height;
for run in buffer.layout_runs() {
let line_i = run.line_i;
let line_y = run.line_y;
let line_top = run.line_top;
@ -134,7 +160,7 @@ impl Editor {
if run.glyphs.is_empty() && end.line > line_i {
// Highlight all of internal empty lines
range_opt = Some((0, self.buffer.size().0 as i32));
range_opt = Some((0, buffer.size().0 as i32));
}
if let Some((mut min, mut max)) = range_opt.take() {
@ -143,7 +169,7 @@ impl Editor {
if run.rtl {
min = 0;
} else {
max = self.buffer.size().0 as i32;
max = buffer.size().0 as i32;
}
}
f(
@ -211,16 +237,29 @@ impl Editor {
);
}
}
});
}
}
impl Edit for Editor {
fn buffer(&self) -> &Buffer {
&self.buffer
impl<'a> Edit for Editor<'a> {
fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
match &self.buffer_ref {
BufferRef::Owned(buffer) => f(buffer),
BufferRef::Borrowed(buffer) => f(buffer),
BufferRef::Arc(buffer) => f(buffer),
}
}
fn buffer_mut(&mut self) -> &mut Buffer {
&mut self.buffer
fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
match &mut self.buffer_ref {
BufferRef::Owned(buffer) => f(buffer),
BufferRef::Borrowed(buffer) => f(buffer),
BufferRef::Arc(arc) => match Arc::get_mut(arc) {
Some(buffer) => f(buffer),
//TODO: use make_mut?
None => panic!("BufferRef::Arc cannot be accessed mutibly"),
},
}
}
fn cursor(&self) -> Cursor {
@ -231,7 +270,7 @@ impl Edit for Editor {
if self.cursor != cursor {
self.cursor = cursor;
self.cursor_moved = true;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
@ -242,7 +281,7 @@ impl Edit for Editor {
fn set_selection(&mut self, selection: Selection) {
if self.selection != selection {
self.selection = selection;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
@ -265,31 +304,32 @@ impl Edit for Editor {
}
if self.tab_width != tab_width {
self.tab_width = tab_width;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
if self.cursor_moved {
self.buffer
.shape_until_cursor(font_system, self.cursor, prune);
let cursor = self.cursor;
self.with_buffer_mut(|buffer| buffer.shape_until_cursor(font_system, cursor, prune));
self.cursor_moved = false;
} else {
self.buffer.shape_until_scroll(font_system, prune);
self.with_buffer_mut(|buffer| buffer.shape_until_scroll(font_system, prune));
}
}
fn delete_range(&mut self, start: Cursor, end: Cursor) {
let change_item = self.with_buffer_mut(|buffer| {
// Collect removed data for change tracking
let mut change_text = String::new();
// Delete the selection from the last line
let end_line_opt = if end.line > start.line {
// Get part of line after selection
let after = self.buffer.lines[end.line].split_off(end.index);
let after = buffer.lines[end.line].split_off(end.index);
// Remove end line
let removed = self.buffer.lines.remove(end.line);
let removed = buffer.lines.remove(end.line);
change_text.insert_str(0, removed.text());
Some(after)
@ -299,7 +339,7 @@ impl Edit for Editor {
// Delete interior lines (in reverse for safety)
for line_i in (start.line + 1..end.line).rev() {
let removed = self.buffer.lines.remove(line_i);
let removed = buffer.lines.remove(line_i);
change_text.insert_str(0, removed.text());
}
@ -307,34 +347,36 @@ impl Edit for Editor {
{
// Get part after selection if start line is also end line
let after_opt = if start.line == end.line {
Some(self.buffer.lines[start.line].split_off(end.index))
Some(buffer.lines[start.line].split_off(end.index))
} else {
None
};
// Delete selected part of line
let removed = self.buffer.lines[start.line].split_off(start.index);
let removed = buffer.lines[start.line].split_off(start.index);
change_text.insert_str(0, removed.text());
// Re-add part of line after selection
if let Some(after) = after_opt {
self.buffer.lines[start.line].append(after);
buffer.lines[start.line].append(after);
}
// Re-add valid parts of end line
if let Some(end_line) = end_line_opt {
self.buffer.lines[start.line].append(end_line);
buffer.lines[start.line].append(end_line);
}
}
if let Some(ref mut change) = self.change {
let item = ChangeItem {
ChangeItem {
start,
end,
text: change_text,
insert: false,
};
change.items.push(item);
}
});
if let Some(ref mut change) = self.change {
change.items.push(change_item);
}
}
@ -349,10 +391,11 @@ impl Edit for Editor {
return cursor;
}
let change_item = self.with_buffer_mut(|buffer| {
// Save cursor for change tracking
let start = cursor;
let line: &mut BufferLine = &mut self.buffer.lines[cursor.line];
let line: &mut BufferLine = &mut buffer.lines[cursor.line];
let insert_line = cursor.line + 1;
// Collect text after insertion as a line
@ -392,7 +435,7 @@ impl Edit for Editor {
Shaping::Advanced,
);
tmp.append(after);
self.buffer.lines.insert(insert_line, tmp);
buffer.lines.insert(insert_line, tmp);
cursor.line += 1;
} else {
line.append(after);
@ -406,23 +449,25 @@ impl Edit for Editor {
final_attrs.split_off(remaining_split_len),
Shaping::Advanced,
);
self.buffer.lines.insert(insert_line, tmp);
buffer.lines.insert(insert_line, tmp);
cursor.line += 1;
}
assert_eq!(remaining_split_len, 0);
// Append the text after insertion
cursor.index = self.buffer.lines[cursor.line].text().len() - after_len;
cursor.index = buffer.lines[cursor.line].text().len() - after_len;
if let Some(ref mut change) = self.change {
let item = ChangeItem {
ChangeItem {
start,
end: cursor,
text: data.to_string(),
insert: true,
};
change.items.push(item);
}
});
if let Some(ref mut change) = self.change {
change.items.push(change_item);
}
cursor
@ -430,32 +475,33 @@ impl Edit for Editor {
fn copy_selection(&self) -> Option<String> {
let (start, end) = self.selection_bounds()?;
self.with_buffer(|buffer| {
let mut selection = String::new();
// Take the selection from the first line
{
// Add selected part of line to string
if start.line == end.line {
selection.push_str(&self.buffer.lines[start.line].text()[start.index..end.index]);
selection.push_str(&buffer.lines[start.line].text()[start.index..end.index]);
} else {
selection.push_str(&self.buffer.lines[start.line].text()[start.index..]);
selection.push_str(&buffer.lines[start.line].text()[start.index..]);
selection.push('\n');
}
}
// Take the selection from all interior lines (if they exist)
for line_i in start.line + 1..end.line {
selection.push_str(self.buffer.lines[line_i].text());
selection.push_str(buffer.lines[line_i].text());
selection.push('\n');
}
// Take the selection from the last line
if end.line > start.line {
// Add selected part of line to string
selection.push_str(&self.buffer.lines[end.line].text()[..end.index]);
selection.push_str(&buffer.lines[end.line].text()[..end.index]);
}
Some(selection)
})
}
fn delete_selection(&mut self) -> bool {
@ -517,18 +563,19 @@ impl Edit for Editor {
match action {
Action::Motion(motion) => {
if let Some((cursor, cursor_x_opt)) =
self.buffer
.cursor_motion(font_system, self.cursor, self.cursor_x_opt, motion)
{
self.cursor = cursor;
self.cursor_x_opt = cursor_x_opt;
let cursor = self.cursor;
let cursor_x_opt = self.cursor_x_opt;
if let Some((new_cursor, new_cursor_x_opt)) = self.with_buffer_mut(|buffer| {
buffer.cursor_motion(font_system, cursor, cursor_x_opt, motion)
}) {
self.cursor = new_cursor;
self.cursor_x_opt = new_cursor_x_opt;
}
}
Action::Escape => {
match self.selection {
Selection::None => {}
_ => self.buffer.set_redraw(true),
_ => self.with_buffer_mut(|buffer| buffer.set_redraw(true)),
}
self.selection = Selection::None;
}
@ -548,8 +595,8 @@ impl Edit for Editor {
//TODO: what about indenting more after opening brackets or parentheses?
if self.auto_indent {
let mut string = String::from("\n");
{
let line = &self.buffer.lines[self.cursor.line];
self.with_buffer(|buffer| {
let line = &buffer.lines[self.cursor.line];
let text = line.text();
for c in text.chars() {
if c.is_whitespace() {
@ -558,14 +605,17 @@ impl Edit for Editor {
break;
}
}
}
});
self.insert_string(&string, None);
} else {
self.insert_string("\n", None);
}
// Ensure line is properly shaped and laid out (for potential immediate commands)
self.buffer.line_layout(font_system, self.cursor.line);
let line_i = self.cursor.line;
self.with_buffer_mut(|buffer| {
buffer.line_layout(font_system, line_i);
});
}
Action::Backspace => {
if self.delete_selection() {
@ -576,16 +626,17 @@ impl Edit for Editor {
if self.cursor.index > 0 {
// Move cursor to previous character index
let line = &self.buffer.lines[self.cursor.line];
self.cursor.index = line.text()[..self.cursor.index]
self.cursor.index = self.with_buffer(|buffer| {
buffer.lines[self.cursor.line].text()[..self.cursor.index]
.char_indices()
.next_back()
.map_or(0, |(i, _)| i);
.map_or(0, |(i, _)| i)
});
} else if self.cursor.line > 0 {
// Move cursor to previous line
self.cursor.line -= 1;
let line = &self.buffer.lines[self.cursor.line];
self.cursor.index = line.text().len();
self.cursor.index =
self.with_buffer(|buffer| buffer.lines[self.cursor.line].text().len());
}
if self.cursor != end {
@ -598,30 +649,34 @@ impl Edit for Editor {
if self.delete_selection() {
// Deleted selection
} else {
// Save current cursor as end
// Save current cursor as start and end
let mut start = self.cursor;
let mut end = self.cursor;
if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &self.buffer.lines[self.cursor.line];
self.with_buffer(|buffer| {
if start.index < buffer.lines[start.line].text().len() {
let line = &buffer.lines[start.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.take_while(|(i, _)| *i <= start.index)
.last()
.map(|(i, c)| i..(i + c.len()));
if let Some(range) = range_opt {
self.cursor.index = range.start;
start.index = range.start;
end.index = range.end;
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
} else if start.line + 1 < buffer.lines.len() {
end.line += 1;
end.index = 0;
}
});
if self.cursor != end {
self.delete_range(self.cursor, end);
if start != end {
self.cursor = start;
self.delete_range(start, end);
}
}
}
@ -636,10 +691,10 @@ impl Edit for Editor {
let tab_width: usize = self.tab_width.into();
for line_i in start.line..=end.line {
// Determine indexes of last indent and first character after whitespace
let mut after_whitespace;
let mut after_whitespace = 0;
let mut required_indent = 0;
{
let line = &self.buffer.lines[line_i];
self.with_buffer(|buffer| {
let line = &buffer.lines[line_i];
let text = line.text();
// Default to end of line if no non-whitespace found
after_whitespace = text.len();
@ -650,7 +705,7 @@ impl Edit for Editor {
break;
}
}
}
});
// No indent required (not possible?)
if required_indent == 0 {
@ -685,7 +740,7 @@ impl Edit for Editor {
}
// Request redraw
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Unindent => {
@ -700,9 +755,9 @@ impl Edit for Editor {
for line_i in start.line..=end.line {
// Determine indexes of last indent and first character after whitespace
let mut last_indent = 0;
let mut after_whitespace;
{
let line = &self.buffer.lines[line_i];
let mut after_whitespace = 0;
self.with_buffer(|buffer| {
let line = &buffer.lines[line_i];
let text = line.text();
// Default to end of line if no non-whitespace found
after_whitespace = text.len();
@ -715,7 +770,7 @@ impl Edit for Editor {
last_indent = index;
}
}
}
});
// No de-indent required
if last_indent == after_whitespace {
@ -746,65 +801,71 @@ impl Edit for Editor {
}
// Request redraw
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Click { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
}
Action::DoubleClick { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
self.selection = Selection::Word(self.cursor);
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::TripleClick { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
}
self.selection = Selection::Line(self.cursor);
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Drag { x, y } => {
if self.selection == Selection::None {
self.selection = Selection::Normal(self.cursor);
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
}
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
self.with_buffer_mut(|buffer| {
let mut scroll = buffer.scroll();
scroll.layout += lines;
self.buffer.set_scroll(scroll);
buffer.set_scroll(scroll);
});
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
self.buffer.set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
@ -825,7 +886,7 @@ impl Edit for Editor {
}
}
impl<'a> BorrowedWithFontSystem<'a, Editor> {
impl<'a, 'b> BorrowedWithFontSystem<'a, Editor<'b>> {
#[cfg(feature = "swash")]
pub fn draw<F>(
&mut self,

View file

@ -131,10 +131,20 @@ pub trait Edit {
}
/// 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
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
fn cursor(&self) -> Cursor;
@ -151,6 +161,7 @@ pub trait Edit {
/// Get the bounds of the current selection
//TODO: will not work with Block select
fn selection_bounds(&self) -> Option<(Cursor, Cursor)> {
self.with_buffer(|buffer| {
let cursor = self.cursor();
match self.selection() {
Selection::None => None,
@ -170,7 +181,7 @@ pub trait Edit {
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();
let end_index = buffer.lines[end_line].text().len();
Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
}
Selection::Word(select) => {
@ -190,7 +201,7 @@ pub trait Edit {
// Move start to beginning of word
{
let line = &self.buffer().lines[start.line];
let line = &buffer.lines[start.line];
start.index = line
.text()
.unicode_word_indices()
@ -202,7 +213,7 @@ pub trait Edit {
// Move end to end of word
{
let line = &self.buffer().lines[end.line];
let line = &buffer.lines[end.line];
end.index = line
.text()
.unicode_word_indices()
@ -214,6 +225,7 @@ pub trait Edit {
Some((start, end))
}
}
})
}
/// Get the current automatic indentation setting
@ -265,13 +277,19 @@ pub trait Edit {
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
pub fn buffer_mut(&mut self) -> BorrowedWithFontSystem<Buffer> {
BorrowedWithFontSystem {
inner: self.inner.buffer_mut(),
pub fn with_buffer_mut<F: FnOnce(&mut BorrowedWithFontSystem<Buffer>) -> T, T>(
&mut self,
f: F,
) -> 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

View file

@ -8,8 +8,8 @@ use syntect::highlighting::{
use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};
use crate::{
Action, AttrsList, BorrowedWithFontSystem, Buffer, Change, Color, Cursor, Edit, Editor,
FontSystem, Selection, Shaping, Style, Weight,
Action, AttrsList, BorrowedWithFontSystem, Buffer, BufferRef, Change, Color, Cursor, Edit,
Editor, FontSystem, Selection, Shaping, Style, Weight,
};
pub use syntect::highlighting::Theme as SyntaxTheme;
@ -33,8 +33,8 @@ impl SyntaxSystem {
/// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`]
#[derive(Debug)]
pub struct SyntaxEditor<'a> {
editor: Editor,
pub struct SyntaxEditor<'a, 'b> {
editor: Editor<'b>,
syntax_system: &'a SyntaxSystem,
syntax: &'a SyntaxReference,
theme: &'a SyntaxTheme,
@ -42,13 +42,17 @@ pub struct SyntaxEditor<'a> {
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.
///
/// A good default theme name is "base16-eighties.dark".
///
/// 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 syntax = syntax_system.syntax_set.find_syntax_plain_text();
let theme = syntax_system.theme_set.themes.get(theme_name)?;
@ -73,7 +77,8 @@ impl<'a> SyntaxEditor<'a> {
self.syntax_cache.clear();
// Reset attrs to match default foreground and no highlighting
for line in self.editor.buffer_mut().lines.iter_mut() {
self.with_buffer_mut(|buffer| {
for line in buffer.lines.iter_mut() {
let mut attrs = line.attrs_list().defaults();
if let Some(foreground) = self.theme.settings.foreground {
attrs = attrs.color(Color::rgba(
@ -85,6 +90,7 @@ impl<'a> SyntaxEditor<'a> {
}
line.set_attrs_list(AttrsList::new(attrs));
}
});
}
true
@ -118,9 +124,9 @@ impl<'a> SyntaxEditor<'a> {
}
let text = fs::read_to_string(path)?;
self.editor
.buffer_mut()
.set_text(font_system, &text, attrs, Shaping::Advanced);
self.editor.with_buffer_mut(|buffer| {
buffer.set_text(font_system, &text, attrs, Shaping::Advanced)
});
//TODO: re-use text
self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) {
@ -194,7 +200,7 @@ impl<'a> SyntaxEditor<'a> {
where
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());
self.editor.draw(
font_system,
@ -207,13 +213,13 @@ impl<'a> SyntaxEditor<'a> {
}
}
impl<'a> Edit for SyntaxEditor<'a> {
fn buffer(&self) -> &Buffer {
self.editor.buffer()
impl<'a, 'b> Edit for SyntaxEditor<'a, 'b> {
fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
self.editor.with_buffer(f)
}
fn buffer_mut(&mut self) -> &mut Buffer {
self.editor.buffer_mut()
fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
self.editor.with_buffer_mut(f)
}
fn cursor(&self) -> Cursor {
@ -253,7 +259,7 @@ impl<'a> Edit for SyntaxEditor<'a> {
let now = std::time::Instant::now();
let cursor = self.cursor();
let buffer = self.editor.buffer_mut();
self.editor.with_buffer_mut(|buffer| {
let visible_lines = buffer.visible_lines();
let scroll = buffer.scroll();
let scroll_end = scroll.layout + visible_lines;
@ -283,8 +289,8 @@ impl<'a> Edit for SyntaxEditor<'a> {
}
highlighted += 1;
let (mut parse_state, scope_stack) = if line_i > 0 && line_i <= self.syntax_cache.len()
{
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())
@ -364,6 +370,7 @@ impl<'a> Edit for SyntaxEditor<'a> {
now.elapsed()
);
}
});
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
///
/// ## Errors

View file

@ -46,8 +46,9 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
let mut cursor = editor.cursor();
let start_line = cursor.line;
if forwards {
while cursor.line < editor.buffer().lines.len() {
if let Some(index) = editor.buffer().lines[cursor.line]
while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) {
if let Some(index) = editor.with_buffer(|buffer| {
buffer.lines[cursor.line]
.text()
.match_indices(value)
.filter_map(|(i, _)| {
@ -58,7 +59,7 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
}
})
.next()
{
}) {
cursor.index = index;
editor.set_cursor(cursor);
return true;
@ -71,7 +72,8 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
while cursor.line > 0 {
cursor.line -= 1;
if let Some(index) = editor.buffer().lines[cursor.line]
if let Some(index) = editor.with_buffer(|buffer| {
buffer.lines[cursor.line]
.text()
.rmatch_indices(value)
.filter_map(|(i, _)| {
@ -82,7 +84,7 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
}
})
.next()
{
}) {
cursor.index = index;
editor.set_cursor(cursor);
return true;
@ -95,8 +97,7 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
fn select_in<E: Edit>(editor: &mut E, start_c: char, end_c: char, include: bool) {
// Find the largest encompasing object, or if there is none, find the next one.
let cursor = editor.cursor();
let buffer = editor.buffer();
let (start, end) = editor.with_buffer(|buffer| {
// Search forwards for isolated end character, counting start and end characters found
let mut end = cursor;
let mut starts = 0;
@ -147,13 +148,16 @@ fn select_in<E: Edit>(editor: &mut E, start_c: char, end_c: char, include: bool)
}
}
(start, end)
});
editor.set_selection(Selection::Normal(start));
editor.set_cursor(end);
}
#[derive(Debug)]
pub struct ViEditor<'a> {
editor: SyntaxEditor<'a>,
pub struct ViEditor<'a, 'b> {
editor: SyntaxEditor<'a, 'b>,
parser: ViParser,
passthrough: bool,
registers: BTreeMap<char, (Selection, String)>,
@ -162,8 +166,8 @@ pub struct ViEditor<'a> {
changed: bool,
}
impl<'a> ViEditor<'a> {
pub fn new(editor: SyntaxEditor<'a>) -> Self {
impl<'a, 'b> ViEditor<'a, 'b> {
pub fn new(editor: SyntaxEditor<'a, 'b>) -> Self {
Self {
editor,
parser: ViParser::new(),
@ -230,7 +234,7 @@ impl<'a> ViEditor<'a> {
pub fn set_passthrough(&mut self, passthrough: bool) {
if passthrough != self.passthrough {
self.passthrough = passthrough;
self.buffer_mut().set_redraw(true);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
@ -264,16 +268,16 @@ impl<'a> ViEditor<'a> {
where
F: FnMut(i32, i32, u32, u32, Color),
{
let size = self.buffer().size();
f(0, 0, size.0 as u32, size.1 as u32, self.background_color());
let font_size = self.buffer().metrics().font_size;
let line_height = self.buffer().metrics().line_height;
let background_color = self.background_color();
let foreground_color = self.foreground_color();
let cursor_color = self.cursor_color();
let selection_color = self.selection_color();
for run in self.buffer().layout_runs() {
self.with_buffer(|buffer| {
let size = buffer.size();
f(0, 0, size.0 as u32, size.1 as u32, background_color);
let font_size = buffer.metrics().font_size;
let line_height = buffer.metrics().line_height;
for run in buffer.layout_runs() {
let line_i = run.line_i;
let line_y = run.line_y;
let line_top = run.line_top;
@ -353,7 +357,7 @@ impl<'a> ViEditor<'a> {
if run.glyphs.is_empty() && end.line > line_i {
// Highlight all of internal empty lines
range_opt = Some((0, self.buffer().size().0 as i32));
range_opt = Some((0, buffer.size().0 as i32));
}
if let Some((mut min, mut max)) = range_opt.take() {
@ -362,7 +366,7 @@ impl<'a> ViEditor<'a> {
if run.rtl {
min = 0;
} else {
max = self.buffer().size().0 as i32;
max = buffer.size().0 as i32;
}
}
f(
@ -469,16 +473,17 @@ impl<'a> ViEditor<'a> {
);
}
}
});
}
}
impl<'a> Edit for ViEditor<'a> {
fn buffer(&self) -> &Buffer {
self.editor.buffer()
impl<'a, 'b> Edit for ViEditor<'a, 'b> {
fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
self.editor.with_buffer(f)
}
fn buffer_mut(&mut self) -> &mut Buffer {
self.editor.buffer_mut()
fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
self.editor.with_buffer_mut(f)
}
fn cursor(&self) -> Cursor {
@ -618,8 +623,9 @@ impl<'a> Edit for ViEditor<'a> {
Event::Delete => Action::Delete,
Event::DeleteInLine => {
let cursor = editor.cursor();
let buffer = editor.buffer();
if cursor.index < buffer.lines[cursor.line].text().len() {
if cursor.index
< editor.with_buffer(|buffer| buffer.lines[cursor.line].text().len())
{
Action::Delete
} else {
return;
@ -638,11 +644,12 @@ impl<'a> Edit for ViEditor<'a> {
Selection::None | Selection::Normal(_) | Selection::Word(_) => {
let mut cursor = editor.cursor();
if after {
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
if let Some(c) = text[cursor.index..].chars().next() {
cursor.index += c.len_utf8();
}
});
editor.set_cursor(cursor);
}
editor.insert_at(cursor, data, None);
@ -682,7 +689,7 @@ impl<'a> Edit for ViEditor<'a> {
return;
}
Event::Redraw => {
editor.buffer_mut().set_redraw(true);
editor.with_buffer_mut(|buffer| buffer.set_redraw(true));
return;
}
Event::SelectClear => {
@ -725,7 +732,7 @@ impl<'a> Edit for ViEditor<'a> {
TextObject::Word(word) => {
let mut cursor = editor.cursor();
let mut selection = editor.selection();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
match WordIter::new(text, word)
.find(|&(i, w)| i <= cursor.index && i + w.len() > cursor.index)
@ -739,6 +746,7 @@ impl<'a> Edit for ViEditor<'a> {
//TODO
}
}
});
editor.set_selection(selection);
editor.set_cursor(cursor);
}
@ -782,7 +790,7 @@ impl<'a> Edit for ViEditor<'a> {
Action::Motion(Motion::GotoLine(line.saturating_sub(1)))
}
modit::Motion::GotoEof => Action::Motion(Motion::GotoLine(
editor.buffer().lines.len().saturating_sub(1),
editor.with_buffer(|buffer| buffer.lines.len().saturating_sub(1)),
)),
modit::Motion::Home => Action::Motion(Motion::Home),
modit::Motion::Inside => {
@ -804,7 +812,7 @@ impl<'a> Edit for ViEditor<'a> {
}
modit::Motion::NextChar(find_c) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
if cursor.index < text.len() {
match text[cursor.index..]
@ -814,29 +822,31 @@ impl<'a> Edit for ViEditor<'a> {
{
Some((i, _)) => {
cursor.index += i;
editor.set_cursor(cursor);
}
None => {}
}
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::NextCharTill(find_c) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
if cursor.index < text.len() {
let mut last_i = 0;
for (i, c) in text[cursor.index..].char_indices() {
if last_i > 0 && c == find_c {
cursor.index += last_i;
editor.set_cursor(cursor);
break;
} else {
last_i = i;
}
}
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::NextSearch => match &self.search_opt {
@ -848,13 +858,17 @@ impl<'a> Edit for ViEditor<'a> {
},
modit::Motion::NextWordEnd(word) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
loop {
let text = buffer.lines[cursor.line].text();
if cursor.index < text.len() {
cursor.index = WordIter::new(text, word)
.map(|(i, w)| {
i + w.char_indices().last().map(|(i, _)| i).unwrap_or(0)
i + w
.char_indices()
.last()
.map(|(i, _)| i)
.unwrap_or(0)
})
.find(|&i| i > cursor.index)
.unwrap_or(text.len());
@ -870,12 +884,13 @@ impl<'a> Edit for ViEditor<'a> {
}
break;
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::NextWordStart(word) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
loop {
let text = buffer.lines[cursor.line].text();
if cursor.index < text.len() {
@ -895,6 +910,7 @@ impl<'a> Edit for ViEditor<'a> {
}
break;
}
});
editor.set_cursor(cursor);
return;
}
@ -902,7 +918,7 @@ impl<'a> Edit for ViEditor<'a> {
modit::Motion::PageUp => Action::Motion(Motion::PageUp),
modit::Motion::PreviousChar(find_c) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
if cursor.index > 0 {
match text[..cursor.index]
@ -912,16 +928,17 @@ impl<'a> Edit for ViEditor<'a> {
{
Some((i, _)) => {
cursor.index = i;
editor.set_cursor(cursor);
}
None => {}
}
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::PreviousCharTill(find_c) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
let text = buffer.lines[cursor.line].text();
if cursor.index > 0 {
match text[..cursor.index]
@ -939,11 +956,12 @@ impl<'a> Edit for ViEditor<'a> {
{
Some(i) => {
cursor.index = i;
editor.set_cursor(cursor);
}
None => {}
}
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::PreviousSearch => match &self.search_opt {
@ -955,13 +973,17 @@ impl<'a> Edit for ViEditor<'a> {
},
modit::Motion::PreviousWordEnd(word) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
loop {
let text = buffer.lines[cursor.line].text();
if cursor.index > 0 {
cursor.index = WordIter::new(text, word)
.map(|(i, w)| {
i + w.char_indices().last().map(|(i, _)| i).unwrap_or(0)
i + w
.char_indices()
.last()
.map(|(i, _)| i)
.unwrap_or(0)
})
.filter(|&i| i < cursor.index)
.last()
@ -978,12 +1000,13 @@ impl<'a> Edit for ViEditor<'a> {
}
break;
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::PreviousWordStart(word) => {
let mut cursor = editor.cursor();
let buffer = editor.buffer();
editor.with_buffer(|buffer| {
loop {
let text = buffer.lines[cursor.line].text();
if cursor.index > 0 {
@ -1004,14 +1027,17 @@ impl<'a> Edit for ViEditor<'a> {
}
break;
}
});
editor.set_cursor(cursor);
return;
}
modit::Motion::Right => Action::Motion(Motion::Right),
modit::Motion::RightInLine => {
let cursor = editor.cursor();
let buffer = editor.buffer();
if cursor.index < buffer.lines[cursor.line].text().len() {
if cursor.index
< editor
.with_buffer(|buffer| buffer.lines[cursor.line].text().len())
{
Action::Motion(Motion::Right)
} else {
return;
@ -1019,33 +1045,43 @@ impl<'a> Edit for ViEditor<'a> {
}
modit::Motion::ScreenHigh => {
//TODO: is this efficient?
if let Some(first) = editor.buffer().layout_runs().next() {
Action::Motion(Motion::GotoLine(first.line_i))
if let Some(line_i) = editor.with_buffer(|buffer| {
buffer.layout_runs().next().map(|first| first.line_i)
}) {
Action::Motion(Motion::GotoLine(line_i))
} else {
return;
}
}
modit::Motion::ScreenLow => {
//TODO: is this efficient?
if let Some(last) = editor.buffer().layout_runs().last() {
Action::Motion(Motion::GotoLine(last.line_i))
if let Some(line_i) = editor.with_buffer(|buffer| {
buffer.layout_runs().last().map(|last| last.line_i)
}) {
Action::Motion(Motion::GotoLine(line_i))
} else {
return;
}
}
modit::Motion::ScreenMiddle => {
//TODO: is this efficient?
let mut layout_runs = editor.buffer().layout_runs();
let action_opt = editor.with_buffer(|buffer| {
let mut layout_runs = buffer.layout_runs();
if let Some(first) = layout_runs.next() {
if let Some(last) = layout_runs.last() {
Action::Motion(Motion::GotoLine(
Some(Action::Motion(Motion::GotoLine(
(last.line_i + first.line_i) / 2,
))
)))
} else {
return;
None
}
} else {
return;
None
}
});
match action_opt {
Some(action) => action,
None => return,
}
}
modit::Motion::Selection => {
@ -1062,7 +1098,7 @@ impl<'a> Edit for ViEditor<'a> {
}
}
impl<'a, 'b> BorrowedWithFontSystem<'b, ViEditor<'a>> {
impl<'a, 'b, 'c> BorrowedWithFontSystem<'c, ViEditor<'a, 'b>> {
/// Load text from a file, and also set syntax to the best option
#[cfg(feature = "std")]
pub fn load_text<P: AsRef<std::path::Path>>(