Skip to main content

ruby_lsp/lsp/highlighter/
mod.rs

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