From 1663bfc96cda7217a293a660d95613b677329a94 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 9 Nov 2022 10:09:42 -0700 Subject: [PATCH] Add SyntaxEditor abstraction using optional synect feature --- Cargo.toml | 1 + examples/editor-orbclient/Cargo.toml | 3 +- examples/editor-orbclient/src/main.rs | 197 ++++----------------- src/editor.rs | 5 +- src/lib.rs | 5 + src/syntect.rs | 237 ++++++++++++++++++++++++++ test.sh | 2 + 7 files changed, 283 insertions(+), 167 deletions(-) create mode 100644 src/syntect.rs diff --git a/Cargo.toml b/Cargo.toml index 0d38418..35525d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ log = "0.4" ouroboros = "0.15.5" rustybuzz = { version = "0.5", default-features = false, features = ["libm"]} swash = { version = "0.1", optional = true } +syntect = { version = "5.0", optional = true } sys-locale = { version = "0.2", optional = true } unicode-linebreak = "0.1" unicode-script = "0.5" diff --git a/examples/editor-orbclient/Cargo.toml b/examples/editor-orbclient/Cargo.toml index d60c6f0..1b26bea 100644 --- a/examples/editor-orbclient/Cargo.toml +++ b/examples/editor-orbclient/Cargo.toml @@ -7,9 +7,8 @@ license = "MIT OR Apache-2.0" publish = false [dependencies] -cosmic-text = { path = "../../" } +cosmic-text = { path = "../..", features = ["syntect"] } env_logger = "0.9" fontdb = "0.9" log = "0.4" orbclient = "0.3.35" -syntect = "5.0" diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index 233f5d0..05e068e 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -1,47 +1,26 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use cosmic_text::{ + Action, Attrs, - AttrsList, - Buffer, Color, - Editor, Family, FontSystem, Metrics, - Style, SwashCache, - Action, - Weight + SyntaxEditor, + SyntaxSystem, }; use orbclient::{EventOption, Renderer, Window, WindowFlag}; -use std::{env, fs, thread, time::{Duration, Instant}}; -use syntect::highlighting::{ - FontStyle, - Highlighter, - HighlightState, - RangedHighlightIterator, - ThemeSet, -}; -use syntect::parsing::{ - ParseState, - ScopeStack, - SyntaxSet, -}; +use std::{env, thread, time::{Duration, Instant}}; fn main() { env_logger::init(); - let (path, text) = if let Some(arg) = env::args().nth(1) { - ( - arg.clone(), - fs::read_to_string(&arg).expect("failed to open file") - ) + let path = if let Some(arg) = env::args().nth(1) { + arg.clone() } else { - ( - String::new(), - String::new() - ) + String::new() }; let display_scale = match orbclient::get_display_size() { @@ -67,6 +46,8 @@ fn main() { let font_system = FontSystem::new(); + let syntax_system = SyntaxSystem::new(&font_system); + let font_sizes = [ Metrics::new(10, 14).scale(display_scale), // Caption Metrics::new(14, 20).scale(display_scale), // Body @@ -79,12 +60,13 @@ fn main() { let mut font_size_i = font_size_default; let line_x = 8 * display_scale; - let mut editor = Editor::new(Buffer::new( - &font_system, - font_sizes[font_size_i] - )); + let mut editor = SyntaxEditor::new( + &syntax_system, + font_sizes[font_size_i], + "base16-eighties.dark" + ).unwrap(); - editor.buffer.set_size( + editor.buffer_mut().set_size( window.width() as i32 - line_x * 2, window.height() as i32 ); @@ -92,20 +74,17 @@ fn main() { let attrs = Attrs::new() .monospaced(true) .family(Family::Monospace); - editor.buffer.set_text(&text, attrs); + match editor.load_text(&path, attrs) { + Ok(()) => (), + Err(err) => { + log::error!("failed to load {:?}: {}", path, err); + } + } let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00); let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF); - let now = Instant::now(); - - //TODO: store newlines in buffer - let ps = SyntaxSet::load_defaults_nonewlines(); - let ts = ThemeSet::load_defaults(); - let theme = &ts.themes["base16-eighties.dark"]; - let highlighter = Highlighter::new(theme); - - if let Some(background) = theme.settings.background { + if let Some(background) = editor.theme.settings.background { bg_color = orbclient::Color::rgba( background.r, background.g, @@ -114,7 +93,7 @@ fn main() { ); } - if let Some(foreground) = theme.settings.foreground { + if let Some(foreground) = editor.theme.settings.foreground { font_color = Color::rgba( foreground.r, foreground.g, @@ -123,110 +102,15 @@ fn main() { ); } - let syntax = match ps.find_syntax_for_file(&path) { - Ok(Some(some)) => some, - Ok(None) => { - log::warn!("no syntax found for {:?}", path); - ps.find_syntax_plain_text() - } - Err(err) => { - log::warn!("failed to determine syntax for {:?}: {:?}", path, err); - ps.find_syntax_plain_text() - } - }; - - log::info!("using syntax {:?}, loaded in {:?}", syntax.name, now.elapsed()); - let mut swash_cache = SwashCache::new(&font_system); - let mut syntax_cache = Vec::<(ParseState, HighlightState)>::new(); - let mut ctrl_pressed = false; let mut mouse_x = -1; let mut mouse_y = -1; let mut mouse_left = false; - let mut rehighlight = true; loop { - if rehighlight { - let now = Instant::now(); - - for line_i in 0..editor.buffer.lines.len() { - let line = &mut editor.buffer.lines[line_i]; - if ! line.is_reset() && line_i < syntax_cache.len() { - continue; - } - - let (mut parse_state, mut highlight_state) = if line_i > 0 && line_i <= syntax_cache.len() { - syntax_cache[line_i - 1].clone() - } else { - ( - ParseState::new(syntax), - HighlightState::new(&highlighter, ScopeStack::new()) - ) - }; - - let ops = parse_state.parse_line(line.text(), &ps).unwrap(); - let ranges = RangedHighlightIterator::new( - &mut highlight_state, - &ops, - line.text(), - &highlighter, - ); - - let mut attrs_list = AttrsList::new(attrs); - for (style, _, range) in ranges { - attrs_list.add_span( - range, - 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 - ); - } - - // Update line attributes. This operation only resets if the line changes - line.set_attrs_list(attrs_list); - line.set_wrap_simple(true); - - //TODO: efficiently do syntax highlighting without having to shape whole buffer - line.shape(&font_system); - - let cache_item = (parse_state.clone(), highlight_state.clone()); - if line_i < syntax_cache.len() { - if syntax_cache[line_i] != cache_item { - syntax_cache[line_i] = cache_item; - if line_i + 1 < editor.buffer.lines.len() { - editor.buffer.lines[line_i + 1].reset(); - } - } - } else { - syntax_cache.push(cache_item); - } - } - - editor.buffer.redraw = true; - rehighlight = false; - - log::info!("Syntax highlighted in {:?}", now.elapsed()); - } - editor.shape_as_needed(); - if editor.buffer.redraw { + if editor.buffer_mut().redraw { let instant = Instant::now(); window.set(bg_color); @@ -239,7 +123,7 @@ fn main() { { let mut start_line_opt = None; let mut end_line = 0; - for run in editor.buffer.layout_runs() { + for run in editor.buffer().layout_runs() { end_line = run.line_i; if start_line_opt == None { start_line_opt = Some(end_line); @@ -247,7 +131,7 @@ fn main() { } let start_line = start_line_opt.unwrap_or(end_line); - let lines = editor.buffer.lines.len(); + let lines = editor.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 { @@ -263,10 +147,9 @@ fn main() { window.sync(); - editor.buffer.redraw = false; + editor.buffer_mut().redraw = false; - let duration = instant.elapsed(); - log::debug!("redraw: {:?}", duration); + log::debug!("redraw: {:?}", instant.elapsed()); } let mut found_event = false; @@ -285,39 +168,29 @@ fn main() { orbclient::K_END if event.pressed => editor.action(Action::End), orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp), orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown), - orbclient::K_ENTER if event.pressed => { - editor.action(Action::Enter); - rehighlight = true; - }, - orbclient::K_BKSP if event.pressed => { - editor.action(Action::Backspace); - rehighlight = true; - }, - orbclient::K_DEL if event.pressed => { - editor.action(Action::Delete); - rehighlight = true; - }, + orbclient::K_ENTER if event.pressed => editor.action(Action::Enter), + orbclient::K_BKSP if event.pressed => editor.action(Action::Backspace), + 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.set_metrics(font_sizes[font_size_i]); + editor.buffer_mut().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.set_metrics(font_sizes[font_size_i]); + editor.buffer_mut().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.set_metrics(font_sizes[font_size_i]); + editor.buffer_mut().set_metrics(font_sizes[font_size_i]); } } _ => (), }, EventOption::TextInput(event) if !ctrl_pressed => { editor.action(Action::Insert(event.character)); - rehighlight = true; } EventOption::Mouse(event) => { mouse_x = event.x; @@ -352,7 +225,7 @@ fn main() { } } EventOption::Resize(event) => { - editor.buffer.set_size(event.width as i32 - line_x * 2, event.height as i32); + editor.buffer_mut().set_size(event.width as i32 - line_x * 2, event.height as i32); } EventOption::Scroll(event) => { editor.action(Action::Scroll { diff --git a/src/editor.rs b/src/editor.rs index eb37e33..72f237f 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,8 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 #[cfg(not(feature = "std"))] -use alloc::string::ToString; -#[cfg(feature = "swash")] +use alloc::string::{String, ToString}; use core::cmp; use unicode_segmentation::UnicodeSegmentation; @@ -226,7 +225,7 @@ impl<'a> Editor<'a> { true } - /// Perform a [Action] on the editor + /// Perform an [Action] on the editor pub fn action(&mut self, action: Action) { let old_cursor = self.cursor; diff --git a/src/lib.rs b/src/lib.rs index e5053f2..0f2c8bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,3 +86,8 @@ mod shape; pub use self::swash::*; #[cfg(feature = "swash")] mod swash; + +#[cfg(feature = "syntect")] +pub use self::syntect::*; +#[cfg(feature = "syntect")] +mod syntect; diff --git a/src/syntect.rs b/src/syntect.rs new file mode 100644 index 0000000..feeab63 --- /dev/null +++ b/src/syntect.rs @@ -0,0 +1,237 @@ +#[cfg(not(feature = "std"))] +use alloc::{ + string::String, + vec::Vec, +}; +#[cfg(feature = "std")] +use std::{ + fs, + io, + path::Path, +}; +use syntect::highlighting::{ + FontStyle, + Highlighter, + HighlightState, + RangedHighlightIterator, + Theme, + ThemeSet, +}; +use syntect::parsing::{ + ParseState, + ScopeStack, + SyntaxReference, + SyntaxSet, +}; + +use crate::{ + Action, + AttrsList, + Buffer, + Color, + Cursor, + Editor, + FontSystem, + Metrics, + Style, + Weight, +}; + +pub struct SyntaxSystem<'a> { + pub font_system: &'a FontSystem, + pub syntax_set: SyntaxSet, + pub theme_set: ThemeSet, +} + +impl<'a> SyntaxSystem<'a> { + /// Create a new [SyntaxSystem] + pub fn new(font_system: &'a FontSystem) -> Self { + Self { + font_system, + //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] +pub struct SyntaxEditor<'a> { + //TODO: should this be pub? + editor: Editor<'a>, + syntax_system: &'a SyntaxSystem<'a>, + syntax: &'a SyntaxReference, + //TODO: should this be pub? + pub theme: &'a Theme, + highlighter: Highlighter<'a>, + syntax_cache: Vec<(ParseState, HighlightState)>, +} + +impl<'a> SyntaxEditor<'a> { + /// Create a new [SyntaxEditor] with the provided [SyntaxSystem], [Metrics], and theme name. + /// A good default theme name is "base16-eighties.dark". + /// Returns None will be returned if theme not found + pub fn new(syntax_system: &'a SyntaxSystem<'a>, metrics: Metrics, theme_name: &str) -> Option { + let editor = Editor::new(Buffer::new(syntax_system.font_system, metrics)); + 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(), + }) + } + + /// Load text from a file, and also set syntax to the best option + #[cfg(feature = "std")] + pub fn load_text>(&mut self, path: P, attrs: crate::Attrs<'a>) -> io::Result<()> { + let path = path.as_ref(); + + let text = fs::read_to_string(path)?; + self.editor.buffer.set_text(&text, attrs); + + //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() + } + }; + + Ok(()) + } + + /// Shape as needed, also doing syntax highlighting + pub fn shape_as_needed(&mut self) { + #[cfg(feature = "std")] + let now = std::time::Instant::now(); + + let mut highlighted = 0; + for line_i in 0..self.editor.buffer.lines.len() { + let line = &mut self.editor.buffer.lines[line_i]; + if ! line.is_reset() && line_i < self.syntax_cache.len() { + continue; + } + highlighted += 1; + + let (mut parse_state, mut highlight_state) = if line_i > 0 && line_i <= self.syntax_cache.len() { + self.syntax_cache[line_i - 1].clone() + } else { + ( + ParseState::new(self.syntax), + HighlightState::new(&self.highlighter, ScopeStack::new()) + ) + }; + + let ops = parse_state.parse_line(line.text(), &self.syntax_system.syntax_set).unwrap(); + 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 { + attrs_list.add_span( + range, + 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 + ); + } + + // Update line attributes. This operation only resets if the line changes + line.set_attrs_list(attrs_list); + line.set_wrap_simple(true); + + //TODO: efficiently do syntax highlighting without having to shape whole buffer + line.shape(&self.syntax_system.font_system); + + let cache_item = (parse_state.clone(), highlight_state.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 < self.editor.buffer.lines.len() { + self.editor.buffer.lines[line_i + 1].reset(); + } + } + } else { + self.syntax_cache.push(cache_item); + } + } + + if highlighted > 0 { + self.editor.buffer.redraw = true; + #[cfg(feature = "std")] + log::debug!("Syntax highlighted {} lines in {:?}", highlighted, now.elapsed()); + } + + self.editor.shape_as_needed(); + } + + /// Get the internal [Buffer] + pub fn buffer(&self) -> &Buffer<'a> { + &self.editor.buffer + } + + /// Get the internal [Buffer], mutably + pub fn buffer_mut(&mut self) -> &mut Buffer<'a> { + &mut self.editor.buffer + } + + /// Get the current [Cursor] position + pub fn cursor(&self) -> Cursor { + self.editor.cursor() + } + + /// Copy selection + pub fn copy_selection(&mut self) -> Option { + self.editor.copy_selection() + } + + /// Delete selection, adjusting cursor and returning true if there was a selection + pub fn delete_selection(&mut self) -> bool { + self.editor.delete_selection() + } + + /// Perform an [Action] on the editor + pub fn action(&mut self, action: Action) { + self.editor.action(action); + } + + /// Draw the editor + #[cfg(feature = "swash")] + pub fn draw(&self, cache: &mut crate::SwashCache, color: Color, f: F) + where F: FnMut(i32, i32, u32, u32, Color) + { + self.editor.draw(cache, color, f); + } +} diff --git a/test.sh b/test.sh index b680820..4ea75ca 100755 --- a/test.sh +++ b/test.sh @@ -7,5 +7,7 @@ cargo test cargo build --release --no-default-features cargo build --release --no-default-features --features std cargo build --release --no-default-features --features swash +cargo build --release --no-default-features --features syntect +cargo build --release --all-features cargo build --release --all env RUST_LOG=editor_test=info target/release/editor-test