Allow Editor to use reference or Arc of Buffer
This commit is contained in:
parent
ae030e9885
commit
cbbf6f0d8f
9 changed files with 1160 additions and 1021 deletions
|
|
@ -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| {
|
||||||
|
for line in buffer.lines.iter() {
|
||||||
text.push_str(line.text());
|
text.push_str(line.text());
|
||||||
text.push('\n');
|
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| {
|
||||||
|
buffer.lines.iter_mut().for_each(|line| {
|
||||||
line.set_attrs_list(AttrsList::new(attrs));
|
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| {
|
||||||
|
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() {
|
for line in lines.iter_mut() {
|
||||||
line.set_align(Some(align));
|
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));
|
line.set_align(Some(align));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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| {
|
||||||
|
for line in buffer.lines.iter() {
|
||||||
match line.layout_opt() {
|
match line.layout_opt() {
|
||||||
Some(layout) => layout_lines += layout.len(),
|
Some(layout) => layout_lines += layout.len(),
|
||||||
None => (),
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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| {
|
||||||
|
for run in buffer.layout_runs() {
|
||||||
end_line = run.line_i;
|
end_line = run.line_i;
|
||||||
if start_line_opt.is_none() {
|
if start_line_opt.is_none() {
|
||||||
start_line_opt = Some(end_line);
|
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 {
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
editor.with_buffer(|buffer| {
|
||||||
for (line_i, line) in text.lines().enumerate() {
|
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 {
|
if buffer_line.text() != line {
|
||||||
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
|
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
|
||||||
wrong += 1;
|
wrong += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
if wrong == 0 {
|
if wrong == 0 {
|
||||||
log::info!("All lines matched!");
|
log::info!("All lines matched!");
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use alloc::string::{String, ToString};
|
use alloc::string::{String, ToString};
|
||||||
|
use alloc::sync::Arc;
|
||||||
use core::{cmp, iter::once};
|
use core::{cmp, iter::once};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
|
|
@ -12,10 +13,35 @@ use crate::{
|
||||||
Edit, FontSystem, Selection, Shaping,
|
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
|
/// A wrapper of [`Buffer`] for easy editing
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Editor {
|
pub struct Editor<'a> {
|
||||||
buffer: Buffer,
|
buffer_ref: BufferRef<'a>,
|
||||||
cursor: Cursor,
|
cursor: Cursor,
|
||||||
cursor_x_opt: Option<i32>,
|
cursor_x_opt: Option<i32>,
|
||||||
selection: Selection,
|
selection: Selection,
|
||||||
|
|
@ -25,11 +51,11 @@ pub struct Editor {
|
||||||
change: Option<Change>,
|
change: Option<Change>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Editor {
|
impl<'a> Editor<'a> {
|
||||||
/// Create a new [`Editor`] with the provided [`Buffer`]
|
/// Create a new [`Editor`] with the provided [`Buffer`]
|
||||||
pub fn new(buffer: Buffer) -> Self {
|
pub fn new(buffer: impl Into<BufferRef<'a>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
buffer,
|
buffer_ref: buffer.into(),
|
||||||
cursor: Cursor::default(),
|
cursor: Cursor::default(),
|
||||||
cursor_x_opt: None,
|
cursor_x_opt: None,
|
||||||
selection: Selection::None,
|
selection: Selection::None,
|
||||||
|
|
@ -53,9 +79,9 @@ impl Editor {
|
||||||
) where
|
) where
|
||||||
F: FnMut(i32, i32, u32, u32, Color),
|
F: FnMut(i32, i32, u32, u32, Color),
|
||||||
{
|
{
|
||||||
let line_height = self.buffer.metrics().line_height;
|
self.with_buffer(|buffer| {
|
||||||
|
let line_height = buffer.metrics().line_height;
|
||||||
for run in self.buffer.layout_runs() {
|
for run in buffer.layout_runs() {
|
||||||
let line_i = run.line_i;
|
let line_i = run.line_i;
|
||||||
let line_y = run.line_y;
|
let line_y = run.line_y;
|
||||||
let line_top = run.line_top;
|
let line_top = run.line_top;
|
||||||
|
|
@ -134,7 +160,7 @@ impl Editor {
|
||||||
|
|
||||||
if run.glyphs.is_empty() && end.line > line_i {
|
if run.glyphs.is_empty() && end.line > line_i {
|
||||||
// Highlight all of internal empty lines
|
// 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() {
|
if let Some((mut min, mut max)) = range_opt.take() {
|
||||||
|
|
@ -143,7 +169,7 @@ impl Editor {
|
||||||
if run.rtl {
|
if run.rtl {
|
||||||
min = 0;
|
min = 0;
|
||||||
} else {
|
} else {
|
||||||
max = self.buffer.size().0 as i32;
|
max = buffer.size().0 as i32;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f(
|
f(
|
||||||
|
|
@ -211,16 +237,29 @@ impl Editor {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Edit for Editor {
|
impl<'a> Edit for Editor<'a> {
|
||||||
fn buffer(&self) -> &Buffer {
|
fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
|
||||||
&self.buffer
|
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 {
|
fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
|
||||||
&mut self.buffer
|
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 {
|
fn cursor(&self) -> Cursor {
|
||||||
|
|
@ -231,7 +270,7 @@ impl Edit for Editor {
|
||||||
if self.cursor != cursor {
|
if self.cursor != cursor {
|
||||||
self.cursor = cursor;
|
self.cursor = cursor;
|
||||||
self.cursor_moved = true;
|
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) {
|
fn set_selection(&mut self, selection: Selection) {
|
||||||
if self.selection != selection {
|
if self.selection != selection {
|
||||||
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 {
|
if self.tab_width != tab_width {
|
||||||
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) {
|
fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
|
||||||
if self.cursor_moved {
|
if self.cursor_moved {
|
||||||
self.buffer
|
let cursor = self.cursor;
|
||||||
.shape_until_cursor(font_system, self.cursor, prune);
|
self.with_buffer_mut(|buffer| buffer.shape_until_cursor(font_system, cursor, prune));
|
||||||
self.cursor_moved = false;
|
self.cursor_moved = false;
|
||||||
} else {
|
} 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) {
|
fn delete_range(&mut self, start: Cursor, end: Cursor) {
|
||||||
|
let change_item = self.with_buffer_mut(|buffer| {
|
||||||
// Collect removed data for change tracking
|
// Collect removed data for change tracking
|
||||||
let mut change_text = String::new();
|
let mut change_text = String::new();
|
||||||
|
|
||||||
// Delete the selection from the last line
|
// Delete the selection from the last line
|
||||||
let end_line_opt = if end.line > start.line {
|
let end_line_opt = if end.line > start.line {
|
||||||
// Get part of line after selection
|
// 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
|
// 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());
|
change_text.insert_str(0, removed.text());
|
||||||
|
|
||||||
Some(after)
|
Some(after)
|
||||||
|
|
@ -299,7 +339,7 @@ impl Edit for Editor {
|
||||||
|
|
||||||
// Delete interior lines (in reverse for safety)
|
// Delete interior lines (in reverse for safety)
|
||||||
for line_i in (start.line + 1..end.line).rev() {
|
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());
|
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
|
// Get part after selection if start line is also end line
|
||||||
let after_opt = if start.line == 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 {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete selected part of line
|
// 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());
|
change_text.insert_str(0, removed.text());
|
||||||
|
|
||||||
// Re-add part of line after selection
|
// Re-add part of line after selection
|
||||||
if let Some(after) = after_opt {
|
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
|
// Re-add valid parts of end line
|
||||||
if let Some(end_line) = end_line_opt {
|
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 {
|
ChangeItem {
|
||||||
let item = ChangeItem {
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
text: change_text,
|
text: change_text,
|
||||||
insert: false,
|
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;
|
return cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let change_item = self.with_buffer_mut(|buffer| {
|
||||||
// Save cursor for change tracking
|
// Save cursor for change tracking
|
||||||
let start = cursor;
|
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;
|
let insert_line = cursor.line + 1;
|
||||||
|
|
||||||
// Collect text after insertion as a line
|
// Collect text after insertion as a line
|
||||||
|
|
@ -392,7 +435,7 @@ impl Edit for Editor {
|
||||||
Shaping::Advanced,
|
Shaping::Advanced,
|
||||||
);
|
);
|
||||||
tmp.append(after);
|
tmp.append(after);
|
||||||
self.buffer.lines.insert(insert_line, tmp);
|
buffer.lines.insert(insert_line, tmp);
|
||||||
cursor.line += 1;
|
cursor.line += 1;
|
||||||
} else {
|
} else {
|
||||||
line.append(after);
|
line.append(after);
|
||||||
|
|
@ -406,23 +449,25 @@ impl Edit for Editor {
|
||||||
final_attrs.split_off(remaining_split_len),
|
final_attrs.split_off(remaining_split_len),
|
||||||
Shaping::Advanced,
|
Shaping::Advanced,
|
||||||
);
|
);
|
||||||
self.buffer.lines.insert(insert_line, tmp);
|
buffer.lines.insert(insert_line, tmp);
|
||||||
cursor.line += 1;
|
cursor.line += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(remaining_split_len, 0);
|
assert_eq!(remaining_split_len, 0);
|
||||||
|
|
||||||
// Append the text after insertion
|
// 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 {
|
ChangeItem {
|
||||||
let item = ChangeItem {
|
|
||||||
start,
|
start,
|
||||||
end: cursor,
|
end: cursor,
|
||||||
text: data.to_string(),
|
text: data.to_string(),
|
||||||
insert: true,
|
insert: true,
|
||||||
};
|
}
|
||||||
change.items.push(item);
|
});
|
||||||
|
|
||||||
|
if let Some(ref mut change) = self.change {
|
||||||
|
change.items.push(change_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor
|
cursor
|
||||||
|
|
@ -430,32 +475,33 @@ impl Edit for Editor {
|
||||||
|
|
||||||
fn copy_selection(&self) -> Option<String> {
|
fn copy_selection(&self) -> Option<String> {
|
||||||
let (start, end) = self.selection_bounds()?;
|
let (start, end) = self.selection_bounds()?;
|
||||||
|
self.with_buffer(|buffer| {
|
||||||
let mut selection = String::new();
|
let mut selection = String::new();
|
||||||
// Take the selection from the first line
|
// Take the selection from the first line
|
||||||
{
|
{
|
||||||
// Add selected part of line to string
|
// Add selected part of line to string
|
||||||
if start.line == end.line {
|
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 {
|
} 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');
|
selection.push('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take the selection from all interior lines (if they exist)
|
// Take the selection from all interior lines (if they exist)
|
||||||
for line_i in start.line + 1..end.line {
|
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');
|
selection.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take the selection from the last line
|
// Take the selection from the last line
|
||||||
if end.line > start.line {
|
if end.line > start.line {
|
||||||
// Add selected part of line to string
|
// 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)
|
Some(selection)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_selection(&mut self) -> bool {
|
fn delete_selection(&mut self) -> bool {
|
||||||
|
|
@ -517,18 +563,19 @@ impl Edit for Editor {
|
||||||
|
|
||||||
match action {
|
match action {
|
||||||
Action::Motion(motion) => {
|
Action::Motion(motion) => {
|
||||||
if let Some((cursor, cursor_x_opt)) =
|
let cursor = self.cursor;
|
||||||
self.buffer
|
let cursor_x_opt = self.cursor_x_opt;
|
||||||
.cursor_motion(font_system, self.cursor, self.cursor_x_opt, motion)
|
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 = cursor;
|
}) {
|
||||||
self.cursor_x_opt = cursor_x_opt;
|
self.cursor = new_cursor;
|
||||||
|
self.cursor_x_opt = new_cursor_x_opt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Escape => {
|
Action::Escape => {
|
||||||
match self.selection {
|
match self.selection {
|
||||||
Selection::None => {}
|
Selection::None => {}
|
||||||
_ => self.buffer.set_redraw(true),
|
_ => self.with_buffer_mut(|buffer| buffer.set_redraw(true)),
|
||||||
}
|
}
|
||||||
self.selection = Selection::None;
|
self.selection = Selection::None;
|
||||||
}
|
}
|
||||||
|
|
@ -548,8 +595,8 @@ impl Edit for Editor {
|
||||||
//TODO: what about indenting more after opening brackets or parentheses?
|
//TODO: what about indenting more after opening brackets or parentheses?
|
||||||
if self.auto_indent {
|
if self.auto_indent {
|
||||||
let mut string = String::from("\n");
|
let mut string = String::from("\n");
|
||||||
{
|
self.with_buffer(|buffer| {
|
||||||
let line = &self.buffer.lines[self.cursor.line];
|
let line = &buffer.lines[self.cursor.line];
|
||||||
let text = line.text();
|
let text = line.text();
|
||||||
for c in text.chars() {
|
for c in text.chars() {
|
||||||
if c.is_whitespace() {
|
if c.is_whitespace() {
|
||||||
|
|
@ -558,14 +605,17 @@ impl Edit for Editor {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
self.insert_string(&string, None);
|
self.insert_string(&string, None);
|
||||||
} else {
|
} else {
|
||||||
self.insert_string("\n", None);
|
self.insert_string("\n", None);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure line is properly shaped and laid out (for potential immediate commands)
|
// 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 => {
|
Action::Backspace => {
|
||||||
if self.delete_selection() {
|
if self.delete_selection() {
|
||||||
|
|
@ -576,16 +626,17 @@ impl Edit for Editor {
|
||||||
|
|
||||||
if self.cursor.index > 0 {
|
if self.cursor.index > 0 {
|
||||||
// Move cursor to previous character index
|
// Move cursor to previous character index
|
||||||
let line = &self.buffer.lines[self.cursor.line];
|
self.cursor.index = self.with_buffer(|buffer| {
|
||||||
self.cursor.index = line.text()[..self.cursor.index]
|
buffer.lines[self.cursor.line].text()[..self.cursor.index]
|
||||||
.char_indices()
|
.char_indices()
|
||||||
.next_back()
|
.next_back()
|
||||||
.map_or(0, |(i, _)| i);
|
.map_or(0, |(i, _)| i)
|
||||||
|
});
|
||||||
} else if self.cursor.line > 0 {
|
} else if self.cursor.line > 0 {
|
||||||
// Move cursor to previous line
|
// Move cursor to previous line
|
||||||
self.cursor.line -= 1;
|
self.cursor.line -= 1;
|
||||||
let line = &self.buffer.lines[self.cursor.line];
|
self.cursor.index =
|
||||||
self.cursor.index = line.text().len();
|
self.with_buffer(|buffer| buffer.lines[self.cursor.line].text().len());
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.cursor != end {
|
if self.cursor != end {
|
||||||
|
|
@ -598,30 +649,34 @@ impl Edit for Editor {
|
||||||
if self.delete_selection() {
|
if self.delete_selection() {
|
||||||
// Deleted selection
|
// Deleted selection
|
||||||
} else {
|
} else {
|
||||||
// Save current cursor as end
|
// Save current cursor as start and end
|
||||||
|
let mut start = self.cursor;
|
||||||
let mut end = self.cursor;
|
let mut end = self.cursor;
|
||||||
|
|
||||||
if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
|
self.with_buffer(|buffer| {
|
||||||
let line = &self.buffer.lines[self.cursor.line];
|
if start.index < buffer.lines[start.line].text().len() {
|
||||||
|
let line = &buffer.lines[start.line];
|
||||||
|
|
||||||
let range_opt = line
|
let range_opt = line
|
||||||
.text()
|
.text()
|
||||||
.grapheme_indices(true)
|
.grapheme_indices(true)
|
||||||
.take_while(|(i, _)| *i <= self.cursor.index)
|
.take_while(|(i, _)| *i <= start.index)
|
||||||
.last()
|
.last()
|
||||||
.map(|(i, c)| i..(i + c.len()));
|
.map(|(i, c)| i..(i + c.len()));
|
||||||
|
|
||||||
if let Some(range) = range_opt {
|
if let Some(range) = range_opt {
|
||||||
self.cursor.index = range.start;
|
start.index = range.start;
|
||||||
end.index = range.end;
|
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.line += 1;
|
||||||
end.index = 0;
|
end.index = 0;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if self.cursor != end {
|
if start != end {
|
||||||
self.delete_range(self.cursor, 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();
|
let tab_width: usize = self.tab_width.into();
|
||||||
for line_i in start.line..=end.line {
|
for line_i in start.line..=end.line {
|
||||||
// Determine indexes of last indent and first character after whitespace
|
// 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 mut required_indent = 0;
|
||||||
{
|
self.with_buffer(|buffer| {
|
||||||
let line = &self.buffer.lines[line_i];
|
let line = &buffer.lines[line_i];
|
||||||
let text = line.text();
|
let text = line.text();
|
||||||
// Default to end of line if no non-whitespace found
|
// Default to end of line if no non-whitespace found
|
||||||
after_whitespace = text.len();
|
after_whitespace = text.len();
|
||||||
|
|
@ -650,7 +705,7 @@ impl Edit for Editor {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// No indent required (not possible?)
|
// No indent required (not possible?)
|
||||||
if required_indent == 0 {
|
if required_indent == 0 {
|
||||||
|
|
@ -685,7 +740,7 @@ impl Edit for Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request redraw
|
// Request redraw
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Unindent => {
|
Action::Unindent => {
|
||||||
|
|
@ -700,9 +755,9 @@ impl Edit for Editor {
|
||||||
for line_i in start.line..=end.line {
|
for line_i in start.line..=end.line {
|
||||||
// Determine indexes of last indent and first character after whitespace
|
// Determine indexes of last indent and first character after whitespace
|
||||||
let mut last_indent = 0;
|
let mut last_indent = 0;
|
||||||
let mut after_whitespace;
|
let mut after_whitespace = 0;
|
||||||
{
|
self.with_buffer(|buffer| {
|
||||||
let line = &self.buffer.lines[line_i];
|
let line = &buffer.lines[line_i];
|
||||||
let text = line.text();
|
let text = line.text();
|
||||||
// Default to end of line if no non-whitespace found
|
// Default to end of line if no non-whitespace found
|
||||||
after_whitespace = text.len();
|
after_whitespace = text.len();
|
||||||
|
|
@ -715,7 +770,7 @@ impl Edit for Editor {
|
||||||
last_indent = index;
|
last_indent = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// No de-indent required
|
// No de-indent required
|
||||||
if last_indent == after_whitespace {
|
if last_indent == after_whitespace {
|
||||||
|
|
@ -746,65 +801,71 @@ impl Edit for Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request redraw
|
// Request redraw
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Click { x, y } => {
|
Action::Click { x, y } => {
|
||||||
self.set_selection(Selection::None);
|
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 {
|
if new_cursor != self.cursor {
|
||||||
self.cursor = new_cursor;
|
self.cursor = new_cursor;
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::DoubleClick { x, y } => {
|
Action::DoubleClick { x, y } => {
|
||||||
self.set_selection(Selection::None);
|
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 {
|
if new_cursor != self.cursor {
|
||||||
self.cursor = new_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.selection = Selection::Word(self.cursor);
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::TripleClick { x, y } => {
|
Action::TripleClick { x, y } => {
|
||||||
self.set_selection(Selection::None);
|
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 {
|
if new_cursor != self.cursor {
|
||||||
self.cursor = new_cursor;
|
self.cursor = new_cursor;
|
||||||
}
|
}
|
||||||
self.selection = Selection::Line(self.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 } => {
|
Action::Drag { x, y } => {
|
||||||
if self.selection == Selection::None {
|
if self.selection == Selection::None {
|
||||||
self.selection = Selection::Normal(self.cursor);
|
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 {
|
if new_cursor != self.cursor {
|
||||||
self.cursor = new_cursor;
|
self.cursor = new_cursor;
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Scroll { lines } => {
|
Action::Scroll { lines } => {
|
||||||
let mut scroll = self.buffer.scroll();
|
self.with_buffer_mut(|buffer| {
|
||||||
|
let mut scroll = buffer.scroll();
|
||||||
scroll.layout += lines;
|
scroll.layout += lines;
|
||||||
self.buffer.set_scroll(scroll);
|
buffer.set_scroll(scroll);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if old_cursor != self.cursor {
|
if old_cursor != self.cursor {
|
||||||
self.cursor_moved = true;
|
self.cursor_moved = true;
|
||||||
self.buffer.set_redraw(true);
|
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
|
|
||||||
/*TODO
|
/*TODO
|
||||||
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
|
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")]
|
#[cfg(feature = "swash")]
|
||||||
pub fn draw<F>(
|
pub fn draw<F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
|
|
@ -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,6 +161,7 @@ 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)> {
|
||||||
|
self.with_buffer(|buffer| {
|
||||||
let cursor = self.cursor();
|
let cursor = self.cursor();
|
||||||
match self.selection() {
|
match self.selection() {
|
||||||
Selection::None => None,
|
Selection::None => None,
|
||||||
|
|
@ -170,7 +181,7 @@ pub trait Edit {
|
||||||
Selection::Line(select) => {
|
Selection::Line(select) => {
|
||||||
let start_line = cmp::min(select.line, cursor.line);
|
let start_line = cmp::min(select.line, cursor.line);
|
||||||
let end_line = cmp::max(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)))
|
Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
|
||||||
}
|
}
|
||||||
Selection::Word(select) => {
|
Selection::Word(select) => {
|
||||||
|
|
@ -190,7 +201,7 @@ pub trait Edit {
|
||||||
|
|
||||||
// Move start to beginning of word
|
// Move start to beginning of word
|
||||||
{
|
{
|
||||||
let line = &self.buffer().lines[start.line];
|
let line = &buffer.lines[start.line];
|
||||||
start.index = line
|
start.index = line
|
||||||
.text()
|
.text()
|
||||||
.unicode_word_indices()
|
.unicode_word_indices()
|
||||||
|
|
@ -202,7 +213,7 @@ pub trait Edit {
|
||||||
|
|
||||||
// Move end to end of word
|
// Move end to end of word
|
||||||
{
|
{
|
||||||
let line = &self.buffer().lines[end.line];
|
let line = &buffer.lines[end.line];
|
||||||
end.index = line
|
end.index = line
|
||||||
.text()
|
.text()
|
||||||
.unicode_word_indices()
|
.unicode_word_indices()
|
||||||
|
|
@ -214,6 +225,7 @@ pub trait Edit {
|
||||||
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,
|
||||||
|
) -> T {
|
||||||
|
self.inner.with_buffer_mut(|buffer| {
|
||||||
|
let mut borrowed = BorrowedWithFontSystem {
|
||||||
|
inner: buffer,
|
||||||
font_system: self.font_system,
|
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
|
||||||
|
|
|
||||||
|
|
@ -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,7 +77,8 @@ 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| {
|
||||||
|
for line in buffer.lines.iter_mut() {
|
||||||
let mut attrs = line.attrs_list().defaults();
|
let mut attrs = line.attrs_list().defaults();
|
||||||
if let Some(foreground) = self.theme.settings.foreground {
|
if let Some(foreground) = self.theme.settings.foreground {
|
||||||
attrs = attrs.color(Color::rgba(
|
attrs = attrs.color(Color::rgba(
|
||||||
|
|
@ -85,6 +90,7 @@ impl<'a> SyntaxEditor<'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,7 +259,7 @@ 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;
|
||||||
|
|
@ -283,8 +289,8 @@ impl<'a> Edit for SyntaxEditor<'a> {
|
||||||
}
|
}
|
||||||
highlighted += 1;
|
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()
|
self.syntax_cache[line_i - 1].clone()
|
||||||
} else {
|
} else {
|
||||||
(ParseState::new(self.syntax), ScopeStack::new())
|
(ParseState::new(self.syntax), ScopeStack::new())
|
||||||
|
|
@ -364,6 +370,7 @@ impl<'a> Edit for SyntaxEditor<'a> {
|
||||||
now.elapsed()
|
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
|
||||||
|
|
|
||||||
152
src/edit/vi.rs
152
src/edit/vi.rs
|
|
@ -46,8 +46,9 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let start_line = cursor.line;
|
let start_line = cursor.line;
|
||||||
if forwards {
|
if forwards {
|
||||||
while cursor.line < editor.buffer().lines.len() {
|
while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) {
|
||||||
if let Some(index) = editor.buffer().lines[cursor.line]
|
if let Some(index) = editor.with_buffer(|buffer| {
|
||||||
|
buffer.lines[cursor.line]
|
||||||
.text()
|
.text()
|
||||||
.match_indices(value)
|
.match_indices(value)
|
||||||
.filter_map(|(i, _)| {
|
.filter_map(|(i, _)| {
|
||||||
|
|
@ -58,7 +59,7 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.next()
|
.next()
|
||||||
{
|
}) {
|
||||||
cursor.index = index;
|
cursor.index = index;
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -71,7 +72,8 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
|
||||||
while cursor.line > 0 {
|
while cursor.line > 0 {
|
||||||
cursor.line -= 1;
|
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()
|
.text()
|
||||||
.rmatch_indices(value)
|
.rmatch_indices(value)
|
||||||
.filter_map(|(i, _)| {
|
.filter_map(|(i, _)| {
|
||||||
|
|
@ -82,7 +84,7 @@ fn search<E: Edit>(editor: &mut E, value: &str, forwards: bool) -> bool {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.next()
|
.next()
|
||||||
{
|
}) {
|
||||||
cursor.index = index;
|
cursor.index = index;
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return true;
|
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) {
|
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.
|
// Find the largest encompasing object, or if there is none, find the next one.
|
||||||
let cursor = editor.cursor();
|
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
|
// Search forwards for isolated end character, counting start and end characters found
|
||||||
let mut end = cursor;
|
let mut end = cursor;
|
||||||
let mut starts = 0;
|
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_selection(Selection::Normal(start));
|
||||||
editor.set_cursor(end);
|
editor.set_cursor(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ViEditor<'a> {
|
pub struct ViEditor<'a, 'b> {
|
||||||
editor: SyntaxEditor<'a>,
|
editor: SyntaxEditor<'a, 'b>,
|
||||||
parser: ViParser,
|
parser: ViParser,
|
||||||
passthrough: bool,
|
passthrough: bool,
|
||||||
registers: BTreeMap<char, (Selection, String)>,
|
registers: BTreeMap<char, (Selection, String)>,
|
||||||
|
|
@ -162,8 +166,8 @@ pub struct ViEditor<'a> {
|
||||||
changed: bool,
|
changed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ViEditor<'a> {
|
impl<'a, 'b> ViEditor<'a, 'b> {
|
||||||
pub fn new(editor: SyntaxEditor<'a>) -> Self {
|
pub fn new(editor: SyntaxEditor<'a, 'b>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
editor,
|
editor,
|
||||||
parser: ViParser::new(),
|
parser: ViParser::new(),
|
||||||
|
|
@ -230,7 +234,7 @@ impl<'a> ViEditor<'a> {
|
||||||
pub fn set_passthrough(&mut self, passthrough: bool) {
|
pub fn set_passthrough(&mut self, passthrough: bool) {
|
||||||
if passthrough != self.passthrough {
|
if passthrough != self.passthrough {
|
||||||
self.passthrough = 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
|
where
|
||||||
F: FnMut(i32, i32, u32, u32, Color),
|
F: FnMut(i32, i32, u32, u32, Color),
|
||||||
{
|
{
|
||||||
let size = self.buffer().size();
|
let background_color = self.background_color();
|
||||||
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 foreground_color = self.foreground_color();
|
let foreground_color = self.foreground_color();
|
||||||
let cursor_color = self.cursor_color();
|
let cursor_color = self.cursor_color();
|
||||||
let selection_color = self.selection_color();
|
let selection_color = self.selection_color();
|
||||||
|
self.with_buffer(|buffer| {
|
||||||
for run in self.buffer().layout_runs() {
|
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_i = run.line_i;
|
||||||
let line_y = run.line_y;
|
let line_y = run.line_y;
|
||||||
let line_top = run.line_top;
|
let line_top = run.line_top;
|
||||||
|
|
@ -353,7 +357,7 @@ impl<'a> ViEditor<'a> {
|
||||||
|
|
||||||
if run.glyphs.is_empty() && end.line > line_i {
|
if run.glyphs.is_empty() && end.line > line_i {
|
||||||
// Highlight all of internal empty lines
|
// 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() {
|
if let Some((mut min, mut max)) = range_opt.take() {
|
||||||
|
|
@ -362,7 +366,7 @@ impl<'a> ViEditor<'a> {
|
||||||
if run.rtl {
|
if run.rtl {
|
||||||
min = 0;
|
min = 0;
|
||||||
} else {
|
} else {
|
||||||
max = self.buffer().size().0 as i32;
|
max = buffer.size().0 as i32;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f(
|
f(
|
||||||
|
|
@ -469,16 +473,17 @@ impl<'a> ViEditor<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Edit for ViEditor<'a> {
|
impl<'a, 'b> Edit for ViEditor<'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 {
|
||||||
|
|
@ -618,8 +623,9 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
Event::Delete => Action::Delete,
|
Event::Delete => Action::Delete,
|
||||||
Event::DeleteInLine => {
|
Event::DeleteInLine => {
|
||||||
let cursor = editor.cursor();
|
let cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
if cursor.index
|
||||||
if cursor.index < buffer.lines[cursor.line].text().len() {
|
< editor.with_buffer(|buffer| buffer.lines[cursor.line].text().len())
|
||||||
|
{
|
||||||
Action::Delete
|
Action::Delete
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
@ -638,11 +644,12 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
Selection::None | Selection::Normal(_) | Selection::Word(_) => {
|
Selection::None | Selection::Normal(_) | Selection::Word(_) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
if after {
|
if after {
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if let Some(c) = text[cursor.index..].chars().next() {
|
if let Some(c) = text[cursor.index..].chars().next() {
|
||||||
cursor.index += c.len_utf8();
|
cursor.index += c.len_utf8();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
}
|
}
|
||||||
editor.insert_at(cursor, data, None);
|
editor.insert_at(cursor, data, None);
|
||||||
|
|
@ -682,7 +689,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Event::Redraw => {
|
Event::Redraw => {
|
||||||
editor.buffer_mut().set_redraw(true);
|
editor.with_buffer_mut(|buffer| buffer.set_redraw(true));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Event::SelectClear => {
|
Event::SelectClear => {
|
||||||
|
|
@ -725,7 +732,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
TextObject::Word(word) => {
|
TextObject::Word(word) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let mut selection = editor.selection();
|
let mut selection = editor.selection();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
match WordIter::new(text, word)
|
match WordIter::new(text, word)
|
||||||
.find(|&(i, w)| i <= cursor.index && i + w.len() > cursor.index)
|
.find(|&(i, w)| i <= cursor.index && i + w.len() > cursor.index)
|
||||||
|
|
@ -739,6 +746,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_selection(selection);
|
editor.set_selection(selection);
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
}
|
}
|
||||||
|
|
@ -782,7 +790,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
Action::Motion(Motion::GotoLine(line.saturating_sub(1)))
|
Action::Motion(Motion::GotoLine(line.saturating_sub(1)))
|
||||||
}
|
}
|
||||||
modit::Motion::GotoEof => Action::Motion(Motion::GotoLine(
|
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::Home => Action::Motion(Motion::Home),
|
||||||
modit::Motion::Inside => {
|
modit::Motion::Inside => {
|
||||||
|
|
@ -804,7 +812,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
modit::Motion::NextChar(find_c) => {
|
modit::Motion::NextChar(find_c) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index < text.len() {
|
if cursor.index < text.len() {
|
||||||
match text[cursor.index..]
|
match text[cursor.index..]
|
||||||
|
|
@ -814,29 +822,31 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
{
|
{
|
||||||
Some((i, _)) => {
|
Some((i, _)) => {
|
||||||
cursor.index += i;
|
cursor.index += i;
|
||||||
editor.set_cursor(cursor);
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::NextCharTill(find_c) => {
|
modit::Motion::NextCharTill(find_c) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index < text.len() {
|
if cursor.index < text.len() {
|
||||||
let mut last_i = 0;
|
let mut last_i = 0;
|
||||||
for (i, c) in text[cursor.index..].char_indices() {
|
for (i, c) in text[cursor.index..].char_indices() {
|
||||||
if last_i > 0 && c == find_c {
|
if last_i > 0 && c == find_c {
|
||||||
cursor.index += last_i;
|
cursor.index += last_i;
|
||||||
editor.set_cursor(cursor);
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
last_i = i;
|
last_i = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::NextSearch => match &self.search_opt {
|
modit::Motion::NextSearch => match &self.search_opt {
|
||||||
|
|
@ -848,13 +858,17 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
},
|
},
|
||||||
modit::Motion::NextWordEnd(word) => {
|
modit::Motion::NextWordEnd(word) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
loop {
|
loop {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index < text.len() {
|
if cursor.index < text.len() {
|
||||||
cursor.index = WordIter::new(text, word)
|
cursor.index = WordIter::new(text, word)
|
||||||
.map(|(i, w)| {
|
.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)
|
.find(|&i| i > cursor.index)
|
||||||
.unwrap_or(text.len());
|
.unwrap_or(text.len());
|
||||||
|
|
@ -870,12 +884,13 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::NextWordStart(word) => {
|
modit::Motion::NextWordStart(word) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
loop {
|
loop {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index < text.len() {
|
if cursor.index < text.len() {
|
||||||
|
|
@ -895,6 +910,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -902,7 +918,7 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
modit::Motion::PageUp => Action::Motion(Motion::PageUp),
|
modit::Motion::PageUp => Action::Motion(Motion::PageUp),
|
||||||
modit::Motion::PreviousChar(find_c) => {
|
modit::Motion::PreviousChar(find_c) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index > 0 {
|
if cursor.index > 0 {
|
||||||
match text[..cursor.index]
|
match text[..cursor.index]
|
||||||
|
|
@ -912,16 +928,17 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
{
|
{
|
||||||
Some((i, _)) => {
|
Some((i, _)) => {
|
||||||
cursor.index = i;
|
cursor.index = i;
|
||||||
editor.set_cursor(cursor);
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::PreviousCharTill(find_c) => {
|
modit::Motion::PreviousCharTill(find_c) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index > 0 {
|
if cursor.index > 0 {
|
||||||
match text[..cursor.index]
|
match text[..cursor.index]
|
||||||
|
|
@ -939,11 +956,12 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
{
|
{
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
cursor.index = i;
|
cursor.index = i;
|
||||||
editor.set_cursor(cursor);
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::PreviousSearch => match &self.search_opt {
|
modit::Motion::PreviousSearch => match &self.search_opt {
|
||||||
|
|
@ -955,13 +973,17 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
},
|
},
|
||||||
modit::Motion::PreviousWordEnd(word) => {
|
modit::Motion::PreviousWordEnd(word) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
loop {
|
loop {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index > 0 {
|
if cursor.index > 0 {
|
||||||
cursor.index = WordIter::new(text, word)
|
cursor.index = WordIter::new(text, word)
|
||||||
.map(|(i, w)| {
|
.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)
|
.filter(|&i| i < cursor.index)
|
||||||
.last()
|
.last()
|
||||||
|
|
@ -978,12 +1000,13 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::PreviousWordStart(word) => {
|
modit::Motion::PreviousWordStart(word) => {
|
||||||
let mut cursor = editor.cursor();
|
let mut cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
editor.with_buffer(|buffer| {
|
||||||
loop {
|
loop {
|
||||||
let text = buffer.lines[cursor.line].text();
|
let text = buffer.lines[cursor.line].text();
|
||||||
if cursor.index > 0 {
|
if cursor.index > 0 {
|
||||||
|
|
@ -1004,14 +1027,17 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
editor.set_cursor(cursor);
|
editor.set_cursor(cursor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modit::Motion::Right => Action::Motion(Motion::Right),
|
modit::Motion::Right => Action::Motion(Motion::Right),
|
||||||
modit::Motion::RightInLine => {
|
modit::Motion::RightInLine => {
|
||||||
let cursor = editor.cursor();
|
let cursor = editor.cursor();
|
||||||
let buffer = editor.buffer();
|
if cursor.index
|
||||||
if cursor.index < buffer.lines[cursor.line].text().len() {
|
< editor
|
||||||
|
.with_buffer(|buffer| buffer.lines[cursor.line].text().len())
|
||||||
|
{
|
||||||
Action::Motion(Motion::Right)
|
Action::Motion(Motion::Right)
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
@ -1019,33 +1045,43 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
}
|
}
|
||||||
modit::Motion::ScreenHigh => {
|
modit::Motion::ScreenHigh => {
|
||||||
//TODO: is this efficient?
|
//TODO: is this efficient?
|
||||||
if let Some(first) = editor.buffer().layout_runs().next() {
|
if let Some(line_i) = editor.with_buffer(|buffer| {
|
||||||
Action::Motion(Motion::GotoLine(first.line_i))
|
buffer.layout_runs().next().map(|first| first.line_i)
|
||||||
|
}) {
|
||||||
|
Action::Motion(Motion::GotoLine(line_i))
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modit::Motion::ScreenLow => {
|
modit::Motion::ScreenLow => {
|
||||||
//TODO: is this efficient?
|
//TODO: is this efficient?
|
||||||
if let Some(last) = editor.buffer().layout_runs().last() {
|
if let Some(line_i) = editor.with_buffer(|buffer| {
|
||||||
Action::Motion(Motion::GotoLine(last.line_i))
|
buffer.layout_runs().last().map(|last| last.line_i)
|
||||||
|
}) {
|
||||||
|
Action::Motion(Motion::GotoLine(line_i))
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modit::Motion::ScreenMiddle => {
|
modit::Motion::ScreenMiddle => {
|
||||||
//TODO: is this efficient?
|
//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(first) = layout_runs.next() {
|
||||||
if let Some(last) = layout_runs.last() {
|
if let Some(last) = layout_runs.last() {
|
||||||
Action::Motion(Motion::GotoLine(
|
Some(Action::Motion(Motion::GotoLine(
|
||||||
(last.line_i + first.line_i) / 2,
|
(last.line_i + first.line_i) / 2,
|
||||||
))
|
)))
|
||||||
} else {
|
} else {
|
||||||
return;
|
None
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return;
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
match action_opt {
|
||||||
|
Some(action) => action,
|
||||||
|
None => return,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modit::Motion::Selection => {
|
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
|
/// Load text from a file, and also set syntax to the best option
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub fn load_text<P: AsRef<std::path::Path>>(
|
pub fn load_text<P: AsRef<std::path::Path>>(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue