Skip to main content

ass_editor/extensions/builtin/syntax_highlight/
extension.rs

1//! Core extension struct, configuration, and construction/cache helpers.
2
3use super::HighlightToken;
4use crate::extensions::{ExtensionCapability, ExtensionInfo, ExtensionState};
5
6#[cfg(not(feature = "std"))]
7use alloc::{
8    collections::BTreeMap as HashMap,
9    string::{String, ToString},
10    vec::Vec,
11};
12#[cfg(feature = "std")]
13use std::collections::HashMap;
14
15/// Syntax highlighting extension
16pub struct SyntaxHighlightExtension {
17    pub(super) info: ExtensionInfo,
18    pub(super) state: ExtensionState,
19    /// Cached tokens for performance
20    pub(super) token_cache: HashMap<String, Vec<HighlightToken>>,
21    /// Configuration
22    pub(super) config: SyntaxHighlightConfig,
23}
24
25/// Configuration for syntax highlighting
26#[derive(Debug, Clone)]
27pub struct SyntaxHighlightConfig {
28    /// Enable semantic highlighting (slower but more accurate)
29    pub semantic_highlighting: bool,
30    /// Highlight override tags
31    pub highlight_tags: bool,
32    /// Highlight errors
33    pub highlight_errors: bool,
34    /// Maximum tokens to process (0 = unlimited)
35    pub max_tokens: usize,
36}
37
38impl Default for SyntaxHighlightConfig {
39    fn default() -> Self {
40        Self {
41            semantic_highlighting: true,
42            highlight_tags: true,
43            highlight_errors: true,
44            max_tokens: 10000,
45        }
46    }
47}
48
49impl SyntaxHighlightExtension {
50    /// Create a new syntax highlighting extension
51    pub fn new() -> Self {
52        let info = ExtensionInfo::new(
53            "syntax-highlight".to_string(),
54            "1.0.0".to_string(),
55            "ASS-RS Team".to_string(),
56            "Built-in syntax highlighting for ASS/SSA files".to_string(),
57        )
58        .with_capability(ExtensionCapability::SyntaxHighlighting)
59        .with_license("MIT".to_string());
60
61        Self {
62            info,
63            state: ExtensionState::Uninitialized,
64            token_cache: HashMap::new(),
65            config: SyntaxHighlightConfig::default(),
66        }
67    }
68
69    /// Clear token cache
70    pub fn clear_cache(&mut self) {
71        self.token_cache.clear();
72    }
73
74    /// Invalidate cache for a specific document
75    pub fn invalidate_document(&mut self, doc_id: &str) {
76        self.token_cache.remove(doc_id);
77    }
78}
79
80impl Default for SyntaxHighlightExtension {
81    fn default() -> Self {
82        Self::new()
83    }
84}