Skip to main content

oak_ruby/lsp/highlighter/
mod.rs

1#![doc = include_str!("readme.md")]
2
3/// Ruby syntax highlighter
4pub struct RubyHighlighter {
5    /// Whether to use parser-based highlighting for better accuracy
6    pub use_parser: bool,
7}
8
9impl Default for RubyHighlighter {
10    fn default() -> Self {
11        Self { use_parser: false }
12    }
13}
14
15impl RubyHighlighter {
16    /// Creates a new Ruby highlighter instance
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Highlights Ruby keywords
22    pub fn highlight(&self, text: &str) -> Vec<(usize, usize, String)> {
23        let mut highlights = Vec::new();
24        let keywords = [
25            "if", "unless", "elsif", "else", "case", "when", "then", "for", "while", "until", "break", "next", "redo", "retry", "return", "yield", "def", "class", "module", "end", "lambda", "proc", "begin", "rescue", "ensure", "raise", "require", "load",
26            "include", "extend", "prepend", "and", "or", "not", "in", "true", "false", "nil", "super", "self", "alias", "undef", "defined", "do",
27        ];
28
29        for keyword in &keywords {
30            let mut start = 0;
31            while let Some(pos) = text[start..].find(keyword) {
32                let absolute_pos = start + pos;
33                let end_pos = absolute_pos + keyword.len();
34
35                let is_word_boundary_before = absolute_pos == 0 || !text.chars().nth(absolute_pos - 1).unwrap_or(' ').is_alphanumeric();
36                let is_word_boundary_after = end_pos >= text.len() || !text.chars().nth(end_pos).unwrap_or(' ').is_alphanumeric();
37
38                if is_word_boundary_before && is_word_boundary_after {
39                    highlights.push((absolute_pos, end_pos, "keyword".to_string()))
40                }
41                start = absolute_pos + 1
42            }
43        }
44        highlights
45    }
46}