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
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