Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

922
vendor/cosmic-text/src/edit/editor.rs vendored Normal file
View File

@@ -0,0 +1,922 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#[cfg(not(feature = "std"))]
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{cmp, iter::once};
use unicode_segmentation::UnicodeSegmentation;
#[cfg(feature = "swash")]
use crate::Color;
use crate::{
Action, Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, BufferRef, Change, ChangeItem,
Cursor, Edit, FontSystem, LayoutRun, Selection, Shaping,
};
/// A wrapper of [`Buffer`] for easy editing
#[derive(Debug, Clone)]
pub struct Editor<'buffer> {
buffer_ref: BufferRef<'buffer>,
cursor: Cursor,
cursor_x_opt: Option<i32>,
selection: Selection,
cursor_moved: bool,
auto_indent: bool,
change: Option<Change>,
}
fn cursor_glyph_opt(cursor: &Cursor, run: &LayoutRun) -> Option<(usize, f32)> {
if cursor.line == run.line_i {
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return Some((glyph_i, 0.0));
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
let mut before = 0;
let mut total = 0;
let cluster = &run.text[glyph.start..glyph.end];
for (i, _) in cluster.grapheme_indices(true) {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph_i, offset));
}
}
match run.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some((run.glyphs.len(), 0.0));
}
}
None => {
return Some((0, 0.0));
}
}
}
None
}
fn cursor_position(cursor: &Cursor, run: &LayoutRun) -> Option<(i32, i32)> {
let (cursor_glyph, cursor_glyph_offset) = cursor_glyph_opt(cursor, run)?;
let x = match run.glyphs.get(cursor_glyph) {
Some(glyph) => {
// Start of detected glyph
if glyph.level.is_rtl() {
(glyph.x + glyph.w - cursor_glyph_offset) as i32
} else {
(glyph.x + cursor_glyph_offset) as i32
}
}
None => match run.glyphs.last() {
Some(glyph) => {
// End of last glyph
if glyph.level.is_rtl() {
glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
}
}
None => {
// Start of empty line
0
}
},
};
Some((x, run.line_top as i32))
}
impl<'buffer> Editor<'buffer> {
/// Create a new [`Editor`] with the provided [`Buffer`]
pub fn new(buffer: impl Into<BufferRef<'buffer>>) -> Self {
Self {
buffer_ref: buffer.into(),
cursor: Cursor::default(),
cursor_x_opt: None,
selection: Selection::None,
cursor_moved: false,
auto_indent: false,
change: None,
}
}
/// Draw the editor
#[cfg(feature = "swash")]
#[allow(clippy::too_many_arguments)]
pub fn draw<F>(
&self,
font_system: &mut FontSystem,
cache: &mut crate::SwashCache,
text_color: Color,
cursor_color: Color,
selection_color: Color,
selected_text_color: Color,
mut f: F,
) where
F: FnMut(i32, i32, u32, u32, Color),
{
let selection_bounds = self.selection_bounds();
self.with_buffer(|buffer| {
for run in buffer.layout_runs() {
let line_i = run.line_i;
let line_y = run.line_y;
let line_top = run.line_top;
let line_height = run.line_height;
// Highlight selection
if let Some((start, end)) = selection_bounds {
if line_i >= start.line && line_i <= end.line {
let mut range_opt = None;
for glyph in run.glyphs.iter() {
// Guess x offset based on characters
let cluster = &run.text[glyph.start..glyph.end];
let total = cluster.grapheme_indices(true).count();
let mut c_x = glyph.x;
let c_w = glyph.w / total as f32;
for (i, c) in cluster.grapheme_indices(true) {
let c_start = glyph.start + i;
let c_end = glyph.start + i + c.len();
if (start.line != line_i || c_end > start.index)
&& (end.line != line_i || c_start < end.index)
{
range_opt = match range_opt.take() {
Some((min, max)) => Some((
cmp::min(min, c_x as i32),
cmp::max(max, (c_x + c_w) as i32),
)),
None => Some((c_x as i32, (c_x + c_w) as i32)),
};
} else if let Some((min, max)) = range_opt.take() {
f(
min,
line_top as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
selection_color,
);
}
c_x += c_w;
}
}
if run.glyphs.is_empty() && end.line > line_i {
// Highlight all of internal empty lines
range_opt = Some((0, buffer.size().0.unwrap_or(0.0) as i32));
}
if let Some((mut min, mut max)) = range_opt.take() {
if end.line > line_i {
// Draw to end of line
if run.rtl {
min = 0;
} else {
max = buffer.size().0.unwrap_or(0.0) as i32;
}
}
f(
min,
line_top as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
selection_color,
);
}
}
}
// Draw cursor
if let Some((x, y)) = cursor_position(&self.cursor, &run) {
f(x, y, 1, line_height as u32, cursor_color);
}
for glyph in run.glyphs.iter() {
let physical_glyph = glyph.physical((0., 0.), 1.0);
let mut glyph_color = match glyph.color_opt {
Some(some) => some,
None => text_color,
};
if text_color != selected_text_color {
if let Some((start, end)) = selection_bounds {
if line_i >= start.line
&& line_i <= end.line
&& (start.line != line_i || glyph.end > start.index)
&& (end.line != line_i || glyph.start < end.index)
{
glyph_color = selected_text_color;
}
}
}
cache.with_pixels(
font_system,
physical_glyph.cache_key,
glyph_color,
|x, y, color| {
f(
physical_glyph.x + x,
line_y as i32 + physical_glyph.y + y,
1,
1,
color,
);
},
);
}
}
});
}
}
impl<'buffer> Edit<'buffer> for Editor<'buffer> {
fn buffer_ref(&self) -> &BufferRef<'buffer> {
&self.buffer_ref
}
fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer> {
&mut self.buffer_ref
}
fn cursor(&self) -> Cursor {
self.cursor
}
fn set_cursor(&mut self, cursor: Cursor) {
if self.cursor != cursor {
self.cursor = cursor;
self.cursor_moved = true;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
fn selection(&self) -> Selection {
self.selection
}
fn set_selection(&mut self, selection: Selection) {
if self.selection != selection {
self.selection = selection;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
fn auto_indent(&self) -> bool {
self.auto_indent
}
fn set_auto_indent(&mut self, auto_indent: bool) {
self.auto_indent = auto_indent;
}
fn tab_width(&self) -> u16 {
self.with_buffer(|buffer| buffer.tab_width())
}
fn set_tab_width(&mut self, font_system: &mut FontSystem, tab_width: u16) {
self.with_buffer_mut(|buffer| buffer.set_tab_width(font_system, tab_width));
}
fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
if self.cursor_moved {
let cursor = self.cursor;
self.with_buffer_mut(|buffer| buffer.shape_until_cursor(font_system, cursor, prune));
self.cursor_moved = false;
} else {
self.with_buffer_mut(|buffer| buffer.shape_until_scroll(font_system, prune));
}
}
fn delete_range(&mut self, start: Cursor, end: Cursor) {
let change_item = self.with_buffer_mut(|buffer| {
// Collect removed data for change tracking
let mut change_lines = Vec::new();
// Delete the selection from the last line
let end_line_opt = if end.line > start.line {
// Get part of line after selection
let after = buffer.lines[end.line].split_off(end.index);
// Remove end line
let removed = buffer.lines.remove(end.line);
change_lines.insert(0, removed.text().to_string());
Some(after)
} else {
None
};
// Delete interior lines (in reverse for safety)
for line_i in (start.line + 1..end.line).rev() {
let removed = buffer.lines.remove(line_i);
change_lines.insert(0, removed.text().to_string());
}
// Delete the selection from the first line
{
// Get part after selection if start line is also end line
let after_opt = if start.line == end.line {
Some(buffer.lines[start.line].split_off(end.index))
} else {
None
};
// Delete selected part of line
let removed = buffer.lines[start.line].split_off(start.index);
change_lines.insert(0, removed.text().to_string());
// Re-add part of line after selection
if let Some(after) = after_opt {
buffer.lines[start.line].append(after);
}
// Re-add valid parts of end line
if let Some(end_line) = end_line_opt {
buffer.lines[start.line].append(end_line);
}
}
ChangeItem {
start,
end,
text: change_lines.join("\n"),
insert: false,
}
});
if let Some(ref mut change) = self.change {
change.items.push(change_item);
}
}
fn insert_at(
&mut self,
mut cursor: Cursor,
data: &str,
attrs_list: Option<AttrsList>,
) -> Cursor {
let mut remaining_split_len = data.len();
if remaining_split_len == 0 {
return cursor;
}
let change_item = self.with_buffer_mut(|buffer| {
// Save cursor for change tracking
let start = cursor;
// Ensure there are enough lines in the buffer to handle this cursor
while cursor.line >= buffer.lines.len() {
let ending = buffer
.lines
.last()
.map(|line| line.ending())
.unwrap_or_default();
let line = BufferLine::new(
String::new(),
ending,
AttrsList::new(attrs_list.as_ref().map_or_else(
|| {
buffer
.lines
.last()
.map_or(Attrs::new(), |line| line.attrs_list().defaults())
},
|x| x.defaults(),
)),
Shaping::Advanced,
);
buffer.lines.push(line);
}
let line: &mut BufferLine = &mut buffer.lines[cursor.line];
let insert_line = cursor.line + 1;
let ending = line.ending();
// Collect text after insertion as a line
let after: BufferLine = line.split_off(cursor.index);
let after_len = after.text().len();
// Collect attributes
let mut final_attrs = attrs_list.unwrap_or_else(|| {
AttrsList::new(line.attrs_list().get_span(cursor.index.saturating_sub(1)))
});
// Append the inserted text, line by line
// we want to see a blank entry if the string ends with a newline
//TODO: adjust this to get line ending from data?
let addendum = once("").filter(|_| data.ends_with('\n'));
let mut lines_iter = data.split_inclusive('\n').chain(addendum);
if let Some(data_line) = lines_iter.next() {
let mut these_attrs = final_attrs.split_off(data_line.len());
remaining_split_len -= data_line.len();
core::mem::swap(&mut these_attrs, &mut final_attrs);
line.append(BufferLine::new(
data_line
.strip_suffix(char::is_control)
.unwrap_or(data_line),
ending,
these_attrs,
Shaping::Advanced,
));
} else {
panic!("str::lines() did not yield any elements");
}
if let Some(data_line) = lines_iter.next_back() {
remaining_split_len -= data_line.len();
let mut tmp = BufferLine::new(
data_line
.strip_suffix(char::is_control)
.unwrap_or(data_line),
ending,
final_attrs.split_off(remaining_split_len),
Shaping::Advanced,
);
tmp.append(after);
buffer.lines.insert(insert_line, tmp);
cursor.line += 1;
} else {
line.append(after);
}
for data_line in lines_iter.rev() {
remaining_split_len -= data_line.len();
let tmp = BufferLine::new(
data_line
.strip_suffix(char::is_control)
.unwrap_or(data_line),
ending,
final_attrs.split_off(remaining_split_len),
Shaping::Advanced,
);
buffer.lines.insert(insert_line, tmp);
cursor.line += 1;
}
assert_eq!(remaining_split_len, 0);
// Append the text after insertion
cursor.index = buffer.lines[cursor.line].text().len() - after_len;
ChangeItem {
start,
end: cursor,
text: data.to_string(),
insert: true,
}
});
if let Some(ref mut change) = self.change {
change.items.push(change_item);
}
cursor
}
fn copy_selection(&self) -> Option<String> {
let (start, end) = self.selection_bounds()?;
self.with_buffer(|buffer| {
let mut selection = String::new();
// Take the selection from the first line
{
// Add selected part of line to string
if start.line == end.line {
selection.push_str(&buffer.lines[start.line].text()[start.index..end.index]);
} else {
selection.push_str(&buffer.lines[start.line].text()[start.index..]);
selection.push('\n');
}
}
// Take the selection from all interior lines (if they exist)
for line_i in start.line + 1..end.line {
selection.push_str(buffer.lines[line_i].text());
selection.push('\n');
}
// Take the selection from the last line
if end.line > start.line {
// Add selected part of line to string
selection.push_str(&buffer.lines[end.line].text()[..end.index]);
}
Some(selection)
})
}
fn delete_selection(&mut self) -> bool {
let (start, end) = match self.selection_bounds() {
Some(some) => some,
None => return false,
};
// Reset cursor to start of selection
self.cursor = start;
// Reset selection to None
self.selection = Selection::None;
// Delete from start to end of selection
self.delete_range(start, end);
true
}
fn apply_change(&mut self, change: &Change) -> bool {
// Cannot apply changes if there is a pending change
if let Some(pending) = self.change.take() {
if !pending.items.is_empty() {
//TODO: is this a good idea?
log::warn!("pending change caused apply_change to be ignored!");
self.change = Some(pending);
return false;
}
}
for item in change.items.iter() {
//TODO: edit cursor if needed?
if item.insert {
self.cursor = self.insert_at(item.start, &item.text, None);
} else {
self.cursor = item.start;
self.delete_range(item.start, item.end);
}
}
true
}
fn start_change(&mut self) {
if self.change.is_none() {
self.change = Some(Change::default());
}
}
fn finish_change(&mut self) -> Option<Change> {
self.change.take()
}
fn action(&mut self, font_system: &mut FontSystem, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Motion(motion) => {
let cursor = self.cursor;
let cursor_x_opt = self.cursor_x_opt;
if let Some((new_cursor, new_cursor_x_opt)) = self.with_buffer_mut(|buffer| {
buffer.cursor_motion(font_system, cursor, cursor_x_opt, motion)
}) {
self.cursor = new_cursor;
self.cursor_x_opt = new_cursor_x_opt;
}
}
Action::Escape => {
match self.selection {
Selection::None => {}
_ => self.with_buffer_mut(|buffer| buffer.set_redraw(true)),
}
self.selection = Selection::None;
}
Action::Insert(character) => {
if character.is_control() && !['\t', '\n', '\u{92}'].contains(&character) {
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else if character == '\n' {
self.action(font_system, Action::Enter);
} else {
let mut str_buf = [0u8; 8];
let str_ref = character.encode_utf8(&mut str_buf);
self.insert_string(str_ref, None);
}
}
Action::Enter => {
//TODO: what about indenting more after opening brackets or parentheses?
if self.auto_indent {
let mut string = String::from("\n");
self.with_buffer(|buffer| {
let line = &buffer.lines[self.cursor.line];
let text = line.text();
for c in text.chars() {
if c.is_whitespace() {
string.push(c);
} else {
break;
}
}
});
self.insert_string(&string, None);
} else {
self.insert_string("\n", None);
}
// Ensure line is properly shaped and laid out (for potential immediate commands)
let line_i = self.cursor.line;
self.with_buffer_mut(|buffer| {
buffer.line_layout(font_system, line_i);
});
}
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else {
// Save current cursor as end
let end = self.cursor;
if self.cursor.index > 0 {
// Move cursor to previous character index
self.cursor.index = self.with_buffer(|buffer| {
buffer.lines[self.cursor.line].text()[..self.cursor.index]
.char_indices()
.next_back()
.map_or(0, |(i, _)| i)
});
} else if self.cursor.line > 0 {
// Move cursor to previous line
self.cursor.line -= 1;
self.cursor.index =
self.with_buffer(|buffer| buffer.lines[self.cursor.line].text().len());
}
if self.cursor != end {
// Delete range
self.delete_range(self.cursor, end);
}
}
}
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else {
// Save current cursor as start and end
let mut start = self.cursor;
let mut end = self.cursor;
self.with_buffer(|buffer| {
if start.index < buffer.lines[start.line].text().len() {
let line = &buffer.lines[start.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= start.index)
.last()
.map(|(i, c)| i..(i + c.len()));
if let Some(range) = range_opt {
start.index = range.start;
end.index = range.end;
}
} else if start.line + 1 < buffer.lines.len() {
end.line += 1;
end.index = 0;
}
});
if start != end {
self.cursor = start;
self.delete_range(start, end);
}
}
}
Action::Indent => {
// Get start and end of selection
let (start, end) = match self.selection_bounds() {
Some(some) => some,
None => (self.cursor, self.cursor),
};
// For every line in selection
let tab_width: usize = self.tab_width().into();
for line_i in start.line..=end.line {
// Determine indexes of last indent and first character after whitespace
let mut after_whitespace = 0;
let mut required_indent = 0;
self.with_buffer(|buffer| {
let line = &buffer.lines[line_i];
let text = line.text();
// Default to end of line if no non-whitespace found
after_whitespace = text.len();
for (count, (index, c)) in text.char_indices().enumerate() {
if !c.is_whitespace() {
after_whitespace = index;
required_indent = tab_width - (count % tab_width);
break;
}
}
});
// No indent required (not possible?)
if required_indent == 0 {
required_indent = tab_width;
}
self.insert_at(
Cursor::new(line_i, after_whitespace),
&" ".repeat(required_indent),
None,
);
// Adjust cursor
if self.cursor.line == line_i {
//TODO: should we be forcing cursor index to current indent location?
if self.cursor.index < after_whitespace {
self.cursor.index = after_whitespace;
}
self.cursor.index += required_indent;
}
// Adjust selection
match self.selection {
Selection::None => {}
Selection::Normal(ref mut select)
| Selection::Line(ref mut select)
| Selection::Word(ref mut select) => {
if select.line == line_i && select.index >= after_whitespace {
select.index += required_indent;
}
}
}
// Request redraw
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Unindent => {
// Get start and end of selection
let (start, end) = match self.selection_bounds() {
Some(some) => some,
None => (self.cursor, self.cursor),
};
// For every line in selection
let tab_width: usize = self.tab_width().into();
for line_i in start.line..=end.line {
// Determine indexes of last indent and first character after whitespace
let mut last_indent = 0;
let mut after_whitespace = 0;
self.with_buffer(|buffer| {
let line = &buffer.lines[line_i];
let text = line.text();
// Default to end of line if no non-whitespace found
after_whitespace = text.len();
for (count, (index, c)) in text.char_indices().enumerate() {
if !c.is_whitespace() {
after_whitespace = index;
break;
}
if count % tab_width == 0 {
last_indent = index;
}
}
});
// No de-indent required
if last_indent == after_whitespace {
continue;
}
// Delete one indent
self.delete_range(
Cursor::new(line_i, last_indent),
Cursor::new(line_i, after_whitespace),
);
// Adjust cursor
if self.cursor.line == line_i && self.cursor.index > last_indent {
self.cursor.index -= after_whitespace - last_indent;
}
// Adjust selection
match self.selection {
Selection::None => {}
Selection::Normal(ref mut select)
| Selection::Line(ref mut select)
| Selection::Word(ref mut select) => {
if select.line == line_i && select.index > last_indent {
select.index -= after_whitespace - last_indent;
}
}
}
// Request redraw
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Click { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
}
Action::DoubleClick { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
self.selection = Selection::Word(self.cursor);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::TripleClick { x, y } => {
self.set_selection(Selection::None);
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
}
self.selection = Selection::Line(self.cursor);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
Action::Drag { x, y } => {
if self.selection == Selection::None {
self.selection = Selection::Normal(self.cursor);
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
if let Some(new_cursor) = self.with_buffer(|buffer| buffer.hit(x as f32, y as f32))
{
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
}
}
}
Action::Scroll { lines } => {
self.with_buffer_mut(|buffer| {
let mut scroll = buffer.scroll();
//TODO: align to layout lines
scroll.vertical += lines as f32 * buffer.metrics().line_height;
buffer.set_scroll(scroll);
});
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
font_opt.as_ref().map_or("?", |font| font.info.family.as_str()),
font_opt.as_ref().map_or("?", |font| font.info.post_script_name.as_str()),
text_glyph,
text_glyph
);
}
*/
}
}
fn cursor_position(&self) -> Option<(i32, i32)> {
self.with_buffer(|buffer| {
buffer
.layout_runs()
.find_map(|run| cursor_position(&self.cursor, &run))
})
}
}
impl BorrowedWithFontSystem<'_, Editor<'_>> {
#[cfg(feature = "swash")]
pub fn draw<F>(
&mut self,
cache: &mut crate::SwashCache,
text_color: Color,
cursor_color: Color,
selection_color: Color,
selected_text_color: Color,
f: F,
) where
F: FnMut(i32, i32, u32, u32, Color),
{
self.inner.draw(
self.font_system,
cache,
text_color,
cursor_color,
selection_color,
selected_text_color,
f,
);
}
}

366
vendor/cosmic-text/src/edit/mod.rs vendored Normal file
View File

@@ -0,0 +1,366 @@
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::cmp;
use unicode_segmentation::UnicodeSegmentation;
use crate::{AttrsList, BorrowedWithFontSystem, Buffer, Cursor, FontSystem, Motion};
pub use self::editor::*;
mod editor;
#[cfg(feature = "syntect")]
pub use self::syntect::*;
#[cfg(feature = "syntect")]
mod syntect;
#[cfg(feature = "vi")]
pub use self::vi::*;
#[cfg(feature = "vi")]
mod vi;
/// An action to perform on an [`Editor`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
/// Move the cursor with some motion
Motion(Motion),
/// Escape, clears selection
Escape,
/// Insert character at cursor
Insert(char),
/// Create new line
Enter,
/// Delete text behind cursor
Backspace,
/// Delete text in front of cursor
Delete,
// Indent text (typically Tab)
Indent,
// Unindent text (typically Shift+Tab)
Unindent,
/// Mouse click at specified position
Click {
x: i32,
y: i32,
},
/// Mouse double click at specified position
DoubleClick {
x: i32,
y: i32,
},
/// Mouse triple click at specified position
TripleClick {
x: i32,
y: i32,
},
/// Mouse drag to specified position
Drag {
x: i32,
y: i32,
},
/// Scroll specified number of lines
Scroll {
lines: i32,
},
}
#[derive(Debug)]
pub enum BufferRef<'buffer> {
Owned(Buffer),
Borrowed(&'buffer mut Buffer),
Arc(Arc<Buffer>),
}
impl Clone for BufferRef<'_> {
fn clone(&self) -> Self {
match self {
Self::Owned(buffer) => Self::Owned(buffer.clone()),
Self::Borrowed(buffer) => Self::Owned((*buffer).clone()),
Self::Arc(buffer) => Self::Arc(buffer.clone()),
}
}
}
impl From<Buffer> for BufferRef<'_> {
fn from(buffer: Buffer) -> Self {
Self::Owned(buffer)
}
}
impl<'buffer> From<&'buffer mut Buffer> for BufferRef<'buffer> {
fn from(buffer: &'buffer mut Buffer) -> Self {
Self::Borrowed(buffer)
}
}
impl From<Arc<Buffer>> for BufferRef<'_> {
fn from(arc: Arc<Buffer>) -> Self {
Self::Arc(arc)
}
}
/// A unique change to an editor
#[derive(Clone, Debug)]
pub struct ChangeItem {
/// Cursor indicating start of change
pub start: Cursor,
/// Cursor indicating end of change
pub end: Cursor,
/// Text to be inserted or deleted
pub text: String,
/// Insert if true, delete if false
pub insert: bool,
}
impl ChangeItem {
// Reverse change item (in place)
pub fn reverse(&mut self) {
self.insert = !self.insert;
}
}
/// A set of change items grouped into one logical change
#[derive(Clone, Debug, Default)]
pub struct Change {
/// Change items grouped into one change
pub items: Vec<ChangeItem>,
}
impl Change {
// Reverse change (in place)
pub fn reverse(&mut self) {
self.items.reverse();
for item in self.items.iter_mut() {
item.reverse();
}
}
}
/// Selection mode
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Selection {
/// No selection
None,
/// Normal selection
Normal(Cursor),
/// Select by lines
Line(Cursor),
/// Select by words
Word(Cursor),
//TODO: Select block
}
/// A trait to allow easy replacements of [`Editor`], like `SyntaxEditor`
pub trait Edit<'buffer> {
/// Mutably borrows `self` together with an [`FontSystem`] for more convenient methods
fn borrow_with<'font_system>(
&'font_system mut self,
font_system: &'font_system mut FontSystem,
) -> BorrowedWithFontSystem<'font_system, Self>
where
Self: Sized,
{
BorrowedWithFontSystem {
inner: self,
font_system,
}
}
/// Get the internal [`BufferRef`]
fn buffer_ref(&self) -> &BufferRef<'buffer>;
/// Get the internal [`BufferRef`]
fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer>;
/// Get the internal [`Buffer`]
fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
match self.buffer_ref() {
BufferRef::Owned(buffer) => f(buffer),
BufferRef::Borrowed(buffer) => f(buffer),
BufferRef::Arc(buffer) => f(buffer),
}
}
/// Get the internal [`Buffer`], mutably
fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
match self.buffer_ref_mut() {
BufferRef::Owned(buffer) => f(buffer),
BufferRef::Borrowed(buffer) => f(buffer),
BufferRef::Arc(buffer) => f(Arc::make_mut(buffer)),
}
}
/// 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;
/// Set the current cursor
fn set_cursor(&mut self, cursor: Cursor);
/// Get the current selection position
fn selection(&self) -> Selection;
/// Set the current selection position
fn set_selection(&mut self, selection: Selection);
/// Get the bounds of the current selection
//TODO: will not work with Block select
fn selection_bounds(&self) -> Option<(Cursor, Cursor)> {
self.with_buffer(|buffer| {
let cursor = self.cursor();
match self.selection() {
Selection::None => None,
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 = 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 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))
}
}
})
}
/// Get the current automatic indentation setting
fn auto_indent(&self) -> bool;
/// Enable or disable automatic indentation
fn set_auto_indent(&mut self, auto_indent: bool);
/// Get the current tab width
fn tab_width(&self) -> u16;
/// Set the current tab width. A `tab_width` of 0 is not allowed, and will be ignored
fn set_tab_width(&mut self, font_system: &mut FontSystem, tab_width: u16);
/// Shape lines until scroll, after adjusting scroll if the cursor moved
fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool);
/// Delete text starting at start Cursor and ending at end Cursor
fn delete_range(&mut self, start: Cursor, end: Cursor);
/// Insert text at specified cursor with specified `attrs_list`
fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor;
/// Copy selection
fn copy_selection(&self) -> Option<String>;
/// Delete selection, adjusting cursor and returning true if there was a selection
// Also used by backspace, delete, insert, and enter when there is a selection
fn delete_selection(&mut self) -> bool;
/// Insert a string at the current cursor or replacing the current selection with the given
/// attributes, or with the previous character's attributes if None is given.
fn insert_string(&mut self, data: &str, attrs_list: Option<AttrsList>) {
self.delete_selection();
let new_cursor = self.insert_at(self.cursor(), data, attrs_list);
self.set_cursor(new_cursor);
}
/// Apply a change
fn apply_change(&mut self, change: &Change) -> bool;
/// Start collecting change
fn start_change(&mut self);
/// Get completed change
fn finish_change(&mut self) -> Option<Change>;
/// Perform an [Action] on the editor
fn action(&mut self, font_system: &mut FontSystem, action: Action);
/// Get X and Y position of the top left corner of the cursor
fn cursor_position(&self) -> Option<(i32, i32)>;
}
impl<'buffer, E: Edit<'buffer>> BorrowedWithFontSystem<'_, E> {
/// Get the internal [`Buffer`], mutably
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)
})
}
/// Set the current tab width. A `tab_width` of 0 is not allowed, and will be ignored
pub fn set_tab_width(&mut self, tab_width: u16) {
self.inner.set_tab_width(self.font_system, tab_width);
}
/// Shape lines until scroll, after adjusting scroll if the cursor moved
pub fn shape_as_needed(&mut self, prune: bool) {
self.inner.shape_as_needed(self.font_system, prune);
}
/// Perform an [Action] on the editor
pub fn action(&mut self, action: Action) {
self.inner.action(self.font_system, action);
}
}

461
vendor/cosmic-text/src/edit/syntect.rs vendored Normal file
View File

@@ -0,0 +1,461 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
#[cfg(feature = "std")]
use std::{fs, io, path::Path};
use syntect::highlighting::{
FontStyle, HighlightState, Highlighter, RangedHighlightIterator, ThemeSet,
};
use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};
use crate::{
Action, AttrsList, BorrowedWithFontSystem, BufferRef, Change, Color, Cursor, Edit, Editor,
FontSystem, Selection, Shaping, Style, Weight,
};
pub use syntect::highlighting::Theme as SyntaxTheme;
#[derive(Debug)]
pub struct SyntaxSystem {
pub syntax_set: SyntaxSet,
pub theme_set: ThemeSet,
}
impl SyntaxSystem {
/// Create a new [`SyntaxSystem`]
pub fn new() -> Self {
Self {
//TODO: store newlines in buffer
syntax_set: SyntaxSet::load_defaults_nonewlines(),
theme_set: ThemeSet::load_defaults(),
}
}
}
/// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`]
#[derive(Debug)]
pub struct SyntaxEditor<'syntax_system, 'buffer> {
editor: Editor<'buffer>,
syntax_system: &'syntax_system SyntaxSystem,
syntax: &'syntax_system SyntaxReference,
theme: &'syntax_system SyntaxTheme,
highlighter: Highlighter<'syntax_system>,
syntax_cache: Vec<(ParseState, ScopeStack)>,
}
impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
/// 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: impl Into<BufferRef<'buffer>>,
syntax_system: &'syntax_system 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)?;
let highlighter = Highlighter::new(theme);
Some(Self {
editor,
syntax_system,
syntax,
theme,
highlighter,
syntax_cache: Vec::new(),
})
}
/// Modifies the theme of the [`SyntaxEditor`], returning false if the theme is missing
pub fn update_theme(&mut self, theme_name: &str) -> bool {
if let Some(theme) = self.syntax_system.theme_set.themes.get(theme_name) {
if self.theme != theme {
self.theme = theme;
self.highlighter = Highlighter::new(theme);
self.syntax_cache.clear();
// Reset attrs to match default foreground and no highlighting
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));
}
});
}
true
} else {
false
}
}
/// Load text from a file, and also set syntax to the best option
///
/// ## Errors
///
/// Returns an [`io::Error`] if reading the file fails
#[cfg(feature = "std")]
pub fn load_text<P: AsRef<Path>>(
&mut self,
font_system: &mut FontSystem,
path: P,
mut attrs: crate::Attrs,
) -> io::Result<()> {
let path = path.as_ref();
// Set attrs to match default foreground
if let Some(foreground) = self.theme.settings.foreground {
attrs = attrs.color(Color::rgba(
foreground.r,
foreground.g,
foreground.b,
foreground.a,
));
}
let text = fs::read_to_string(path)?;
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) {
Ok(Some(some)) => some,
Ok(None) => {
log::warn!("no syntax found for {:?}", path);
self.syntax_system.syntax_set.find_syntax_plain_text()
}
Err(err) => {
log::warn!("failed to determine syntax for {:?}: {:?}", path, err);
self.syntax_system.syntax_set.find_syntax_plain_text()
}
};
// Clear syntax cache
self.syntax_cache.clear();
Ok(())
}
/// Set syntax highlighting by file extension
pub fn syntax_by_extension(&mut self, extension: &str) {
self.syntax = match self
.syntax_system
.syntax_set
.find_syntax_by_extension(extension)
{
Some(some) => some,
None => {
log::warn!("no syntax found for {}", extension);
self.syntax_system.syntax_set.find_syntax_plain_text()
}
};
self.syntax_cache.clear();
}
/// Get the default background color
pub fn background_color(&self) -> Color {
if let Some(background) = self.theme.settings.background {
Color::rgba(background.r, background.g, background.b, background.a)
} else {
Color::rgb(0, 0, 0)
}
}
/// Get the default foreground (text) color
pub fn foreground_color(&self) -> Color {
if let Some(foreground) = self.theme.settings.foreground {
Color::rgba(foreground.r, foreground.g, foreground.b, foreground.a)
} else {
Color::rgb(0xFF, 0xFF, 0xFF)
}
}
/// Get the default cursor color
pub fn cursor_color(&self) -> Color {
if let Some(some) = self.theme.settings.caret {
Color::rgba(some.r, some.g, some.b, some.a)
} else {
self.foreground_color()
}
}
/// Get the default selection color
pub fn selection_color(&self) -> Color {
if let Some(some) = self.theme.settings.selection {
Color::rgba(some.r, some.g, some.b, some.a)
} else {
let foreground_color = self.foreground_color();
Color::rgba(
foreground_color.r(),
foreground_color.g(),
foreground_color.b(),
0x33,
)
}
}
/// Get the current syntect theme
pub fn theme(&self) -> &SyntaxTheme {
self.theme
}
/// Draw the editor
#[cfg(feature = "swash")]
pub fn draw<F>(&self, font_system: &mut FontSystem, cache: &mut crate::SwashCache, mut f: F)
where
F: FnMut(i32, i32, u32, u32, Color),
{
let size = self.with_buffer(|buffer| buffer.size());
if let Some(width) = size.0 {
if let Some(height) = size.1 {
f(0, 0, width as u32, height as u32, self.background_color());
}
}
self.editor.draw(
font_system,
cache,
self.foreground_color(),
self.cursor_color(),
self.selection_color(),
self.foreground_color(),
f,
);
}
}
impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
fn buffer_ref(&self) -> &BufferRef<'buffer> {
self.editor.buffer_ref()
}
fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer> {
self.editor.buffer_ref_mut()
}
fn cursor(&self) -> Cursor {
self.editor.cursor()
}
fn set_cursor(&mut self, cursor: Cursor) {
self.editor.set_cursor(cursor);
}
fn selection(&self) -> Selection {
self.editor.selection()
}
fn set_selection(&mut self, selection: Selection) {
self.editor.set_selection(selection);
}
fn auto_indent(&self) -> bool {
self.editor.auto_indent()
}
fn set_auto_indent(&mut self, auto_indent: bool) {
self.editor.set_auto_indent(auto_indent);
}
fn tab_width(&self) -> u16 {
self.editor.tab_width()
}
fn set_tab_width(&mut self, font_system: &mut FontSystem, tab_width: u16) {
self.editor.set_tab_width(font_system, tab_width);
}
fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
#[cfg(feature = "std")]
let now = std::time::Instant::now();
let cursor = self.cursor();
self.editor.with_buffer_mut(|buffer| {
let metrics = buffer.metrics();
let scroll = buffer.scroll();
let scroll_end = scroll.vertical + buffer.size().1.unwrap_or(f32::INFINITY);
let mut total_height = 0.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_height > 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!
if line_i >= scroll.line && total_height < 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) => {
for layout_line in layout_lines.iter() {
total_height +=
layout_line.line_height_opt.unwrap_or(metrics.line_height);
}
}
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_height < scroll_end {
match buffer.line_layout(font_system, line_i) {
Some(layout_lines) => {
for layout_line in layout_lines.iter() {
total_height +=
layout_line.line_height_opt.unwrap_or(metrics.line_height);
}
}
None => {
//TODO: should this be possible?
}
}
}
let cache_item = (parse_state.clone(), highlight_state.path.clone());
if line_i < self.syntax_cache.len() {
if self.syntax_cache[line_i] != cache_item {
self.syntax_cache[line_i] = cache_item;
if line_i + 1 < buffer.lines.len() {
buffer.lines[line_i + 1].reset();
}
}
} else {
buffer.lines[line_i].set_metadata(self.syntax_cache.len());
self.syntax_cache.push(cache_item);
}
}
if highlighted > 0 {
buffer.set_redraw(true);
#[cfg(feature = "std")]
log::debug!(
"Syntax highlighted {} lines in {:?}",
highlighted,
now.elapsed()
);
}
});
self.editor.shape_as_needed(font_system, prune);
}
fn delete_range(&mut self, start: Cursor, end: Cursor) {
self.editor.delete_range(start, end);
}
fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor {
self.editor.insert_at(cursor, data, attrs_list)
}
fn copy_selection(&self) -> Option<String> {
self.editor.copy_selection()
}
fn delete_selection(&mut self) -> bool {
self.editor.delete_selection()
}
fn apply_change(&mut self, change: &Change) -> bool {
self.editor.apply_change(change)
}
fn start_change(&mut self) {
self.editor.start_change();
}
fn finish_change(&mut self) -> Option<Change> {
self.editor.finish_change()
}
fn action(&mut self, font_system: &mut FontSystem, action: Action) {
self.editor.action(font_system, action);
}
fn cursor_position(&self) -> Option<(i32, i32)> {
self.editor.cursor_position()
}
}
impl BorrowedWithFontSystem<'_, SyntaxEditor<'_, '_>> {
/// Load text from a file, and also set syntax to the best option
///
/// ## Errors
///
/// Returns an [`io::Error`] if reading the file fails
#[cfg(feature = "std")]
pub fn load_text<P: AsRef<Path>>(&mut self, path: P, attrs: crate::Attrs) -> io::Result<()> {
self.inner.load_text(self.font_system, path, attrs)
}
#[cfg(feature = "swash")]
pub fn draw<F>(&mut self, cache: &mut crate::SwashCache, f: F)
where
F: FnMut(i32, i32, u32, u32, Color),
{
self.inner.draw(self.font_system, cache, f);
}
}

1173
vendor/cosmic-text/src/edit/vi.rs vendored Normal file

File diff suppressed because it is too large Load Diff