Skip to main content

ass_editor/extensions/builtin/syntax_highlight/
editor_ext.rs

1//! `EditorExtension` trait implementation for the syntax highlighter.
2
3use super::SyntaxHighlightExtension;
4use crate::core::Result;
5use crate::extensions::{
6    EditorExtension, ExtensionCommand, ExtensionContext, ExtensionInfo, ExtensionResult,
7    ExtensionState, MessageLevel,
8};
9
10#[cfg(not(feature = "std"))]
11use alloc::{
12    collections::BTreeMap as HashMap,
13    format,
14    string::{String, ToString},
15    vec,
16    vec::Vec,
17};
18#[cfg(feature = "std")]
19use std::collections::HashMap;
20
21impl EditorExtension for SyntaxHighlightExtension {
22    fn info(&self) -> &ExtensionInfo {
23        &self.info
24    }
25
26    fn initialize(&mut self, context: &mut dyn ExtensionContext) -> Result<()> {
27        self.state = ExtensionState::Active;
28
29        // Load configuration
30        if let Some(semantic) = context.get_config("syntax.semantic_highlighting") {
31            self.config.semantic_highlighting = semantic == "true";
32        }
33        if let Some(tags) = context.get_config("syntax.highlight_tags") {
34            self.config.highlight_tags = tags == "true";
35        }
36        if let Some(errors) = context.get_config("syntax.highlight_errors") {
37            self.config.highlight_errors = errors == "true";
38        }
39        if let Some(max_tokens) = context.get_config("syntax.max_tokens") {
40            if let Ok(max) = max_tokens.parse() {
41                self.config.max_tokens = max;
42            }
43        }
44
45        context.show_message("Syntax highlighting initialized", MessageLevel::Info)?;
46        Ok(())
47    }
48
49    fn shutdown(&mut self, _context: &mut dyn ExtensionContext) -> Result<()> {
50        self.state = ExtensionState::Shutdown;
51        self.clear_cache();
52        Ok(())
53    }
54
55    fn state(&self) -> ExtensionState {
56        self.state
57    }
58
59    fn execute_command(
60        &mut self,
61        command_id: &str,
62        _args: &HashMap<String, String>,
63        context: &mut dyn ExtensionContext,
64    ) -> Result<ExtensionResult> {
65        match command_id {
66            "syntax.highlight" => {
67                if let Some(doc) = context.current_document() {
68                    let tokens = self.tokenize_document(doc)?;
69                    Ok(ExtensionResult::success_with_message(format!(
70                        "Document highlighted with {} tokens",
71                        tokens.len()
72                    )))
73                } else {
74                    Ok(ExtensionResult::failure(
75                        "No active document to highlight".to_string(),
76                    ))
77                }
78            }
79            "syntax.clear_cache" => {
80                self.clear_cache();
81                Ok(ExtensionResult::success_with_message(
82                    "Syntax highlight cache cleared".to_string(),
83                ))
84            }
85            "syntax.get_tokens" => {
86                if let Some(doc) = context.current_document() {
87                    let tokens = self.tokenize_document(doc)?;
88                    let mut result = ExtensionResult::success_with_message(format!(
89                        "Found {} tokens",
90                        tokens.len()
91                    ));
92                    result
93                        .data
94                        .insert("token_count".to_string(), tokens.len().to_string());
95                    Ok(result)
96                } else {
97                    Ok(ExtensionResult::failure("No active document".to_string()))
98                }
99            }
100            _ => Ok(ExtensionResult::failure(format!(
101                "Unknown command: {command_id}"
102            ))),
103        }
104    }
105
106    fn commands(&self) -> Vec<ExtensionCommand> {
107        vec![
108            ExtensionCommand::new(
109                "syntax.highlight".to_string(),
110                "Highlight Document".to_string(),
111                "Apply syntax highlighting to the current document".to_string(),
112            )
113            .with_category("Syntax".to_string()),
114            ExtensionCommand::new(
115                "syntax.clear_cache".to_string(),
116                "Clear Highlight Cache".to_string(),
117                "Clear the syntax highlighting cache".to_string(),
118            )
119            .with_category("Syntax".to_string())
120            .requires_document(false),
121            ExtensionCommand::new(
122                "syntax.get_tokens".to_string(),
123                "Get Highlight Tokens".to_string(),
124                "Get syntax highlighting tokens for the current document".to_string(),
125            )
126            .with_category("Syntax".to_string()),
127        ]
128    }
129
130    fn config_schema(&self) -> HashMap<String, String> {
131        let mut schema = HashMap::new();
132        schema.insert(
133            "syntax.semantic_highlighting".to_string(),
134            "boolean".to_string(),
135        );
136        schema.insert("syntax.highlight_tags".to_string(), "boolean".to_string());
137        schema.insert("syntax.highlight_errors".to_string(), "boolean".to_string());
138        schema.insert("syntax.max_tokens".to_string(), "number".to_string());
139        schema
140    }
141}