oak-highlight 0.0.11

A lightweight syntax highlighter for Rust with support for multiple programming languages and customizable themes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Core highlighting logic and structures.
//!
//! This module contains the main highlighting engine, style definitions,
//! and theme resolution logic for the Oak language framework.

use crate::exporters::Exporter;
use core::range::Range;
use oak_core::{
    TokenType,
    language::{ElementRole, Language, TokenRole, UniversalElementRole, UniversalTokenRole},
    tree::{RedNode, RedTree},
    visitor::Visitor,
};
use std::{
    borrow::Cow,
    collections::HashMap,
    string::{String, ToString},
    vec::Vec,
};

/// Highlight style configuration for visual text formatting.
///
/// This struct defines the visual appearance of highlighted text segments,
/// including colors, font weight, and text decorations.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HighlightStyle {
    /// Foreground text color in hex format (e.g., "#FF0000" for red)
    pub color: Option<String>,
    /// Background color in hex format (e.g., "#FFFF00" for yellow)
    pub background_color: Option<String>,
    /// Whether text should be displayed in bold
    pub bold: bool,
    /// Whether text should be displayed in italic
    pub italic: bool,
    /// Whether text should be underlined
    pub underline: bool,
}

impl Default for HighlightStyle {
    fn default() -> Self {
        Self { color: None, background_color: None, bold: false, italic: false, underline: false }
    }
}

/// Highlight theme configuration containing style definitions for different roles.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HighlightTheme {
    /// Theme name identifier
    pub name: String,
    /// Style mapping for various scopes.
    /// Scopes are dot-separated strings (e.g., "keyword.control.rust").
    pub styles: HashMap<String, HighlightStyle>,
}

impl Default for HighlightTheme {
    fn default() -> Self {
        let mut styles = HashMap::new();

        // Token Styles (Standard TextMate-like Scopes)
        styles.insert("keyword".to_string(), HighlightStyle { color: Some("#0000FF".to_string()), bold: true, ..Default::default() });
        styles.insert("keyword.operator".to_string(), HighlightStyle { color: Some("#800080".to_string()), ..Default::default() });
        styles.insert("variable.other".to_string(), HighlightStyle { color: Some("#001080".to_string()), ..Default::default() });
        styles.insert("constant".to_string(), HighlightStyle { color: Some("#098658".to_string()), ..Default::default() });
        styles.insert("constant.character.escape".to_string(), HighlightStyle { color: Some("#FF6600".to_string()), ..Default::default() });
        styles.insert("punctuation".to_string(), HighlightStyle { color: Some("#000080".to_string()), ..Default::default() });
        styles.insert("comment".to_string(), HighlightStyle { color: Some("#808080".to_string()), italic: true, ..Default::default() });
        styles.insert("punctuation.whitespace".to_string(), HighlightStyle::default());

        // Element Styles
        styles.insert("entity.name.function".to_string(), HighlightStyle { color: Some("#795E26".to_string()), bold: true, ..Default::default() });
        styles.insert("entity.name.type".to_string(), HighlightStyle { color: Some("#267F99".to_string()), ..Default::default() });
        styles.insert("variable.other.declaration".to_string(), HighlightStyle { color: Some("#795E26".to_string()), ..Default::default() });
        styles.insert("comment.block.documentation".to_string(), HighlightStyle { color: Some("#008000".to_string()), italic: true, ..Default::default() });
        styles.insert("meta.preprocessor".to_string(), HighlightStyle { color: Some("#AF00DB".to_string()), ..Default::default() });
        styles.insert("entity.other.attribute-name".to_string(), HighlightStyle { color: Some("#AF00DB".to_string()), ..Default::default() });
        styles.insert("entity.other.attribute-name.key".to_string(), HighlightStyle { color: Some("#001080".to_string()), ..Default::default() });

        // Common
        styles.insert("invalid".to_string(), HighlightStyle { color: Some("#FF0000".to_string()), background_color: Some("#FFCCCC".to_string()), ..Default::default() });
        styles.insert("none".to_string(), HighlightStyle::default());

        Self { name: "default".to_string(), styles }
    }
}

impl HighlightTheme {
    /// Resolves the style for a given scope, with fallback to parent scopes.
    /// Example: "keyword.control.rust" -> "keyword.control" -> "keyword" -> None
    pub fn resolve_style(&self, scope: &str) -> HighlightStyle {
        let mut current_scope = scope;
        while !current_scope.is_empty() {
            if let Some(style) = self.styles.get(current_scope) {
                return style.clone();
            }
            if let Some(pos) = current_scope.rfind('.') { current_scope = &current_scope[..pos] } else { break }
        }
        self.styles.get("none").cloned().unwrap_or_default()
    }

    /// Resolves the style for multiple scopes, returning the best (most specific) match.
    /// This follows TextMate's specificity rules where the deepest match across all scopes wins.
    pub fn resolve_styles(&self, scopes: &[String]) -> HighlightStyle {
        let mut best_style = None;
        let mut best_depth = -1;

        for scope in scopes {
            let mut current_scope = scope.as_str();
            // Count segments for depth
            let mut depth = (current_scope.split('.').count()) as i32;

            while !current_scope.is_empty() {
                if let Some(style) = self.styles.get(current_scope) {
                    if depth > best_depth {
                        best_depth = depth;
                        best_style = Some(style.clone())
                    }
                    break; // Found the most specific match for this scope string
                }
                if let Some(pos) = current_scope.rfind('.') {
                    current_scope = &current_scope[..pos];
                    depth -= 1
                }
                else {
                    break;
                }
            }
        }

        best_style.unwrap_or_else(|| self.styles.get("none").cloned().unwrap_or_default())
    }

    /// Gets the highlight style for a given token role.
    pub fn get_token_style(&self, role: oak_core::UniversalTokenRole) -> HighlightStyle {
        use oak_core::TokenRole;
        self.resolve_style(role.name())
    }

    /// Gets the highlight style for a given element role.
    pub fn get_element_style(&self, role: oak_core::UniversalElementRole) -> HighlightStyle {
        use oak_core::ElementRole;
        self.resolve_style(role.name())
    }
}

/// Helper to get scopes for a token role.
fn get_token_scopes<R: TokenRole>(role: R, language: &str, category: oak_core::language::LanguageCategory) -> Vec<String> {
    let specific_name = role.name();
    let universal_role = role.universal();
    let universal_name = universal_role.name();
    let category_prefix = match category {
        oak_core::language::LanguageCategory::Markup => "markup",
        oak_core::language::LanguageCategory::Config => "config",
        oak_core::language::LanguageCategory::Programming => "source",
        oak_core::language::LanguageCategory::Dsl => "dsl",
        _ => "source",
    };

    let mut scopes = Vec::with_capacity(5);

    // 1. Language-specific scope (e.g., "keyword.control.rust")
    scopes.push(format!("{}.{}", specific_name, language));

    // 2. Base name scope (e.g., "keyword.control")
    if specific_name != universal_name {
        scopes.push(specific_name.to_string());
    }

    // 3. Category + Universal name (e.g., "source.keyword")
    scopes.push(format!("{}.{}", category_prefix, universal_name));

    // 4. Pure Universal name (e.g., "keyword")
    scopes.push(universal_name.to_string());

    // 5. Category + Language (e.g., "source.rust")
    scopes.push(format!("{}.{}", category_prefix, language));

    scopes
}

/// Helper to get scopes for an element role.
fn get_element_scopes<R: ElementRole>(role: R, language: &str, category: oak_core::language::LanguageCategory) -> Vec<String> {
    let specific_name = role.name();
    let universal_role = role.universal();
    let universal_name = universal_role.name();
    let category_prefix = match category {
        oak_core::language::LanguageCategory::Markup => "markup",
        oak_core::language::LanguageCategory::Config => "config",
        oak_core::language::LanguageCategory::Programming => "source",
        oak_core::language::LanguageCategory::Dsl => "dsl",
        _ => "source",
    };

    let mut scopes = Vec::with_capacity(5);

    // 1. Language-specific scope
    scopes.push(format!("{}.{}", specific_name, language));

    // 2. Base name scope
    if specific_name != universal_name {
        scopes.push(specific_name.to_string());
    }

    // 3. Category + Universal name
    scopes.push(format!("{}.{}", category_prefix, universal_name));

    // 4. Pure Universal name
    scopes.push(universal_name.to_string());

    // 5. Category + Language
    scopes.push(format!("{}.{}", category_prefix, language));

    scopes
}

/// Trait for providing scopes for highlighting.
pub trait ScopeProvider {
    /// Returns the scopes associated with this provider for a given language and category.
    fn scopes(&self, language: &str, category: oak_core::language::LanguageCategory) -> Vec<String>;
}

impl ScopeProvider for UniversalTokenRole {
    fn scopes(&self, language: &str, category: oak_core::language::LanguageCategory) -> Vec<String> {
        get_token_scopes(*self, language, category)
    }
}

impl ScopeProvider for UniversalElementRole {
    fn scopes(&self, language: &str, category: oak_core::language::LanguageCategory) -> Vec<String> {
        get_element_scopes(*self, language, category)
    }
}

/// A serializable span representing a range in the source text.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HighlightSpan {
    /// Starting byte offset of the span.
    pub start: usize,
    /// Ending byte offset of the span.
    pub end: usize,
}

impl From<Range<usize>> for HighlightSpan {
    fn from(range: Range<usize>) -> Self {
        Self { start: range.start, end: range.end }
    }
}

/// A segment of highlighted text with associated style and content.
///
/// Represents a contiguous range of text that shares the same highlighting style.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HighlightSegment<'a> {
    /// Byte range in the source text that this segment covers
    pub span: HighlightSpan,
    /// Visual style to apply to this text segment
    pub style: HighlightStyle,
    /// The actual text content of this segment
    pub text: Cow<'a, str>,
}

/// Result of token highlighting containing styled text segments.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HighlightResult<'a> {
    /// The collection of styled text segments.
    pub segments: Vec<HighlightSegment<'a>>,
    /// The original source text that was highlighted.
    pub source: Cow<'a, str>,
}

/// A visitor that traverses the syntax tree to generate highlighting segments.
pub struct HighlightVisitor<'a, 't> {
    /// The theme used for style resolution.
    pub theme: &'t HighlightTheme,
    /// The segments collected during traversal.
    pub segments: Vec<HighlightSegment<'a>>,
    /// The source text.
    pub source: &'a str,
}

impl<'a, 't, 'tree, L: Language> Visitor<'tree, L> for HighlightVisitor<'a, 't> {
    fn visit_node(&mut self, node: RedNode<'tree, L>) {
        // Elements usually don't have direct colors unless they override token styles.
        // For now, we follow the visitor pattern to traverse children.
        for child in node.children() {
            match child {
                RedTree::Node(n) => <Self as Visitor<L>>::visit_node(self, n),
                RedTree::Leaf(t) => <Self as Visitor<L>>::visit_token(self, t),
            }
        }
    }

    fn visit_token(&mut self, token: oak_core::tree::RedLeaf<L>) {
        // Use scopes for highlighting
        let scopes = get_token_scopes(token.kind.role(), L::NAME, L::CATEGORY);
        let style = self.theme.resolve_styles(&scopes);

        let text = &self.source[token.span.start..token.span.end];

        self.segments.push(HighlightSegment { span: HighlightSpan { start: token.span.start, end: token.span.end }, style, text: Cow::Borrowed(text) });
    }
}

/// Base trait for kind highlighters.
///
/// This trait defines the interface for kind highlighting implementations
/// that can analyze source code and produce styled text segments.
pub trait Highlighter {
    /// Highlight the given source code for a specific language and theme.
    fn highlight<'a>(&self, source: &'a str, language: &str, theme: crate::themes::Theme) -> Result<HighlightResult<'a>, oak_core::errors::OakError>;
}

impl Highlighter for OakHighlighter {
    fn highlight<'a>(&self, source: &'a str, language: &str, theme: crate::themes::Theme) -> Result<HighlightResult<'a>, oak_core::errors::OakError> {
        self.highlight(source, language, theme)
    }
}

/// The main highlighter implementation that coordinates the highlighting process.
///
/// # Example
///
/// ```rust
/// use oak_highlight::{OakHighlighter, Theme};
///
/// let highlighter = OakHighlighter::new();
/// let result = highlighter.highlight("fn main() {}", "rust", Theme::OneDarkPro).unwrap();
/// assert!(!result.segments.is_empty());
/// ```
pub struct OakHighlighter {
    /// The theme used for highlighting.
    pub theme: HighlightTheme,
}

impl Default for OakHighlighter {
    fn default() -> Self {
        Self { theme: HighlightTheme::default() }
    }
}

impl OakHighlighter {
    /// Creates a new `OakHighlighter` with the default theme.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the theme for the highlighter.
    pub fn with_theme(mut self, theme: HighlightTheme) -> Self {
        self.theme = theme;
        self
    }

    /// Set theme by name using the predefined themes.
    pub fn theme(mut self, theme: crate::themes::Theme) -> Self {
        self.theme = theme.get_theme();
        self
    }

    /// Main highlight method matching README API.
    pub fn highlight<'a>(&self, source: &'a str, _language: &str, theme: crate::themes::Theme) -> Result<HighlightResult<'a>, oak_core::errors::OakError> {
        let theme_config = theme.get_theme();

        // Default implementation just treats everything as a single segment for now
        // if no specific language parser is used.
        // In a real scenario, we'd look up a parser from a registry.
        let segments = vec![HighlightSegment { span: Range { start: 0, end: source.len() }.into(), style: theme_config.resolve_style("none"), text: Cow::Borrowed(source) }];

        Ok(HighlightResult { segments, source: Cow::Borrowed(source) })
    }

    /// Highlights the source text using a specific language implementation.
    pub fn highlight_with_language<'a, L, P, LX>(&self, source: &'a str, theme: crate::themes::Theme, parser: &P, _lexer: &LX) -> Result<HighlightResult<'a>, oak_core::errors::OakError>
    where
        L: Language + Send + Sync + 'static,
        P: oak_core::parser::Parser<L>,
        LX: oak_core::Lexer<L>,
        L::ElementType: From<L::TokenType>,
    {
        let theme_config = theme.get_theme();
        let source_text = oak_core::source::SourceText::new(source.to_string());
        let mut cache = oak_core::parser::session::ParseSession::<L>::new(1024);
        let parse_result = parser.parse(&source_text, &[], &mut cache);

        let mut visitor = HighlightVisitor { theme: &theme_config, segments: Vec::new(), source };

        let root_node = parse_result.result.map_err(|e| e)?;
        let red_root = RedNode::new(root_node, 0);

        <HighlightVisitor<'a, '_> as Visitor<L>>::visit_node(&mut visitor, red_root);

        Ok(HighlightResult { segments: visitor.segments, source: Cow::Borrowed(source) })
    }

    /// Highlight and format to a string directly.
    pub fn highlight_format(&self, source: &str, language: &str, theme: crate::themes::Theme, format: crate::exporters::ExportFormat) -> Result<String, oak_core::errors::OakError> {
        let result = self.highlight(source, language, theme)?;

        let content = match format {
            crate::exporters::ExportFormat::Html => crate::exporters::HtmlExporter::new(true, true).export(&result),
            crate::exporters::ExportFormat::Json => crate::exporters::JsonExporter { pretty: true }.export(&result),
            crate::exporters::ExportFormat::Ansi => crate::exporters::AnsiExporter.export(&result),
            crate::exporters::ExportFormat::Css => crate::exporters::CssExporter.export(&result),
            _ => {
                return Err(oak_core::errors::OakError::unsupported_format(format!("{format:?}")));
            }
        };

        Ok(content)
    }
}