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>,
|
||||
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() {
|
||||
text.push_str(line.text());
|
||||
text.push('\n');
|
||||
}
|
||||
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| {
|
||||
line.set_attrs_list(AttrsList::new(attrs));
|
||||
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) {
|
||||
for line in lines.iter_mut() {
|
||||
line.set_align(Some(align));
|
||||
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) = buffer.lines.get_mut(current_line) {
|
||||
line.set_align(Some(align));
|
||||
}
|
||||
} else if let Some(line) = editor.buffer_mut().lines.get_mut(current_line) {
|
||||
line.set_align(Some(align));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
match line.layout_opt() {
|
||||
Some(layout) => layout_lines += layout.len(),
|
||||
None => (),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
end_line = run.line_i;
|
||||
if start_line_opt.is_none() {
|
||||
start_line_opt = Some(end_line);
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
for (line_i, line) in text.lines().enumerate() {
|
||||
let buffer_line = &editor.buffer().lines[line_i];
|
||||
if buffer_line.text() != line {
|
||||
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
|
||||
wrong += 1;
|
||||
editor.with_buffer(|buffer| {
|
||||
for (line_i, line) in text.lines().enumerate() {
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
_ => (),
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
138
src/edit/mod.rs
138
src/edit/mod.rs
|
|
@ -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,69 +161,71 @@ pub trait Edit {
|
|||
/// Get the bounds of the current selection
|
||||
//TODO: will not work with Block select
|
||||
fn selection_bounds(&self) -> Option<(Cursor, Cursor)> {
|
||||
let cursor = self.cursor();
|
||||
match self.selection() {
|
||||
Selection::None => None,
|
||||
Selection::Normal(select) => match select.line.cmp(&cursor.line) {
|
||||
cmp::Ordering::Greater => Some((cursor, select)),
|
||||
cmp::Ordering::Less => Some((select, cursor)),
|
||||
cmp::Ordering::Equal => {
|
||||
/* select.line == cursor.line */
|
||||
if select.index < cursor.index {
|
||||
Some((select, cursor))
|
||||
} else {
|
||||
/* select.index >= cursor.index */
|
||||
Some((cursor, select))
|
||||
}
|
||||
}
|
||||
},
|
||||
Selection::Line(select) => {
|
||||
let start_line = cmp::min(select.line, cursor.line);
|
||||
let end_line = cmp::max(select.line, cursor.line);
|
||||
let end_index = self.buffer().lines[end_line].text().len();
|
||||
Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
|
||||
}
|
||||
Selection::Word(select) => {
|
||||
let (mut start, mut end) = match select.line.cmp(&cursor.line) {
|
||||
cmp::Ordering::Greater => (cursor, select),
|
||||
cmp::Ordering::Less => (select, cursor),
|
||||
self.with_buffer(|buffer| {
|
||||
let cursor = self.cursor();
|
||||
match self.selection() {
|
||||
Selection::None => None,
|
||||
Selection::Normal(select) => match select.line.cmp(&cursor.line) {
|
||||
cmp::Ordering::Greater => Some((cursor, select)),
|
||||
cmp::Ordering::Less => Some((select, cursor)),
|
||||
cmp::Ordering::Equal => {
|
||||
/* select.line == cursor.line */
|
||||
if select.index < cursor.index {
|
||||
(select, cursor)
|
||||
Some((select, cursor))
|
||||
} else {
|
||||
/* select.index >= cursor.index */
|
||||
(cursor, select)
|
||||
Some((cursor, select))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Move start to beginning of word
|
||||
{
|
||||
let line = &self.buffer().lines[start.line];
|
||||
start.index = line
|
||||
.text()
|
||||
.unicode_word_indices()
|
||||
.rev()
|
||||
.map(|(i, _)| i)
|
||||
.find(|&i| i < start.index)
|
||||
.unwrap_or(0);
|
||||
},
|
||||
Selection::Line(select) => {
|
||||
let start_line = cmp::min(select.line, cursor.line);
|
||||
let end_line = cmp::max(select.line, cursor.line);
|
||||
let end_index = buffer.lines[end_line].text().len();
|
||||
Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
|
||||
}
|
||||
Selection::Word(select) => {
|
||||
let (mut start, mut end) = match select.line.cmp(&cursor.line) {
|
||||
cmp::Ordering::Greater => (cursor, select),
|
||||
cmp::Ordering::Less => (select, cursor),
|
||||
cmp::Ordering::Equal => {
|
||||
/* select.line == cursor.line */
|
||||
if select.index < cursor.index {
|
||||
(select, cursor)
|
||||
} else {
|
||||
/* select.index >= cursor.index */
|
||||
(cursor, select)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Move end to end of word
|
||||
{
|
||||
let line = &self.buffer().lines[end.line];
|
||||
end.index = line
|
||||
.text()
|
||||
.unicode_word_indices()
|
||||
.map(|(i, word)| i + word.len())
|
||||
.find(|&i| i > end.index)
|
||||
.unwrap_or(line.text().len());
|
||||
// Move start to beginning of word
|
||||
{
|
||||
let line = &buffer.lines[start.line];
|
||||
start.index = line
|
||||
.text()
|
||||
.unicode_word_indices()
|
||||
.rev()
|
||||
.map(|(i, _)| i)
|
||||
.find(|&i| i < start.index)
|
||||
.unwrap_or(0);
|
||||
}
|
||||
|
||||
// Move end to end of word
|
||||
{
|
||||
let line = &buffer.lines[end.line];
|
||||
end.index = line
|
||||
.text()
|
||||
.unicode_word_indices()
|
||||
.map(|(i, word)| i + word.len())
|
||||
.find(|&i| i > end.index)
|
||||
.unwrap_or(line.text().len());
|
||||
}
|
||||
|
||||
Some((start, end))
|
||||
}
|
||||
|
||||
Some((start, end))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the current automatic indentation setting
|
||||
|
|
@ -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(),
|
||||
font_system: self.font_system,
|
||||
}
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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,18 +77,20 @@ 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() {
|
||||
let mut attrs = line.attrs_list().defaults();
|
||||
if let Some(foreground) = self.theme.settings.foreground {
|
||||
attrs = attrs.color(Color::rgba(
|
||||
foreground.r,
|
||||
foreground.g,
|
||||
foreground.b,
|
||||
foreground.a,
|
||||
));
|
||||
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(
|
||||
foreground.r,
|
||||
foreground.g,
|
||||
foreground.b,
|
||||
foreground.a,
|
||||
));
|
||||
}
|
||||
line.set_attrs_list(AttrsList::new(attrs));
|
||||
}
|
||||
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,23 +259,84 @@ impl<'a> Edit for SyntaxEditor<'a> {
|
|||
let now = std::time::Instant::now();
|
||||
|
||||
let cursor = self.cursor();
|
||||
let buffer = self.editor.buffer_mut();
|
||||
let visible_lines = buffer.visible_lines();
|
||||
let scroll = buffer.scroll();
|
||||
let scroll_end = scroll.layout + visible_lines;
|
||||
let mut total_layout = 0;
|
||||
let mut highlighted = 0;
|
||||
for line_i in 0..buffer.lines.len() {
|
||||
// Break out if we have reached the end of scroll and are past the cursor
|
||||
if total_layout >= scroll_end && line_i > cursor.line {
|
||||
break;
|
||||
}
|
||||
self.editor.with_buffer_mut(|buffer| {
|
||||
let visible_lines = buffer.visible_lines();
|
||||
let scroll = buffer.scroll();
|
||||
let scroll_end = scroll.layout + visible_lines;
|
||||
let mut total_layout = 0;
|
||||
let mut highlighted = 0;
|
||||
for line_i in 0..buffer.lines.len() {
|
||||
// Break out if we have reached the end of scroll and are past the cursor
|
||||
if total_layout >= scroll_end && line_i > cursor.line {
|
||||
break;
|
||||
}
|
||||
|
||||
let line = &mut buffer.lines[line_i];
|
||||
if line.metadata().is_some() && line_i < self.syntax_cache.len() {
|
||||
//TODO: duplicated code!
|
||||
let line = &mut buffer.lines[line_i];
|
||||
if line.metadata().is_some() && line_i < self.syntax_cache.len() {
|
||||
//TODO: duplicated code!
|
||||
if line_i >= scroll.line && total_layout < scroll_end {
|
||||
// Perform shaping and layout of this line in order to count if we have reached scroll
|
||||
match buffer.line_layout(font_system, line_i) {
|
||||
Some(layout_lines) => {
|
||||
total_layout += layout_lines.len() as i32;
|
||||
}
|
||||
None => {
|
||||
//TODO: should this be possible?
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
highlighted += 1;
|
||||
|
||||
let (mut parse_state, scope_stack) =
|
||||
if line_i > 0 && line_i <= self.syntax_cache.len() {
|
||||
self.syntax_cache[line_i - 1].clone()
|
||||
} else {
|
||||
(ParseState::new(self.syntax), ScopeStack::new())
|
||||
};
|
||||
let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
|
||||
let ops = parse_state
|
||||
.parse_line(line.text(), &self.syntax_system.syntax_set)
|
||||
.expect("failed to parse syntax");
|
||||
let ranges = RangedHighlightIterator::new(
|
||||
&mut highlight_state,
|
||||
&ops,
|
||||
line.text(),
|
||||
&self.highlighter,
|
||||
);
|
||||
|
||||
let attrs = line.attrs_list().defaults();
|
||||
let mut attrs_list = AttrsList::new(attrs);
|
||||
for (style, _, range) in ranges {
|
||||
let span_attrs = attrs
|
||||
.color(Color::rgba(
|
||||
style.foreground.r,
|
||||
style.foreground.g,
|
||||
style.foreground.b,
|
||||
style.foreground.a,
|
||||
))
|
||||
//TODO: background
|
||||
.style(if style.font_style.contains(FontStyle::ITALIC) {
|
||||
Style::Italic
|
||||
} else {
|
||||
Style::Normal
|
||||
})
|
||||
.weight(if style.font_style.contains(FontStyle::BOLD) {
|
||||
Weight::BOLD
|
||||
} else {
|
||||
Weight::NORMAL
|
||||
}); //TODO: underline
|
||||
if span_attrs != attrs {
|
||||
attrs_list.add_span(range, span_attrs);
|
||||
}
|
||||
}
|
||||
|
||||
// Update line attributes. This operation only resets if the line changes
|
||||
line.set_attrs_list(attrs_list);
|
||||
|
||||
// Perform shaping and layout of this line in order to count if we have reached scroll
|
||||
if line_i >= scroll.line && total_layout < scroll_end {
|
||||
// Perform shaping and layout of this line in order to count if we have reached scroll
|
||||
match buffer.line_layout(font_system, line_i) {
|
||||
Some(layout_lines) => {
|
||||
total_layout += layout_lines.len() as i32;
|
||||
|
|
@ -279,91 +346,31 @@ impl<'a> Edit for SyntaxEditor<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
highlighted += 1;
|
||||
|
||||
let (mut parse_state, scope_stack) = if line_i > 0 && line_i <= self.syntax_cache.len()
|
||||
{
|
||||
self.syntax_cache[line_i - 1].clone()
|
||||
} else {
|
||||
(ParseState::new(self.syntax), ScopeStack::new())
|
||||
};
|
||||
let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
|
||||
let ops = parse_state
|
||||
.parse_line(line.text(), &self.syntax_system.syntax_set)
|
||||
.expect("failed to parse syntax");
|
||||
let ranges = RangedHighlightIterator::new(
|
||||
&mut highlight_state,
|
||||
&ops,
|
||||
line.text(),
|
||||
&self.highlighter,
|
||||
);
|
||||
|
||||
let attrs = line.attrs_list().defaults();
|
||||
let mut attrs_list = AttrsList::new(attrs);
|
||||
for (style, _, range) in ranges {
|
||||
let span_attrs = attrs
|
||||
.color(Color::rgba(
|
||||
style.foreground.r,
|
||||
style.foreground.g,
|
||||
style.foreground.b,
|
||||
style.foreground.a,
|
||||
))
|
||||
//TODO: background
|
||||
.style(if style.font_style.contains(FontStyle::ITALIC) {
|
||||
Style::Italic
|
||||
} else {
|
||||
Style::Normal
|
||||
})
|
||||
.weight(if style.font_style.contains(FontStyle::BOLD) {
|
||||
Weight::BOLD
|
||||
} else {
|
||||
Weight::NORMAL
|
||||
}); //TODO: underline
|
||||
if span_attrs != attrs {
|
||||
attrs_list.add_span(range, span_attrs);
|
||||
let cache_item = (parse_state.clone(), highlight_state.path.clone());
|
||||
if line_i < self.syntax_cache.len() {
|
||||
if self.syntax_cache[line_i] != cache_item {
|
||||
self.syntax_cache[line_i] = cache_item;
|
||||
if line_i + 1 < buffer.lines.len() {
|
||||
buffer.lines[line_i + 1].reset();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buffer.lines[line_i].set_metadata(self.syntax_cache.len());
|
||||
self.syntax_cache.push(cache_item);
|
||||
}
|
||||
}
|
||||
|
||||
// Update line attributes. This operation only resets if the line changes
|
||||
line.set_attrs_list(attrs_list);
|
||||
|
||||
// Perform shaping and layout of this line in order to count if we have reached scroll
|
||||
if line_i >= scroll.line && total_layout < scroll_end {
|
||||
match buffer.line_layout(font_system, line_i) {
|
||||
Some(layout_lines) => {
|
||||
total_layout += layout_lines.len() as i32;
|
||||
}
|
||||
None => {
|
||||
//TODO: should this be possible?
|
||||
}
|
||||
}
|
||||
if highlighted > 0 {
|
||||
buffer.set_redraw(true);
|
||||
#[cfg(feature = "std")]
|
||||
log::debug!(
|
||||
"Syntax highlighted {} lines in {:?}",
|
||||
highlighted,
|
||||
now.elapsed()
|
||||
);
|
||||
}
|
||||
|
||||
let cache_item = (parse_state.clone(), highlight_state.path.clone());
|
||||
if line_i < self.syntax_cache.len() {
|
||||
if self.syntax_cache[line_i] != cache_item {
|
||||
self.syntax_cache[line_i] = cache_item;
|
||||
if line_i + 1 < buffer.lines.len() {
|
||||
buffer.lines[line_i + 1].reset();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buffer.lines[line_i].set_metadata(self.syntax_cache.len());
|
||||
self.syntax_cache.push(cache_item);
|
||||
}
|
||||
}
|
||||
|
||||
if highlighted > 0 {
|
||||
buffer.set_redraw(true);
|
||||
#[cfg(feature = "std")]
|
||||
log::debug!(
|
||||
"Syntax highlighted {} lines in {:?}",
|
||||
highlighted,
|
||||
now.elapsed()
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
self.editor.shape_as_needed(font_system, prune);
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
882
src/edit/vi.rs
882
src/edit/vi.rs
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue