use std::fmt::Debug;
use syntect::{highlighting::Theme, parsing::SyntaxSet};
use crate::{load_default_theme,
try_load_r3bl_theme,
DocumentStorage,
PartialFlexBox,
Size,
StyleUSSpanLines};
#[derive(Clone, Debug)]
pub struct EditorEngine {
pub current_box: PartialFlexBox,
pub config_options: EditorEngineConfig,
pub syntax_set: SyntaxSet,
pub theme: Theme,
pub parser_byte_cache: ParserByteCache,
pub ast_cache: Option<StyleUSSpanLines>,
}
pub type ParserByteCache = DocumentStorage;
pub const PARSER_BYTE_CACHE_PAGE_SIZE: usize = 1024;
impl Default for EditorEngine {
fn default() -> Self { EditorEngine::new(Default::default()) }
}
impl EditorEngine {
pub fn new(config_options: EditorEngineConfig) -> Self {
Self {
current_box: Default::default(),
config_options,
syntax_set: SyntaxSet::load_defaults_newlines(),
theme: try_load_r3bl_theme().unwrap_or_else(|_| load_default_theme()),
parser_byte_cache: ParserByteCache::new(),
ast_cache: None,
}
}
pub fn viewport(&self) -> Size { self.current_box.style_adjusted_bounds_size }
pub fn set_ast_cache(&mut self, ast_cache: StyleUSSpanLines) {
self.ast_cache = Some(ast_cache)
}
pub fn clear_ast_cache(&mut self) { self.ast_cache = None }
pub fn get_ast_cache(&self) -> Option<&StyleUSSpanLines> { self.ast_cache.as_ref() }
pub fn ast_cache_is_empty(&self) -> bool { self.ast_cache.is_none() }
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EditorEngineConfig {
pub multiline_mode: LineMode,
pub syntax_highlight: SyntaxHighlightMode,
pub edit_mode: EditMode,
}
mod editor_engine_config_options_impl {
use super::*;
impl Default for EditorEngineConfig {
fn default() -> Self {
Self {
multiline_mode: LineMode::MultiLine,
syntax_highlight: SyntaxHighlightMode::Enable,
edit_mode: EditMode::ReadWrite,
}
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EditMode {
ReadOnly,
ReadWrite,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LineMode {
SingleLine,
MultiLine,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SyntaxHighlightMode {
Disable,
Enable,
}